我使用从图像中提取单词。这是一个用于tesseract的python包装器,它是一个OCR代码。
我使用以下代码获取单词:
import tesseract
api = tesseract.TessBaseAPI()
api.Init(".","eng",tesseract.OEM_DEFAULT)
api.SetVariable("tessedit_char_whitelist", "0123456789abcdefghijklmnopqrstuvwxyz")
api.SetPageSegMode(tesseract.PSM_AUTO)
mImgFile = "test.jpg"
mBuffer=open(mImgFile,"rb").read()
result = tesseract.ProcessPagesBuffer(mBuffer,len(mBuffer),api)
print "result(ProcessPagesBuffer)=",result它只返回图像中的单词,而不返回它们的位置/大小/方向(或者换句话说,返回包含它们的边框)。我想知道是否也有什么办法
发布于 2019-01-06 06:23:07
使用pytesseract.image_to_data()
import pytesseract
from pytesseract import Output
import cv2
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):
(x, y, w, h) = (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()是您要找的。
发布于 2013-12-30 02:18:22
tesseract.GetBoxText()方法返回数组中每个字符的确切位置。
此外,还有一个命令行选项tesseract test.jpg result hocr,它将生成一个result.html文件,其中包含每个可识别的单词的坐标。但我不确定是否可以通过python脚本调用它。
发布于 2018-04-20 12:16:29
Python tesseract可以在不写入文件的情况下使用image_to_boxes函数来完成这一任务:
import cv2
import pytesseract
filename = 'image.png'
# read the image and get the dimensions
img = cv2.imread(filename)
h, w, _ = img.shape # assumes color image
# run tesseract, returning the bounding boxes
boxes = pytesseract.image_to_boxes(img) # also include any config options you use
# draw the bounding boxes on the image
for b in boxes.splitlines():
b = b.split(' ')
img = cv2.rectangle(img, (int(b[1]), h - int(b[2])), (int(b[3]), h - int(b[4])), (0, 255, 0), 2)
# show annotated image and wait for keypress
cv2.imshow(filename, img)
cv2.waitKey(0)https://stackoverflow.com/questions/20831612
复制相似问题