Hayk Arutyunyan commited on
Commit ·
81fcb4f
1
Parent(s): 60b90b9
Add filename and title export to Excel; update hf_process to build DataFrame from title + sensors; improve file name handling
Browse files- app.py +18 -4
- src/ocr_utils_demo.py +1 -16
- src/pipeline_hf.py +0 -4
app.py
CHANGED
|
@@ -28,6 +28,9 @@ def draw_boxes(img_rgb, sensors):
|
|
| 28 |
def hf_process(img):
|
| 29 |
|
| 30 |
cfg = load_config("configs/config.yaml")
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Запуск пайплайна
|
| 33 |
title, sensors = process_single_image(img, color_ranges=cfg["colors"])
|
|
@@ -36,8 +39,19 @@ def hf_process(img):
|
|
| 36 |
boxed = draw_boxes(img, sensors)
|
| 37 |
|
| 38 |
# --- DataFrame сенсоров ---
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# --- Excel на скачивание ---
|
| 43 |
tmp = NamedTemporaryFile(delete=False, suffix=".xlsx")
|
|
@@ -46,8 +60,8 @@ def hf_process(img):
|
|
| 46 |
return (
|
| 47 |
boxed,
|
| 48 |
title,
|
| 49 |
-
|
| 50 |
-
|
| 51 |
)
|
| 52 |
|
| 53 |
|
|
|
|
| 28 |
def hf_process(img):
|
| 29 |
|
| 30 |
cfg = load_config("configs/config.yaml")
|
| 31 |
+
|
| 32 |
+
# Имя файла (если доступно)
|
| 33 |
+
filename = getattr(img, "name", "uploaded_image.png")
|
| 34 |
|
| 35 |
# Запуск пайплайна
|
| 36 |
title, sensors = process_single_image(img, color_ranges=cfg["colors"])
|
|
|
|
| 39 |
boxed = draw_boxes(img, sensors)
|
| 40 |
|
| 41 |
# --- DataFrame сенсоров ---
|
| 42 |
+
df = pd.DataFrame([
|
| 43 |
+
{
|
| 44 |
+
"filename": filename,
|
| 45 |
+
"title": title,
|
| 46 |
+
"text": s["text"],
|
| 47 |
+
"score": s["score"],
|
| 48 |
+
"x": s["x"],
|
| 49 |
+
"y": s["y"],
|
| 50 |
+
"w": s["w"],
|
| 51 |
+
"h": s["h"]
|
| 52 |
+
}
|
| 53 |
+
for s in sensors
|
| 54 |
+
])
|
| 55 |
|
| 56 |
# --- Excel на скачивание ---
|
| 57 |
tmp = NamedTemporaryFile(delete=False, suffix=".xlsx")
|
|
|
|
| 60 |
return (
|
| 61 |
boxed,
|
| 62 |
title,
|
| 63 |
+
tmp.name, # путь к скачиваемому файлу
|
| 64 |
+
df
|
| 65 |
)
|
| 66 |
|
| 67 |
|
src/ocr_utils_demo.py
CHANGED
|
@@ -63,7 +63,6 @@ def ocr_sensors(rois: list[np.ndarray]):
|
|
| 63 |
for roi in rois:
|
| 64 |
try:
|
| 65 |
ocr_res = paddle_ocr.ocr(roi, det=False, cls=False)
|
| 66 |
-
print(f"Результат оцифровки: {ocr_res}") # для отладки
|
| 67 |
except Exception as e:
|
| 68 |
print(f"⚠ Ошибка OCR.ocr: {e}")
|
| 69 |
results.append({"text": "?", "score": 0.0})
|
|
@@ -89,18 +88,4 @@ def ocr_sensors(rois: list[np.ndarray]):
|
|
| 89 |
"score": float(score) if score else 0.0
|
| 90 |
})
|
| 91 |
|
| 92 |
-
return results
|
| 93 |
-
|
| 94 |
-
"""
|
| 95 |
-
|
| 96 |
-
for out in ocr_results:
|
| 97 |
-
|
| 98 |
-
texts = out.get("rec_texts", ["?"])
|
| 99 |
-
scores = out.get("rec_scores", [0.0])
|
| 100 |
-
|
| 101 |
-
# --- распаковка ---
|
| 102 |
-
text = texts[0] if texts else "?"
|
| 103 |
-
score = scores[0] if scores else 0.0
|
| 104 |
-
|
| 105 |
-
results.append({"text": text, "score": round(score, 2)})
|
| 106 |
-
"""
|
|
|
|
| 63 |
for roi in rois:
|
| 64 |
try:
|
| 65 |
ocr_res = paddle_ocr.ocr(roi, det=False, cls=False)
|
|
|
|
| 66 |
except Exception as e:
|
| 67 |
print(f"⚠ Ошибка OCR.ocr: {e}")
|
| 68 |
results.append({"text": "?", "score": 0.0})
|
|
|
|
| 88 |
"score": float(score) if score else 0.0
|
| 89 |
})
|
| 90 |
|
| 91 |
+
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/pipeline_hf.py
CHANGED
|
@@ -31,23 +31,19 @@ def process_single_image(img: np.ndarray, color_ranges: dict):
|
|
| 31 |
mask = cur if mask is None else cv2.bitwise_or(mask, cur)
|
| 32 |
|
| 33 |
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 34 |
-
print("Contours found:", len(contours)) # для отладки
|
| 35 |
|
| 36 |
rois, positions = [], []
|
| 37 |
for cnt in contours:
|
| 38 |
x, y, ww, hh = cv2.boundingRect(cnt)
|
| 39 |
-
print("Contour bbox:", x, y, ww, hh) # для отладки
|
| 40 |
if ww < 90 or hh < 17:
|
| 41 |
continue
|
| 42 |
if hh / ww > 1.5:
|
| 43 |
continue
|
| 44 |
|
| 45 |
-
print("ROI accepted:", ww, hh) # для отладки
|
| 46 |
hh_clamped = min(hh, 17)
|
| 47 |
roi = img[y:y + hh_clamped, x:x + ww]
|
| 48 |
rois.append(roi)
|
| 49 |
positions.append((x, y, ww, hh))
|
| 50 |
-
print("ROIs after filtering:", len(rois)) # для отладки
|
| 51 |
|
| 52 |
# ---------- 3. OCR сенсоров ----------
|
| 53 |
sensors = []
|
|
|
|
| 31 |
mask = cur if mask is None else cv2.bitwise_or(mask, cur)
|
| 32 |
|
| 33 |
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
|
|
|
| 34 |
|
| 35 |
rois, positions = [], []
|
| 36 |
for cnt in contours:
|
| 37 |
x, y, ww, hh = cv2.boundingRect(cnt)
|
|
|
|
| 38 |
if ww < 90 or hh < 17:
|
| 39 |
continue
|
| 40 |
if hh / ww > 1.5:
|
| 41 |
continue
|
| 42 |
|
|
|
|
| 43 |
hh_clamped = min(hh, 17)
|
| 44 |
roi = img[y:y + hh_clamped, x:x + ww]
|
| 45 |
rois.append(roi)
|
| 46 |
positions.append((x, y, ww, hh))
|
|
|
|
| 47 |
|
| 48 |
# ---------- 3. OCR сенсоров ----------
|
| 49 |
sensors = []
|