Skip to main content

文字识别

tesseract - ocr

官方网站、版本下载

校验

tesseract -v

python调用

pip install pytesseract

应用案例

识别图片中是否存在某个字符串

import cv2
import pytesseract
from typing import Tuple


def find_text_coords(image_path: str, search_string: str) -> Tuple[int, int]:
# 读取图像
image = cv2.imread(image_path)

# 将图像转换为灰度
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# 设置中文语言数据文件路径
# tessdata_dir_config = '--tessdata-dir "path/to/tessdata"'

# 对图像进行文本识别,指定中文语言
text = pytesseract.image_to_string(gray, lang="chi_sim")

# 在识别到的文本中搜索指定的字符串
if search_string in text:
# 获取搜索字符串的位置
x = image.shape[1] // 2
y = image.shape[0] // 2
return (x, y)
else:
return ()


# 图像文件路径
image_path = "./test.jpg"

# 指定要搜索的字符串
search_string = "水利部珠江水利委员会水文局"

# 调用函数进行识别和搜索
result = find_text_coords(image_path, search_string)

# 打印结果
print(result)