Issue
Got a strange behavior using Tesseract via pytesseract when running my Python script on basically identical images.
Background: I got a Raspberry Pi running retrieving an image via a cam. The picture of the cam is stored in its original resolution. Via cv2 I crop the part of interest - that is a number. For this image I increase contrast and I also tried changing black/white (inverting the image) The quality of this picture you can evaluate here:
The part I run Tesseract with is:
value = pytesseract.image_to_string(image, config=r"--psm 6 --oem 3 digits")
But running my script sometimes the numbers are identified perfectly (ca. in 1 of 10 runs), but more often there are no numbers identified at all, sometimes only one digit or even more than the five presented digits.
I have no clue what may cause these variations regarding the qualitiy of the result - any ideas what could I improve? Can you replicate this bevaior based on the image shown above?
Solution
In order to improve your accuracy, you have to make sure the image is as clear as it can be. The image itself looks clear for human eye but tesseract needs big contrast between text and backround color. This is how i did it:
image=cv2.imread("image.png",0)
The above line reads the image in grayscale.(your image is already grayscale) Generally speaking, pixels are represented as RGB (Red,Green,Blue). While reading in grayscale, a pixel can only have a value between 0 (black) and 255 (white). So you will have 255 shades of gray.
_,thresh1 = cv2.threshold(image,100,255,cv2.THRESH_BINARY)
The above line takes every pixel and compares it with the threshold(100 in this case). If the value of the pixel is smaller than the threshold, the pixel will have it's value changed to 0. If its bigger, it will have it's color set to 255 in this case. You can adjust these values to get the cleanest image. This is what the image looks like after binarization.
After reading the text from this image i get 70150 every time. Don't forget import cv2
. I will leave some links for further reading on how to improve image quality for better accuracy. https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html
https://nanonets.com/blog/ocr-with-tesseract/
Answered By - yoxhall
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.