Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image, ImageDraw
|
4 |
+
from huggingface_hub import hf_hub_download
|
5 |
+
# carica SAM e GroundingDINO direttamente dall’HF Hub
|
6 |
+
SAM_CHECKPOINT = hf_hub_download("facebook/sam-vit-base", "sam_vit_b.pth")
|
7 |
+
GDINO_CONFIG = hf_hub_download("IDEA-Research/GroundingDINO", "GroundingDINO_SwinT_OGC.py")
|
8 |
+
GDINO_CHECKPT = hf_hub_download("IDEA-Research/GroundingDINO", "groundingdino_swint_ogc.pth")
|
9 |
+
|
10 |
+
# — pseudocodice di import —
|
11 |
+
from segment_anything import sam_model
|
12 |
+
from groundingdino.util.inference import load_model, predict
|
13 |
+
|
14 |
+
sam = sam_model.load_from_checkpoint(SAM_CHECKPOINT)
|
15 |
+
gdino = load_model(GDINO_CONFIG, GDINO_CHECKPT)
|
16 |
+
|
17 |
+
def recognize(img, prompt, conf):
|
18 |
+
masks = sam.segment(img) # 1. segmentazione zero‑shot
|
19 |
+
out = Image.fromarray(img).convert("RGBA")
|
20 |
+
draw = ImageDraw.Draw(out, "RGBA")
|
21 |
+
results = []
|
22 |
+
for m in masks:
|
23 |
+
label, score = predict(gdino, img, m, prompt) # 2. classificazione zero‑shot
|
24 |
+
if score<conf: continue
|
25 |
+
yy, xx = np.where(m)
|
26 |
+
bbox = (xx.min(), yy.min(), xx.max(), yy.max())
|
27 |
+
area = int(m.sum())
|
28 |
+
draw.rectangle(bbox, outline=(255,0,0,180), width=3)
|
29 |
+
draw.text((bbox[0], bbox[1]-10), f"{label} {score:.2f}", fill=(255,0,0,180))
|
30 |
+
results.append({"label":label, "score":score, "area":area, "bbox":bbox})
|
31 |
+
return np.array(out), results
|
32 |
+
|
33 |
+
app = gr.Interface(
|
34 |
+
fn=recognize,
|
35 |
+
inputs=[
|
36 |
+
gr.Image(type="numpy", label="Upload Image"),
|
37 |
+
gr.Textbox(label="Prompt (comma‑separated)"),
|
38 |
+
gr.Slider(0,1,0.25, label="Confidence Threshold"),
|
39 |
+
],
|
40 |
+
outputs=[
|
41 |
+
gr.Image(label="Overlay"),
|
42 |
+
gr.JSON(label="Detections")
|
43 |
+
],
|
44 |
+
title="Zero‑Shot Component Recognition",
|
45 |
+
description="Segmenta e classifica componenti meccanici da foto, senza training specifico."
|
46 |
+
)
|
47 |
+
|
48 |
+
if __name__=="__main__":
|
49 |
+
app.launch()
|