Spaces:
Sleeping
Sleeping
Kesheratmex
commited on
Commit
Β·
fc24e72
1
Parent(s):
735205a
Add simple metrics persistence for image/video jobs
Browse filesTrack total jobs, videos, images, detections, perβlabel counts, and expose the
metrics via a UI button. This adds persistence to `/tmp/blade_metrics.json`
and helper functions to load, save, and record metrics.
app.py
CHANGED
|
@@ -56,6 +56,59 @@ def get_model():
|
|
| 56 |
model = YOLO("best2.pt")
|
| 57 |
return model
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
# ββββββββββββββββββββββββββββ
|
| 60 |
# Funciones de Inferencia
|
| 61 |
# ββββββββββββββββββββββββββββ
|
|
@@ -129,6 +182,8 @@ def infer_media(media_path, conf=0.25, iou=0.45, out_res="720p", preset="default
|
|
| 129 |
if cap:
|
| 130 |
cap.release()
|
| 131 |
|
|
|
|
|
|
|
| 132 |
return {"video": out_vid, "classes": counts}
|
| 133 |
|
| 134 |
# β Imagen ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -154,6 +209,8 @@ def infer_media(media_path, conf=0.25, iou=0.45, out_res="720p", preset="default
|
|
| 154 |
|
| 155 |
out_path = os.path.join(tmpdir, "annotated.png")
|
| 156 |
cv2.imwrite(out_path, img)
|
|
|
|
|
|
|
| 157 |
return {"path": out_path, "classes": counts}
|
| 158 |
|
| 159 |
else:
|
|
@@ -775,5 +832,10 @@ with gr.Blocks(
|
|
| 775 |
demo.queue()
|
| 776 |
|
| 777 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 778 |
# Permitir acceso de descarga a directorio temporal para evitar 403
|
| 779 |
demo.launch(allowed_paths=[tempfile.gettempdir()])
|
|
|
|
| 56 |
model = YOLO("best2.pt")
|
| 57 |
return model
|
| 58 |
|
| 59 |
+
# ββββββββββββββββββββββββββββ
|
| 60 |
+
# MΓ©tricas simples (persistidas en /tmp)
|
| 61 |
+
# ββββββββββββββββββββββββββββ
|
| 62 |
+
METRICS_PATH = os.path.join(tempfile.gettempdir(), "blade_metrics.json")
|
| 63 |
+
|
| 64 |
+
def _load_metrics():
|
| 65 |
+
try:
|
| 66 |
+
if os.path.exists(METRICS_PATH):
|
| 67 |
+
with open(METRICS_PATH, "r", encoding="utf-8") as f:
|
| 68 |
+
return json.load(f)
|
| 69 |
+
except Exception:
|
| 70 |
+
pass
|
| 71 |
+
return {
|
| 72 |
+
"total_jobs": 0,
|
| 73 |
+
"videos": 0,
|
| 74 |
+
"images": 0,
|
| 75 |
+
"detections_total": 0,
|
| 76 |
+
"per_label": {},
|
| 77 |
+
"last_job": None,
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def _save_metrics(m):
|
| 82 |
+
try:
|
| 83 |
+
with open(METRICS_PATH, "w", encoding="utf-8") as f:
|
| 84 |
+
json.dump(m, f, ensure_ascii=False, indent=2)
|
| 85 |
+
except Exception:
|
| 86 |
+
pass
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def _record_metrics(job_type, counts):
|
| 90 |
+
m = _load_metrics()
|
| 91 |
+
m["total_jobs"] += 1
|
| 92 |
+
if job_type == "video":
|
| 93 |
+
m["videos"] += 1
|
| 94 |
+
elif job_type == "image":
|
| 95 |
+
m["images"] += 1
|
| 96 |
+
dets = int(sum(counts.values())) if isinstance(counts, dict) else 0
|
| 97 |
+
m["detections_total"] += dets
|
| 98 |
+
# per label aggregate
|
| 99 |
+
if isinstance(counts, dict):
|
| 100 |
+
per = m.get("per_label", {})
|
| 101 |
+
for k, v in counts.items():
|
| 102 |
+
per[k] = int(per.get(k, 0)) + int(v)
|
| 103 |
+
m["per_label"] = per
|
| 104 |
+
m["last_job"] = {"type": job_type, "detections": dets}
|
| 105 |
+
_save_metrics(m)
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def get_metrics():
|
| 109 |
+
"""Devuelve el snapshot actual de mΓ©tricas."""
|
| 110 |
+
return _load_metrics()
|
| 111 |
+
|
| 112 |
# ββββββββββββββββββββββββββββ
|
| 113 |
# Funciones de Inferencia
|
| 114 |
# ββββββββββββββββββββββββββββ
|
|
|
|
| 182 |
if cap:
|
| 183 |
cap.release()
|
| 184 |
|
| 185 |
+
# registrar mΓ©tricas
|
| 186 |
+
_record_metrics("video", counts)
|
| 187 |
return {"video": out_vid, "classes": counts}
|
| 188 |
|
| 189 |
# β Imagen ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 209 |
|
| 210 |
out_path = os.path.join(tmpdir, "annotated.png")
|
| 211 |
cv2.imwrite(out_path, img)
|
| 212 |
+
# registrar mΓ©tricas
|
| 213 |
+
_record_metrics("image", counts)
|
| 214 |
return {"path": out_path, "classes": counts}
|
| 215 |
|
| 216 |
else:
|
|
|
|
| 832 |
demo.queue()
|
| 833 |
|
| 834 |
if __name__ == "__main__":
|
| 835 |
+
# MΓ©tricas (UI)
|
| 836 |
+
btn_metrics = gr.Button("Ver mΓ©tricas")
|
| 837 |
+
out_metrics = gr.JSON(label="MΓ©tricas", visible=True)
|
| 838 |
+
btn_metrics.click(fn=get_metrics, outputs=out_metrics, api_name="metrics")
|
| 839 |
+
|
| 840 |
# Permitir acceso de descarga a directorio temporal para evitar 403
|
| 841 |
demo.launch(allowed_paths=[tempfile.gettempdir()])
|