""" OCR-инструменты: pytesseract для титула и PaddleOCR для датчиков. """ import cv2 import numpy as np from paddleocr import PaddleOCR import easyocr # Инициализация EasyOCR _reader_title = easyocr.Reader(['en'], gpu=False) # -------------------------- # OCR титула (EasyOCR) # -------------------------- def ocr_title(img: np.ndarray): """ OCR верхней области (титул мнемосхемы). """ if img is None or img.size == 0: return "" gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = cv2.convertScaleAbs(gray, alpha=2.0, beta=-40) binary = cv2.adaptiveThreshold( gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 15, 9 ) results = _reader_title.readtext(binary, detail=0, paragraph=False) if not results: return "Титул не оцифрован" return results[0].strip() # -------------------------- # OCR датчиков (PaddleOCR) # -------------------------- paddle_ocr = PaddleOCR( lang="en", use_gpu=False ) def ocr_sensors(rois: list[np.ndarray]): """ OCR областей сенсоров через PaddleOCR.predict(). Формат вывода: [{"text": str, "score": float}, ...] """ results = [] if not rois: return [] for roi in rois: try: ocr_res = paddle_ocr.ocr(roi, det=False, cls=False) except Exception as e: print(f"⚠ Ошибка OCR.ocr: {e}") results.append({"text": "?", "score": 0.0}) continue # -------- Безопасная распаковка -------- if ( not ocr_res or not isinstance(ocr_res, list) or not ocr_res[0] or not isinstance(ocr_res[0], list) or not ocr_res[0][0] or not isinstance(ocr_res[0][0], tuple) or len(ocr_res[0][0]) < 2 ): results.append({"text": "?", "score": 0.0}) continue text, score = ocr_res[0][0] results.append({ "text": text if text else "?", "score": round(float(score), 2) if score else 0.0 }) return results