Issue
Here is a hard to decypher image. I started a project where i want to take a photo of the coordinates on google earth pro and show them, for now. It works well but not on all surfaces where it will either mess up numbers or show me gibberish.This is my code.What can I do to improve the number detection?
It works by taking a screenshot of my screen, cropping it and taking out the numbers.(I know it executes infinitely for now it is no problem)
from pynput import keyboard
from PIL import ImageGrab, Image, ImageEnhance, ImageFilter
import pyautogui
import pytesseract
import PIL.ImageOps
pytesseract.pytesseract.tesseract_cmd = r"/usr/bin/tesseract"
h = 1280
w = 1024
leftc = 0.65*h
topc = 0.98*w
rightc = 0.808*h
bottomc = w-4
def on_press(key) :
if key == keyboard.Key.shift: # handles if key press is shift
image = ImageGrab.grab(bbox=(0,0,h,w))
image = image.crop((leftc, topc, rightc, bottomc))
image = image.resize((202,16),5)
image.save('sc.png')
image_to_text = pytesseract.image_to_string(image,lang='eng')
print(image_to_text)
def on_release(key) :
if key == keyboard.Key.shift:
print()
def get_current_key_input() :
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
get_current_key_input()
Solution
I agree with @furas make sure to read his suggested documentation. On the other hand, let this answer be a tutorial for you to dealing with small images. To make an accurate recognition, you need to:
-
- Upsample
-
- Center the digits
-
- Apply simple-thresholding.
For small images, upsampling and centering is crucial to make the characters or digits readable by human-eye and recognizable by tesseract. Thresholding will make the features (strokes of the character and digit) available.
Code:
import cv2
import pytesseract
img = cv2.imread("dCbPd.png")
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
(h, w) = gry.shape[:2]
gry = cv2.resize(gry, (w*4, h*4))
gry = cv2.copyMakeBorder(gry, 40, 40, 40, 40, cv2.BORDER_CONSTANT, value=255)
thr = cv2.threshold(gry, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
txt = pytesseract.image_to_string(thr)
print(txt)
print("Pytesseract version: {}".format(pytesseract.get_tesseract_version()))
cv2.imshow("thr", thr)
cv2.waitKey(0)
Answered By - Ahx
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.