File size: 2,297 Bytes
d249b39 01dc506 d249b39 01dc506 d249b39 01dc506 d249b39 bd636c2 d249b39 01dc506 d249b39 01dc506 d249b39 8c4d89a d249b39 bd636c2 d249b39 d9372d5 d249b39 bd636c2 d249b39 5543f69 bd636c2 5543f69 bd636c2 60b90b9 bd636c2 60b90b9 bd636c2 60b90b9 38e057b bd636c2 5543f69 81fcb4f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | """
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 |