OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Pytesseract is very slow for real time OCR, any way to optimise my code?

  • Thread starter Thread starter Vamsi
  • Start date Start date
V

Vamsi

Guest
I'm trying to create a real time OCR in python using mss and pytesseract.

So far, I've been able to capture my entire screen which has a steady FPS of 30. If I wanted to capture a smaller area of around 500x500, I've been able to get 100+ FPS.

However, as soon as I include this line of code, text = pytesseract.image_to_string(img), boom 0.8 FPS. Is there any way I could optimise my code to get a better FPS? Also the code is able to detect text, its just extremely slow.

Code:
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
<p>I'm trying to create a real time OCR in python using <code>mss</code> and <code>pytesseract</code>.</p>
<p>So far, I've been able to capture my entire screen which has a steady FPS of 30. If I wanted to capture a smaller area of around 500x500, I've been able to get 100+ FPS.</p>
<p>However, as soon as I include this line of code, <code>text = pytesseract.image_to_string(img)</code>, boom 0.8 FPS. Is there any way I could optimise my code to get a better FPS? Also the code is able to detect text, its just extremely slow.</p>
<pre class="lang-py prettyprint-override"><code>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
</code></pre>
 

Latest posts

M
Replies
0
Views
1
Marketerhire
M
E
Replies
0
Views
1
EMAPPLY
E
Top