Optimized OCR Engine Initialization to only take place after a node that uses OCR is created.

This commit is contained in:
Nicole Rappe 2025-03-10 05:15:29 -06:00
parent e56b5a860f
commit 7d39baad90

View File

@ -20,10 +20,15 @@ from PyQt5.QtCore import QRect, QPoint, Qt, QMutex, QTimer
from PyQt5.QtGui import QPainter, QPen, QColor, QFont from PyQt5.QtGui import QPainter, QPen, QColor, QFont
# Initialize EasyOCR with CUDA support # Initialize EasyOCR with CUDA support
reader_cpu = easyocr.Reader(['en'], gpu=False) reader_cpu = None
reader_gpu = easyocr.Reader(['en'], gpu=True if torch.cuda.is_available() else False) 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_WIDTH = 180
DEFAULT_HEIGHT = 130 DEFAULT_HEIGHT = 130
@ -59,7 +64,6 @@ 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. Creates an OCR region with a visible, resizable box on the screen.
Allows setting custom color (RGB) and line thickness. Allows setting custom color (RGB) and line thickness.
""" """
_ensure_qapplication() _ensure_qapplication()
collector_mutex.lock() collector_mutex.lock()
@ -73,7 +77,6 @@ def create_ocr_region(region_id, x=250, y=50, w=DEFAULT_WIDTH, h=DEFAULT_HEIGHT,
} }
collector_mutex.unlock() collector_mutex.unlock()
def get_raw_text(region_id): def get_raw_text(region_id):
collector_mutex.lock() collector_mutex.lock()
if region_id not in regions: if region_id not in regions:
@ -83,12 +86,11 @@ def get_raw_text(region_id):
collector_mutex.unlock() collector_mutex.unlock()
return text return text
def start_collector(): def start_collector():
initialize_ocr_engines()
t = threading.Thread(target=_update_ocr_loop, daemon=True) t = threading.Thread(target=_update_ocr_loop, daemon=True)
t.start() t.start()
def _update_ocr_loop(): def _update_ocr_loop():
while True: while True:
collector_mutex.lock() collector_mutex.lock()
@ -112,7 +114,6 @@ def _update_ocr_loop():
time.sleep(0.7) time.sleep(0.7)
def _preprocess_image(image): def _preprocess_image(image):
gray = image.convert("L") gray = image.convert("L")
scaled = gray.resize((gray.width * 3, gray.height * 3)) scaled = gray.resize((gray.width * 3, gray.height * 3))