Python 无法从照片中提取数字 Linux

Python Can't extract numbers from photo Linux

Here is a hard to decypher image. 我开始了一个项目,我想暂时在 google earth pro 上拍摄坐标照片并展示给他们看。它运作良好,但不是在所有表面上它都会弄乱数字或告诉我 gibberish.This 是我的 code.What 我可以做些什么来改进数字检测吗?

它的工作原理是截取我的屏幕截图,裁剪它并取出数字。(我知道它无限执行现在没问题)

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()     

我同意@furas 的观点,请务必阅读他建议的文档。另一方面,让这个答案成为您处理小图像的教程。要准确识别,您需要:


    1. 上采样
    1. 将数字居中
    1. 应用简单阈值。

对于小图像,上采样和居中对于使字符或数字可被人眼读取和被 tesseract 识别至关重要。阈值化将使特征(字符和数字的笔画)可用。


Up
sampling
Centering
Threshold
tesseract 44.429595° lon 26.108718"

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)