File size: 867 Bytes
eb6d647
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import gradio as gr
from gradio import components
from PIL import Image
import os

model = None

def object_detection(im):
    global model
    if model is None:
        model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', force_reload=True)
    results = model(im)
    results.render()
    return Image.fromarray(results.ims[0])

image = components.Image(shape=(640, 640), image_mode="RGB", source="upload", label="Imagem")
outputs = components.Image(type="pil", label="Output Image")

iface = gr.Interface(
    fn=object_detection,
    inputs=image,
    outputs=outputs,
    title='Garbage Detection',
    description='A simple demo app for an object detection model to detect garbage in natural and urban environments.',
    examples=sorted([f'examples/{filename}' for filename in os.listdir('examples')]),
)
iface.launch(debug=True)