Reputation: 23
I'm trying to read and get the location of the white text on the black background using pytesseract, but am not having any success. Here is an example of an image that I'm dealing with.
Here is the code:
import cv2
import pytesseract
from pytesseract import Output
img = cv2.imread("ocr_example.png")
img = cv2.bitwise_not(img)
_, binary = cv2.threshold(img, 150, 255, cv2.THRESH_BINARY)
custom_config = r'--oem 3 --psm 6'
d = pytesseract.image_to_data(binary, output_type=Output.DICT, config=custom_config)
print(d["text"])
Here is the output of the text found:
['', '', '', '', 'Home', 'Address', '', 'Use', 'Current', 'Location', '', '>', '', 'Unable', 'to', 'find', 'location']
If I save the white text on black background to its own file and scan, the text is found without a problem. But I need to get the location of the text on the image as a whole.
I've tried using many of the Preprocessing suggestions on sites like https://nanonets.com/blog/ocr-with-tesseract/, but nothing seems to work. I don't mind doing a second search that finds only the missing text.
Upvotes: 0
Views: 3501
Reputation: 47
# gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# threshold_img = cv2.threshold(binary, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
worked better. Passing the threshold_img to the pytesseract.image_to_data with custom_config = r'--oem 3 --psm 4'
Upvotes: 0
Reputation: 7985
The problem is you set page-segmentation-mode to recognize single uniform block of text from the given image. (6
)
Change to recognize single column of text of variable sizes. (4
)
All you need to change --psm 6
to --psm 4
.
Now read
Home Address
Enter address here
Use Current Location
Unable to find location
DISMISS SAVE
Code:
import cv2
import pytesseract
img = cv2.imread("hm-adrs.png")
img = cv2.bitwise_not(img)
_, binary = cv2.threshold(img, 150, 255, cv2.THRESH_BINARY)
txt = pytesseract.image_to_string(binary, config="--oem 3 --psm 4")
print(txt)
Upvotes: 1