Muzamilkhan4571's picture
Duplicate from keremberke/garbage-object-detection
8e5ad27
raw
history blame
2.35 kB
import json
import gradio as gr
import yolov5
from PIL import Image
from huggingface_hub import hf_hub_download
app_title = "Garbage Object Detection"
models_ids = ['keremberke/yolov5n-garbage', 'keremberke/yolov5s-garbage', 'keremberke/yolov5m-garbage']
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>"
current_model_id = models_ids[-1]
model = yolov5.load(current_model_id)
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']]
def predict(image, threshold=0.25, model_id=None):
# update model if required
global current_model_id
global model
if model_id != current_model_id:
model = yolov5.load(model_id)
current_model_id = model_id
# get model input size
config_path = hf_hub_download(repo_id=model_id, filename="config.json")
with open(config_path, "r") as f:
config = json.load(f)
input_size = config["input_size"]
# perform inference
model.conf = threshold
results = model(image, size=input_size)
numpy_image = results.render()[0]
output_image = Image.fromarray(numpy_image)
return output_image
gr.Interface(
title=app_title,
description="Created by 'keremberke'",
article=article,
fn=predict,
inputs=[
gr.Image(type="pil"),
gr.Slider(maximum=1, step=0.01, value=0.25),
gr.Dropdown(models_ids, value=models_ids[-1]),
],
outputs=gr.Image(type="pil"),
examples=examples,
cache_examples=True if examples else False,
).launch(enable_queue=True)