使用pytesseract.image_to_data()
import pytesseract
import cv2
from pytesseract import Output
img = cv2.imread('image.jpg')
d = pytesseract.image_to_data(img, output_type=Output.DICT)
n_boxes = len(d['level'])
for i in range(n_boxes):
(text,x,y,w,h) = (d['text'][i],d['left'][i],d['top'][i],d['width'][i],d['height'][i])
cv2.rectangle(img, (x,y), (x+w,y+h) , (0,255,0), 2)
cv2.imshow('img',img)
cv2.waitkey(0)
在pytesseract.image_to_data()返回的数据中:
-
left 是离边界框左上角的距离,
到图片的左边框。
-
top 是离边界框左上角的距离,
到图片的上边框。
-
width 和 height 是边界框的宽度和高度。
-
conf 是模型对其中单词预测的置信度
那个边界框。如果conf 为-1,则表示对应的
边界框包含一个文本块,而不仅仅是一个
字。
pytesseract.image_to_boxes() 返回的边界框包含字母,所以我相信pytesseract.image_to_data() 就是您要查找的内容。