Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from sahi.prediction import ObjectPrediction
|
| 4 |
+
from sahi.utils.cv import visualize_object_predictions, read_image
|
| 5 |
+
from ultralyticsplus import YOLO, render_result
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def yolov8_inference(
|
| 9 |
+
image,
|
| 10 |
+
model_path,
|
| 11 |
+
image_size,
|
| 12 |
+
conf_threshold,
|
| 13 |
+
iou_threshold,
|
| 14 |
+
):
|
| 15 |
+
"""
|
| 16 |
+
YOLOv8 inference function
|
| 17 |
+
Args:
|
| 18 |
+
image: Input image
|
| 19 |
+
model_path: Path to the model
|
| 20 |
+
image_size: Image size
|
| 21 |
+
conf_threshold: Confidence threshold
|
| 22 |
+
iou_threshold: IOU threshold
|
| 23 |
+
Returns:
|
| 24 |
+
Rendered image
|
| 25 |
+
"""
|
| 26 |
+
model = YOLO(f'kadirnar/{model_path}-v8.0')
|
| 27 |
+
# set model parameters
|
| 28 |
+
model.overrides['conf'] = conf_threshold # NMS confidence threshold
|
| 29 |
+
model.overrides['iou'] = iou_threshold # NMS IoU threshold
|
| 30 |
+
model.overrides['agnostic_nms'] = False # NMS class-agnostic
|
| 31 |
+
model.overrides['max_det'] = 1000 # maximum number of detections per image
|
| 32 |
+
results = model.predict(image, imgsz=image_size)
|
| 33 |
+
render = render_result(model=model, image=image, result=results[0])
|
| 34 |
+
return render
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
inputs = [
|
| 38 |
+
gr.Image(type="filepath", label="Input Image"),
|
| 39 |
+
gr.Dropdown(["yolov8n", "yolov8m", "yolov8l", "yolov8x"],
|
| 40 |
+
value="yolov8m", label="Model"),
|
| 41 |
+
gr.Slider(minimum=320, maximum=1280, value=640, step=320, label="Image Size"),
|
| 42 |
+
gr.Slider(minimum=0.0, maximum=1.0, value=0.25, step=0.05, label="Confidence Threshold"),
|
| 43 |
+
gr.Slider(minimum=0.0, maximum=1.0, value=0.45, step=0.05, label="IOU Threshold"),
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
outputs = gr.Image(type="filepath", label="Output Image")
|
| 47 |
+
title = "State-of-the-Art YOLO Models for Object detection"
|
| 48 |
+
|
| 49 |
+
examples = [['demo_01.jpg', 'yolov8n', 640, 0.25, 0.45], ['demo_02.jpg', 'yolov8l', 640, 0.25, 0.45], ['demo_03.jpg', 'yolov8x', 1280, 0.25, 0.45]]
|
| 50 |
+
demo_app = gr.Interface(
|
| 51 |
+
fn=yolov8_inference,
|
| 52 |
+
inputs=inputs,
|
| 53 |
+
outputs=outputs,
|
| 54 |
+
title=title,
|
| 55 |
+
examples=examples,
|
| 56 |
+
cache_examples=True,
|
| 57 |
+
)
|
| 58 |
+
demo_app.launch(debug=True)
|