AI Intruder Detection System with HuskyLens
In this project, we use DFRobot's HuskyLens AI camera to detect intruders and report intrusions via text message. This is a simple project that requires very little setup and can be created in under an hour.
HuskyLens is a very capable camera and LCD module with integrated AI capabilities. It can perform facial recognition, object recognition, color identification, and much more completely independently from a host processor. For this system, we'll use the facial recognition feature to train a set of recognized faces that the intrusion detection system will not respond to. When the HuskyLens module sees a face it doesn't recognize, it will trigger a Python script to send a text message to your phone alerting you of a possible intruder.
The only materials you'll need for this project are a HuskyLens module and a computer. You can use a Raspberry Pi, Mac, Windows machine, or any computer you desire.
HuskyLens supports communication with microcontrollers and computers through either I2C or serial. HuskyLens also has an on-board serial to USB converter, so you can talk to it from any device with USB support. In order to get this feature to work, you'll need to install drivers for your system. If you're on macOS or Windows, you can get the drivers from Silicon Labs. If you're running Linux, your HuskyLens should automatically be recognized and does not require drivers.
I had some issues getting the drivers installed with macOS Big Sur. Until SiLabs updates their drivers for the latest version of macOS, you'll probably need to follow this workaround. If you're on macOS or Linux, you can verify that your HuskyLens is recognized successfully by running the command lsusb in a shell prompt before and after plugging in the device. You should see one entry present in the lsusb output not present before the device was plugged in.
My cat doesn't know what to think of HuskyLens.
Connect your HuskyLens to your computer via USB to power it up. You can then follow the instructions in the HuskyLens Wiki to train it on your face. Pay special attention to the section that describes how to capture your face from multiple angles. In my testing, this dramatically improved the performance of the facial detection system.
Also note that you can train your HuskyLens on multiple faces. This allows you to have multiple people for which the intruder detection system will not trigger an alarm.
import time import json from huskylib import HuskyLensLibrary from twilio.rest import Client # Program Parameters text_on = True sms_interval = 10 last_sms_time = 0 # Change for your Serial Port hl = HuskyLensLibrary("SERIAL", "/dev/cu.SLAB_USBtoUART", 3000000) # Twilio setup account_sid = "YOUR SID HERE" auth_token = "YOUR TOKEN HERE" # Examples - paste in your own dest_phone="+1111111111" source_phone="+1111111111" client = Client(account_sid, auth_token) while True: blocks = hl.requestAll() for block in blocks: if block.learned == True: print("Known Face!") else: print("Unknown Face!") if text_on and time.time() - last_sms_time > sms_interval: print("Sending Text Message") message = client.messages.create( to=dest_phone, from_=source_phone, body="Intruder Detected!") last_sms_time = time.time() time.sleep(0.5)
The HuskyLens module is extremely easy to use in Python with the provided HuskyLib python library. Capturing what the on-board AI module detects is as simple as calling one function. To send text messages, I'm using the Twilio SMS API. You'll need to sign up for an account here.
To run the code above, you first need to ensure that you have Python 3 on your system and install the pyserial and pypng packages via pip. You next need to download the HuskyLib python file from the HuskyLens GitHub and place it in the same directory as your code. I've also included this file and the intruder detection software above in the files section.
Next, you need to find the identifier of the serial device on your computer. On macOS, this will likely be something like “/dev/cu.SLAB_USBtoUART”. On Linux, it will probably be something like “/dev/ttyUSB0”. If you run “ls /dev/cu.*” on macOS or “ls /dev/ttyUSB*" on Linux in a shell it will output a list of options you can try. On Windows, it will probably be something like “COM1”. If you're unsure, you can contact DFRobot for support. You enter the device name in the code where it currently says “/dev/cu.SLAB_USBtoUART”.
Lastly, you need to go to your account overview page on Twilio and paste your account SID and auth token into the spots indicated in the Python script. Also paste the phone number it gave you when you created your account and your verified destination phone number into the code. After that, you should be good to go!
Now that your software is all set up, you should be ready to test! Ensure your HuskyLens is in face detection mode and try pointing the camera at an unknown face while the Python script is running. You should soon get a text message from Twilio alerting you that there's an intruder. The software is currently configured to only send text messages at most once every ten seconds. To change this behavior, try modifying the sms_interval variable.
You can permanently set this up wherever you want by mounting the HuskyLens module using the included mounting equipment. The small camera has poor low-light performance, but you can configure it to provide some illumination with on-board LEDs.
It's very easy to modify this code to do whatever else you desire. You could use IFTTT to control smart home devices, a Python package to make your computer say something if it sees an unidentified face, or just connect up a good old beeper to annoy intruders.