Spaces:
Runtime error

TrashDetect / app.py
Roopalav's picture
Update app.py
d3475e0
Raw History Blame Contribute Delete
1.31 kB
import gradio as gr
from ultralytics import YOLO
from collections import Counter
import torch
import cv2
def yolov8_predictor(img: gr.inputs.Image = None,
conf= 0.4,
iou= 0.7
):
model= YOLO(model= 'trashbest.pt')
res= model.predict(source= img, imgsz=640, 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.Slider(minimum=0.0, maximum=1.0, default=0.4, 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 = "Trash Detection"
#examples = [['IMG_1315.JPG',0.4, 0.7], ['IMG_1348.JPG',0.4, 0.7]]
demo_app = gr.Interface(
fn=yolov8_predictor,
inputs=inputs,
outputs=outputs,
title=title,
theme='huggingface',
)
demo_app.launch(debug=True, enable_queue=True)