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)
gc.collectstuff 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.