Spaces:
Runtime error
Runtime error
Commit
Β·
64c289c
1
Parent(s):
1c0355a
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import gradio as gr
|
3 |
+
import yolov5
|
4 |
+
from PIL import Image
|
5 |
+
from huggingface_hub import hf_hub_download
|
6 |
+
|
7 |
+
app_title = "Garbage Object Detection"
|
8 |
+
models_ids = ['keremberke/yolov5n-garbage', 'keremberke/yolov5s-garbage', 'keremberke/yolov5m-garbage']
|
9 |
+
article = f"<p style='text-align: center'> <a href='https://huggingface.co/{models_ids[-1]}'>model</a> | <a href='https://huggingface.co/keremberke/garbage-object-detection'>dataset</a> | <a href='https://github.com/keremberke/awesome-yolov5-models'>awesome-yolov5-models</a> </p>"
|
10 |
+
|
11 |
+
current_model_id = models_ids[-1]
|
12 |
+
model = yolov5.load(current_model_id)
|
13 |
+
|
14 |
+
examples = [['test_images/biodegradable26_jpg.rf.8a913791d009e2fab0a2e6fe09354e42.jpg', 0.25, 'keremberke/yolov5m-garbage'], ['test_images/biodegradable545_jpg.rf.221b16c94387b66692f4e25e3c67c662.jpg', 0.25, 'keremberke/yolov5m-garbage'], ['test_images/biodegradable89_jpg.rf.2097a8a4f14b2d8e7ac994ed5fdc13a9.jpg', 0.25, 'keremberke/yolov5m-garbage'], ['test_images/cardboard1696_jpg.rf.c7d8edf6d266cb501f877f5d129ca32a.jpg', 0.25, 'keremberke/yolov5m-garbage'], ['test_images/glass1467_jpg.rf.d2f0a3ed76205c01fc26c555680ddc81.jpg', 0.25, 'keremberke/yolov5m-garbage'], ['test_images/glass887_jpg.rf.8993139c864267e74f501703b5a02a1b.jpg', 0.25, 'keremberke/yolov5m-garbage']]
|
15 |
+
|
16 |
+
|
17 |
+
def predict(image, threshold=0.25, model_id=None):
|
18 |
+
# update model if required
|
19 |
+
global current_model_id
|
20 |
+
global model
|
21 |
+
if model_id != current_model_id:
|
22 |
+
model = yolov5.load(model_id)
|
23 |
+
current_model_id = model_id
|
24 |
+
|
25 |
+
# get model input size
|
26 |
+
config_path = hf_hub_download(repo_id=model_id, filename="config.json")
|
27 |
+
with open(config_path, "r") as f:
|
28 |
+
config = json.load(f)
|
29 |
+
input_size = config["input_size"]
|
30 |
+
|
31 |
+
# perform inference
|
32 |
+
model.conf = threshold
|
33 |
+
results = model(image, size=input_size)
|
34 |
+
numpy_image = results.render()[0]
|
35 |
+
output_image = Image.fromarray(numpy_image)
|
36 |
+
return output_image
|
37 |
+
|
38 |
+
|
39 |
+
gr.Interface(
|
40 |
+
title=app_title,
|
41 |
+
description="Created by 'keremberke'",
|
42 |
+
article=article,
|
43 |
+
fn=predict,
|
44 |
+
inputs=[
|
45 |
+
gr.Image(type="pil"),
|
46 |
+
gr.Slider(maximum=1, step=0.01, value=0.25),
|
47 |
+
gr.Dropdown(models_ids, value=models_ids[-1]),
|
48 |
+
],
|
49 |
+
outputs=gr.Image(type="pil"),
|
50 |
+
examples=examples,
|
51 |
+
cache_examples=True if examples else False,
|
52 |
+
).launch(enable_queue=True)
|