Spaces:
Sleeping
Sleeping
File size: 1,077 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 |
from ultralytics import YOLO
# Initialize video capture
video_path = "Media/traffic.mp4"
cap = cv2.VideoCapture(video_path)
# Load YOLO model with custom weights
model = YOLO("Weights/best.pt")
# Define class names
classNames = ['With Helmet', 'Without Helmet']
# For the use of Webcam
# Open the webcam (use 0 for the default camera, or 1, 2, etc. for additional cameras)
# cap = cv2.VideoCapture(0)
while True:
success, img = cap.read()
results = model(img, stream=True)
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])
cvzone.putTextRect(img, f'{classNames[cls]} {conf}', (max(0, x1), max(35, y1)), scale=1, thickness=1)
cv2.imshow("Image", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break |