main
index
/home/bedrock/Desktop/project/AlertWatch/Production/detector/main.py

Real-time Face Detection and Landmark Inference with WebSocket Streaming
 
This script performs real-time face detection, landmark inference, and monitoring
using a camera feed. It also streams the processed video frames over a WebSocket server.
 
Features:
    - Real-time face detection and facial landmarks inference.
    - Blink detection and drowsiness monitoring.
    - WebSocket server for streaming video frames to clients.
    - Modular design with support for Hailo inference models.
 
Modules:
    - time, os, platform: System utilities for managing time, files, and platform specifics.
    - threading, asyncio: For managing multithreading and asynchronous tasks.
    - collections.deque: For maintaining a rolling buffer of video frames.
    - cv2, numpy: OpenCV and NumPy for image processing.
    - base64: For encoding video frames as base64 strings for WebSocket transmission.
    - inference: Initializes Hailo inference models.
    - util: Utility functions for preprocessing, blink/drowsiness detection, and state management.
    - drawUtil: Functions for drawing bounding boxes, landmarks, and other visual indicators.
    - prePostProcessing: Functions for preprocessing input and postprocessing model outputs.
 
Constants:
    - CLASS_NUM: Number of facial landmarks (136/2 for x, y coordinates).
    - EAR_THRESHOLD: Eye Aspect Ratio threshold for blink detection.
    - CONSEC_FRAMES: Consecutive frames below EAR threshold to count a blink.
    - BUFFER_DURATION: Duration (in seconds) of the frame buffer.
    - FRAMES_TO_SKIP: Number of frames to skip for face detection.
    - FACES: Store detected face bounding boxes when skipping frames.
 
Functions:
    - handle_faces(faces, frame, hailo_inference, face_land_output_name, ...): Handles face processing, including landmark inference, blink, and drowsiness detection.
    - get_faces(frame, hailo_inference, face_detection_input_shape): Runs face detection on a video frame and returns bounding boxes.
    - video_processing_loop(hailo_inference, face_detection_input_shape, ...): Processes video frames, detects faces/landmarks, and manages display.
    - send_frames(websocket): Continuously sends frames to a WebSocket client.
    - websocket_handler(websocket, path): Handles new WebSocket connections and sends video frames.
    - start_websocket_server(): Starts the WebSocket server on port 8765.
    - main(): Initializes the system, starts video processing, and launches the WebSocket server.
 
Usage:
    Run the script to start real-time face monitoring and WebSocket streaming.
    Clients can connect to the WebSocket server to receive the video feed.
 
Requirements:
    - Python 3.8+
    - OpenCV
    - NumPy
    - Hailo inference libraries
    - asyncio, websockets
 
Entry Point:
    The `main()` function initializes the system and runs the video processing
    and WebSocket server in separate threads.
 
Author:
    Lior Jigalo
 
License:
    MIT

 
Modules
       
asyncio
base64
cv2
numpy
platform
threading
time
inference
drawUtil
util

 
Functions
       
get_faces(frame, hailo_inference, face_detection_input_shape) -> list[int, int, int, int, float] | None
Preprocess frame, run face detection, and return bounding boxes.
handle_faces(faces, frame, hailo_inference, face_land_output_name, face_landmarks_input_shape, all_landmarks, state: util.AppState.AppState, tensors) -> None
Handle detected faces: run landmark inference, blink, drowsiness detection.
main() -> None
async send_frames(websocket)
Continuously send frames to connected client over WebSocket.
async start_websocket_server() -> None
Launch the WebSocket server on port 8765.
video_processing_loop(hailo_inference, face_detection_input_shape, face_landmarks_input_shape, face_land_output_name, state: util.AppState.AppState)
Continually capture frames, run face detection/landmarks, and manage display.
async websocket_handler(websocket, path: str) -> None
Handle new WebSocket connection.

 
Data
        BUFFER_DURATION = 30
CLASS_NUM = 68
CONSEC_FRAMES = 2
EAR_THRESHOLD = 0.25
FACES = None
FRAMES_TO_SKIP = 4
__annotations__ = {'BUFFER_DURATION': <class 'int'>, 'CLASS_NUM': <class 'int'>, 'CONSEC_FRAMES': <class 'int'>, 'EAR_THRESHOLD': <class 'float'>, 'FACES': int | None, 'FRAMES_TO_SKIP': <class 'int'>}
latest_frame = None
lock = <unlocked _thread.lock object>