首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用python-tesseract获取识别单词的边界框。

使用python-tesseract获取识别单词的边界框。
EN

Stack Overflow用户
提问于 2013-12-30 00:15:13
回答 8查看 114.9K关注 0票数 56

我使用从图像中提取单词。这是一个用于tesseract的python包装器,它是一个OCR代码。

我使用以下代码获取单词:

代码语言:javascript
复制
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

它只返回图像中的单词,而不返回它们的位置/大小/方向(或者换句话说,返回包含它们的边框)。我想知道是否也有什么办法

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2019-01-06 06:23:07

使用pytesseract.image_to_data()

代码语言:javascript
复制
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是从边框左上角到图像顶部边框的距离。
  • widthheight是边框的宽度和高度。
  • conf是模型对边界框中单词的预测的信心。如果conf是-1,这意味着相应的边框包含一个文本块,而不仅仅是一个单词。

pytesseract.image_to_boxes()退回的边框随信附上,所以我相信pytesseract.image_to_data()是您要找的。

票数 111
EN

Stack Overflow用户

发布于 2013-12-30 02:18:22

tesseract.GetBoxText()方法返回数组中每个字符的确切位置。

此外,还有一个命令行选项tesseract test.jpg result hocr,它将生成一个result.html文件,其中包含每个可识别的单词的坐标。但我不确定是否可以通过python脚本调用它。

票数 19
EN

Stack Overflow用户

发布于 2018-04-20 12:16:29

Python tesseract可以在不写入文件的情况下使用image_to_boxes函数来完成这一任务:

代码语言:javascript
复制
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)
票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20831612

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档