Spaces:
Runtime error
Runtime error
Edvin Behdadijd
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,24 +7,43 @@ from ultralytics import YOLO
|
|
| 7 |
# Load the YOLOv8 model
|
| 8 |
model = YOLO('best.pt')
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def detect_objects(image):
|
| 11 |
# Convert the input image to a format YOLO can work with
|
| 12 |
image = np.array(image)
|
| 13 |
|
| 14 |
# Perform detection
|
| 15 |
-
results = model(image
|
| 16 |
|
| 17 |
# Draw bounding boxes on the image
|
| 18 |
for box in results.boxes.data.cpu().numpy():
|
| 19 |
x1, y1, x2, y2, score, class_id = box
|
| 20 |
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Put the class name above the bounding box
|
| 26 |
class_name = model.model.names[int(class_id)]
|
| 27 |
-
cv2.putText(image, f'{class_name} {score:.2f}', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
|
| 28 |
|
| 29 |
# Convert back to PIL image
|
| 30 |
return Image.fromarray(image)
|
|
|
|
| 7 |
# Load the YOLOv8 model
|
| 8 |
model = YOLO('best.pt')
|
| 9 |
|
| 10 |
+
# Define a list of colors for different classes
|
| 11 |
+
colors = [
|
| 12 |
+
(255, 0, 0), # Red
|
| 13 |
+
(0, 255, 0), # Green
|
| 14 |
+
(0, 0, 255), # Blue
|
| 15 |
+
(255, 255, 0), # Cyan
|
| 16 |
+
(255, 0, 255), # Magenta
|
| 17 |
+
(0, 255, 255), # Yellow
|
| 18 |
+
(128, 0, 0), # Maroon
|
| 19 |
+
(0, 128, 0), # Olive
|
| 20 |
+
(128, 128, 0), # Teal
|
| 21 |
+
(0, 0, 128), # Navy
|
| 22 |
+
(128, 0, 128), # Purple
|
| 23 |
+
(0, 128, 128) # Aqua
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
def detect_objects(image):
|
| 27 |
# Convert the input image to a format YOLO can work with
|
| 28 |
image = np.array(image)
|
| 29 |
|
| 30 |
# Perform detection
|
| 31 |
+
results = model(image)[0]
|
| 32 |
|
| 33 |
# Draw bounding boxes on the image
|
| 34 |
for box in results.boxes.data.cpu().numpy():
|
| 35 |
x1, y1, x2, y2, score, class_id = box
|
| 36 |
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
|
| 37 |
|
| 38 |
+
# Select color for the class id
|
| 39 |
+
color = colors[int(class_id) % len(colors)]
|
| 40 |
+
|
| 41 |
+
# Draw the bounding box with a thinner line
|
| 42 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), color, 1) # Line width set to 1
|
| 43 |
|
| 44 |
# Put the class name above the bounding box
|
| 45 |
class_name = model.model.names[int(class_id)]
|
| 46 |
+
cv2.putText(image, f'{class_name} {score:.2f}', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1)
|
| 47 |
|
| 48 |
# Convert back to PIL image
|
| 49 |
return Image.fromarray(image)
|