From 7d39baad90d4eed4b4f590cabf1d7aaa30886f00 Mon Sep 17 00:00:00 2001 From: Nicole Rappe Date: Mon, 10 Mar 2025 05:15:29 -0600 Subject: [PATCH] Optimized OCR Engine Initialization to only take place after a node that uses OCR is created. --- Data/Modules/data_collector.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Data/Modules/data_collector.py b/Data/Modules/data_collector.py index 50ef5fd..713e7bf 100644 --- a/Data/Modules/data_collector.py +++ b/Data/Modules/data_collector.py @@ -20,10 +20,15 @@ from PyQt5.QtCore import QRect, QPoint, Qt, QMutex, QTimer from PyQt5.QtGui import QPainter, QPen, QColor, QFont # Initialize EasyOCR with CUDA support -reader_cpu = easyocr.Reader(['en'], gpu=False) -reader_gpu = easyocr.Reader(['en'], gpu=True if torch.cuda.is_available() else False) +reader_cpu = None +reader_gpu = None -pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe" +def initialize_ocr_engines(): + global reader_cpu, reader_gpu + reader_cpu = easyocr.Reader(['en'], gpu=False) + reader_gpu = easyocr.Reader(['en'], gpu=True if torch.cuda.is_available() else False) + +pytesseract.pytesseract.tesseract_cmd = r"C:\\Program Files\\Tesseract-OCR\\tesseract.exe" DEFAULT_WIDTH = 180 DEFAULT_HEIGHT = 130 @@ -59,9 +64,8 @@ def create_ocr_region(region_id, x=250, y=50, w=DEFAULT_WIDTH, h=DEFAULT_HEIGHT, Creates an OCR region with a visible, resizable box on the screen. Allows setting custom color (RGB) and line thickness. """ - _ensure_qapplication() - + collector_mutex.lock() if region_id in regions: collector_mutex.unlock() @@ -73,7 +77,6 @@ def create_ocr_region(region_id, x=250, y=50, w=DEFAULT_WIDTH, h=DEFAULT_HEIGHT, } collector_mutex.unlock() - def get_raw_text(region_id): collector_mutex.lock() if region_id not in regions: @@ -83,12 +86,11 @@ def get_raw_text(region_id): collector_mutex.unlock() return text - def start_collector(): + initialize_ocr_engines() t = threading.Thread(target=_update_ocr_loop, daemon=True) t.start() - def _update_ocr_loop(): while True: collector_mutex.lock() @@ -112,7 +114,6 @@ def _update_ocr_loop(): time.sleep(0.7) - def _preprocess_image(image): gray = image.convert("L") scaled = gray.resize((gray.width * 3, gray.height * 3))