faceController
index
/home/bedrock/Desktop/project/AlertWatch/Production/detector/util/faceController.py

Drowsiness Detection and Blink Monitoring Utilities
 
This module provides utilities for processing face bounding boxes, running landmark inference,
detecting blinks, and identifying signs of drowsiness. It also includes functionality for sending
alerts over WebSocket and saving video when drowsiness is detected.
 
Constants:
    - WS_URL (str): WebSocket URL for sending alerts.
    - RECONNECT_INTERVAL (int): Interval in seconds for reconnect attempts to the WebSocket server.
 
Functions:
    - process_bounding_box: Processes a bounding box to fit within frame boundaries.
    - run_landmark_inference: Runs inference for facial landmarks and reshapes the output.
    - handle_blink_detection: Detects blinks based on Eye Aspect Ratio (EAR) and updates state.
    - handle_drowsiness_detection: Detects drowsiness based on EAR and triggers alerts or video saving.
    - send_drowsiness_alert: Sends drowsiness alerts as JSON messages over a WebSocket.
 
Dependencies:
    - asyncio: For asynchronous WebSocket communication.
    - datetime: For timestamping video files and alerts.
    - json: For creating JSON messages.
    - threading: For running alert and video saving tasks in separate threads.
    - time: For measuring durations and handling debouncing.
    - cv2: For OpenCV operations like drawing text on frames.
 
Usage:
    Use the functions to process video frames, monitor blinks, and handle drowsiness alerts in
    real-time applications.
 
Author:
    Lior Jigalo
 
License:
    MIT

 
Modules
       
asyncio
cv2
datetime
json
numpy
threading
time

 
Functions
       
handle_blink_detection(left_eye, right_eye, state, ear_threshold, consec_frames) -> float
Detects blinks based on the Eye Aspect Ratio (EAR) and updates the application state.
 
Args:
    left_eye (np.ndarray): Landmarks for the left eye.
    right_eye (np.ndarray): Landmarks for the right eye.
    state (AppState): The application state object.
    ear_threshold (float): Threshold for EAR to indicate eye closure.
    consec_frames (int): Minimum consecutive frames below threshold to count as a blink.
 
Returns:
    float: The average EAR value for the current frame.
handle_drowsiness_detection(avg_EAR, state, frame) -> None
Detects drowsiness based on the EAR and application state, triggers alerts or video saving.
 
Args:
    avg_EAR (float): The average EAR value for the current frame.
    state (AppState): The application state object.
    frame (np.ndarray): The video frame for annotation and display.
process_bounding_box(face, frame) -> tuple[int, int, int, int, float]
Processes a bounding box to ensure it fits within frame boundaries.
 
Args:
    face (tuple): Bounding box coordinates and confidence score (x1, y1, x2, y2, score).
    frame (np.ndarray): The video frame.
 
Returns:
    tuple: Adjusted bounding box coordinates (x1, y1, x2, y2, score).
run_landmark_inference(hailo_inference, preprocessed_face, face_land_output_name, class_num) -> numpy.ndarray | None
Runs inference for facial landmarks using a Hailo inference object.
 
Args:
    hailo_inference: The inference object for running models.
    preprocessed_face (np.ndarray): Preprocessed face ROI.
    face_land_output_name (str): Name of the output tensor for landmarks.
    class_num (int): Number of landmark points.
 
Returns:
    np.ndarray | None: Reshaped landmark coordinates (class_num x 2), or None if no output.
send_drowsiness_alert(ws_url: str, reconnect_interval: int, reason: str) -> None
Sends a drowsiness alert as a JSON message over a WebSocket connection.
 
Args:
    ws_url (str): The WebSocket server URL.
    reconnect_interval (int): Interval in seconds for reconnect attempts.
    reason (str): Reason for the drowsiness alert.

 
Data
        RECONNECT_INTERVAL = 2
WS_URL = 'ws://192.168.0.63:5000'
__annotations__ = {'RECONNECT_INTERVAL': <class 'int'>, 'WS_URL': <class 'str'>}