Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from ultralytics import YOLO
|
2 |
import torch
|
3 |
import cv2
|
4 |
import numpy as np
|
@@ -11,6 +11,9 @@ model = YOLO("yolov5s.pt") # Load pre-trained YOLOv5s model
|
|
11 |
model.to(device)
|
12 |
model.eval()
|
13 |
|
|
|
|
|
|
|
14 |
def preprocess_image(image):
|
15 |
image = Image.fromarray(image)
|
16 |
image = image.convert("RGB")
|
@@ -21,16 +24,18 @@ def detect_objects(image):
|
|
21 |
results = model.predict(image) # Run YOLOv5 inference
|
22 |
|
23 |
# Convert results to bounding box format
|
24 |
-
|
25 |
for result in results:
|
26 |
-
for box in result.boxes.xyxy:
|
27 |
x1, y1, x2, y2 = map(int, box[:4])
|
28 |
-
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
34 |
|
35 |
return image
|
36 |
|
|
|
1 |
+
from ultralytics import YOLO
|
2 |
import torch
|
3 |
import cv2
|
4 |
import numpy as np
|
|
|
11 |
model.to(device)
|
12 |
model.eval()
|
13 |
|
14 |
+
# Load COCO class labels
|
15 |
+
CLASS_NAMES = model.names # YOLOv5's built-in class names
|
16 |
+
|
17 |
def preprocess_image(image):
|
18 |
image = Image.fromarray(image)
|
19 |
image = image.convert("RGB")
|
|
|
24 |
results = model.predict(image) # Run YOLOv5 inference
|
25 |
|
26 |
# Convert results to bounding box format
|
27 |
+
image = np.array(image)
|
28 |
for result in results:
|
29 |
+
for box, cls in zip(result.boxes.xyxy, result.boxes.cls):
|
30 |
x1, y1, x2, y2 = map(int, box[:4])
|
31 |
+
class_name = CLASS_NAMES[int(cls)] # Get class name
|
32 |
|
33 |
+
# Draw bounding box
|
34 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
35 |
+
|
36 |
+
# Put class label
|
37 |
+
cv2.putText(image, class_name, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX,
|
38 |
+
0.5, (255, 0, 0), 2, cv2.LINE_AA)
|
39 |
|
40 |
return image
|
41 |
|