Search code examples
pythonopencvtesseracttext-extractionpython-tesseract

How to use pytesseract to extract text from specific cordinates from image?


I have an image and i need to extract text from that image. The text i need to extract is from a specified co-ordinate location. Let say cordinate=[ymin:ymax, xmin:xmax]. i have tried creating a new image using this co-ordinates and extracted the text. But the accuracy was very low due to the low quality of the new image. So now i am researching on how to extract the text with in the co-ordinate values with out creating a new image. I am using OpenCv to read the image and pytesseract to extract text.

import cv2
import pytesseract
import os
from PIL import Image
import sys
import numpy as np
# import textacy
config = ('-l eng --oem 2 --psm 1')

image = cv2.imread('C:/DocumentProcessing/IMAGE/2 (8).jpg')
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
cv2.imwrite('gray.jpg',gray)

_,thresh = cv2.threshold(gray,150,255,cv2.THRESH_BINARY_INV) # threshold
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
dilated = cv2.dilate(thresh,kernel,iterations = 30)

_,contours, hierarchy = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
my_list = []
for contour in contours:
    [x,y,w,h] = cv2.boundingRect(contour)
    cv2.rectangle(image,(x,y),(x+w,y+h),(127.5,0,255),1)
    new_image = image[y:y+h, x:x+w]
    cv2.imwrite(f"contoured-{y}-{x}.jpg", new_image)
    result = pytesseract.image_to_string(new_image,config=config)

Tesseract 4.0.0 pytesseract 0.3.0 OpenCv 3.4.3


Solution

  • You can use OPENCV to draw coordinates and then extract each section using tesseract