how to get character position in pytesseract

半城伤御伤魂 提交于 2019-12-08 18:21:30

问题


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 there any library for getting each position of character


回答1:


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")



回答2:


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

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



来源:https://stackoverflow.com/questions/32175014/how-to-get-character-position-in-pytesseract

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!