Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
-
from ultralytics import YOLO # Import YOLO from ultralytics
|
| 5 |
|
| 6 |
# Load the YOLOv5 model
|
| 7 |
-
model =
|
| 8 |
|
| 9 |
# Function to run inference on an image
|
| 10 |
def run_inference(image):
|
|
@@ -12,10 +12,11 @@ def run_inference(image):
|
|
| 12 |
image = np.array(image)
|
| 13 |
|
| 14 |
# Run YOLOv5 inference
|
| 15 |
-
results = model
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
annotated_image = results[0]
|
|
|
|
| 19 |
|
| 20 |
return annotated_image
|
| 21 |
|
|
@@ -23,7 +24,7 @@ def run_inference(image):
|
|
| 23 |
interface = gr.Interface(
|
| 24 |
fn=run_inference,
|
| 25 |
inputs=gr.Image(type="pil"),
|
| 26 |
-
outputs=gr.Image(type="
|
| 27 |
title="YOLOv5 Object Detection",
|
| 28 |
description="Upload an image to run YOLOv5 object detection and see the results."
|
| 29 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
+
import torch
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
|
| 6 |
# Load the YOLOv5 model
|
| 7 |
+
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
|
| 8 |
|
| 9 |
# Function to run inference on an image
|
| 10 |
def run_inference(image):
|
|
|
|
| 12 |
image = np.array(image)
|
| 13 |
|
| 14 |
# Run YOLOv5 inference
|
| 15 |
+
results = model(image)
|
| 16 |
|
| 17 |
+
# Convert the annotated image from BGR to RGB for display
|
| 18 |
+
annotated_image = results.render()[0]
|
| 19 |
+
annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
|
| 20 |
|
| 21 |
return annotated_image
|
| 22 |
|
|
|
|
| 24 |
interface = gr.Interface(
|
| 25 |
fn=run_inference,
|
| 26 |
inputs=gr.Image(type="pil"),
|
| 27 |
+
outputs=gr.Image(type="pil"),
|
| 28 |
title="YOLOv5 Object Detection",
|
| 29 |
description="Upload an image to run YOLOv5 object detection and see the results."
|
| 30 |
)
|