Spaces:
Runtime error
Runtime error
File size: 1,824 Bytes
1efffb1 d14dce3 b365605 5f97f4d d14dce3 1efffb1 a9a71b9 5f97f4d d14dce3 1efffb1 e460624 d14dce3 1efffb1 70e3e53 1efffb1 e460624 1efffb1 d14dce3 1efffb1 d14dce3 e460624 d14dce3 5f97f4d d14dce3 5076199 d14dce3 5f97f4d d14dce3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | import gradio as gr
from ultralytics import YOLO
from collections import Counter
import torch
import cv2
# torch.hub.download_url_to_file('https://ultralytics.com/images/zidane.jpg', 'zidane.jpg')
# torch.hub.download_url_to_file('https://ultralytics.com/images/bus.jpg', 'bus.jpg')
def yolov8_predictor(img: gr.inputs.Image = None,
model_path: gr.inputs.Dropdown = None,
image_size= 640,
conf= 0.25,
iou= 0.7
):
model= YOLO(model= model_path)
res= model.predict(source= img, imgsz=image_size, conf= conf, iou= iou)
cls_names= res[0].names
dets_cls= res[0].boxes.cls.cpu().numpy().astype(int)
dets= [cls_names[i] for i in dets_cls]
dets= dict(Counter(dets))
res_plotted = res[0].plot()
res_plotted= cv2.cvtColor(res_plotted, cv2.COLOR_BGR2RGB)
return res_plotted, str(dets)
inputs = [
gr.inputs.Image(type="filepath", label="Input Image"),
gr.inputs.Dropdown(["yolov8n.pt", "yolov8s.pt"],
default="yolov8s.pt", label="Model"),
gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.7, step=0.05, label="IOU Threshold"),
]
outputs = [gr.outputs.Image(type="filepath", label="Output Image"), "text"]
title = "YOLOv8"
examples = [['zidane.jpg', 'yolov8s.pt', 640, 0.25, 0.7], ['bus.jpg', 'yolov8s.pt', 640, 0.25, 0.7]]
demo_app = gr.Interface(
fn=yolov8_predictor,
inputs=inputs,
outputs=outputs,
title=title,
examples=examples,
cache_examples=True,
theme='huggingface',
)
demo_app.launch(debug=True, enable_queue=True) |