Professional Documents
Culture Documents
Build Your Own AI Personal Assistant and Smart Security Camera in Python
by abbasmj
In this instructable, I will show you how you can create two easy and practical projects using Face Recognition in Python.
The rst project will be Using Face Recognition as a Security Camera
The second project will be A personal Assistant using Face Recognition
This tutorial assumes you have some experience with python and installing python modules using PIP
Supplies:
Hardware Requirements:
A laptop/desktop with a webcam
Andriod Phone to receive a push noti cation (only for Step 2)
Software Requirements:
Python 3
External Python Libraries
face_recongition
google tts
playsound
opencv
pushbullet (only for Step 2)
todoist-api (only for Step 3)
Build Your Own AI Personal Assistant and Smart Security Camera in Python: Page 1
https://youtu.be/StoU0O3o7vY
In the rst step of the project, we will write the code to teach a face to the program and then see if it can correctly
identify/recognize the face that we taught it. To do this we will use the face_recogniztion library/module in python
Install Python Library
Build Your Own AI Personal Assistant and Smart Security Camera in Python: Page 2
pip3 install face_recognition
pip3 install opencv-python
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
rgb_frame = frame[:, :, ::-1]
# Find all the faces and face enqcodings in the frame of video
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
name = "Unknown"
Build Your Own AI Personal Assistant and Smart Security Camera in Python: Page 3
Now when you run your program your a window will pop up and you should see your face is detected and also correctly
identi ed. You can also exit the program by pressing 'q' on your keyboard
Now that we are able to recognize a face using python let's use this to create a face-recognizing security camera that can
alert you when an unknown person is detected. We will reuse the code from step 1 for the face recognition part and for
the alarm/announcement, we will use Google Text To Speech (gTTs) and playsound library. We will also send an alert
noti cation on our smartphone using the Push Bullet API (only on Android)
Let's start by installing the necessary libraries using pip
pip3 install gTTs
pip3 install pushbullet.py
pip3 install playsound
pip3 install PyObjc
Adding an announcement
We will add this code in an else clause after the compare operation. Basically what we do is "if a match is not found then
we create a custom speech mp3 using google text to speech and play it on our computer's speaker using the playsound
library. The message will be played every 2s.
Build Your Own AI Personal Assistant and Smart Security Camera in Python: Page 4
from gtts import gTTs
import datetime
import playsound
if True in matches:
first_match_index = matches.index(True)
name = known_face_names[first_match_index]
else:
current_time = datetime.datetime.now()
if current_time <= last_time + datetime.timedelta(seconds=2):
tts = gTTS('Intruder Alert ! Intruder Alert.', lang='en')
tts.save('intruder.mp3')
playsound.playsound('intruder.mp3')
last_time = current_time
Now run the script and when an "Unknown" person is detected an alarm should be played on the your computer and
you should receive a push noti cation on your smartphone.
https://youtu.be/un39MrKpopg
Build Your Own AI Personal Assistant and Smart Security Camera in Python: Page 5
Download
https://www.instructables.com/ORIG/F7M/9O3X/KX0ERUW5/F7M9O3XKX0ERUW5.py
Another use of Face Recognition could be to use it as a personal assistant. The inspiration for this use case came from my
laziness in checking my to-do list when I came to my home o ce desk and as a reason missing important deadlines.
Furthermore, I also wanted it to only tell me the list and not when someone else ( e.g wife or kids) walked in, and
therefore face recognition was a perfect t
The features of this assistant are the following:
1. Greets you
Build Your Own AI Personal Assistant and Smart Security Camera in Python: Page 6
2. Tells you the latest weather
3. Reads you your incomplete task from your Todoist Todo list
weather_api_key = "YOUR_API_KEY"
base_url = "http://api.openweathermap.org/data/2.5/weather?"
city_name = "YOUR_CITY_NAME"
def get_latest_weather():
raw_response = requests.get(base_url + "appid=" + weather_api_key + "&q=" + city_name)
data = raw_response.json()
current_temp = round(int(data["main"]["temp"]) - 273.15)
current_hum = round(int(data["main"]["humidity"]))
current_cond = data["weather"][0]["description"]
tts = gTTS(f'The current condition is {current_cond} with the temperature of {current_temp} degree celcius and current humidity at {current_hum} percent')
tts.save("current_weather.mp3")
playsound.playsound("current_weather.mp3")
Don't forget to call the "get_latest_weather" function from the "True in matches" section in the while loop
We will also need to get the "id" of the new project that we created. We can do this by running these commands from the
python repl
Build Your Own AI Personal Assistant and Smart Security Camera in Python: Page 7
from todoist.api import TodoistAPI
api = TodoistAPI("YOUR_API_KEY")
api.sync()
print(api.state['projects'])
When you run this you might have a big list. Look for the 'name' of your project and once you have found it copy the "id"
to your python program (see image). Once you have all this you can query and get all the uncompleted tasks running
this code:
api_key = "YOUR API_KEY"
api = TodoistAPI(api_key)
def get_latest_todo_list():
api.sync()
all_data = api.projects.get_data(YOUR_PROJECT_ID)
todo_list = [items["content"] for items in all_data["items"]]
audio = f'''
You have {len(todo_list)} items in your to do list
'''
for item in todo_list:
audio = audio + item + ', '
tts = gTTS(audio)
tts.save("todo_list.mp3")
playsound.playsound("todo_list.mp3")
Don't forget to call the "get_latest_todo_list" function from the "True in matches" section in the while loop
https://youtu.be/_wdRAWZBHC8
Build Your Own AI Personal Assistant and Smart Security Camera in Python: Page 8
Build Your Own AI Personal Assistant and Smart Security Camera in Python: Page 9
Download
https://www.instructables.com/ORIG/F0E/2CN7/KX1TX7A1/F0E2CN7KX1TX7A1.py
Build Your Own AI Personal Assistant and Smart Security Camera in Python: Page 10