Pytesseract 对于实时 OCR 来说非常慢,有什么方法可以优化我的代码吗?

Vam*_*msi 10 python ocr opencv tesseract python-tesseract

我正在尝试使用 python 创建实时 OCRmsspytesseract

到目前为止,我已经能够捕获整个屏幕,其 FPS 稳定为 30。如果我想捕获大约 500x500 的较小区域,我已经能够获得 100+ FPS。

然而,一旦我添加这行代码,text = pytesseract.image_to_string(img)FPS 就会飙升 0.8。有什么方法可以优化我的代码以获得更好的 FPS?该代码还能够检测文本,只是速度非常慢。

from mss import mss
import cv2
import numpy as np
from time import time
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r'C:\\Users\\Vamsi\\AppData\\Local\\Programs\\Tesseract-OCR\\tesseract.exe'

with mss() as sct:
    # Part of the screen to capture
    monitor = {"top": 200, "left": 200, "width": 500, "height": 500}

    while "Screen capturing":
        begin_time = time()

        # Get raw pixels from the screen, save it to a Numpy array
        img = np.array(sct.grab(monitor))

        # Finds text from the images
        text = pytesseract.image_to_string(img)

        # Display the picture
        cv2.imshow("Screen Capture", img)

        # Display FPS
        print('FPS {}'.format(1 / (time() - begin_time)))

        # Press "q" to quit
        if cv2.waitKey(25) & 0xFF == ord("q"):
            cv2.destroyAllWindows()
            break
Run Code Online (Sandbox Code Playgroud)

use*_*678 1

pytesseract “默认情况下”效率不高,因为它包装了 tesseract 可执行文件,它将临时文件保存到磁盘等...如果您认真对待性能,则需要直接使用 tesseract API(例如通过tesserocr或通过创建自定义 API 包装器

  • 我在 pytesseract 和 tesserocr 之间做了一些对比测试,但性能并没有说的那么不同。 (2认同)
  • 将 pytesseract 与 Linux 上的 tesserocr 进行比较,发现运行时间几乎相同。 (2认同)