Spaces:
Runtime error
Runtime error
Commit ·
158d13b
1
Parent(s): d27552b
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import supervision as sv
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
+
import sahi
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
# Images
|
| 9 |
+
sahi.utils.file.download_from_url(
|
| 10 |
+
"https://transform.roboflow.com/zD7y6XOoQnh7WC160Ae7/4d51f997137c0dca78fa2c9154e0b51a/thumb.jpg",
|
| 11 |
+
"f1.jpg",
|
| 12 |
+
)
|
| 13 |
+
sahi.utils.file.download_from_url(
|
| 14 |
+
"https://transform.roboflow.com/zD7y6XOoQnh7WC160Ae7/48174c7c26c2cbca52b084ebbb03d215/thumb.jpg",
|
| 15 |
+
"f2.jpg",
|
| 16 |
+
)
|
| 17 |
+
sahi.utils.file.download_from_url(
|
| 18 |
+
"https://transform.roboflow.com/zD7y6XOoQnh7WC160Ae7/3d1f22e387164a6719995aa0d9dc16a1/thumb.jpg",
|
| 19 |
+
"f3.jpg",
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
annotatorbbox = sv.BoxAnnotator()
|
| 26 |
+
annotatormask=sv.MaskAnnotator()
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def yolov8_inference(
|
| 30 |
+
image: gr.inputs.Image = None,
|
| 31 |
+
model_name: gr.inputs.Dropdown = None,
|
| 32 |
+
image_size: gr.inputs.Slider = 320,
|
| 33 |
+
conf_threshold: gr.inputs.Slider = 0.25,
|
| 34 |
+
iou_threshold: gr.inputs.Slider = 0.45,
|
| 35 |
+
):
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
model = YOLO("/content/segment/train/weights/best.pt")
|
| 39 |
+
|
| 40 |
+
results = model(image,conf=conf_threshold,iou=iou_threshold ,imgsz=320)[0]
|
| 41 |
+
detections = sv.Detections.from_yolov8(results)
|
| 42 |
+
annotated_image = annotatorbbox.annotate(scene=image, detections=detections)
|
| 43 |
+
annotated_image = annotatormask.annotate(scene=annotated_image, detections=detections)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
return annotated_image
|
| 49 |
+
|
| 50 |
+
image_input = gr.inputs.Image() # Adjust the shape according to your requirements
|
| 51 |
+
|
| 52 |
+
inputs = [
|
| 53 |
+
gr.inputs.Image(label="Input Image"),
|
| 54 |
+
gr.Slider(
|
| 55 |
+
minimum=0.0, maximum=1.0, value=0.25, step=0.05, label="Confidence Threshold"
|
| 56 |
+
),
|
| 57 |
+
gr.Slider(minimum=0.0, maximum=1.0, value=0.45, step=0.05, label="IOU Threshold"),
|
| 58 |
+
]
|
| 59 |
+
|
| 60 |
+
outputs = gr.Image(type="filepath", label="Output Image")
|
| 61 |
+
title = "Ultralytics YOLOv8 Segmentation Demo"
|
| 62 |
+
import os
|
| 63 |
+
examples = [
|
| 64 |
+
["f1.jpg", 0.6, 0.45],
|
| 65 |
+
["f2.jpg", 0.25, 0.45],
|
| 66 |
+
["f3.jpg", 0.25, 0.45],
|
| 67 |
+
]
|
| 68 |
+
demo_app = gr.Interface(examples=examples,
|
| 69 |
+
fn=yolov8_inference,
|
| 70 |
+
inputs=inputs,
|
| 71 |
+
outputs=outputs,
|
| 72 |
+
title=title,
|
| 73 |
+
cache_examples=True,
|
| 74 |
+
theme="default",
|
| 75 |
+
)
|
| 76 |
+
demo_app.launch(debug=True, enable_queue=True)
|