Python 使用 OpenCV 和 pytesseract 进行实时 OCR

Python Real Time OCR With OpenCV and pytesseract

我刚刚开始 python,我正在尝试创建一个代码,在我的屏幕的一部分上执行实时 OCR。我确信这段代码可以工作,但它只会给我带来一堆 tesseract 错误。是否需要保存图像才能使 Tesseract 工作?这个任务有更好的 OCR 库吗? OpenCV 部分完美运行并显示图像。

import numpy as np
import cv2
from PIL import ImageGrab
import pytesseract

while True:
    orig_img = ImageGrab.grab(box)

    np_im = np.array(orig_img)

    img = cv2.cvtColor(np_im, cv2.COLOR_BGR2GRAY)

    text = pytesseract.image_to_string(img)

    cv2.imshow('window',img)
    if cv2.waitKey(25) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        print(text)

我修好了。我不知道我需要在我的电脑上安装 tesseract。我还添加了

im = Image.fromarray(img)

im.save("img.png")

保存图片