Spaces:
Running
Running
Orkhan Hasanli commited on
Commit ·
bb55c4d
1
Parent(s): bf32d2c
Add Threshold slider for min display confidence (default 0.7)
Browse files- app.py +11 -2
- dfine_jina_pipeline.py +8 -4
app.py
CHANGED
|
@@ -108,7 +108,7 @@ def run_detection(image, model):
|
|
| 108 |
return out_img, det_json
|
| 109 |
|
| 110 |
|
| 111 |
-
def run_dfine_classify(image, encoder_choice, refs_path):
|
| 112 |
"""Tab 2: D-FINE first, then classify crops with Jina or Nomic.
|
| 113 |
Returns (group_crop_gallery, known_crop_gallery, status_message).
|
| 114 |
"""
|
|
@@ -131,6 +131,7 @@ def run_dfine_classify(image, encoder_choice, refs_path):
|
|
| 131 |
gap_threshold=0.02,
|
| 132 |
min_side=24,
|
| 133 |
crop_dedup_iou=0.4,
|
|
|
|
| 134 |
)
|
| 135 |
|
| 136 |
if status is not None:
|
|
@@ -264,6 +265,14 @@ with gr.Blocks(title="Small Object Detection") as app:
|
|
| 264 |
|
| 265 |
with gr.Column(scale=1):
|
| 266 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
out_gallery_dfine = gr.Gallery(
|
| 268 |
label="Person/car crops (bboxes: gun, knife, cigarette, phone only)",
|
| 269 |
height=IMG_HEIGHT,
|
|
@@ -286,7 +295,7 @@ with gr.Blocks(title="Small Object Detection") as app:
|
|
| 286 |
|
| 287 |
btn_dfine.click(
|
| 288 |
fn=run_dfine_classify,
|
| 289 |
-
inputs=[inp_dfine, encoder_choice, refs_path],
|
| 290 |
outputs=[out_gallery_dfine, out_gallery_known, out_status_dfine],
|
| 291 |
concurrency_limit=1,
|
| 292 |
)
|
|
|
|
| 108 |
return out_img, det_json
|
| 109 |
|
| 110 |
|
| 111 |
+
def run_dfine_classify(image, encoder_choice, refs_path, min_display_conf=0.7):
|
| 112 |
"""Tab 2: D-FINE first, then classify crops with Jina or Nomic.
|
| 113 |
Returns (group_crop_gallery, known_crop_gallery, status_message).
|
| 114 |
"""
|
|
|
|
| 131 |
gap_threshold=0.02,
|
| 132 |
min_side=24,
|
| 133 |
crop_dedup_iou=0.4,
|
| 134 |
+
min_display_conf=float(min_display_conf),
|
| 135 |
)
|
| 136 |
|
| 137 |
if status is not None:
|
|
|
|
| 265 |
|
| 266 |
with gr.Column(scale=1):
|
| 267 |
|
| 268 |
+
threshold_slider = gr.Slider(
|
| 269 |
+
minimum=0.0,
|
| 270 |
+
maximum=1.0,
|
| 271 |
+
value=0.7,
|
| 272 |
+
step=0.05,
|
| 273 |
+
label="Threshold (min display confidence)",
|
| 274 |
+
)
|
| 275 |
+
|
| 276 |
out_gallery_dfine = gr.Gallery(
|
| 277 |
label="Person/car crops (bboxes: gun, knife, cigarette, phone only)",
|
| 278 |
height=IMG_HEIGHT,
|
|
|
|
| 295 |
|
| 296 |
btn_dfine.click(
|
| 297 |
fn=run_dfine_classify,
|
| 298 |
+
inputs=[inp_dfine, encoder_choice, refs_path, threshold_slider],
|
| 299 |
outputs=[out_gallery_dfine, out_gallery_known, out_status_dfine],
|
| 300 |
concurrency_limit=1,
|
| 301 |
)
|
dfine_jina_pipeline.py
CHANGED
|
@@ -533,6 +533,7 @@ def run_single_image(
|
|
| 533 |
min_side=40,
|
| 534 |
crop_dedup_iou=0.35,
|
| 535 |
squarify=True,
|
|
|
|
| 536 |
):
|
| 537 |
"""
|
| 538 |
Run D-FINE on one image, then classify small-object crops with Jina or Nomic.
|
|
@@ -546,6 +547,9 @@ def run_single_image(
|
|
| 546 |
- status_message: None on success, or error/empty-state string.
|
| 547 |
"""
|
| 548 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 549 |
from PIL import Image
|
| 550 |
|
| 551 |
global _APP_DFINE, _APP_JINA, _APP_NOMIC, _APP_REFS_JINA, _APP_REFS_NOMIC
|
|
@@ -703,7 +707,7 @@ def run_single_image(
|
|
| 703 |
|
| 704 |
boxes_to_draw = []
|
| 705 |
for (gidx2, (bx1, by1, bx2, by2), _crop_pil, pred, conf) in results_per_crop:
|
| 706 |
-
if gidx2 != gidx or pred not in KNOWN_DISPLAY_CLASSES or conf <
|
| 707 |
continue
|
| 708 |
# Convert to group-crop-relative coords and clamp
|
| 709 |
rx1 = max(0, min(crop_w, bx1 - gx1))
|
|
@@ -713,16 +717,16 @@ def run_single_image(
|
|
| 713 |
if rx2 > rx1 and ry2 > ry1:
|
| 714 |
boxes_to_draw.append((rx1, ry1, rx2, ry2, pred, conf))
|
| 715 |
|
| 716 |
-
# Only show this group crop if it has at least one known object >=
|
| 717 |
if not boxes_to_draw:
|
| 718 |
continue
|
| 719 |
group_crop = draw_bboxes_on_image(group_crop, boxes_to_draw)
|
| 720 |
group_crop_images.append(np.array(group_crop))
|
| 721 |
|
| 722 |
-
# Build known-only gallery: only objects with conf >=
|
| 723 |
known_crop_composites = []
|
| 724 |
for (_gidx, _box, crop_pil, pred, conf) in results_per_crop:
|
| 725 |
-
if pred not in KNOWN_DISPLAY_CLASSES or conf <
|
| 726 |
continue
|
| 727 |
composite = draw_label_on_image(crop_pil, pred, conf)
|
| 728 |
known_crop_composites.append(np.array(composite))
|
|
|
|
| 533 |
min_side=40,
|
| 534 |
crop_dedup_iou=0.35,
|
| 535 |
squarify=True,
|
| 536 |
+
min_display_conf=None,
|
| 537 |
):
|
| 538 |
"""
|
| 539 |
Run D-FINE on one image, then classify small-object crops with Jina or Nomic.
|
|
|
|
| 547 |
- status_message: None on success, or error/empty-state string.
|
| 548 |
"""
|
| 549 |
import numpy as np
|
| 550 |
+
|
| 551 |
+
if min_display_conf is None:
|
| 552 |
+
min_display_conf = MIN_DISPLAY_CONF
|
| 553 |
from PIL import Image
|
| 554 |
|
| 555 |
global _APP_DFINE, _APP_JINA, _APP_NOMIC, _APP_REFS_JINA, _APP_REFS_NOMIC
|
|
|
|
| 707 |
|
| 708 |
boxes_to_draw = []
|
| 709 |
for (gidx2, (bx1, by1, bx2, by2), _crop_pil, pred, conf) in results_per_crop:
|
| 710 |
+
if gidx2 != gidx or pred not in KNOWN_DISPLAY_CLASSES or conf < min_display_conf:
|
| 711 |
continue
|
| 712 |
# Convert to group-crop-relative coords and clamp
|
| 713 |
rx1 = max(0, min(crop_w, bx1 - gx1))
|
|
|
|
| 717 |
if rx2 > rx1 and ry2 > ry1:
|
| 718 |
boxes_to_draw.append((rx1, ry1, rx2, ry2, pred, conf))
|
| 719 |
|
| 720 |
+
# Only show this group crop if it has at least one known object >= min_display_conf
|
| 721 |
if not boxes_to_draw:
|
| 722 |
continue
|
| 723 |
group_crop = draw_bboxes_on_image(group_crop, boxes_to_draw)
|
| 724 |
group_crop_images.append(np.array(group_crop))
|
| 725 |
|
| 726 |
+
# Build known-only gallery: only objects with conf >= min_display_conf
|
| 727 |
known_crop_composites = []
|
| 728 |
for (_gidx, _box, crop_pil, pred, conf) in results_per_crop:
|
| 729 |
+
if pred not in KNOWN_DISPLAY_CLASSES or conf < min_display_conf:
|
| 730 |
continue
|
| 731 |
composite = draw_label_on_image(crop_pil, pred, conf)
|
| 732 |
known_crop_composites.append(np.array(composite))
|