Replies: 1 comment
-
To address your question, let's break it down into two parts: 1. How to Avoid Initializing PaddleOCR Every Time You Perform OCRInitializing PaddleOCR every time you perform OCR is inefficient and can significantly slow down your application. Instead, you should initialize the PaddleOCR instance once and reuse it for subsequent OCR operations. This can be achieved by moving the initialization of the Here’s how you can modify your code: # Utils/OcrWorker.py
import os
import time
import psutil
from paddleocr import PaddleOCR
class OcrWorker:
# Initialize PaddleOCR once and reuse it
ocr = PaddleOCR(use_angle_cls=True, lang='en', det=False, cls=True, cpu_threads=1, ocr_version='PP-OCRv4')
def __init__(self):
self.peak_cpu = 0
self.execution_time = 0
self.process = psutil.Process(os.getpid())
def limit_cpu_usage(self, percentage):
total_cores = psutil.cpu_count(logical=True)
cores_to_use = max(1, int((total_cores * percentage) / 100))
self.process.cpu_affinity(list(range(cores_to_use))))
def process_images(self, images):
try:
self.limit_cpu_usage(10)
start_time = time.time()
all_text_regions = []
all_full_texts = []
for img_path in images:
result = self.ocr.ocr(img_path, cls=True)
text_regions = []
full_text = ""
if result[0]:
for line in result[0]:
coords = line[0]
text = line[1][0]
x1 = min(coord[0] for coord in coords)
y1 = min(coord[1] for coord in coords)
x2 = max(coord[0] for coord in coords)
y2 = max(coord[1] for coord in coords)
width = x2 - x1
height = y2 - y1
text_regions.append({
'x': x1,
'y': y1,
'width': width,
'height': height,
'text': text
})
full_text += text + "\n"
all_text_regions.append(text_regions)
all_full_texts.append(full_text)
self.execution_time = time.time() - start_time
self.peak_cpu = psutil.cpu_percent()
self.process.cpu_affinity(list(range(psutil.cpu_count(logical=True))))
return all_full_texts, all_text_regions, self.peak_cpu, 0, self.execution_time
except Exception as e:
print(f"Error during OCR: {e}")
return [], [], 0, 0, 0 By making 2. Why PaddleOCR Initialization Affects the Main UIThe initialization of PaddleOCR can be resource-intensive, especially if it involves loading large models or setting up GPU/CPU resources. When this initialization happens on the main thread (or a thread that is not properly managed), it can block the UI, causing it to freeze or become unresponsive. To prevent this, you should ensure that the initialization and OCR processing happen in a separate thread, which you are already doing with To further isolate the initialization from the main UI thread, you can initialize the class OcrThread(QThread):
finished = pyqtSignal(list, list, float, float, float)
def __init__(self, images):
super().__init__()
self.images = images
def run(self):
worker = OcrWorker() # Initialize OcrWorker (and PaddleOCR) in the thread
results = worker.process_images(self.images)
self.finished.emit(*results) This ensures that the initialization of Summary
By following these steps, you should see improved performance and a more responsive UI. Response generated by 🤖 feifei-bot | deepseek-chat |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
code snippets, every time i click, start_ocr runs.
ocr worker below :
Beta Was this translation helpful? Give feedback.
All reactions