Today’s blog post is the long-awaited tutorial on real-time drowsiness detection on the Raspberry Pi!
Back in May I wrote a (laptop-based) drowsiness detector that can be used to detect if the driver of a motor vehicle was getting tired and potentially falling asleep at the wheel.
The driver drowsiness detector project was inspired by a conversation I had with my Uncle John, a long haul truck driver who has witnessed a more than a few accidents due to fatigued drivers.
The post was really popular and a lot of readers got value out of it…
…but the method was not optimized for the Raspberry Pi!
Since then readers have been requesting me to write a followup blog post that covers the necessary optimizations to run the drowsiness detector on the Raspberry Pi.
I caught up with my Uncle John a few weeks ago and asked him what he would think of a small computer that could be mounted inside his truck cab to help determine if he was getting tired at the wheel.
He wasn’t crazy about the idea of being monitored by a camera his entire work day (and I don’t necessarily blame him either — I wouldn’t want to be monitored all the time either). But he dideventually concede that a device like this, and ideally less invasive, would certainly help avoid accidents due to fatigued drivers.
To learn more about these facial landmark optimizations and how to run our drowsiness detector on the Raspberry Pi, just keep reading!
Raspberry Pi: Facial landmarks + drowsiness detection with OpenCV and dlib
Today’s tutorial is broken into four parts:
Discussing the tradeoffs between Haar cascades and HOG + Linear SVM detectors.
Examining the TrafficHAT used to create the alarm that will sound if a driver/user gets tired.
Implementing dlib facial landmark optimizations so we can deploy our drowsiness detector to the Raspberry Pi.
Viewing the results of our optimized driver drowsiness detection algorithm on the Raspberry Pi.
Before we get started I would highly encourage you to read through my previous tutorial onDrowsiness detection with OpenCV.
While I’ll be reviewing the code in its entirety here, you should still read the previous post as I discuss the actual Eye Aspect Ratio (EAR) algorithm in more detail.
The EAR algorithm is responsible for detecting driver drowsiness.
Haar cascades: less accurate, but faster than HOG
The major optimization we need to run our driver drowsiness detection algorithm on the Raspberry Pi is to swap out the default dlib HOG + Linear SVM face detector and replace it with OpenCV’s Haar cascade face detector.
While HOG + Linear SVM detectors tend to be significantly more accurate than Haar cascades, the cascade method is also much faster than HOG + Linear SVM detection algorithms.
A complete review of both HOG + Linear SVM and Haar cascades work is outside the scope of this blog post, but I would encourage you to:
Read this post on Histogram of Oriented Gradients and Object Detection where I discuss the pros and cons of HOG + Linear SVM and Haar cascades.
Work through the PyImageSearch Gurus course where I demonstrate how to implement your own custom HOG + Linear SVM object detectors from scratch.
The Raspberry Pi TrafficHAT
In our previous tutorial on drowsiness detection I used my laptop to execute driver drowsiness detection code — this enabled me to:
Ensure the drowsiness detection algorithm would run in real-time due to the faster hardware.
Use the laptop speaker to sound an alarm by playing a .WAV file.
The Raspberry Pi does not have a speaker so we cannot play any loud alarms to wake up the driver…
…but the Raspberry Pi is a highly versatile piece of hardware that includes a large array of hardware add-ons.