Hayk Arutyunyan commited on
Commit
bd636c2
·
1 Parent(s): 9eebbbf

Fix ocr code

Browse files
Files changed (2) hide show
  1. src/ocr_utils_demo.py +18 -8
  2. src/pipeline_hf.py +2 -2
src/ocr_utils_demo.py CHANGED
@@ -14,7 +14,7 @@ _reader_title = easyocr.Reader(['en'], gpu=False)
14
  # --------------------------
15
  # OCR титула (EasyOCR)
16
  # --------------------------
17
- def ocr_title(img: np.ndarray) -> str:
18
  """
19
  OCR верхней области (титул мнемосхемы).
20
  """
@@ -49,26 +49,36 @@ paddle_ocr = PaddleOCR(
49
  )
50
 
51
 
52
- def ocr_sensors(rois: list[np.ndarray]) -> list[dict]:
53
  """
54
  OCR областей сенсоров через PaddleOCR.predict().
55
  Формат вывода:
56
  [{"text": str, "score": float}, ...]
57
  """
58
  results = []
59
- rois = []
60
 
61
  if not rois:
62
- return results
63
 
64
  for roi in rois:
65
  try:
66
- result = paddle_ocr.ocr(roi)
67
- print(result) # для отладки
68
  except Exception as e:
69
  print(f"⚠ Ошибка OCR.ocr: {e}")
70
- return [{"text": "?", "score": 0.0} for _ in rois]
71
- results.append(result)
 
 
 
 
 
 
 
 
 
 
 
72
 
73
  return results
74
 
 
14
  # --------------------------
15
  # OCR титула (EasyOCR)
16
  # --------------------------
17
+ def ocr_title(img: np.ndarray):
18
  """
19
  OCR верхней области (титул мнемосхемы).
20
  """
 
49
  )
50
 
51
 
52
+ def ocr_sensors(rois: list[np.ndarray]):
53
  """
54
  OCR областей сенсоров через PaddleOCR.predict().
55
  Формат вывода:
56
  [{"text": str, "score": float}, ...]
57
  """
58
  results = []
 
59
 
60
  if not rois:
61
+ return []
62
 
63
  for roi in rois:
64
  try:
65
+ ocr_res = paddle_ocr.ocr(roi, det=False, cls=False)
66
+ print(ocr_res) # для отладки
67
  except Exception as e:
68
  print(f"⚠ Ошибка OCR.ocr: {e}")
69
+ results.append({"text": "?", "score": 0.0})
70
+ continue
71
+
72
+ if not ocr_res or not ocr_res[0]:
73
+ results.append({"text": "?", "score": 0.0})
74
+ continue
75
+
76
+ text, score = ocr_res[0][1]
77
+
78
+ results.append({
79
+ "text": text,
80
+ "score": float(score)
81
+ })
82
 
83
  return results
84
 
src/pipeline_hf.py CHANGED
@@ -57,8 +57,8 @@ def process_single_image(img: np.ndarray, color_ranges: dict):
57
 
58
  for (x, y, ww, hh), result in zip(positions, ocr_results):
59
  sensors.append({
60
- "text": result[0],
61
- "score": result[1],
62
  "x": x,
63
  "y": y,
64
  "w": ww,
 
57
 
58
  for (x, y, ww, hh), result in zip(positions, ocr_results):
59
  sensors.append({
60
+ "text": result["text"],
61
+ "score": result["score"],
62
  "x": x,
63
  "y": y,
64
  "w": ww,