Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,39 @@
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
4 |
|
5 |
-
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
import torch
|
4 |
+
import cv2
|
5 |
import gradio as gr
|
6 |
+
from pathlib import Path
|
7 |
+
|
8 |
+
# Load the YOLOv5 model
|
9 |
+
MODEL_PATH = "best.pt" # Path to your custom model
|
10 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
11 |
+
model = torch.hub.load('ultralytics/yolov5', 'custom', path=MODEL_PATH, force_reload=True).to(device)
|
12 |
+
|
13 |
+
# Define the prediction function
|
14 |
+
def predict(image):
|
15 |
+
# Convert the input image (numpy) to a compatible format
|
16 |
+
img_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
17 |
+
results = model(img_bgr) # Run inference
|
18 |
|
19 |
+
# Render results
|
20 |
+
annotated_image = results.render()[0]
|
21 |
+
annotated_image_rgb = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
|
22 |
+
return annotated_image_rgb
|
23 |
|
24 |
+
# Gradio Interface
|
25 |
+
title = "Custom YOLOv5 Model Deployment"
|
26 |
+
description = "Upload an image to get predictions using a custom YOLOv5 model trained with your dataset."
|
27 |
|
28 |
+
interface = gr.Interface(
|
29 |
+
fn=predict, # Prediction function
|
30 |
+
inputs=gr.Image(type="numpy", label="Upload Image"),
|
31 |
+
outputs=gr.Image(type="numpy", label="Detected Objects"),
|
32 |
+
title=title,
|
33 |
+
description=description,
|
34 |
+
live=True # Enable live processing
|
35 |
+
)
|
36 |
|
37 |
+
# Launch the Gradio app
|
38 |
+
if __name__ == "__main__":
|
39 |
+
interface.launch()
|