0
点赞
收藏
分享

微信扫一扫

How to install Miniconda on ubuntu 22.04

敬亭阁主 03-24 18:00 阅读 1

Tesseract 是一款强大的开源 OCR(光学字符识别)引擎,可以用于从图像中提取文本。以下是使用 Python 和 Tesseract 进行文字识别的示例代码:
首先,确保你已经安装了 Tesseract。你可以按照官方网站上的指南进行安装,并设置好环境变量。
接下来,你可以使用 Python 中的 pytesseract 库来调用 Tesseract。以下是一个示例代码,它会加载一张图片,进行简单的图像处理,然后使用 Tesseract 进行文字识别:

-- coding: utf-8 --

“”"
Created on Fri Mar 22 15:06:57 2024
OCR字符识别 Tesseract
@author: cnliutz
“”"

1.安装 Tesseract OCR 引擎

#tesseract 安装下载地址 https://github.com/UB-Mannheim/tesseract/wiki
#tesseract 语言包下载地址 https://github.com/tesseract-ocr/tessdata Global Language
#中文包 https://github.com/tesseract-ocr/tessdata/blob/main/chi_sim.traineddata
#请把文件chi_sim.traineddata放到 C:\Program Files\Tesseract-OCR\tessdata\chi_sim.traineddata

2.打开DOS命令窗口,安装需要的库

pip install pytesseract
pip install pillow
pip install opencv-python
pip install numpy

3.python代码

# -*- coding: utf-8 -*-
"""
Created on Fri Mar 22 15:06:57 2024
OCR字符识别 Tesseract
@author: czliutz
"""
# 1.安装 Tesseract OCR 引擎
#tesseract 安装下载地址 https://github.com/UB-Mannheim/tesseract/wiki
#tesseract 语言包下载地址 https://github.com/tesseract-ocr/tessdata  Global Language
#中文包 https://github.com/tesseract-ocr/tessdata/blob/main/chi_sim.traineddata 
#请把文件chi_sim.traineddata放到 C:\Program Files\Tesseract-OCR\tessdata\chi_sim.traineddata
# 2.打开DOS命令窗口,安装需要的库
#pip install pytesseract
#pip install pillow
#pip install opencv-python
#pip install numpy


# 导入所需的库
from PIL import Image
import pytesseract
import argparse
import cv2
import os

# Set the path to the Tesseract OCR engine
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR/tesseract.exe'

# 构建参数解析器并解析参数
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="输入要进行OCR的图像路径")
ap.add_argument("-p", "--preprocess", type=str, default="thresh", help="预处理类型PREPROCESS: 'thresh' 或者 'blur'")
args = vars(ap.parse_args())

# 加载示例图像并将其转换为灰度图像
image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# 根据预处理类型对图像进行处理
if args["preprocess"] == "thresh":
    gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
elif args["preprocess"] == "blur":
    gray = cv2.medianBlur(gray, 3)

# 将灰度图像保存为临时文件,以便进行 OCR
filename = "{}.png".format(os.getpid())
cv2.imwrite(filename, gray)

# 使用 Tesseract 进行文字识别
text = pytesseract.image_to_string(Image.open(filename),lang='chi_sim')
os.remove(filename)  # 删除临时文件

# 打印识别结果
print(text)

在上述代码中,我们首先使用 Pillow 库打开图像并将其转换为灰度图像。然后,根据预处理类型(阈值分割或模糊处理),对图像进行相应的处理。最后,我们使用 pytesseract.image_to_string() 函数对处理后的图像进行文字识别。
你可以将上述代码保存为 ocr.py 文件,然后在命令行窗口中运行:

python ocr.py --image images/example.png 
# OR
python ocr.py --image images/example.png   -p blur
#查看帮助信息
python ocr.py -h

这将对示例图像进行文字识别。请确保替换 images/example.png 为你要识别的实际图像路径。

4.OCR 简化代码

#简化版OCR代码!^^!
import pytesseract
from PIL import Image
# Set the path to the Tesseract OCR engine
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR/tesseract.exe'
# Open an image file
image_path = r'C:\Users\czliu\Documents\python/wz.png'
image = Image.open(image_path)
# Perform OCR on the image
text = pytesseract.image_to_string(image,lang='chi_sim')
# Print the extracted text
print(text)

以上简化代码可保存文件名ocr1.py, 图片文件名需自行更改,运行:

python  ocr1.py

tesseract 电脑环境安装,处理中文
[参考网址:] (https://blog.csdn.net/weixin_41013322/category_8760189.html)

举报

相关推荐

0 条评论