Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,33 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import supervision as sv
|
| 3 |
import PIL.Image as Image
|
| 4 |
from ultralytics import YOLO, YOLOv10
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
-
import spaces
|
| 7 |
|
| 8 |
|
| 9 |
def download_models(model_id):
|
| 10 |
hf_hub_download("atalaydenknalbant/asl-models", filename=f"{model_id}", local_dir=f"./")
|
| 11 |
return f"./{model_id}"
|
| 12 |
|
|
|
|
|
|
|
| 13 |
box_annotator = sv.BoxAnnotator()
|
| 14 |
category_dict = {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E', 5: 'F', 6: 'G', 7: 'H', 8: 'I',
|
| 15 |
9: 'J', 10: 'K', 11: 'L', 12: 'M', 13: 'N', 14: 'O', 15: 'P', 16: 'Q',
|
| 16 |
17: 'R', 18: 'S', 19: 'T', 20: 'U', 21: 'V', 22: 'W', 23: 'X', 24: 'Y', 25: 'Z'}
|
| 17 |
|
|
|
|
|
|
|
|
|
|
| 18 |
@spaces.GPU(duration=200)
|
| 19 |
-
def yolo_inference(image, model_id, conf_threshold, iou_threshold):
|
| 20 |
model_path = download_models(model_id)
|
| 21 |
if model_id[:7] == 'yolov10':
|
| 22 |
model = YOLOv10(model_path)
|
| 23 |
else:
|
| 24 |
model = YOLO(model_path)
|
| 25 |
-
results = model(source=image, imgsz=416, iou=iou_threshold, conf=conf_threshold, verbose=False, max_det=
|
| 26 |
detections = sv.Detections.from_ultralytics(results)
|
| 27 |
|
| 28 |
labels = [
|
|
@@ -34,7 +39,7 @@ def yolo_inference(image, model_id, conf_threshold, iou_threshold):
|
|
| 34 |
return annotated_image
|
| 35 |
|
| 36 |
def app():
|
| 37 |
-
with gr.Blocks()
|
| 38 |
with gr.Row():
|
| 39 |
with gr.Column():
|
| 40 |
image = gr.Image(type="pil", label="Image")
|
|
@@ -63,18 +68,26 @@ def app():
|
|
| 63 |
step=0.1,
|
| 64 |
value=0.45,
|
| 65 |
)
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
with gr.Column():
|
| 69 |
output_image = gr.Image(type="pil", label="Annotated Image")
|
| 70 |
|
| 71 |
-
|
| 72 |
fn=yolo_inference,
|
| 73 |
inputs=[
|
| 74 |
image,
|
| 75 |
model_id,
|
| 76 |
conf_threshold,
|
| 77 |
iou_threshold,
|
|
|
|
| 78 |
],
|
| 79 |
outputs=[output_image],
|
| 80 |
)
|
|
@@ -83,15 +96,17 @@ def app():
|
|
| 83 |
examples=[
|
| 84 |
[
|
| 85 |
"a.jpg",
|
| 86 |
-
"
|
| 87 |
0.25,
|
| 88 |
0.45,
|
|
|
|
| 89 |
],
|
| 90 |
[
|
| 91 |
"y.jpg",
|
| 92 |
"yolov10x.pt",
|
| 93 |
0.25,
|
| 94 |
0.45,
|
|
|
|
| 95 |
],
|
| 96 |
],
|
| 97 |
fn=yolo_inference,
|
|
@@ -100,11 +115,16 @@ def app():
|
|
| 100 |
model_id,
|
| 101 |
conf_threshold,
|
| 102 |
iou_threshold,
|
|
|
|
| 103 |
],
|
| 104 |
outputs=[output_image],
|
| 105 |
cache_examples="lazy",
|
| 106 |
)
|
| 107 |
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
import supervision as sv
|
| 4 |
import PIL.Image as Image
|
| 5 |
from ultralytics import YOLO, YOLOv10
|
| 6 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
def download_models(model_id):
|
| 10 |
hf_hub_download("atalaydenknalbant/asl-models", filename=f"{model_id}", local_dir=f"./")
|
| 11 |
return f"./{model_id}"
|
| 12 |
|
| 13 |
+
|
| 14 |
+
|
| 15 |
box_annotator = sv.BoxAnnotator()
|
| 16 |
category_dict = {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E', 5: 'F', 6: 'G', 7: 'H', 8: 'I',
|
| 17 |
9: 'J', 10: 'K', 11: 'L', 12: 'M', 13: 'N', 14: 'O', 15: 'P', 16: 'Q',
|
| 18 |
17: 'R', 18: 'S', 19: 'T', 20: 'U', 21: 'V', 22: 'W', 23: 'X', 24: 'Y', 25: 'Z'}
|
| 19 |
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
@spaces.GPU(duration=200)
|
| 24 |
+
def yolo_inference(image, model_id, conf_threshold, iou_threshold, max_detection):
|
| 25 |
model_path = download_models(model_id)
|
| 26 |
if model_id[:7] == 'yolov10':
|
| 27 |
model = YOLOv10(model_path)
|
| 28 |
else:
|
| 29 |
model = YOLO(model_path)
|
| 30 |
+
results = model(source=image, imgsz=416, iou=iou_threshold, conf=conf_threshold, verbose=False, max_det=max_detection)[0]
|
| 31 |
detections = sv.Detections.from_ultralytics(results)
|
| 32 |
|
| 33 |
labels = [
|
|
|
|
| 39 |
return annotated_image
|
| 40 |
|
| 41 |
def app():
|
| 42 |
+
with gr.Blocks():
|
| 43 |
with gr.Row():
|
| 44 |
with gr.Column():
|
| 45 |
image = gr.Image(type="pil", label="Image")
|
|
|
|
| 68 |
step=0.1,
|
| 69 |
value=0.45,
|
| 70 |
)
|
| 71 |
+
|
| 72 |
+
max_detection = gr.Slider(
|
| 73 |
+
label="Max Detection",
|
| 74 |
+
minimum=1.0,
|
| 75 |
+
step=1.0,
|
| 76 |
+
value=1.0,
|
| 77 |
+
)
|
| 78 |
+
yolov_infer = gr.Button(value="Detect Objects")
|
| 79 |
|
| 80 |
with gr.Column():
|
| 81 |
output_image = gr.Image(type="pil", label="Annotated Image")
|
| 82 |
|
| 83 |
+
yolov_infer.click(
|
| 84 |
fn=yolo_inference,
|
| 85 |
inputs=[
|
| 86 |
image,
|
| 87 |
model_id,
|
| 88 |
conf_threshold,
|
| 89 |
iou_threshold,
|
| 90 |
+
max_detection,
|
| 91 |
],
|
| 92 |
outputs=[output_image],
|
| 93 |
)
|
|
|
|
| 96 |
examples=[
|
| 97 |
[
|
| 98 |
"a.jpg",
|
| 99 |
+
"yolov10s.pt",
|
| 100 |
0.25,
|
| 101 |
0.45,
|
| 102 |
+
1,
|
| 103 |
],
|
| 104 |
[
|
| 105 |
"y.jpg",
|
| 106 |
"yolov10x.pt",
|
| 107 |
0.25,
|
| 108 |
0.45,
|
| 109 |
+
1,
|
| 110 |
],
|
| 111 |
],
|
| 112 |
fn=yolo_inference,
|
|
|
|
| 115 |
model_id,
|
| 116 |
conf_threshold,
|
| 117 |
iou_threshold,
|
| 118 |
+
max_detection,
|
| 119 |
],
|
| 120 |
outputs=[output_image],
|
| 121 |
cache_examples="lazy",
|
| 122 |
)
|
| 123 |
|
| 124 |
+
gradio_app = gr.Blocks()
|
| 125 |
+
with gradio_app:
|
| 126 |
+
with gr.Row():
|
| 127 |
+
with gr.Column():
|
| 128 |
+
app()
|
| 129 |
|
| 130 |
+
gradio_app.launch(debug=True)
|