Issue
I'm trying to read numbers from a picture with pytesseract. This is my code:
import matplotlib.pyplot as plt
import pytesseract
img = cv2.imread(r'bild4.jpg', 2)
[ret, bw_img] = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY)
imgplot = plt.imshow(bw_img)
plt.show()
text = pytesseract.image_to_string(bw_img, config='digits')
print("Text: " + text)
I tried many ways to preprocess the image, the best I got is: "673 504 . 5 552" Only the last line is correct. Without the config='digits' I get: "673 ost
Fir 504 .
5 552
ii"
I tried with black and withe only, witch is really easy to read for me as a human, but it don't recognice numbers at all...
Solution
You could use inRange thresholding
- Read the image and convert it to the HSV color-scale
bgr = cv2.imread("JSe5v.jpg")
hsv = cv2.cvtColor(bgr, cv2.COLOR_BGR2HSV)
- Initialize mask and kernel
mask = cv2.inRange(hsv, np.array([0, 0, 244]), np.array([179, 35, 255]))
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 3))
- Remove the background using mask and kernel
dilated = cv2.dilate(mask, kernel, iterations=1)
thresh = cv2.bitwise_and(dilated, mask)
The result will be:
If you read it with psm mode 6
675 OS!
312504
5 552
1st line second part is not correct "51" is recognized as "S!"
You could look for improving the tesseract accuracy
Code:
import cv2
import pytesseract
import numpy as np
bgr = cv2.imread("JSe5v.jpg")
hsv = cv2.cvtColor(bgr, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, np.array([0, 0, 244]), np.array([179, 35, 255]))
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 3))
dilated = cv2.dilate(mask, kernel, iterations=1)
thresh = cv2.bitwise_and(dilated, mask)
text = pytesseract.image_to_string(thresh, config="--psm 6")
print(text)
Answered By - Ahx
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.