Learn how to enhance a magic mirror with motion sensors, face detection, and face recognition using PIR sensors, radar sensors, and image processing on a Raspberry Pi.
[0:00] This is going to be a jam-packed video. We’re going to cover PIR sensors, radar sensors,
[0:05] do a bit of image processing, and we’ll stray into face detection and recognition.
[0:09] I could have made 5 or 6 videos, but I like you all so much, I’m going to smash it all into
[0:13] one bumper video. If you follow the channel, you’ll know I’ve got a magic mirror. And if you
[0:18] don’t follow the channel, what’s wrong with you? Hit the subscribe button, like this video,
[0:22] and leave a funny comment. So, I love my magic mirror, but I’ve got a big problem with it.
[0:27] There’s nothing wrong with it functionally, it does everything I need. The problem I have
[0:31] is it’s always on, and it’s always drawing maximum power. It’s not a lot of power, but have you seen
[0:36] the cost of electricity lately? I want to fix this problem, but what can I do? If this was a
[0:42] desktop or laptop, then we’d just have it turn off the screen after a period of inactivity.
[0:46] Pretty simple when you have a mouse or keyboard, but a magic mirror with a mouse and keyboard
[0:50] would look pretty silly. We could add a button, we could even try and get clever and add a
[0:55] capacitive touch sensor, maybe in the frame or behind the glass. But then we’d end up with
[0:59] mucky fingerprints all over the mirror, and it kind of defeats the whole point of having
[1:03] a magic mirror in the first place. I might as well just use my phone. What we really want to do
[1:08] is to only have the screen turn on if someone is actually in front of the mirror.
[1:13] So, what can we do? Well, the obvious solution is to just have a motion sensor. It’s not a
[1:18] bad idea, so we’ll look at that first. We’ve got a couple of options. We’ll start off with the
[1:24] old classic Passive InfraRed motion sensor. These are readily available, and are dead easy to hook
[1:30] up. There’s only three pins to worry about, power, ground, and the output pin. The power pin
[1:36] can take anything between 4.5 and 12 volts, so that’s easy enough. We can just take 5 volts
[1:41] directly from the Pi. The output pin goes high if movement is detected, so that’s also pretty
[1:46] simple. There’s a couple of potentiometers for tuning the behavior. One of them adjusts the
[1:51] sensitivity, and the other adjusts how long the output pin stays high for when the motion is detected.
[1:56] The only slightly confusing option is this jumper. This switches the board between
[2:01] re-trigger or normal mode. In re-trigger mode, the pin goes high when motion is detected,
[2:06] and will go low after the delay from the time from the last motion was detected. Basically,
[2:11] the pin will stay high for as long as something is moving. In normal mode, the output will trigger
[2:17] every second or so when there is motion. You’ll typically be wanting to use re-trigger mode,
[2:22] as this behaves in the most sensible way that most people would expect. I put you up to an LED,
[2:27] and it lights up when there’s movement. Pretty simple. So, we could easily connect this to our
[2:32] Raspberry Pi. But I can hear a few of you shouting, STOP! You’re about to connect a 5 volt output to
[2:38] the 3.3 volt input of the Raspberry Pi. Don’t you realize how bad that could be and how precious
[2:43] your Raspberry Pi is? Well, worry not. These boards have a cheeky 3.3 volt regulator on them,
[2:49] so they are actually running at 3.3 volt logic levels. You can in fact bypass this regulator
[2:54] if you only have 3.3 volts available to you. So we can just hook it straight up to the Pi
[2:59] without any worries, and it works pretty well. The problem I have with this module is it’s
[3:04] pretty thick. If we stick this behind the mirror, then it’s going to stick out from the wall.
[3:09] I’m also not even sure that it will work through the mirror. It’s entirely possible that the
[3:13] mirror will block any infrared light, and if we put it outside of the mirror, it’s going to look
[3:18] pretty ugly. So let’s have a look at something slightly more modern. I’ve had this Doppler
[3:24] radar microwave motion sensor module lying around for ages. The nice thing about this module is it’s
[3:29] pretty much a drop in replacement for the PIR module. It can take 4 volts to 28 volts on its
[3:34] VIN pin, and it also has a 3.3 volt compatible output. There’s even a 3.3 volt regulator supply
[3:41] that you can use to power other low current devices. It will do around 100mA or so. The other
[3:47] really nice thing is that it’s super slim, so it will fit behind the mirror quite nicely.
[3:52] These are really very clever devices. They use a PCB antenna to send out microwaves at around 3.18
[3:58] GHz, and it measures the radiation that is reflected back. They are also dead cheap,
[4:04] on Aliexpress you can get them for just over 20p each. That’s less than half the price of a PIR
[4:09] sensor. Now there are much more sophisticated Doppler radar sensors. Great Scott did a really
[4:15] good recent video where he built a really nice alarm system using a human presence radar sensor
[4:20] module, but this module is 10 times more expensive than the cheap board I am using. I hooked up my
[4:25] module to a Raspberry Pi and stuck it behind the mirror. Let’s see if it actually works. I was
[4:30] actually quite worried that the half-silvered mirror might block the radar, but it seems to work
[4:35] fine. If anything, it might be too good. We may get a lot of false positives from people moving
[4:40] around in the rest of the house. This is a problem. We really only want to power things up if there’s
[4:45] definitely someone in front of the mirror. Since we’re looking at PCBs, it’s a good time to slip
[4:50] in a mention of PCBWay who are sponsoring this video. They’ve got their 9th anniversary going on
[4:55] right now, so there’s a bunch of money off coupons. If you need a PCB, then pop over to
[4:59] PCBWay. They’re really great and do a great service. So let’s get a bit more sophisticated.
[5:06] How about using a camera attached to the Pi? This might give us some really interesting options.
[5:12] We should be able to use the camera to detect motion, and if we get clever, we should be able
[5:16] to actually detect if someone is looking at the mirror. As a stretch goal, we could even
[5:21] potentially recognise who the person is. That would be really cool.
[5:26] Let’s cover the motion detection option first. This is actually really trivial to do with a
[5:31] camera. All you need to do is compare the latest frame from the camera with the previous frame and
[5:36] see if they are different. The easiest way to do this is to take the absolute difference between
[5:40] each frame. You can then threshold this to remove any noise, and then it’s just a case of counting
[5:45] how many pixels have changed. If there’s a lot of changed pixels, then there must have been some
[5:50] movement in front of the camera. It’s dead simple, and it works pretty well. Here I am
[5:54] moving around in front of the Pi. What about face detection? We’ve got the camera anyway,
[5:59] so we might as well go to town. What are our options? Well, we have OpenCV. This runs pretty well
[6:06] on the Pi Zero. They have a haar cascade classifier already trained for detecting faces, so we just
[6:11] need to download this. The code is really simple, it’s just a few lines. I’ve got it set up and
[6:16] running in a loop. It’s really fast, and it only takes around 50 to 100 milliseconds per frame.
[6:22] That’s pretty amazing. Not bad for a Pi Zero too. So that’s face detection. What about our stretch
[6:28] goal of face recognition? Move a mirror on the wall? Who am I? Well, I found another Python
[6:35] library aptly named Face Recognition. This is built on top of the really cool Dlib library.
[6:41] The Dlib library has state-of-the-art face recognition built in. So not only can we
[6:45] detect a face, we should be able to identify who the face belongs to. That’s magic!
[6:51] All you need to do is provide the code with pictures of each face you want to recognize.
[6:56] It detects the location of the face, and then matches the detected face against the known faces.
[7:02] The only downside is this library is quite a bit slower than the OpenCV version,
[7:06] and takes around 0.85 seconds to run the face detection. If there is a face in the frame,
[7:12] then it takes an additional 0.7 seconds to do the face recognition.
[7:16] But it feels like we can get the best of both worlds. We could use the OpenCV face detection,
[7:21] and then use this face recognition library for doing the actual face recognition.
[7:25] The only problem with all this clever stuff is that running the camera and doing image processing
[7:29] is pretty intensive, and it maxes out the CPU. When the Pi is idling along doing nothing,
[7:34] then it draws around 100mA. If we run the camera and motion detection code,
[7:39] this goes up to over 200mA, and when we’re running the face detection code,
[7:43] it goes up to over 300mA. This kind of goes against our original goal of trying to reduce power
[7:49] consumption. The PiN camera also starts to get quite warm when we run them continuously.
[7:55] We can see this by looking at it with my infrared camera. I have to say, I absolutely
[7:59] love this gadget. It was quite expensive, but it’s now one of my favorite toys.
[8:04] So why not combine everything together in one awesome package?
[8:08] We’ll use the radar detector to see if anyone is near the mirror. If there is,
[8:12] then we’ll use the camera to detect if there’s motion in front of the mirror.
[8:16] And then we’ll use the OpenCV face detection code to see if there’s a face in front of it,
[8:20] and finally, only if there’s a face present, we’ll power on the screen.
[8:25] We’ll kick off the face recognition as a background job to see who the person is.
[8:29] We want to have anonymous access to the mirror so we can show it off to any visitors.
[8:33] I’ll use this very simple state machine to control all of this.
[8:36] We’ll start off in idle mode, waiting for the radar detector to fire.
[8:40] We can do this in a really power efficient way using interrupts.
[8:44] When the motion sense of fires, we’ll switch into movement detection mode.
[8:48] This will run the camera along with our simple motion detection algorithm.
[8:52] If there’s movement, then we’ll switch into face acquisition mode.
[8:55] This will run the OpenCV face detection code at full speed.
[8:59] If we detect a face, we’ll switch into active mode.
[9:01] This will power on the screen and try to recognize who the face belongs to.
[9:06] We’ll then just grab a frame every second and use the OpenCV code to make sure there’s
[9:10] still someone looking at the mirror.
[9:12] If we don’t see a face for more than 5 seconds and the motion detector says there’s no motion,
[9:16] we’ll power off the screen and drop back into idle mode.
[9:20] So does it actually work?
[9:21] Well, as with most things, the answer is a qualified yes.
[9:25] I’ve hooked up the camera and I’m running my simple state machine.
[9:29] In idle mode, the screen is off.
[9:31] We walk near the mirror and detect motion with the radar sensor.
[9:35] And then we confirm the movement with the camera and detect the face.
[9:39] The screen turns on and we even manage to recognize the face.
[9:42] That’s pretty amazing.
[9:43] It does work.
[9:45] When we walk away, the screen eventually turns off.
[9:48] So are we actually saving any power?
[9:50] I’ve hooked up my USB power meter and used it to trigger the 12 volts needed by the mirror.
[9:55] We can see that in idle mode, we are just using around 150 mA.
[9:59] When we switch the camera on and start image processing, we go up to around 350 mA.
[10:04] And then, when the screen powers up, we hit almost 700 mA.
[10:09] When we drop back into idle mode, the current consumption goes down to about 400 mA.
[10:14] And then the screen briefly wakes up and then goes into standby mode.
[10:18] Once the screen is in standby mode, we drop back down to around 150 mA.
[10:24] So that’s pretty good.
[10:25] We’ve come from around 600 mA all the time, down to around 150 mA most of the time.
[10:30] So we’re running at about 25% the original power consumption.
[10:34] Not bad at all.
[10:35] But as I said, it’s a qualified yes.
[10:37] There are some issues.
[10:38] The camera is behind the half-silvered mirror, so a lot of the light is being blocked.
[10:42] The image processing all works on a bright day, but it struggles quite a bit at night,
[10:46] even with all the lights turned on.
[10:47] We could install a motion-activated light, or we could use infrared LEDs to eliminate the face
[10:52] and use the camera with no IR filter.
[10:54] But this seems slightly over the top.
[10:55] There’s also an issue with how long the screen takes to turn on.
[10:58] It takes around 1 or 2 seconds for the screen to wake up from standby mode.
[11:02] This is pretty quick, but it is noticeable.
[11:05] So at the moment, my feeling is the full system is not quite ready for production.
[11:09] So I’m just going to use the motion sensor while I work on improving it.
[11:13] Well in all though, a pretty interesting project.
[11:15] Let me know what you think in the comments.
[11:17] And since you’re here, why not check out the original Magic Mirror video.
[11:20] It’s pretty interesting.