Issue
I am trying to parse a number from an image. Here's an example of the images
I tried first to extract all the text to have a look at the final result but the code didn't recognize the desired number This is my try
from PyPDF2 import PdfFileWriter, PdfFileReader
import fitz, pytesseract, os, re
import cv2
def readNumber(img):
img = cv2.imread(img)
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
txt = pytesseract.image_to_string(gry)
return txt
I am trying to parse the number after the slash in the second line. Here the expected is 502630
Here's another sample picture that Ahx's code failed to parse the number from it
Solution
I think you are missing the image processing part.
You could apply adaptive thresholding.
For example:
Now, you want 22 / 502630
, so you need to check whether /
is in the current line, then if the line contains the '/' character, then take the right part.
for line in text.split('\n'):
if '/' in line:
line = line.split('/')[1].split(' ')[0]
print(line)
The result will be:
502630
Code:
import cv2
import pytesseract
bgr_image = cv2.imread("C8EE6.png")
scaled_image = cv2.resize(bgr_image, (0, 0), fx=3, fy=3)
gray_image = cv2.cvtColor(scaled_image, cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray_image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 61, 93)
text = pytesseract.image_to_string(thresh, config="psm 6")
for line in text.split('\n'):
if '/' in line:
line = line.split('/')[1].split(' ')[0]
print(line)
For the second image, if we apply the previous solution, the result will be:
We need different parameters for this solution since the output is not a clear image. We need to change the constant C
and the block size
From the documentation:
The blockSize determines the size of the neighborhood area and C is a constant that is subtracted from the mean or weighted sum of the neighborhood pixels.
If we set blockSize
=13 and C
=2, the output image will be:
If you compare the 2-images, the latter will be more readable than the previous image. Now if you read it:
502630
Updated Code:
import cv2
import pytesseract
bgr_image = cv2.imread("UVetb.png")
gray_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray_image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 13, 2)
text = pytesseract.image_to_string(thresh, config="psm 6")
for line in text.split('\n'):
if '/' in line:
line = line.split('/')[1].split(' ')[0]
print(line)
Will the updated code work on every image?
Can't guarantee, since each image requires a different block sizes and C parameters for getting the desired result.
Answered By - Ahx
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.