Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
# Download the model from Hugging Face model repo
|
| 7 |
+
model_path = "best.pt"
|
| 8 |
+
if not os.path.exists(model_path):
|
| 9 |
+
url = "https://huggingface.co/AlaaElsayed/YoloModel/resolve/main/best.pt"
|
| 10 |
+
r = requests.get(url)
|
| 11 |
+
with open(model_path, 'wb') as f:
|
| 12 |
+
f.write(r.content)
|
| 13 |
+
|
| 14 |
+
# Load the YOLOv5 model
|
| 15 |
+
model = torch.hub.load('ultralytics/yolov5', 'custom', path=model_path, source='local')
|
| 16 |
+
|
| 17 |
+
# Define the detection function
|
| 18 |
+
def detect(image):
|
| 19 |
+
results = model(image)
|
| 20 |
+
return Image.fromarray(results.render()[0]) # Convert to PIL for Gradio
|
| 21 |
+
|
| 22 |
+
# Create Gradio interface
|
| 23 |
+
demo = gr.Interface(
|
| 24 |
+
fn=detect,
|
| 25 |
+
inputs=gr.Image(type="pil"),
|
| 26 |
+
outputs=gr.Image(type="pil"),
|
| 27 |
+
title="YOLOv5 Object Detector",
|
| 28 |
+
description="Upload an image and let YOLOv5 detect objects using your custom model!"
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
demo.launch()
|