Spaces:
Runtime error
Runtime error
piyush230502@gmail.com
commited on
Commit
·
cd6147f
1
Parent(s):
2353253
Add YOLOv12 Gradio app, requirements, and models
Browse files- app.py +166 -0
- brain_tumor.pt +3 -0
- requirements.txt +21 -0
- yolo11n-cls.pt +3 -0
- yolo11n-pose.pt +3 -0
- yolo11n.pt +3 -0
- yolo11s-seg.pt +3 -0
- yolov12l.pt +3 -0
- yolov12n.pt +3 -0
- yolov12s.pt +3 -0
- yolov12x.pt +3 -0
app.py
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# --------------------------------------------------------
|
| 2 |
+
# Based on yolov10
|
| 3 |
+
# https://github.com/THU-MIG/yolov10/app.py
|
| 4 |
+
# --------------------------------------------------------'
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import cv2
|
| 8 |
+
import tempfile
|
| 9 |
+
from ultralytics import YOLO
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def yolov12_inference(image, video, model_id, image_size, conf_threshold):
|
| 13 |
+
model = YOLO(model_id)
|
| 14 |
+
if image:
|
| 15 |
+
results = model.predict(source=image, imgsz=image_size, conf=conf_threshold)
|
| 16 |
+
annotated_image = results[0].plot()
|
| 17 |
+
return annotated_image[:, :, ::-1], None
|
| 18 |
+
else:
|
| 19 |
+
video_path = tempfile.mktemp(suffix=".webm")
|
| 20 |
+
with open(video_path, "wb") as f:
|
| 21 |
+
with open(video, "rb") as g:
|
| 22 |
+
f.write(g.read())
|
| 23 |
+
|
| 24 |
+
cap = cv2.VideoCapture(video_path)
|
| 25 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 26 |
+
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 27 |
+
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 28 |
+
|
| 29 |
+
output_video_path = tempfile.mktemp(suffix=".webm")
|
| 30 |
+
out = cv2.VideoWriter(output_video_path, cv2.VideoWriter_fourcc(*'vp80'), fps, (frame_width, frame_height))
|
| 31 |
+
|
| 32 |
+
while cap.isOpened():
|
| 33 |
+
ret, frame = cap.read()
|
| 34 |
+
if not ret:
|
| 35 |
+
break
|
| 36 |
+
|
| 37 |
+
results = model.predict(source=frame, imgsz=image_size, conf=conf_threshold)
|
| 38 |
+
annotated_frame = results[0].plot()
|
| 39 |
+
out.write(annotated_frame)
|
| 40 |
+
|
| 41 |
+
cap.release()
|
| 42 |
+
out.release()
|
| 43 |
+
|
| 44 |
+
return None, output_video_path
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def yolov12_inference_for_examples(image, model_path, image_size, conf_threshold):
|
| 48 |
+
annotated_image, _ = yolov12_inference(image, None, model_path, image_size, conf_threshold)
|
| 49 |
+
return annotated_image
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def app():
|
| 53 |
+
with gr.Blocks():
|
| 54 |
+
with gr.Row():
|
| 55 |
+
with gr.Column():
|
| 56 |
+
image = gr.Image(type="pil", label="Image", visible=True)
|
| 57 |
+
video = gr.Video(label="Video", visible=False)
|
| 58 |
+
input_type = gr.Radio(
|
| 59 |
+
choices=["Image", "Video"],
|
| 60 |
+
value="Image",
|
| 61 |
+
label="Input Type",
|
| 62 |
+
)
|
| 63 |
+
model_id = gr.Dropdown(
|
| 64 |
+
label="Model",
|
| 65 |
+
choices=[
|
| 66 |
+
"yolov12n.pt",
|
| 67 |
+
"yolov12s.pt",
|
| 68 |
+
"yolov12m.pt",
|
| 69 |
+
"yolov12l.pt",
|
| 70 |
+
"yolov12x.pt",
|
| 71 |
+
"brain_tumor.pt",
|
| 72 |
+
],
|
| 73 |
+
value="yolov12m.pt",
|
| 74 |
+
)
|
| 75 |
+
image_size = gr.Slider(
|
| 76 |
+
label="Image Size",
|
| 77 |
+
minimum=320,
|
| 78 |
+
maximum=1280,
|
| 79 |
+
step=32,
|
| 80 |
+
value=640,
|
| 81 |
+
)
|
| 82 |
+
conf_threshold = gr.Slider(
|
| 83 |
+
label="Confidence Threshold",
|
| 84 |
+
minimum=0.0,
|
| 85 |
+
maximum=1.0,
|
| 86 |
+
step=0.05,
|
| 87 |
+
value=0.25,
|
| 88 |
+
)
|
| 89 |
+
yolov12_infer = gr.Button(value="Detect Objects")
|
| 90 |
+
|
| 91 |
+
with gr.Column():
|
| 92 |
+
output_image = gr.Image(type="numpy", label="Annotated Image", visible=True)
|
| 93 |
+
output_video = gr.Video(label="Annotated Video", visible=False)
|
| 94 |
+
|
| 95 |
+
def update_visibility(input_type):
|
| 96 |
+
image = gr.update(visible=True) if input_type == "Image" else gr.update(visible=False)
|
| 97 |
+
video = gr.update(visible=False) if input_type == "Image" else gr.update(visible=True)
|
| 98 |
+
output_image = gr.update(visible=True) if input_type == "Image" else gr.update(visible=False)
|
| 99 |
+
output_video = gr.update(visible=False) if input_type == "Image" else gr.update(visible=True)
|
| 100 |
+
|
| 101 |
+
return image, video, output_image, output_video
|
| 102 |
+
|
| 103 |
+
input_type.change(
|
| 104 |
+
fn=update_visibility,
|
| 105 |
+
inputs=[input_type],
|
| 106 |
+
outputs=[image, video, output_image, output_video],
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
def run_inference(image, video, model_id, image_size, conf_threshold, input_type):
|
| 110 |
+
if input_type == "Image":
|
| 111 |
+
return yolov12_inference(image, None, model_id, image_size, conf_threshold)
|
| 112 |
+
else:
|
| 113 |
+
return yolov12_inference(None, video, model_id, image_size, conf_threshold)
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
yolov12_infer.click(
|
| 117 |
+
fn=run_inference,
|
| 118 |
+
inputs=[image, video, model_id, image_size, conf_threshold, input_type],
|
| 119 |
+
outputs=[output_image, output_video],
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
gr.Examples(
|
| 123 |
+
examples=[
|
| 124 |
+
[
|
| 125 |
+
"ultralytics/assets/bus.jpg",
|
| 126 |
+
"yolov12s.pt",
|
| 127 |
+
640,
|
| 128 |
+
0.25,
|
| 129 |
+
],
|
| 130 |
+
[
|
| 131 |
+
"ultralytics/assets/zidane.jpg",
|
| 132 |
+
"yolov12x.pt",
|
| 133 |
+
640,
|
| 134 |
+
0.25,
|
| 135 |
+
],
|
| 136 |
+
],
|
| 137 |
+
fn=yolov12_inference_for_examples,
|
| 138 |
+
inputs=[
|
| 139 |
+
image,
|
| 140 |
+
model_id,
|
| 141 |
+
image_size,
|
| 142 |
+
conf_threshold,
|
| 143 |
+
],
|
| 144 |
+
outputs=[output_image],
|
| 145 |
+
cache_examples='lazy',
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
gradio_app = gr.Blocks()
|
| 149 |
+
with gradio_app:
|
| 150 |
+
gr.HTML(
|
| 151 |
+
"""
|
| 152 |
+
<h1 style='text-align: center'>
|
| 153 |
+
YOLOv12: Attention-Centric Real-Time Object Detectors
|
| 154 |
+
</h1>
|
| 155 |
+
""")
|
| 156 |
+
gr.HTML(
|
| 157 |
+
"""
|
| 158 |
+
<h3 style='text-align: center'>
|
| 159 |
+
<a href='https://arxiv.org/abs/2502.12524' target='_blank'>arXiv</a> | <a href='https://github.com/sunsmarterjie/yolov12' target='_blank'>github</a>
|
| 160 |
+
</h3>
|
| 161 |
+
""")
|
| 162 |
+
with gr.Row():
|
| 163 |
+
with gr.Column():
|
| 164 |
+
app()
|
| 165 |
+
if __name__ == '__main__':
|
| 166 |
+
gradio_app.launch(share=True)
|
brain_tumor.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c9e1470b4e7325d868c3d06e3e94970281b418941a9c96b003f6fd025b78d4f3
|
| 3 |
+
size 19184787
|
requirements.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch==2.2.2
|
| 2 |
+
torchvision==0.17.2
|
| 3 |
+
#flash_attn-2.7.3+cu11torch2.2cxx11abiFALSE-cp311-cp311-linux_x86_64.whl
|
| 4 |
+
timm==1.0.14
|
| 5 |
+
albumentations==2.0.4
|
| 6 |
+
onnx==1.14.0
|
| 7 |
+
onnxruntime==1.15.1
|
| 8 |
+
pycocotools==2.0.7
|
| 9 |
+
PyYAML==6.0.1
|
| 10 |
+
scipy==1.13.0
|
| 11 |
+
onnxslim==0.1.31
|
| 12 |
+
onnxruntime-gpu==1.18.0
|
| 13 |
+
gradio==4.44.1
|
| 14 |
+
opencv-python==4.9.0.80
|
| 15 |
+
psutil==5.9.8
|
| 16 |
+
py-cpuinfo==9.0.0
|
| 17 |
+
huggingface-hub==0.23.2
|
| 18 |
+
safetensors==0.4.3
|
| 19 |
+
numpy==1.26.4
|
| 20 |
+
lap>=0.5.12
|
| 21 |
+
ultralytics
|
yolo11n-cls.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c62d41bf9625777760018bf914d2e6cd472420ccd01706d97a61cb6c82502bd7
|
| 3 |
+
size 5790624
|
yolo11n-pose.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:869e83fcdffdc7371fa4e34cd8e51c838cc729571d1635e5141e3075e9319dc0
|
| 3 |
+
size 6255593
|
yolo11n.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0ebbc80d4a7680d14987a577cd21342b65ecfd94632bd9a8da63ae6417644ee1
|
| 3 |
+
size 5613764
|
yolo11s-seg.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1caa81c0195412efa411b632bcfb8c184939dddb6ae41f6a80c41b211ff257c3
|
| 3 |
+
size 20669228
|
yolov12l.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0babd8dc8f775bb64bb052debdff3d8b9e9b57efa9d7bfa11c84bb82c3fec336
|
| 3 |
+
size 53699086
|
yolov12n.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:419ff3dca37d69bacc93a50fa0c186a1c6f9fe62fae0f108b0872829689e9ca6
|
| 3 |
+
size 5595063
|
yolov12s.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e915c2c4286e3f6f8610ef106fa3f94a7b8c19b30eccede5887e22c33ef75f58
|
| 3 |
+
size 19006455
|
yolov12x.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:682ce8dadee004dbe964950f1bf3eda451671815a6ed62db80b398916b9b7c6f
|
| 3 |
+
size 119322638
|