Skip to content Skip to sidebar Skip to footer

How To Get Character Position In Pytesseract

I am trying to get character position of image files using pytesseract library . import pytesseract from PIL import Image print pytesseract.image_to_string(Image.open('5.png')) Is

Solution 1:

Did you try use pytesseract.image_to_data()?

data = pytesseract.image_to_data(img, output_type='dict')
boxes = len(data['level'])
for i in range(boxes ):
    (x, y, w, h) = (data['left'][i], data['top'][i], data['width'][i], data['height'][i])
    #Draw box        
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)

Solution 2:

Using pytesseract doesn't seem the best idea to have the position but you can do this :

from pytesseract import pytesseract
pytesseract.run_tesseract('image.png', 'output', lang=None, boxes=False, config="hocr")

Solution 3:

you can save each character as an image using the script below and this is for python 3.6+

import cv2
import pytesseract 
import numpy
import time
from PIL import ImageGrab

path_to_save = "img_out/"
img = cv2.imread("image_INAUTXQ_www.facebook.com.png")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

hImg, wImg,_ = img.shape
boxes = pytesseract.image_to_boxes(img)
ROI_number=0for b in boxes.splitlines():
    b = b.split(' ')
    x, y, w, h = int(b[1]), int(b[2]), int(b[3]), int(b[4])
    # cv2.rectangle(img, (x,hImg- y), (w,hImg- h), (50, 50, 255), 1)
    x1,y1=hImg-h,hImg-y
    x2,y2=x,w
    roi=img[x1:y1,x2:y2]
    output = cv2.resize(roi,(300,300))
    # cv2.imshow('roi', roi)# cv2.imshow('img',img)# cv2.waitKey(0)
    cv2.imwrite("test"+str(ROI_number)+".jpeg",output)
    ROI_number+=1

Solution 4:

The position of the character can be found as follows.

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 = []
withopen('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)

While using this method, one may miss some texts. It would require some preprocessing (viz. background subtraction) of the image to get better results.

Post a Comment for "How To Get Character Position In Pytesseract"