Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,41 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import hf_hub_download
|
3 |
+
import torch
|
4 |
|
5 |
+
# Download the YOLOv5 model from Hugging Face
|
6 |
+
model_path = hf_hub_download(repo_id="ultralytics/yolov5", filename="yolov5s.pt", revision="v7.0")
|
7 |
|
8 |
+
# Load the YOLOv5 model
|
9 |
+
model = torch.hub.load('ultralytics/yolov5', 'custom', path=model_path)
|
10 |
+
|
11 |
+
# Function to perform object detection
|
12 |
+
def detect_objects(image):
|
13 |
+
# Perform inference with the YOLOv5 model
|
14 |
+
results = model(image)
|
15 |
+
|
16 |
+
# Get the detected bounding boxes and labels
|
17 |
+
bboxes = results.xyxy[0].cpu().numpy()
|
18 |
+
labels = results.names
|
19 |
+
|
20 |
+
# Create a list of dictionaries for each detected object
|
21 |
+
detections = []
|
22 |
+
for bbox, label in zip(bboxes, labels):
|
23 |
+
x1, y1, x2, y2, conf, cls = bbox
|
24 |
+
detections.append({
|
25 |
+
'label': label,
|
26 |
+
'confidence': float(conf),
|
27 |
+
'x1': float(x1),
|
28 |
+
'y1': float(y1),
|
29 |
+
'x2': float(x2),
|
30 |
+
'y2': float(y2)
|
31 |
+
})
|
32 |
+
|
33 |
+
return detections
|
34 |
+
|
35 |
+
# Create the Gradio app
|
36 |
+
app = gr.Interface(detect_objects, gr.Image(type="pil"), "label", examples=[
|
37 |
+
["example_image.jpg"] # Replace with your own example image
|
38 |
+
])
|
39 |
+
|
40 |
+
# Run the app
|
41 |
+
app.launch()
|