File size: 1,906 Bytes
7b0cda0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f66f92b
7b0cda0
 
 
 
 
 
 
f66f92b
177fbfb
 
 
 
7b0cda0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d46e42
 
 
 
 
 
 
 
 
7b0cda0
 
 
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
"""
OCR-инструменты: pytesseract для титула и PaddleOCR для датчиков.
"""

import cv2
import numpy as np
from paddleocr import PaddleOCR
import pytesseract


# --------------------------
# OCR титула (pytesseract)
# --------------------------
def ocr_title(img: np.ndarray) -> str:
    """
    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
    )

    text = pytesseract.image_to_string(binary, lang="eng+rus", config="--psm 7").strip()
    return text


# --------------------------
# OCR датчиков (PaddleOCR)
# --------------------------
paddle_ocr = PaddleOCR(
    lang="ru",
    ocr_version="PP-OCRv4",
    use_doc_orientation_classify=False,
    use_doc_unwarping=False,
    use_textline_orientation=False
)


def ocr_sensors(rois: list[np.ndarray]) -> list[dict]:
    """
    OCR областей сенсоров через PaddleOCR.predict().
    Формат вывода:
        [{"text": str, "score": float}, ...]
    """
    results = []

    if not rois:
        return []

    try:
        ocr_results = paddle_ocr.predict(rois)
    except Exception as e:
        print(f"⚠ Ошибка OCR.predict: {e}")
        return [{"text": "?", "score": 0.0} for _ in rois]

    for out in ocr_results:
        
        texts = out.get("rec_texts", ["?"])
        scores = out.get("rec_scores", [0.0])

        # --- распаковка ---
        text = texts[0] if texts else "?"
        score = scores[0] if scores else 0.0

        results.append({"text": text, "score": round(score, 2)})

    return results