Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
from cvu.detector.yolov5 import Yolov5 as Yolov5Onnx
|
6 |
+
|
7 |
+
# Load the model outside of the function so it's not reloaded every time the function is called
|
8 |
+
model = Yolov5Onnx(classes="coco", backend="onnx", weight='./jtbz_opt.onnx', device='cpu')
|
9 |
+
|
10 |
+
def detect_objects(image: np.ndarray):
|
11 |
+
# Convert the input image to OpenCV format
|
12 |
+
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
13 |
+
|
14 |
+
# Perform the object detection
|
15 |
+
preds = model(image)
|
16 |
+
|
17 |
+
# Draw the predictions on the image
|
18 |
+
preds.draw(image)
|
19 |
+
|
20 |
+
# Convert the image back to PIL format and return it
|
21 |
+
return Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
22 |
+
|
23 |
+
# Define the Gradio interface
|
24 |
+
iface = gr.Interface(fn=detect_objects,
|
25 |
+
inputs=gr.inputs.Image(shape=(416, 416)),
|
26 |
+
outputs="image",
|
27 |
+
title="YOLOv5 Object Detection",
|
28 |
+
description="This is an interactive demo for YOLOv5 object detection. Upload an image to see the detected objects.")
|
29 |
+
|
30 |
+
iface.launch()
|