Spaces:
Runtime error

File size: 1,311 Bytes
c35c6d2
 
 
 
 
 
 
 
f102a43
c35c6d2
 
 
 
d3475e0
f102a43
c35c6d2
 
 
 
 
 
 
 
 
 
 
 
f102a43
c35c6d2
 
 
 
 
 
 
f102a43
c35c6d2
 
 
 
 
f102a43
c35c6d2
 
 
 
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
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)