0

Good day. I'm trying to identify the both printed and hand written text from the below check leaf

enter image description here

and here is the image after preprocessing, used below code

enter image description here

import cv2 
import pytesseract
import numpy as np

img = cv2.imread('Images/cheque_leaf.jpg')

# Rescaling the image (it's recommended if you’re working with images that have a DPI of less than 300 dpi)
img = cv2.resize(img, None, fx=1.2, fy=1.2, interpolation=cv2.INTER_CUBIC)
h, w = img.shape[:2]

# By default OpenCV stores images in BGR format and since pytesseract assumes RGB format,
# we need to convert from BGR to RGB format/mode:
# it to reduce noise
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]  # perform OTSU threhold
thresh = cv2.rectangle(thresh, (0, 0), (w, h), (0, 0, 0), 2) # draw a rectangle around regions of interest in an image

# Dilates an image by using a specific structuring element.
# enrich the charecters(to large)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2))

# The function erodes the source image using the specified structuring element that determines 
# the shape of a pixel neighborhood over which the minimum is taken
erode = cv2.erode(thresh, kernel, iterations = 1)

# To extract the text
custom_config = r'--oem 3 --psm 6'
pytesseract.image_to_string(thresh, config=custom_config)

and now using pytesseract.image_to_string() method to convert image to text. here I'm getting irrelavant output. In that above image I wanted to identify the date,branch payee,amount in both numbers and wordings and digital signature name followed by account number.

any OCR Techniques to solve the above problem by extract the exact data as mentioned above. Thanks in advance

Sabarish R
  • 71
  • 1
  • 7
  • you can try [EasyOCR](https://github.com/JaidedAI/EasyOCR) as an alternative to pytesseract. – Bilal Apr 27 '22 at 06:30
  • @Bilal, hand written text may vary based on the user so I need to increase the font quality by doing erode and all. – Sabarish R Apr 27 '22 at 06:40
  • 1
    Does this answer your question? [Using Tesseract for handwriting recognition](https://stackoverflow.com/questions/39556443/using-tesseract-for-handwriting-recognition) – Christoph Rackwitz Apr 27 '22 at 06:56
  • @ChristophRackwitz, Thanks for the suggestion. Hoping above API in C++, I just need a source in python – Sabarish R Apr 27 '22 at 07:11
  • the point isn't the languages. the point is that handwriting is difficult and tesseract isn't made for it. *training* tesseract is independent of programming language. – Christoph Rackwitz Apr 27 '22 at 07:19
  • @ChristophRackwitz, as per my requirement I'm trying to implement it in python – Sabarish R Apr 27 '22 at 09:17

1 Answers1

0

The following is just one of the several approaches.

I would suggest using Sauvola threshold technique. Threshold is calculated for each pixel in the image using a specific formula mentioned here. It involves calculating the mean and standard deviation of pixel values within a certain window.

This functionality is available in the skimage library (also known as scikit-image)

Following is the working example for the given image:

from skimage.filters import threshold_sauvola
img = cv2.imread('cheque.jpg', cv2.IMREAD_GRAYSCALE)

# choosing a window size of 13 (feel free to change it and visualize)
thresh_sauvola = threshold_sauvola(img, window_size=13)
binary_sauvola = img > thresh_sauvola

# converting resulting Boolean array to unsigned integer array of 8-bit (0 - 255) 
binary_sauvola_int = binary_sauvola.astype(np.uint8)
result = cv2.normalize(binary_sauvola_int, dst=None, alpha=0, beta=255,norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)

Result:

enter image description here

Note: This result is just a launchpad to try out other image processing techniques to get your desired result.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • Hii Jeru. Here the above gives the Image preprossing. from there how to clear unwanted shapes and to extract the text from the image. I wanted to identify the date,branch payee,amount in both numbers and wordings and digital signature name followed by account number. – Sabarish R Apr 28 '22 at 10:06
  • any update on the above – Sabarish R May 03 '22 at 09:55
  • @SabarishR Hi, the best we can do is preprocess the image and feed to an OCR engine. If it does not work you can look for handwritten text recognition modules built using deep learning. My post was only one of the many ways to preprocess an image – Jeru Luke May 03 '22 at 10:29
  • I've already done the preprocessing. I'm looking for the Image to text conversions. any suggestions are welcome – Sabarish R May 03 '22 at 11:11
  • @SabarishR I just used [OpenCV's EAST text detector](https://github.com/YCICI/EAST-OpenCv). It only **detects** textual regions. Perhaps after cropping those regions individually, you can perform OCR to obtain the text. – Jeru Luke May 03 '22 at 14:18
  • https://www.folio3.ai/blog/text-detection-by-using-opencv-and-east/ – Jeru Luke May 03 '22 at 14:20
  • I'll try the above and update you further – Sabarish R May 04 '22 at 09:03
  • @SabarishR You can check an illustration of it here: https://stackoverflow.com/questions/24385714/detect-text-region-in-image-using-opencv/72101499#72101499 – Jeru Luke May 04 '22 at 09:06