File size: 1,831 Bytes
e6f63f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import streamlit as st
import torch
from pygame import mixer
import numpy as np
import cv2
cv2.__version__
# Load YOLOv5 model
model = torch.hub.load('ultralytics/yolov5', 'custom', path=r"C:\Users\gokul\PycharmProjects\driver_drowsiness\last.pt", force_reload=True)

# Initialize the mixer
mixer.init()
sound = mixer.Sound(r"C:\Users\gokul\PycharmProjects\driver_drowsiness\alram.wav")

# Initialize the score variable

# Function to start the camera and process frames
def start_camera():
    global score
    score = 0

    cap = cv2.VideoCapture(0)

    while cap.isOpened():
        ret, frame = cap.read()
        # Make detections
        results = model(frame)

        height, width = frame.shape[0:2]  # Extract height and width
        class_id = 16
        if class_id in results.pred[0][:, 5]:
            # Increment the score variable
            score += 1
            if score > 15:
                try:
                    sound.play()
                except:
                    pass

        else:
            print((results.pred[0][:, 5]))
            score -= 1
            if score < 0:
                score = 0

                # Display the score on the frame
        cv2.putText(frame, 'Score: ' + str(score), (100, height - 20),
                    fontFace=cv2.FONT_HERSHEY_COMPLEX_SMALL,
                    fontScale=1, color=(255, 255, 255),
                    thickness=1, lineType=cv2.LINE_AA)

        rendered = np.squeeze(results.render())
        frame[0:rendered.shape[0], 0:rendered.shape[1]] = rendered

        cv2.imshow('YOLO', frame)
        if cv2.waitKey(10) & 0xFF == ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows()
# Streamlit App
st.title("Drowsiness Detection with YOLOv5")

# Button to start the camera
if st.button("Start Camera"):
    start_camera()