Spaces:
Running
on
Zero
Running
on
Zero
chore: adding demo
Browse files- .gitattributes +1 -0
- app.py +189 -0
- image.jpg +3 -0
- requirements.txt +63 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,189 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from transformers.image_utils import load_image
|
4 |
+
|
5 |
+
checkpoints = [
|
6 |
+
'ustc-community/dfine_n_coco',
|
7 |
+
'ustc-community/dfine_s_coco',
|
8 |
+
'ustc-community/dfine_m_coco',
|
9 |
+
'ustc-community/dfine_l_coco',
|
10 |
+
'ustc-community/dfine_x_coco',
|
11 |
+
'ustc-community/dfine_s_obj365',
|
12 |
+
'ustc-community/dfine_m_obj365',
|
13 |
+
'ustc-community/dfine_l_obj365',
|
14 |
+
'ustc-community/dfine_x_obj365',
|
15 |
+
'ustc-community/dfine_s_obj2coco',
|
16 |
+
'ustc-community/dfine_m_obj2coco',
|
17 |
+
'ustc-community/dfine_l_obj2coco_e25',
|
18 |
+
'ustc-community/dfine_x_obj2coco',
|
19 |
+
]
|
20 |
+
|
21 |
+
def detect_objects(image, checkpoint, confidence_threshold=0.3, use_url=False, url=""):
|
22 |
+
pipe = pipeline(
|
23 |
+
"object-detection",
|
24 |
+
model=checkpoint,
|
25 |
+
image_processor=checkpoint,
|
26 |
+
device="cpu",
|
27 |
+
)
|
28 |
+
|
29 |
+
if use_url and url:
|
30 |
+
input_image = load_image(url)
|
31 |
+
elif image is not None:
|
32 |
+
input_image = image
|
33 |
+
else:
|
34 |
+
return None, gr.Markdown("**Error**: Please provide an image or URL.", visible=True)
|
35 |
+
|
36 |
+
# Run detection
|
37 |
+
results = pipe(input_image, threshold=confidence_threshold)
|
38 |
+
|
39 |
+
# Get image dimensions for validation
|
40 |
+
img_width, img_height = input_image.size
|
41 |
+
|
42 |
+
# Prepare annotations in the format: list of (bounding_box, label)
|
43 |
+
annotations = []
|
44 |
+
for result in results:
|
45 |
+
score = result["score"]
|
46 |
+
if score < confidence_threshold:
|
47 |
+
continue
|
48 |
+
label = f"{result['label']} ({score:.2f})"
|
49 |
+
box = result["box"]
|
50 |
+
# Validate and convert box to (x1, y1, x2, y2)
|
51 |
+
x1 = max(0, int(box["xmin"]))
|
52 |
+
y1 = max(0, int(box["ymin"]))
|
53 |
+
x2 = min(img_width, int(box["xmax"]))
|
54 |
+
y2 = min(img_height, int(box["ymax"]))
|
55 |
+
# Ensure valid box
|
56 |
+
if x2 <= x1 or y2 <= y1:
|
57 |
+
continue
|
58 |
+
bounding_box = (x1, y1, x2, y2)
|
59 |
+
annotations.append((bounding_box, label))
|
60 |
+
|
61 |
+
# Handle empty annotations
|
62 |
+
if not annotations:
|
63 |
+
return (input_image, []), gr.Markdown(
|
64 |
+
"**Warning**: No objects detected above the confidence threshold. Try lowering the threshold.",
|
65 |
+
visible=True
|
66 |
+
)
|
67 |
+
|
68 |
+
# Return base image and annotations
|
69 |
+
return (input_image, annotations), gr.Markdown(visible=False)
|
70 |
+
|
71 |
+
# Gradio interface
|
72 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
73 |
+
gr.Markdown(
|
74 |
+
"""
|
75 |
+
# Real-Time Object Detection Demo
|
76 |
+
Experience state-of-the-art object detection with USTC's Dfine models. Upload an image, provide a URL, or try an example below. Select a model and adjust the confidence threshold to see detections in real time!
|
77 |
+
|
78 |
+
**Instructions**:
|
79 |
+
- Upload an image or enter a URL.
|
80 |
+
- Choose a model checkpoint from the dropdown.
|
81 |
+
- Adjust the confidence threshold (0.1 to 1.0).
|
82 |
+
- Click "Detect Objects" to view results, or select an example.
|
83 |
+
- Use "Clear" to reset inputs and outputs.
|
84 |
+
""",
|
85 |
+
elem_classes="header-text"
|
86 |
+
)
|
87 |
+
|
88 |
+
with gr.Row():
|
89 |
+
with gr.Column(scale=1, min_width=300):
|
90 |
+
with gr.Group():
|
91 |
+
image_input = gr.Image(
|
92 |
+
label="Upload Image",
|
93 |
+
type="pil",
|
94 |
+
sources=["upload", "webcam"],
|
95 |
+
interactive=True,
|
96 |
+
elem_classes="input-component",
|
97 |
+
)
|
98 |
+
use_url = gr.Checkbox(label="Use Image URL Instead", value=False)
|
99 |
+
url_input = gr.Textbox(
|
100 |
+
label="Image URL",
|
101 |
+
placeholder="https://example.com/image.jpg",
|
102 |
+
visible=False,
|
103 |
+
elem_classes="input-component",
|
104 |
+
)
|
105 |
+
checkpoint = gr.Dropdown(
|
106 |
+
choices=checkpoints,
|
107 |
+
label="Select Model Checkpoint",
|
108 |
+
value=checkpoints[0],
|
109 |
+
elem_classes="input-component",
|
110 |
+
)
|
111 |
+
confidence_threshold = gr.Slider(
|
112 |
+
minimum=0.1,
|
113 |
+
maximum=1.0,
|
114 |
+
value=0.3,
|
115 |
+
step=0.1,
|
116 |
+
label="Confidence Threshold",
|
117 |
+
elem_classes="input-component",
|
118 |
+
)
|
119 |
+
with gr.Row():
|
120 |
+
detect_button = gr.Button(
|
121 |
+
"Detect Objects",
|
122 |
+
variant="primary",
|
123 |
+
elem_classes="action-button",
|
124 |
+
)
|
125 |
+
clear_button = gr.Button(
|
126 |
+
"Clear",
|
127 |
+
variant="secondary",
|
128 |
+
elem_classes="action-button",
|
129 |
+
)
|
130 |
+
|
131 |
+
with gr.Column(scale=2):
|
132 |
+
output_annotated = gr.AnnotatedImage(
|
133 |
+
label="Detection Results",
|
134 |
+
show_label=True,
|
135 |
+
color_map=None, # Let Gradio assign colors
|
136 |
+
elem_classes="output-component",
|
137 |
+
)
|
138 |
+
error_message = gr.Markdown(visible=False, elem_classes="error-text")
|
139 |
+
|
140 |
+
gr.Examples(
|
141 |
+
examples=[
|
142 |
+
["./image.jpg", False, "", checkpoints[0], 0.3],
|
143 |
+
[None, True, "https://live.staticflickr.com/65535/33021460783_1646d43c54_b.jpg", checkpoints[0], 0.3],
|
144 |
+
],
|
145 |
+
inputs=[image_input, use_url, url_input, checkpoint, confidence_threshold],
|
146 |
+
outputs=[output_annotated, error_message],
|
147 |
+
fn=detect_objects,
|
148 |
+
cache_examples=False, # Avoid caching due to model size
|
149 |
+
label="Select an example to run the model",
|
150 |
+
)
|
151 |
+
|
152 |
+
# Dynamic visibility for URL input
|
153 |
+
use_url.change(
|
154 |
+
fn=lambda x: gr.update(visible=x),
|
155 |
+
inputs=use_url,
|
156 |
+
outputs=url_input,
|
157 |
+
)
|
158 |
+
|
159 |
+
# Clear button functionality
|
160 |
+
clear_button.click(
|
161 |
+
fn=lambda: (
|
162 |
+
None, # image_input
|
163 |
+
False, # use_url
|
164 |
+
"", # url_input
|
165 |
+
checkpoints[0], # checkpoint
|
166 |
+
0.3, # confidence_threshold
|
167 |
+
None, # output_annotated
|
168 |
+
gr.Markdown(visible=False), # error_message
|
169 |
+
),
|
170 |
+
outputs=[
|
171 |
+
image_input,
|
172 |
+
use_url,
|
173 |
+
url_input,
|
174 |
+
checkpoint,
|
175 |
+
confidence_threshold,
|
176 |
+
output_annotated,
|
177 |
+
error_message,
|
178 |
+
],
|
179 |
+
)
|
180 |
+
|
181 |
+
# Detect button event
|
182 |
+
detect_button.click(
|
183 |
+
fn=detect_objects,
|
184 |
+
inputs=[image_input, checkpoint, confidence_threshold, use_url, url_input],
|
185 |
+
outputs=[output_annotated, error_message],
|
186 |
+
)
|
187 |
+
|
188 |
+
if __name__ == "__main__":
|
189 |
+
demo.launch()
|
image.jpg
ADDED
![]() |
Git LFS Details
|
requirements.txt
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
aiofiles==24.1.0
|
2 |
+
annotated-types==0.7.0
|
3 |
+
anyio==4.9.0
|
4 |
+
certifi==2025.4.26
|
5 |
+
charset-normalizer==3.4.1
|
6 |
+
click==8.1.8
|
7 |
+
fastapi==0.115.12
|
8 |
+
ffmpy==0.5.0
|
9 |
+
filelock==3.18.0
|
10 |
+
fsspec==2025.3.2
|
11 |
+
gradio==5.28.0
|
12 |
+
gradio-client==1.10.0
|
13 |
+
groovy==0.1.2
|
14 |
+
h11==0.16.0
|
15 |
+
httpcore==1.0.9
|
16 |
+
httpx==0.28.1
|
17 |
+
huggingface-hub==0.30.2
|
18 |
+
idna==3.10
|
19 |
+
jinja2==3.1.6
|
20 |
+
markdown-it-py==3.0.0
|
21 |
+
markupsafe==3.0.2
|
22 |
+
mdurl==0.1.2
|
23 |
+
mpmath==1.3.0
|
24 |
+
networkx==3.4.2
|
25 |
+
numpy==2.2.5
|
26 |
+
orjson==3.10.18
|
27 |
+
packaging==25.0
|
28 |
+
pandas==2.2.3
|
29 |
+
pillow==11.2.1
|
30 |
+
pydantic==2.11.4
|
31 |
+
pydantic-core==2.33.2
|
32 |
+
pydub==0.25.1
|
33 |
+
pygments==2.19.1
|
34 |
+
python-dateutil==2.9.0.post0
|
35 |
+
python-multipart==0.0.20
|
36 |
+
pytz==2025.2
|
37 |
+
pyyaml==6.0.2
|
38 |
+
regex==2024.11.6
|
39 |
+
requests==2.32.3
|
40 |
+
rich==14.0.0
|
41 |
+
ruff==0.11.8
|
42 |
+
safehttpx==0.1.6
|
43 |
+
safetensors==0.5.3
|
44 |
+
semantic-version==2.10.0
|
45 |
+
setuptools==80.1.0
|
46 |
+
shellingham==1.5.4
|
47 |
+
six==1.17.0
|
48 |
+
sniffio==1.3.1
|
49 |
+
starlette==0.46.2
|
50 |
+
sympy==1.14.0
|
51 |
+
tokenizers==0.21.1
|
52 |
+
tomlkit==0.13.2
|
53 |
+
torch==2.7.0
|
54 |
+
torchvision==0.22.0
|
55 |
+
tqdm==4.67.1
|
56 |
+
transformers @ git+https://github.com/huggingface/transformers@ee25d57ed18f2dc06e88bd041830c6a32f80ff88
|
57 |
+
typer==0.15.3
|
58 |
+
typing-extensions==4.13.2
|
59 |
+
typing-inspection==0.4.0
|
60 |
+
tzdata==2025.2
|
61 |
+
urllib3==2.4.0
|
62 |
+
uvicorn==0.34.2
|
63 |
+
websockets==15.0.1
|