Hayk Arutyunyan commited on
Commit
d456ca5
·
1 Parent(s): 387ddd5

Fix pipeline code for OCR ensors

Browse files
Files changed (1) hide show
  1. src/pipeline_hf.py +13 -34
src/pipeline_hf.py CHANGED
@@ -6,39 +6,27 @@ from pathlib import Path
6
  from src.config_loader import load_config
7
  from src.ocr_utils_demo import ocr_title, ocr_sensors
8
 
9
- def process_single_image(img: np.ndarray, cfg_path: str | Path = "configs/config.yaml"):
10
  """
11
  Обрабатывает одно изображение:
12
  - вытаскивает титул;
13
  - находит и оцифровывает сенсоры;
14
  - возвращает структуру с результатом.
15
  """
16
- cfg = load_config(cfg_path)
17
-
18
- # Цветовые диапазоны сенсоров
19
- color_ranges_cfg = cfg["colors"]
20
-
21
- color_ranges = []
22
-
23
- for key, rng in color_ranges_cfg.items():
24
- lo = np.array(rng[0], dtype=np.uint8)
25
- hi = np.array(rng[1], dtype=np.uint8)
26
- color_ranges.append((lo, hi))
27
 
28
  # ---------- 1. Оцифровка титула ----------
29
  h, w = img.shape[:2]
30
-
31
- img_bgr = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
32
-
33
- title_roi = img_bgr[:45, :int(w / 2.4)]
34
  title_text = ocr_title(title_roi)
35
 
36
  # ---------- 2. Оцифровка сенсоров ----------
37
- hsv = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2HSV)
38
 
39
  mask = None
40
 
41
- for lo_np, hi_np in color_ranges:
 
 
42
  cur = cv2.inRange(hsv, lo_np, hi_np)
43
  mask = cur if mask is None else cv2.bitwise_or(mask, cur)
44
 
@@ -50,10 +38,8 @@ def process_single_image(img: np.ndarray, cfg_path: str | Path = "configs/config
50
  if ww < 90 or hh < 17:
51
  continue
52
 
53
- #hh_clamped = min(hh, 17)
54
-
55
- roi = img_bgr[y:y + hh, x:x + ww]
56
-
57
  rois.append(roi)
58
  positions.append((x, y, ww, hh))
59
 
@@ -72,11 +58,7 @@ def process_single_image(img: np.ndarray, cfg_path: str | Path = "configs/config
72
  "h": hh
73
  })
74
 
75
- return {
76
- "title": title_text,
77
- "sensors": sensors
78
- }
79
-
80
 
81
  # ---------------------------------------------------------
82
  # Основной pipeline → Excel
@@ -89,22 +71,19 @@ def process_image_to_excel(cfg: dict):
89
  output_dir.mkdir(parents=True, exist_ok=True)
90
  excel_path = output_dir / excel_name
91
 
 
 
92
  results = []
93
 
94
  for img_path in input_dir.glob("*.png"):
95
  name = img_path.name
96
-
97
  img = cv2.imread(str(img_path))
 
98
  if img is None:
99
  print(f"Не могу прочитать файл {name}")
100
  continue
101
 
102
- img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
103
-
104
- res = process_single_image(img_rgb, cfg_path="configs/config.yaml")
105
-
106
- title_text = res["title"]
107
- sensors = res["sensors"]
108
 
109
  for sen in sensors:
110
  results.append({
 
6
  from src.config_loader import load_config
7
  from src.ocr_utils_demo import ocr_title, ocr_sensors
8
 
9
+ def process_single_image(img: np.ndarray, color_ranges: dict):
10
  """
11
  Обрабатывает одно изображение:
12
  - вытаскивает титул;
13
  - находит и оцифровывает сенсоры;
14
  - возвращает структуру с результатом.
15
  """
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  # ---------- 1. Оцифровка титула ----------
18
  h, w = img.shape[:2]
19
+ title_roi = img[:45, :int(w / 2.4)]
 
 
 
20
  title_text = ocr_title(title_roi)
21
 
22
  # ---------- 2. Оцифровка сенсоров ----------
23
+ hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
24
 
25
  mask = None
26
 
27
+ for (lo, hi) in color_ranges.values():
28
+ lo_np = np.array(lo, dtype=np.uint8)
29
+ hi_np = np.array(hi, dtype=np.uint8)
30
  cur = cv2.inRange(hsv, lo_np, hi_np)
31
  mask = cur if mask is None else cv2.bitwise_or(mask, cur)
32
 
 
38
  if ww < 90 or hh < 17:
39
  continue
40
 
41
+ hh_clamped = min(hh, 17)
42
+ roi = img[y:y + hh_clamped, x:x + ww]
 
 
43
  rois.append(roi)
44
  positions.append((x, y, ww, hh))
45
 
 
58
  "h": hh
59
  })
60
 
61
+ return title_text, sensors
 
 
 
 
62
 
63
  # ---------------------------------------------------------
64
  # Основной pipeline → Excel
 
71
  output_dir.mkdir(parents=True, exist_ok=True)
72
  excel_path = output_dir / excel_name
73
 
74
+ color_ranges = cfg["colors"]
75
+
76
  results = []
77
 
78
  for img_path in input_dir.glob("*.png"):
79
  name = img_path.name
 
80
  img = cv2.imread(str(img_path))
81
+
82
  if img is None:
83
  print(f"Не могу прочитать файл {name}")
84
  continue
85
 
86
+ title_text, sensors = process_single_image(img, color_ranges)
 
 
 
 
 
87
 
88
  for sen in sensors:
89
  results.append({