3

I'm working on a Python script that continuously monitors a screen region, extracts text using Tesseract OCR, and sends serial commands to an Arduino based on the detected text. However, I notice that the memory usage keeps growing over time, even though I explicitly try to free up memory.

Any insights on potential memory leaks and how to fix them would be greatly appreciated!

import cv2
import pytesseract
import numpy as np
import pyautogui
import time
import serial
import gc
import psutil

# Set up serial connection
ser = serial.Serial('COM3', 9600, timeout=1)
time.sleep(2)

ROI = (100, 100, 200, 100)  # Example region

def capture_screen(region):
    """Captures a screenshot of a given region"""
    x, y, w, h = region
    screenshot = pyautogui.screenshot(region=(x, y, w, h))
    img = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2BGR)
    del screenshot
    gc.collect()
    return img

def extract_text(img):
    """Extracts text using Tesseract"""
    config = "--psm 8 -c tessedit_char_whitelist=0123456789-AS"
    text = pytesseract.image_to_string(img, config=config)
    del img  # Free memory
    gc.collect()
    return text.strip()

process = psutil.Process()

while True:
    print(f"Memory usage: {process.memory_info().rss / 1024 / 1024:.2f} MB")
    img = capture_screen(ROI)
    text = extract_text(img)
    print(f"Detected text: {text}")
    time.sleep(1)
2
  • There are some older articles and posts about Tesseract leaking memory. Have you tried sys.get_ref_count() on various objects? Also, is the size of the leak equal to or a multiple of the size in bytes of img? Commented Feb 9 at 2:11
  • @Rethunk since that "library" doesn't actually use tesseract as a library (at least last I checked), but runs it as a subprocess, leaks in that part would surprise me. -- this question needs more debugging, to determine which parts are actually required. all that gc.collect stuff is utterly unnecessary. also there has been no quantification of the leak. the claim itself is not supported. "I said so", with memory leaks, cannot be accepted, on the basis of logic and on the basis of almost nobody claiming it on SO having the established credibility for others to take that at face value. Commented Feb 9 at 10:07

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.