Spaces:
Sleeping
Sleeping
File size: 1,126 Bytes
a8917eb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import cv2
import math
import cvzone
from ultralytics import YOLO
# Load YOLO model with custom weights
yolo_model = YOLO("Weights/best.pt")
# Define class names
class_labels = ['With Helmet', 'Without Helmet']
# Load the image
image_path = "Media/riders_1.jpg"
img = cv2.imread(image_path)
# Perform object detection
results = yolo_model(img)
# Loop through the detections and draw bounding boxes
for r in results:
boxes = r.boxes
for box in boxes:
x1, y1, x2, y2 = box.xyxy[0]
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
w, h = x2 - x1, y2 - y1
cvzone.cornerRect(img, (x1, y1, w, h))
conf = math.ceil((box.conf[0] * 100)) / 100
cls = int(box.cls[0])
if conf > 0.1:
cvzone.putTextRect(img, f'{class_labels[cls]} {conf}', (x1, y1 - 10), scale=0.8, thickness=1, colorR=(255, 0, 0))
# Display the image with detections
cv2.imshow("Image", img)
# Close window when 'q' button is pressed
while True:
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
cv2.waitKey(1) |