I'm using python tesseract wrapper (tesserocr) over tesseract v4. I'm running some sample code over a simple "HELLO WORLD" white-on-black image, but although the letters are correctly recognised, I'm getting wrong bounding boxes, see the original image overlayed with results
Here is the code based on this SO post. Any idea how to get correct bboxes? Thank you!
import cv2
import json
from PIL import Image
from tesserocr import PyTessBaseAPI, RIL
img = cv2.imread('helloworld.jpg')
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
pillowImg = Image.fromarray(gray_img)
with PyTessBaseAPI() as api:
api.SetImage(pillowImg)
api.Recognize()
ri = api.GetIterator()
level = RIL.SYMBOL
for r in tesserocr.iterate_level(ri, level):
bbox = r.BoundingBoxInternal(level)
symbol = r.GetUTF8Text(level)
conf = r.Confidence(level)
print(json.dumps([symbol, conf, bbox]))
And here is the output:
["H", 99.57249450683594, [185, 361, 234, 427]]
["E", 99.54733276367188, [251, 361, 292, 427]]
["L", 99.50984954833984, [311, 361, 353, 427]]
["L", 99.4959716796875, [362, 361, 404, 427]]
["O", 99.55082702636719, [420, 359, 472, 428]]
["W", 99.52144622802734, [529, 361, 589, 427]]
["O", 99.55513763427734, [589, 361, 611, 427]]
["R", 99.56971740722656, [647, 359, 721, 428]]
["L", 99.55563354492188, [756, 361, 779, 427]]
["D", 99.56954956054688, [807, 361, 861, 427]]