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

时间:2021-06-24 08:59:10

I am using python-tesseract to extract words from an image. This is a python wrapper for tesseract which is an OCR code.

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

I am using the following code for getting the words:

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

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

This returns only the words and not their location/size/orientation (or in other words a bounding box containing them) in the image. I was wondering if there is any way to get that as well

这只返回图像中的单词,而不返回它们的位置/大小/方向(或包含它们的边框)。我想知道是否有什么方法可以达到这个目的

3 个解决方案

#1


11  

tesseract.GetBoxText() method returns the exact position of each character in an array.

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

Besides, there is a command line option tesseract test.jpg result hocr that will generate a result.html file with each recognized word's coordinates in it. But I'm not sure whether it can be called through python script.

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

#2


7  

Using the below code you can get the bounding box corresponding to each character.

使用下面的代码,您可以得到对应于每个字符的边框。

import csv
import cv2
from pytesseract import pytesseract as pt

pt.run_tesseract('bw.png', 'output', lang=None, boxes=True, config="hocr")

# To read the coordinates
boxes = []
with open('output.box', 'rb') as f:
    reader = csv.reader(f, delimiter = ' ')
    for row in reader:
        if(len(row)==6):
            boxes.append(row)

# Draw the bounding box
img = cv2.imread('bw.png')
h, w, _ = img.shape
for b in boxes:
    img = cv2.rectangle(img,(int(b[1]),h-int(b[2])),(int(b[3]),h-int(b[4])),(255,0,0),2)

cv2.imshow('output',img)

#3


1  

Python tesseract can do this without writing to file, using the image_to_boxes function:

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)

#1


11  

tesseract.GetBoxText() method returns the exact position of each character in an array.

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

Besides, there is a command line option tesseract test.jpg result hocr that will generate a result.html file with each recognized word's coordinates in it. But I'm not sure whether it can be called through python script.

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

#2


7  

Using the below code you can get the bounding box corresponding to each character.

使用下面的代码,您可以得到对应于每个字符的边框。

import csv
import cv2
from pytesseract import pytesseract as pt

pt.run_tesseract('bw.png', 'output', lang=None, boxes=True, config="hocr")

# To read the coordinates
boxes = []
with open('output.box', 'rb') as f:
    reader = csv.reader(f, delimiter = ' ')
    for row in reader:
        if(len(row)==6):
            boxes.append(row)

# Draw the bounding box
img = cv2.imread('bw.png')
h, w, _ = img.shape
for b in boxes:
    img = cv2.rectangle(img,(int(b[1]),h-int(b[2])),(int(b[3]),h-int(b[4])),(255,0,0),2)

cv2.imshow('output',img)

#3


1  

Python tesseract can do this without writing to file, using the image_to_boxes function:

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)