# Driver Identification and Detection of Drowsiness while Driving
## 5 Implementation
### Server
#### 5.1.2 Facial Recognition Function  
This code section is responsible for facial recognition in drivers. In general terms, it receives images from the client inside the vehicle, analyzes them, searches for matches among authorized individuals' faces, and sends a signal to the vehicle to start or not.  
The code starts with the definition of the state_recognition function, which takes four parameters:  
* img: This parameter stores the current frame of the real-time camera;
* name_prove: This variable stores the name of the person who wants to start the vehicle at that moment. Later, it will be compared with classNames to see if it has permissions;
* classNames: This is in charge of storing the names of the people authorized to start the vehicle;
* authorizeEmbeddings: This parameter stores the facial embeddings of the people authorized to drive the vehicle.  
Next, the current frame captured by the camera is stored in the im variable, transforming it to a format suitable for processing. A transformation is performed to adjust the range of pixel values from [-1, 1] to [0, 255] and is converted to uint8. Two empty lists are initialized: faceDistances, which will store the Euclidean distance when comparing the face in the current frame with all authorized faces, i.e., how similar the faces are, and permission to store the authorization status. The facesCurrFrame variable stores the matrix of bounding boxes for the faces found in that frame, i.e., their location, whereas encodesCurrFrame stores the vector of 128 key points that make each face unique.  
In this step, the current frame is compared to all images of the authorized individuals to find any matches, which will be stored in'matches' using the compare_faces function. If the vector of 128 points differs by 0.6 or less from the default value, it means both facesare considered a match. Therefore, all face vectors that meet this condition are stored in faceDistances. If there is a match, the authorized person's name is stored in the 'name' variable, but the output is formatted differently. By default, the images have the format \"First_Last.jpg\"; therefore, this function changes this format to \"First Last\". Finally, the words \"Authorized\" or \"Refused\" are stored in the 'permission' variable. This will be used to send the starting signal to the vehicle (or not), in addition to other uses for KPIs.  
In relation to facial recognition, Figure 4 depicts the outcome of a run, illustrating a vector of facial embeddings stored in the vehicle's database. The individual facing the camera corresponds to the first value of the facial embedding vector, displaying a value closest to 0, indicating the degree of similarity to the individual. A value approaching 1 suggests no resemblance to the person in front of the camera. It is evident across multiple iterations that the individual is consistently recognized correctly, thereby confirming their identity and authorization to initiate the vehicle. This numerical representation signifies the accuracy of resemblance between the individual facing the camera and those authorized to drive, serving as a metric to assess the quality of facial recognition.