By Brandon Jackson
Project started December 2012
nap-alert is a fatigue detection system that alerts users when they are falling asleep. The app is designed to combat narcolepsy and microsleep.
nap-alert was built using the OpenCV framework.
Microsleep strikes quickly. You probably don't even realize you're in the process of falling asleep, and almost certainly don't notice that you are blinking for longer than usual. nap-alert uses your computer's webcam to monitor your blink rate and average blink duration. If your eyes stay closed for long periods of time, then the program lets you know that a nap is imminent. Research indicates that this is one of the most effective ways to predict impending microsleep. For a good review of recent experimental findings, see Caffier et al.
nap-alert should work on any OSX, Linux or Windows system with an OpenCV-compatible webcam. OpenCV maintains a list of these cameras here.
The code was run with OpenCV 3 and Python 3 The following python modules should be downoaded and installed:
- numpy
- Python Imaging Library PIL (Pillow)
Run
pip install -r requirements.txt
to install them.
- Initialization
- Webcam initialized, capture size scaled down for speed
FaceModel
andBlinkStats
classes are initialized. These classes synthesize information from multiple frames into a more complete picture.
- Controller Loop:
- The highest-level
while
loop which initiates each frame of image capture - On each new frame the following information is gathered and sent to the
BlinkStats
class (see below):- runs the image processing loop (see below) which returns a guess as to whether the image captures a blink event and a reliability indicator
- measures the time that has elapsed since the last frame
- The highest-level
- The Image Processing Loop: each new frame from the webcam goes through the following steps of processing.
FaceDetector
scans the image for faces and eyes, and returns a set of rectangles using Haar Cascade Classifiers.FaceModel
integrates data from the new frame into a model that keeps track of where the eyes are. To do this it uses:- A moving average of the most recent frames
- Facial geometry to fill in missing data The resulting model generates a set of two specific regions of interest (ROI's) where blinking is expected to take place.
BlinkDetector
watches the blinking ROI for blinking. Detection uses a variety of techniques:- If eye rectangles suddenly disappear and then quickly reappear, this is interpreted as a blink. This method only used when signal quality is very high, to prevent false positives.
- @todo Watches for increase increased brightness in red channel of ROI, caused by the presence of more red pigment in the eyelid than in the eye itself.
- @todo Tracks rapid motion in the ROI The class returns a guess as to whether the current frame captures a blink event, along with a signal quality / confidence / reliability indicator
BlinkStats
receives blink, time and reliability information and does high-level analysis:- keeps track of blink event durations and inter-blink intervals
- determines how much weight to give to blink event based on reliability metric
- finds larger-scale blink trends, such as a slow ramping up of blink duration that corresponds to increasing sleepiness
- If
BlinkStats
finds dangerous trends, the app broadcasts a NAP ALERT!
- Designed and tested using the iSight camera of a 15" Late 2011 Macbook Pro