We are not done geeking out on the Raspberry Pi. One of the  cool things you can do with it is to setup an automated monitoring system to detect when something or someone is at the door. Caroline Dunn shows how its done in this awesome tutorial. One thing we did not quite love was Part 3 of the tutorial on email notifications. Mailgun did not quite do it for us, as we wanted something more immediate than an email. After researching our options, we fell in love with Twilio, a platform to automate communications such as SMS or phone calls. Since we are feeling mischievous, we are going to customize the alarms to play a few practical jokes on our friends.

OK so let us show you how to set up Caroline’s face recognition system with an automated call & SMS on your Raspberry Pi! We pick up at Caroline’s Step 3.

Create a Twilio Account

To begin, navigate to the Twilio website and sign up for an account. Creating a Twilio account grants you access to their communication APIs and allows you to manage your communication services efficiently.

Obtain a Twilio Number

Once you have a Twilio account, you need to acquire a Twilio phone number. This number will serve as the sender when initiating facial recognition requests via phone. Make sure to note this number down as we will use it later to populate the TWILIO_PHONE_SENDER variable in our Python script.

Verify Caller IDs

To ensure security and prevent misuse, Twilio requires you to verify the recipient phone number that will receive the facial recognition requests. Add the recipient’s phone number to the “Verified Caller IDs” section in your Twilio account settings. This step ensures that the recipient can receive and respond to the facial recognition requests.

Configure Voice Geographic Permissions

If you plan to send facial recognition requests to recipients located in specific countries, it’s necessary to configure the Voice Geographic Permissions in your Twilio account. In this case, we want to allow “Spain” for the phone-based facial recognition requests.

Update “facial_req_phone.py” Script

Now it’s time to update the “facial_req_phone.py” script to include your Twilio account details. The “facial_req_phone.py” script is adapted from the “facial_req_email.py” script, but instead of sending requests via email, it utilizes Twilio for phone-based communication. The first block remains as is:

# import the necessary packages
from imutils.video import VideoStream
from imutils.video import FPS
import face_recognition
import imutils
import pickle
import time
import cv2
import requests# Initialize ‘currentname’ to trigger only when a new person is identified.
currentname = “unknown”
# Determine faces from encodings.pickle file model created from train_model.py encodingsP = “encodings.pickle”
# use this xml file
cascade = “haarcascade_frontalface_default.xml”


Then you need to update the following variables with your Twilio account information:

#function for setting up SMS
from twilio.rest import ClientTWILIO_ACCOUNT_SID = ‘XXXXXXXXXXXXXX’ # replace with Account SID
TWILIO_AUTH_TOKEN = ‘XXXXXXXXXXXXXX’ # replace with Auth Token
TWILIO_PHONE_SENDER = ‘+1XXXXXXXXXX’ # replace with Twilio phone number (US number provided by Twilio)
TWILIO_PHONE_RECIPIENT = ‘+34XXXXXXXXX’ # replace with number your phone number
TWIML = “https://handler.twilio.com/twiml/XXXXXXXXXXXXXXXXXXXXXX”


The next block defines a couple functions the send SMS and an automated phone call. The send_text_alert function is a simple SMS with the message that the recognized person “is here!”.

def send_text_alert(name):
message = client.messages.create(
body = name + ” is here!”)


We are going to have a bit more fun with the call function and we are going to use it to play practical jokes on friends, by customizing the twiml_message based on the face recognized by the model.

def call(twiml_message):
call = client.calls.create(


The next block runs the standard face recognition loop from Caroline’s original post:

# initialize the video stream and allow the camera sensor to warm up print(“[INFO] starting video stream…”) vs = VideoStream(src=0).start() # vs = VideoStream(usePiCamera=True).start()
time.sleep(2.0)# start the FPS counter
fps = FPS().start()# loop over frames from the video file stream while True:
# grab the frame from the threaded video stream and resize it
# to 500px (to speedup processing)
frame = vs.read()
frame = imutils.resize(frame, width=500)# convert the input frame from (1) BGR to grayscale (for face
# detection) and (2) from BGR to RGB (for face recognition)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)# detect faces in the grayscale frame
rects = detector.detectMultiScale(gray, scaleFactor=1.1,
minNeighbors=5, minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE)# OpenCV returns bounding box coordinates in (x, y, w, h) order
# but we need them in (top, right, bottom, left) order, so we
# need to do a bit of reordering
boxes = [(y, x + w, y + h, x) for (x, y, w, h) in rects]# compute the facial embeddings for each face bounding box
encodings = face_recognition.face_encodings(rgb, boxes)
names = []# loop over the facial embeddings
for encoding in encodings:
# attempt to match each face in the input image to our known
# encodings
matches = face_recognition.compare_faces(data[“encodings”],
name = “Unknown”# check to see if we have found a match
if True in matches:
# find the indexes of all matched faces then initialize a
# dictionary to count the total number of times each face
# was matched
matchedIdxs = [i for (i, b) in enumerate(matches) if b]
counts = {}

# loop over the matched indexes and maintain a count for
# each recognized face face
for i in matchedIdxs:
name = data[“names”][i]
counts[name] = counts.get(name, 0) + 1

# determine the recognized face with the largest number
# of votes (note: in the event of an unlikely tie Python
# will select first entry in the dictionary)
name = max(counts, key=counts.get)


Next we are going to fork the twiml message to produce a standard polite message for one of the faces using a nice and gentle femenine voice:

#If someone in your dataset is identified, print their name on the screen
if currentname != name and name == “Friend_1″:
currentname = name
twiml_message = f”
Hello {name}! How are you doing? Have a great day!


For the other friend we are going to use a stern male voice alerting the user that they are in close proximity to a dangerous criminal with an arrest warrant from Interpol.

elif currentname != name and name != “Friend_2″:
currentname = name
twiml_message = f”
You are in close proximity to {name}, a dangerous criminal wanted by Interpol. Call 911 immediately and run for your life. Run!call(twiml_message)
# update the list of names


The rest goes back to the standard code:

# loop over the recognized faces
for ((top, right, bottom, left), name) in zip(boxes, names):
# draw the predicted face name on the image – color is in BGR
cv2.rectangle(frame, (left, top), (right, bottom),
(0, 255, 225), 2)
y = top – 15 if top – 15 > 15 else top + 15
cv2.putText(frame, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX,
.8, (0, 255, 255), 2)# display the image to our screen
cv2.imshow(“Facial Recognition is Running”, frame)
key = cv2.waitKey(1) & 0xFF# if the `q` key was pressed, break from the loop
if key == ord(“q”):
break# update the FPS counter
fps.update()# stop the timer and display FPS information
print(“[INFO] elasped time: {:.2f}”.format(fps.elapsed())) print(“[INFO] approx. FPS: {:.2f}”.format(fps.fps()))# do a bit of cleanup


Execute the Script

With the “facial_req_phone.py” script ready, open your preferred Python development environment, such as Thonny. Locate the script and double-click on it to open it in the editor. Once open, simply hit the “Run” button to execute the script. This will initiate the phone-based facial recognition request using Twilio’s communication capabilities.


By integrating Twilio into your facial recognition application, you can extend its functionality by enabling phone-based requests. Twilio’s cloud communications platform simplifies the process of sending and receiving phone calls, allowing you to focus on what really matters: hijinks and hilarity.

About Rosan International

ROSAN is a technology company specialized in the development of Data Science and Artificial Intelligence solutions with the aim of solving the most challenging global projects. Contact us to discover how we can help you gain valuable insights from your data and optimize your processes.