# Driver Identification and Detection of Drowsiness while Driving
## 5 Implementation
### Server
#### 5.1.3 Drowsiness Detection Function  
In the drowsiness monitoring section, an ML model is employed in conjunction with the OpenCV library to discern the driver's state of drowsiness by scrutinizing images acquired from a camera installed within the vehicle. The model proficiently categorizes the condition of the driver's eyes, classifying them as either \"open\" or \"closed\". The dataset utilized for training this model has been sourced from DataFlair. The procedural methodology encompasses the creation of a dataset through the capture of images of the driver's eyes via a camera, followed by their classification into \"open\" or \"closed\" categories. During the data cleaning phase, meticulous criteria are applied, involving the elimination of superfluous images, particularly those characterized by suboptimal quality or lacking relevance for model training. This curation process encompasses approximately 7,000 photographs, capturing human eyes in diverse environmental settings. Subsequently, the ML model undergoes a comprehensive training process. The conclusive weights and architecture file of the model, denominated 'cnnCat2.h5', are enclosed within the'models' folder. The resultant model proves instrumental in ascertaining whether the driver's eyes are open or closed, thereby facilitating the identification of indications of drowsiness during the act of driving. The sequential steps employed for the detection of driver drowsiness are delineated as follows and are shown in Figure 5.  
Figure 4: Result of the facial recognition code. 1. Capture an image through the camera: A webcam is used to capture an image for input. An infinite loop is constructed to record each frame from the camera. The camera is accessed, and the capture object cap is set using the OpenCV method cv2.VideoCapture(0). Each frame is read with cap.read(), and the image is stored in a frame variable. 2. Creation of a region of interest (ROI) by detecting faces: In order to detect faces in an image, the image is converted to gray scale since the OpenCV object detection algorithm accepts gray scale images as input. In order to find faces, a cascaded classifier is used. Next, faces = face.detectMultiScale(grey) is used for detection. It returns an array of hits containing the x, y, height, and width co-ordinates of the bounding box of the object. Now, it is possible to iterate over the faces and draw bounding boxes for each face. 3. Determining the eye region of interest (ROI) and providing it to the classifier: Eyes are discovered using the same technique as for the faces. The left eye (leye) and right eye (eye) cascade classifiers are initialized, and the detectMultiScale(gray) function is used to detect the eyes. Then, only the eye data are extracted from the entire image. This is accomplished by removing the eye-bounding box. The variable l_eye contains the image data for the left eye and r_eye for the right eye. This is fed into a CNN classifier, which predicts whether the eyes are open or closed. 4. The classifier detects whether the eyes are open or closed. A CNN classifier is used to predict the state of the eyes. First, the color image is converted to a gray scale. Then the image is resized to 24 \(\times\) 24 pixels. The code lpred = model.predict_classes(l_eye) can be used to predict each eye using the trained model. The predict_classes function returns the class 0 or 1, as predicted by the model for the given input image. If the value of lpred[0] is equal to 1, it indicates that the eyes are open. On the other hand, if the value of lpred[0] is equal to 0, it means that the eyes are closed. 5. It calculates a score to determine if the subject is asleep: A score is essentially a value used to determine how long a person keeps their eyes closed. Therefore, if both eyes are closed, the score will always increase, and if both eyes are open, the score will decrease. The sleep detection function must receive the following variables: 1. **img**: The image received from the customer of the Jetson Nano, i.e., an image of the driver. 2.