问题
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