pytesseract image_to_data检测并定位图片中的文字
pytesseract是用python包装Google Tesseract-OCR引擎的OCR工具,它通过调用系统中安装的tesseract.exe来识别图片中的文字并以多种格式进行输出。本文将记录如何使用pytesseract中image_to_data定位图片中的文字,以及该函数输出数据的格式详解。
环境说明
Tesseract安装
1 2 3 4 5 6 7 |
# If you don't have tesseract executable in your PATH, include the following: pytesseract.pytesseract.tesseract_cmd = r'<full_path_to_your_tesseract_executable>' # Example tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract' # Example config: r'--tessdata-dir "C:\Program Files (x86)\Tesseract-OCR\tessdata"' # It's important to add double quotes around the dir path. tessdata_dir_config = r'--tessdata-dir "<replace_with_your_tessdata_dir_path>"' pytesseract.image_to_data(image, lang='chi_sim', config=tessdata_dir_config) |
Tesseract TSV格式的输出
在pytesseract的介绍中,函数image_to_data返回识别结果中包含框边界,置信度和其他信息的结果。若需要得到更详细的信息,需要参考Tesseract TSV documentation。
为了得到更详细的信息,我们先使用tesseract在命令行中得到图像的tsv识别文件。
1 2 3 4 |
D:\PyTesseract>tesseract.exe Think.png Think -l eng tsv Tesseract Open Source OCR Engine v4.1.0 with Leptonica Warning: Invalid resolution 0 dpi. Using 70 instead. Estimating resolution as 395 |
1 2 |
D:\PyTesseract>tesseract.exe Think.png Think -l eng --dpi 300 tsv Tesseract Open Source OCR Engine v4.1.0 with Leptonica |
运行完成后,在Think.png统一路径下会生成一个名为Think.tsv的文件。
有兴趣的同学可以测试一下本文使用的测试图片,会发现是否设定[–dpi 300]的输出结果是有差异的。加上该设定后,Tesseract识别出的文字为[Think different],若不加此设定,除了[Think different]之外,Tesseract还识别出一个字母[g]。后面在代码部分会说明在python代码中如何设定[–dpi 300]。
为了后面对比不同字符的置信度,本文均使用无[–dpi 300]设定的输出结果,其内容如下:
1 2 3 4 5 6 7 8 9 10 11 |
level page_num block_num par_num line_num word_num left top width height conf text 1 1 0 0 0 0 0 0 283 178 -1 2 1 1 0 0 0 134 24 14 16 -1 3 1 1 1 0 0 134 24 14 16 -1 4 1 1 1 1 0 134 24 14 16 -1 5 1 1 1 1 1 134 24 14 16 14 g 2 1 1 0 0 0 26 120 231 32 -1 3 1 1 1 0 0 26 120 231 32 -1 4 1 1 1 1 0 26 120 231 32 -1 5 1 1 1 1 1 26 120 87 31 96 Think 5 1 1 1 1 2 123 120 134 32 96 different. |
image_to_data
接下来我们来看pytesseract中image_to_data中的应用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
from pytesseract import Output import pytesseract import argparse import cv2 import imutils import numpy as np image = cv2.imread("Think.png") h, w = image.shape[:2] #swap color channel ordering from BGR (OpenCV’s default) to RGB (compatible with Tesseract and pytesseract). # By default OpenCV stores images in BGR format and since pytesseract assumes RGB format, # we need to convert from BGR to RGB format/mode: rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) results = pytesseract.image_to_data(rgb, output_type=Output.DICT,lang='eng') print(results) for i in range(0, len(results["text"])): # extract the bounding box coordinates of the text region from the current result tmp_tl_x = results["left"][i] tmp_tl_y = results["top"][i] tmp_br_x = tmp_tl_x + results["width"][i] tmp_br_y = tmp_tl_y + results["height"][i] tmp_level = results["level"][i] conf = results["conf"][i] text = results["text"][i] if(tmp_level == 5): cv2.putText(image, text, (tmp_tl_x, tmp_tl_y - 10), cv2.FONT_HERSHEY_SIMPLEX,0.5, (0, 0, 255), 1) cv2.rectangle(image, (tmp_tl_x, tmp_tl_y), (tmp_br_x, tmp_br_y), (0, 0, 255), 2) cv2.imshow("image",image) cv2.waitKey(0) |
1 2 3 4 5 6 7 8 9 10 11 |
'page_num': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 'block_num': [0, 1, 1, 1, 1, 2, 2, 2, 2, 2], 'par_num': [0, 0, 1, 1, 1, 0, 1, 1, 1, 1], 'line_num': [0, 0, 0, 1, 1, 0, 0, 1, 1, 1], 'word_num': [0, 0, 0, 0, 1, 0, 0, 0, 1, 2], 'left': [0, 134, 134, 134, 134, 26, 26, 26, 26, 123], 'top': [0, 24, 24, 24, 24, 120, 120, 120, 120, 120], 'width': [283, 14, 14, 14, 14, 231, 231, 231, 87, 134], 'height': [178, 16, 16, 16, 16, 32, 32, 32, 31, 32], 'conf': ['-1', '-1', '-1', '-1', 14, '-1', '-1', '-1', 96, 96], 'text': ['', '', '', '', 'g', '', '', '', 'Think', 'different.'] |
第18~30行遍历results中的元素,当results[“level”]=5时,当前项次为Tesseract检测后认为有效单词的项,获取当前单词的外界矩形与识别结果,并绘制在测试图像中。
从图示结果中,我们可以看到Tesseract将苹果上的小叶片识别为字母”g”,查看其对应的conf为14,而另外两个单词”Think”和”different”的conf则均为96。因此我们在实际应用中,可以通过设置conf的值来对识别结果进行筛选。
假如输入图像的分辨率较低,可以考虑在代码中添加[–dpi 300]的设定,如下:
1 |
results = pytesseract.image_to_data(rgb, output_type=Output.DICT,lang='eng',config='--dpi 300') |