Build a Person Detection Security Robot That Sends You a Photo of the Person Stealing Your Chocolates
Caution
There are breaking changes in the Vision Service. This tutorial has not yet been updated.
Note
This tutorial is part of our built-in webcam projects series. For a similar project that integrates a Kasa smart plug, see Use Object Detection to Turn Your Lights On.
In this tutorial you will create a desk security system with no hardware other than your laptop and built-in webcam.
Maybe you keep a box of chocolates on your desk to snack on when you are hungry. Maybe someone is eating your chocolates when you are away. You’re not sure who, but you suspect Steve. This robot will help you catch the culprit.
When someone comes to your desk, the robot will use the Vision Service and an ML model to detect a person, take their photo, and text you an alert with a photo of the person.
Hardware requirements
You need the following hardware for this tutorial:
- Computer with a webcam
- This tutorial uses a MacBook Pro but any computer running macOS or 64-bit Linux will work
- Mobile phone (to receive the text messages)
Software requirements
You will use the following software in this tutorial:
- Python 3.8 or newer
viam-server
- Viam Python SDK
- The Viam Python SDK (software development kit) lets you control your Viam-powered robot by writing custom scripts in the Python programming language. Install the Viam Python SDK by following these instructions.
- yagmail
- A gmail account to send emails. You can use an existing account, or create a new one.
Configure your robot on the Viam app
If you followed the Use Object Detection to Turn Your Lights On tutorial, you already have a robot set up on the Viam app, connected and live, with a webcam configured.
How to use yagmail
Install yagmail (Yet Another Gmail/SMTP client) by running the following command in a terminal on your computer:
pip3 install yagmail
Tip
As you are programming the yagmail section of this project, you will be prompted to use your Gmail username and password within the code. If you use 2-step verification for your email, some apps or devices may be blocked from accessing your Google account. You can get an “Application-Specific Password” following this guide.
App Passwords are 16-digit passcodes that allow the app or device access to your Google account. This step is optional.
Then we have to indicate whom to send a message to, the subject, and the contents of the text message (which can be a string, image or audio). Example code below (though you don’t have to use it yet, this will get used in the next section):
yag.send('phone_number@gatewayaddress.com', 'subject', contents)
You will need to access to your phone number through your carrier.
For this tutorial, you are going to send the text to yourself.
You will replace to@someone.com
with your phone number and SMS gateway address.
You can find yours here: Gateway Addresses for Mobile Phone Carrier Text Message.
Some common ones:
- AT&T:
txt.att.net
- T-Mobile:
tmomail.net
- Verizon Wireless:
vtext.com
As an example, if you have T-Mobile your code will look like this:
yag.send('xxxxxxxxxx@tmomail.net', 'subject', contents)
This allows us to route the email to our phone as a text message.
Use the Viam Python SDK to control your security robot
If you followed the Use Object Detection to Turn Your Lights On tutorial, you already set up a folder with some Python code that connects to your robot and implements the Vision Service.
If you are starting with this tutorial, follow the steps here to create the main script file, connect the code to the robot, and select the model and label path. Ignore the step about the Kasa smart plug host address.
Instead of using this person detector to activate a smart plug, you will send yourself a text message.
Make a copy of the
Delete the from kasa import Discover, SmartPlug
line and replace it with the following to import the Yagmail Python library:
import yagmail
Now you need to rewrite the if/else function.
If a person is detected, your robot will print sending a message
, take a photo, and save it to your computer as
Then you will create a yagmail.SMTP
instance to initialize the server connection.
Refer to the code below and the yagmail instructions to edit your
Save your code file.
Run the code
You are ready to test your robot!
From a command line on your computer, navigate to the project directory and run the code with this command:
python3 chocolate_security.py
If you are in front of your computer’s webcam, you should get a text!
Your terminal should look like this as your project runs if you are in front of the camera for a bit, and then move away from the screen:
python3 chocolate_security.py
This is a person!
sending message
x_min: 7
y_min: 0
x_max: 543
y_max: 480
confidence: 0.94140625
class_name: "Person"
This is a person!
sending message
x_min: 51
y_min: 0
x_max: 588
y_max: 480
confidence: 0.9375
class_name: "Person"
This is a person!
sending message
There's nobody here, don't send message
There's nobody here, don't send message
Summary and next steps
In this tutorial, you learned how to build a security robot using the Vision Service, your computer, and your mobile phone, and we all learned not to trust Steve.
Have you heard about the chocolate box thief? He’s always got a few Twix up his Steve.
For more robotics projects, check out our other tutorials.
You can also ask questions in the Community Discord and we will be happy to help.
Have questions, or want to meet other people working on robots? Join our Community Discord.
Was this page helpful?
Glad to hear it! If there is anything we could be doing better, please create an issue.
We're sorry about that. If you'd like to talk to us for help, please join the Community Discord. To ensure we know what's wrong with this page, you can also open an issue.