2

I want to detect the location of each character in an image.

I tried pytesseract as suggested in how to get character position in pytesseract but gives me an error

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)



---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-38-bad5c94b3e13> in <module>()
      3 from pytesseract import pytesseract as pt
      4 
----> 5 pt.run_tesseract('input/1230.jpg', 'output', lang=None, boxes=True, config="hocr")
      6 
      7 # To read the coordinates

TypeError: run_tesseract() got an unexpected keyword argument 'boxes'

I do realise its for python 2.7, can someone guide me or give me a start on how to do it for python 3.x

Then i tried tesserocr which cannot seem to resolve the path of tessdata no matter what, i tried using tesseracct-ocr, tried manually downloading tessdata but still same error.

from PIL import Image
from tesserocr import PyTessBaseAPI, RIL

image = Image.open('train/1230.jpg')
with PyTessBaseAPI(path="C:\\Users\light\Desktop\tessdata-master") as api:
    api.SetImage(image)
    boxes = api.GetComponentImages(RIL.TEXTLINE, True)
    print('Found {} textline image components.'.format(len(boxes)))
    for i, (im, box, _, _) in enumerate(boxes):
        # im is a PIL image object
        # box is a dict with x, y, w and h keys
        api.SetRectangle(box['x'], box['y'], box['w'], box['h'])
        ocrResult = api.GetUTF8Text()
        conf = api.MeanTextConf()
        print (u"Box[{0}]: x={x}, y={y}, w={w}, h={h}, "
               "confidence: {1}, text: {2}").format(i, conf, ocrResult, **box)


---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-11-68789b2bbe93> in <module>()
      3 
      4 image = Image.open('train/1230.jpg')
----> 5 with PyTessBaseAPI(path="C:\\Users\light\Desktop\tessdata-master") as api:
      6     api.SetImage(image)
      7     boxes = api.GetComponentImages(RIL.TEXTLINE, True)

tesserocr.pyx in tesserocr._tesserocr.PyTessBaseAPI.__cinit__()

tesserocr.pyx in tesserocr._tesserocr.PyTessBaseAPI._init_api()

RuntimeError: Failed to init API, possibly an invalid tessdata path: C:\Users\light\Desktop essdata-master
Srihari
  • 177
  • 1
  • 9

2 Answers2

1

The boxes parameter was only valid for pytesseract version 0.1.x. It has been removed in version 0.2.0.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Yes, can you please suggest some other method to do the task. I edited my question with other attempt but still gives me an error – Srihari Apr 02 '18 at 16:13
  • @Bryan Oakley i am using pytesseract version tesseract 3.04.01 and got following error TypeError: run_tesseract() got an unexpected keyword argument 'boxes',how to fix it? – Azam Rafique Apr 19 '19 at 04:01
  • @Srihari i got the same error TypeError: run_tesseract() got an unexpected keyword argument 'boxes' , i have tesseract 3.04.01 , how to fix it? – Azam Rafique Apr 19 '19 at 07:10
1

To get the characters and their position in the image using pytesseract with the method image_to_boxes:

ret = pt.image_to_boxes(pil_img)

The result is a string where every line is a character and it's position (x1, y1, X2, y2) separated by spaces :

a 30 10 45 20
b 49 12 54 23
Alex Covizzi
  • 971
  • 8
  • 9
  • Posting this here after different tries `from PIL import Image image = Image.open('C:/Users/light/Desktop/Internship submission/train/1350.jpg') ret = pt.image_to_boxes(image) Gives me an error " ` ` FileNotFoundError: [WinError 2] The system cannot find the file specified "` – Srihari Apr 03 '18 at 15:00
  • Try using double backslash for the image path: C:\\Users\\light\\Desktop\\Internship submission\\train\\1350.jpg – Alex Covizzi Apr 03 '18 at 15:03
  • Has anyone seen am issue where the wrong co-ordinates are returned with the right characters??? – Dr T Jun 11 '20 at 17:52