Download app.py from Samp21/detect: direct link, hf CLI and curl.
- Browser
- Download file 705 Bytes
-
https://huggingface.co/spaces/Samp21/detect/resolve/main/app.py
- Command line
-
hf download hf://spaces/Samp21/detect/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Samp21/detect/resolve/main/app.py
705 Bytes
| import torch | |
| import gradio as gr | |
| from PIL import Image | |
| # Load pre-trained YOLOv5 model from ultralytics | |
| model = torch.hub.load('ultralytics/yolov5', 'yolov5s') | |
| def detect_objects(image): | |
| # Perform inference | |
| results = model(image) | |
| results.render() # updates results.imgs with boxes and labels | |
| detected_image = Image.fromarray(results.imgs[0]) | |
| return detected_image | |
| # Define Gradio interface | |
| iface = gr.Interface( | |
| fn=detect_objects, | |
| inputs=gr.inputs.Image(type="pil"), | |
| outputs=gr.outputs.Image(type="pil"), | |
| title="Object Detection with YOLOv5", | |
| description="Upload an image and get the detected objects with their names." | |
| ) | |
| # Launch the interface | |
| iface.launch() | |