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

Application State Management for Drowsiness Detection
 
This module defines the `AppState` class, which manages the application state
for blink detection, drowsiness analysis, and related metrics. It includes methods
to compute blink rates, evaluate drowsiness, and maintain a buffer of EAR (Eye Aspect Ratio) measurements.
 
Constants:
    - EAR_THRESHOLD (float): Threshold below which the EAR indicates eye closure.
 
Classes:
    - AppState: Manages application state for blink detection and drowsiness monitoring.
 
Dependencies:
    - dataclasses: For defining the `AppState` class with default values and field types.
    - collections.deque: For maintaining rolling buffers of EAR measurements and timestamps.
    - threading: For managing locks to synchronize alerts and video processing.
    - time: For time-based computations such as timestamps and durations.
 
Usage:
    Create an `AppState` instance and use its methods to track drowsiness-related metrics.
 
Author:
    Lior Jigalo
 
License:
    MIT

 
Modules
       
threading
time

 
Classes
       
builtins.object
AppState

 
class AppState(builtins.object)
    AppState(blink_counter: int = 0, total_blinks: int = 0, EAR_consec_frames: int = 0, frame_buffer: collections.deque = <factory>, buffer_size: int = 300, fps: float = 0.0, blink_durations: list = <factory>, is_blinking: bool = False, current_blink_start: float = 0.0, analysis_window: int = 20, blink_rate_threshold: float = 90.0, prolonged_EAR_duration_threshold: float = 1, blink_timestamps: collections.deque = <factory>, last_alert_time: float = 0.0, debounce_time_alert: float = 4, last_video_time: float = 0.0, debounce_time_video: float = 30, alert_lock: <built-in function allocate_lock> = <factory>, video_lock: <built-in function allocate_lock> = <factory>, ear_measurements: collections.deque = <factory>, ear_sum: float = 0.0, last_ear_reset_time: float = 0.0, ear_reset_cooldown: float = 5.0, avg_ear_threshold: float = 0.25) -> None
 
Manages the application state for drowsiness detection and blink analysis.
 
Attributes:
    blink_counter (int): Count of consecutive EAR frames below the threshold.
    total_blinks (int): Total number of blinks detected.
    EAR_consec_frames (int): Consecutive frames with EAR below the threshold.
    frame_buffer (deque): Buffer for storing recent video frames.
    buffer_size (int): Maximum size of the frame buffer.
    fps (float): Current frames per second.
    blink_durations (list): List of durations for individual blinks.
    is_blinking (bool): Indicates if a blink is currently happening.
    current_blink_start (float): Timestamp for when the current blink started.
 
    analysis_window (int): Time window (in seconds) for computing blink rates and EAR averages.
    blink_rate_threshold (float): Threshold for high blink rate (blinks per minute).
    prolonged_EAR_duration_threshold (float): Duration threshold for prolonged low EAR.
 
    blink_timestamps (deque): Timestamps of recent blinks for rate computation.
 
    last_alert_time (float): Timestamp for the last alert.
    debounce_time_alert (float): Minimum time between consecutive alerts.
    last_video_time (float): Timestamp for the last video notification.
    debounce_time_video (float): Minimum time between video notifications.
    alert_lock (threading.Lock): Lock to synchronize alert generation.
    video_lock (threading.Lock): Lock to synchronize video processing.
 
    ear_measurements (deque): Rolling buffer of EAR measurements and timestamps.
    ear_sum (float): Running sum of EAR measurements for fast average computation.
 
    last_ear_reset_time (float): Timestamp for the last EAR measurement reset.
    ear_reset_cooldown (float): Cooldown duration for resetting EAR measurements.
    avg_ear_threshold (float): Threshold for average EAR indicating drowsiness.
 
  Methods defined here:
__eq__(self, other)
Return self==value.
__init__(self, blink_counter: int = 0, total_blinks: int = 0, EAR_consec_frames: int = 0, frame_buffer: collections.deque = <factory>, buffer_size: int = 300, fps: float = 0.0, blink_durations: list = <factory>, is_blinking: bool = False, current_blink_start: float = 0.0, analysis_window: int = 20, blink_rate_threshold: float = 90.0, prolonged_EAR_duration_threshold: float = 1, blink_timestamps: collections.deque = <factory>, last_alert_time: float = 0.0, debounce_time_alert: float = 4, last_video_time: float = 0.0, debounce_time_video: float = 30, alert_lock: <built-in function allocate_lock> = <factory>, video_lock: <built-in function allocate_lock> = <factory>, ear_measurements: collections.deque = <factory>, ear_sum: float = 0.0, last_ear_reset_time: float = 0.0, ear_reset_cooldown: float = 5.0, avg_ear_threshold: float = 0.25) -> None
Initialize self.  See help(type(self)) for accurate signature.
__repr__(self)
Return repr(self).
add_ear_measurement(self, ear_value: float) -> None
Adds a new EAR measurement and removes those older than 'analysis_window'.
Maintains a running sum for quick average computation.
check_prolonged_EAR(self, current_EAR, ear_start_time) -> bool
Checks if the eye aspect ratio has been continuously below EAR_THRESHOLD
for longer than 'prolonged_EAR_duration_threshold' seconds.
get_average_ear(self) -> float
Returns the average EAR over the last 'analysis_window' seconds.
Computation is O(1) due to the running sum.
is_drowsy(self, current_EAR, ear_start_time) -> tuple[bool, str]
Evaluates multiple drowsiness conditions:
  1. High blink rate.
  2. Prolonged eye closure (EAR < threshold for too long).
  3. Low average EAR over 'analysis_window'.
 
Resets the EAR measurements if a 'low average EAR' alert occurs
and a cooldown has elapsed.
 
Returns:
    (drowsy: bool, reason: str)
update_blink_rate(self) -> float
Removes blink timestamps outside the analysis window and
returns the current blink rate (blinks per minute).

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

Data and other attributes defined here:
EAR_consec_frames = 0
__annotations__ = {'EAR_consec_frames': <class 'int'>, 'alert_lock': <built-in function allocate_lock>, 'analysis_window': <class 'int'>, 'avg_ear_threshold': <class 'float'>, 'blink_counter': <class 'int'>, 'blink_durations': <class 'list'>, 'blink_rate_threshold': <class 'float'>, 'blink_timestamps': <class 'collections.deque'>, 'buffer_size': <class 'int'>, 'current_blink_start': <class 'float'>, ...}
__dataclass_fields__ = {'EAR_consec_frames': Field(name='EAR_consec_frames',type=<class 'int'...appingproxy({}),kw_only=False,_field_type=_FIELD), 'alert_lock': Field(name='alert_lock',type=<built-in function ...appingproxy({}),kw_only=False,_field_type=_FIELD), 'analysis_window': Field(name='analysis_window',type=<class 'int'>,...appingproxy({}),kw_only=False,_field_type=_FIELD), 'avg_ear_threshold': Field(name='avg_ear_threshold',type=<class 'floa...appingproxy({}),kw_only=False,_field_type=_FIELD), 'blink_counter': Field(name='blink_counter',type=<class 'int'>,de...appingproxy({}),kw_only=False,_field_type=_FIELD), 'blink_durations': Field(name='blink_durations',type=<class 'list'>...appingproxy({}),kw_only=False,_field_type=_FIELD), 'blink_rate_threshold': Field(name='blink_rate_threshold',type=<class 'f...appingproxy({}),kw_only=False,_field_type=_FIELD), 'blink_timestamps': Field(name='blink_timestamps',type=<class 'colle...appingproxy({}),kw_only=False,_field_type=_FIELD), 'buffer_size': Field(name='buffer_size',type=<class 'int'>,defa...appingproxy({}),kw_only=False,_field_type=_FIELD), 'current_blink_start': Field(name='current_blink_start',type=<class 'fl...appingproxy({}),kw_only=False,_field_type=_FIELD), ...}
__dataclass_params__ = _DataclassParams(init=True,repr=True,eq=True,order=False,unsafe_hash=False,frozen=False)
__hash__ = None
__match_args__ = ('blink_counter', 'total_blinks', 'EAR_consec_frames', 'frame_buffer', 'buffer_size', 'fps', 'blink_durations', 'is_blinking', 'current_blink_start', 'analysis_window', 'blink_rate_threshold', 'prolonged_EAR_duration_threshold', 'blink_timestamps', 'last_alert_time', 'debounce_time_alert', 'last_video_time', 'debounce_time_video', 'alert_lock', 'video_lock', 'ear_measurements', ...)
analysis_window = 20
avg_ear_threshold = 0.25
blink_counter = 0
blink_rate_threshold = 90.0
buffer_size = 300
current_blink_start = 0.0
debounce_time_alert = 4
debounce_time_video = 30
ear_reset_cooldown = 5.0
ear_sum = 0.0
fps = 0.0
is_blinking = False
last_alert_time = 0.0
last_ear_reset_time = 0.0
last_video_time = 0.0
prolonged_EAR_duration_threshold = 1
total_blinks = 0

 
Data
        EAR_THRESHOLD = 0.25