See what I have to deal with? Red Shirt Jeff did that yesterday. He snuck in my office,
grabbed the Pi 4 I use for testing, and cut the thing in half! He said he was angry
there was no Pi 4 model A, and he thinks it'll still work but that's just nonsense.
So I decided revenge was the best course of action.
I've neglected my office defenses far too long,
so I thought the best thing to do would be to add some sort of intruder detection system.
And I've always seen people building machine vision cameras with Raspberry Pis, but I'd never
done it myself. I have a spare High Quality camera I use for testing, and look at this!
This little box came in the mail, and it has one of the world's
smallest Raspberry Pi Compute Module 4 boards. It's the CM4Ext Nano from Harlab.
I've built cameras using a Pi Zero or model A before, but they were always a
little underpowered. Using the CM4 with this little board means I get a small footprint,
but I also get all the power of the fastest Raspberry Pi model, with up to 8 gigs of RAM!
I should mention this video is sponsored by Arm as part of its virtual conference, Arm DevSummit,
coming up on October 19th to the 21st. I'll talk more about it towards the end of the video,
so if you want to find out how you can learn about
even more amazing projects built with Arm devices, stay tuned!
So here's the plan... Red Shirt Jeff comes sneaking in here FAR
too often. I need to discourage that. Next time he tries,
I'll have the Pi set up to blast him with an air cannon—a.k.a. my leaf blower.
And if I have a camera attached, I can also have it record and broadcast the moment for
real-time revenge. But first, let's take a quick look at the board itself.
It's got connectors for the Compute Module 4 on the bottom, and a whole lot of IO on top.
It has USB 2, USB-C power input, two HDMI ports, an audio output jack, a 5V power supply connector,
a Stemma QT or Qwiic connector, microSD for Lite Compute Modules, and a reset button.
And if that's not enough, it even has more in the middle!
There's a NeoPixel you can control in software, mode switches,
a DSI and CSI display and camera connector, and even a full 40 pin GPIO header!
That's a lot to cram in such a small board!
I should note that the 40-pin header comes with an included spacer,
and if you want to use it you need to solder it in manually. That's fine with me, though, because
if you don't need the GPIO, it's safer to not have the pins sticking out on such a tiny board.
Assembling everything together is easy enough. First, you mount up the camera to the adapter
plate that comes with the Ext Nano. You basically use standoffs to separate the
camera from the adapter plate, and another set to separate the adapter plate from the Ext Nano.
Then you connect the camera ribbon cable to the CSI port on the board,
screw the Ext Nano into the standoffs, and pop a Compute Module on it.
And bingo, now I have the Pi and camera ready to go.
Next is the blower. Luckily, since the HQ camera has a built-in tripod screw hole,
it's easy to mount. In this case, I'll just tape a little tripod on the blower handle.
There. A little gaff tape fixes anything!
Next, I need to wire up one GPIO pin, 5 volts, and ground, to a servo.
I have this trusty little servo here, and I'll
strap it to my blower so it can trigger the switch.
I plugged it into the right GPIO pins, and now it's time for software.
The first thing I had to do was actually get the Pi camera to work! With the Compute Module 4,
things aren't quite as plug-and-play as the other Pis you might be used to.
First I wanted to use my USB keyboard and mouse. But the USB ports on the CM4 aren't even enabled
by default! After a small addition to the boot config, I was up and running.
For the camera to work, I had to download a special 'device tree' file,
and the instructions for that are on the Compute Module documentation site.
With all that out of the way,
I tested the camera was working with raspistill, which is built into Pi OS.
Now that the hardware and software all seem to be working,
it's time to 'train' the Pi how to identify Red Shirt Jeff. I needed to find a way for the Pi
camera to monitor the doorway and detect if someone was present. But that's not enough;
I also wanted to make sure it would only do the air blast if Red Shirt Jeff tried coming in.
So I found a guide by Caroline Dunn on Tom's Hardware. She actually has
her own YouTube channel, so go check it out!
I used Caroline's code as a starting point, and set up the Pi for real-time
facial recognition using machine learning.
First, I set up the software I'd need: OpenCV, face-recognition, and IMutils.
These three libraries make it so I can build a 'model' of someone's face, then use that model
to detect that face in any still image or video. As with every project I do, all the code is free
and open source over on my GitHub, and there's a link to it below.
Once you have the software, the first step is training a model.
You have to 'teach' the computer how to identify someone's face.
And luckily, I have a lot of footage of Red Shirt Jeff.
So I grabbed a bunch of still frames and put them in a folder called 'RedShirtJeff'.
I ran this script to build the computer model, which is stored in a 'pickle' file.
Wait, a pickle file? Is that some weird Rick and Morty reference? No, it's just a special
file that stores Python data structures, like our computed facial recognition data.
The next step is to use that pickle file for live facial recognition.
I won't go too deep here, but this script basically tells the Pi camera to start
capturing video. It grabs a frame, looks for any facial matches, reacts to those matches,
then moves on to another frame.
If that sounds like it might be a lot of real-time processing for a tiny little Pi, well, it is!
And that's why I've been trying to get a Coral TPU working with the Pi
over PCI Express, because it could speed up things like face detection dramatically.
The Arm Cortex A72 on the Pi is a great little processor but alas,
it's not built to be a specialized machine vision processor! So I'm stuck at a little over two
frames per second. You can see what that looks like in this little test video I did of myself.
It's good enough, though, because Red Shirt Jeff can't move fast enough to
not be detected at that frame rate! At least I hope. And I also set up
the script to record everything to a video file so I'd get Red Shirt Jeff's reaction.
So the software side is done.
The next step is to trigger the blower when the Pi sees Red Shirt.
Lucky for me, I have some experience building a really simple circuit for this,
since I built my Pi Notification Bell last year, triggered by a servo.
In this brief hacker montage, I slammed out the code to control the servo and
make it turn on the blower for 3 seconds, and tested it by training the script to blow me.
That's not...
Nevermind. Anyways, it looks like it's working, so it's time to set it up for Red Shirt Jeff now!
The last thing I wanted to do before letting it loose is make sure I can watch the camera
feed remotely. The quickest way to do that was to set up VNC,
so I could just stream the Pi's desktop to my reTerminal.
Wait, what's that? I don't believe I've introduced you to this little guy!
Seeed Studio sent me this thing to test a couple weeks ago. It's the reTerminal,
and it's generally used for building HMI, or Human Machine Interfaces,
like you'd have set up in factories, but it's also a really cool portable tablet-style device—and
it runs on a Pi Compute Module 4, just like the CM4Ext Nano! I'll hopefully
have more time to talk about it in a future video, so make sure you subscribe to see it!
But back to the streaming; I set up VNC on the Ext Nano,
and started monitoring the stream so I could see Red Shirt Jeff's reaction remotely.
Ah, revenge. A dish best served cold. And at high velocity!
Now, when I came back to my office, I uncovered a pretty massive bug.
And luckily I left my camera rolling so I could show you:
I forgot that Red Shirt Jeff looks like me. So much, in fact, that the machine learning
model I trained thinks that he and I are the same person! I should probably also make sure the Pi
identifies the shirt color to make sure it only blasts Red Shirt Jeff, and not also me.
That might add a bit of complexity. But that's why I'm always trying to expand my knowledge,
so I can make these projects better and better. And that brings me to this video's sponsor:
If you want to expand your knowledge on embedded devices
like my Arm-powered air blaster, you need to check out Arm DevSummit! It's free,
and you can learn from tons of industry experts, like Raspberry Pi,
talk about things like embedded computing, edge, and even high-performance clusters!
You can also discuss your interests and network in Arm's official Discord channel—maybe you'll see
me chatting about this session on how WaterScope tests water quality in low income areas with a Pi.
If you're at all interested in computing you should register for Arm DevSummit.
The event is from October 19 to 21; register online using the link in the description.
So yeah, maybe after that I'll have the time and knowledge to make it so I don't
get blasted every time I walk iton my own office. For now, I think I'll have to shut down the blower,
and maybe think of a better way to get back at Red Shirt Jeff.
And he told me again that he thinks he can get his Franken-Pi working, but I had to go back
and buy another one to replace it, because even if he does get it working,
he chopped off all the IO ports, and I need those for my testing!
If you want to build a fun, powerful, and tiny Pi-based camera like I did,
Harlab's CM4 Ext Nano will be $30 and will hopefully be available soon. Like all projects,
it's production timeline has been delayed due to the chip shortage.
But I'll update the link in the description as soon as it's available!
Until next time, I'm Jeff Geerling.