how to get character position in pytesseract

前端 未结 3 1318
遥遥无期
遥遥无期 2020-12-20 17:26

I am trying to get character position of image files using pytesseract library .

import pytesseract
from PIL import Image
print pytesseract.image_to_string(I         


        
相关标签:
3条回答
  • 2020-12-20 17:58

    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.

    0 讨论(0)
  • 2020-12-20 18:07

    Did you try use pytesseract.image_to_data()?

    data = pytesseract.image_to_data(img, output_type=Output.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)
    
    0 讨论(0)
  • 2020-12-20 18:17

    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")
    
    0 讨论(0)
提交回复
热议问题