Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
aman_a_shastry
Published © GPL3+

Face recognition with Python

An easy way to get into AI with facial recognition and Google Home integration!!

AdvancedFull instructions provided1,190
Face recognition with Python

Things used in this project

Hardware components

Webcam, Logitech® HD Pro
Webcam, Logitech® HD Pro
You can use any webcam for this project
×1
Google Home
×1

Software apps and online services

VS Code
Microsoft VS Code
Python 64 bit
For Windows: https://www.python.org/downloads/windows/ For Mac: https://www.python.org/downloads/mac-osx/ For Linux: https://www.python.org/downloads/source/
Visual Studio 2015
Microsoft Visual Studio 2015
For installing the face recognition library

Story

Read more

Code

Face recognition code

Python
import smtplib
from email.message import EmailMessage
import face_recognition
import cv2
import pickle
import pychromecast
import http.server
import multiprocessing

train_encodings = []
Names = []
scale_factor = 0.25

cast = pychromecast.Chromecast(" *Your Google home's local IP address* ")
cast.wait()
print(cast.name)
mc = cast.media_controller
speak = True
mail = True
nameOld = ''

def caster(url):
    global speak 
    if speak == True: 
        mc.play_media(url, 'audio/mp3')
        mc.block_until_active()
        mc.pause()
        mc.play()
        speak = False

def email_alert(subject, body, to):
    global mail
    if mail == True:
        msg = EmailMessage()
        msg.set_content(body)
        msg['subject'] = subject
        msg['to'] = to
        
        user = " *Your email ID* "
        msg['from'] = user
        password = " *Your Google account's double authentication password* "

        server = smtplib.SMTP('smtp.gmail.com',587)
        server.starttls()
        server.login(user,password)
        with open(r'login.jpg', 'rb') as f:
            image_data = f.read()
            image_name = f.name
            image_type = image_name.split(".")[1]
        msg.add_attachment(image_data, maintype='image', subtype=image_type, filename=image_name)

        server.send_message(msg)
        server.quit()
        mail = False

def web_server():
    httpd = http.server.HTTPServer(server_address=('',8000),RequestHandlerClass=http.server.SimpleHTTPRequestHandler)
    httpd.serve_forever(poll_interval=0.5)

with open ('train.pkl','rb') as f:
    Names = pickle.load(f)
    train_encodings = pickle.load(f)

print(Names)

cam = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX

while __name__ == '__main__':
    p = multiprocessing.Process(target=web_server, args=())
    p.daemon = True
    p.start()

    r,img = cam.read()
    img_small = cv2.resize(img,(0,0),fx = scale_factor,fy = scale_factor)
    img_rgb = cv2.cvtColor(img_small,cv2.COLOR_BGR2RGB)
    face_positions = face_recognition.face_locations(img_rgb,model = 'cnn')

    if not face_positions:
        continue

    all_encodings = face_recognition.face_encodings(img_rgb,face_positions)

    for (top,right,bottom,left),face_encoding in zip(face_positions,all_encodings):
        name = "Unknown person"
        matches = face_recognition.compare_faces(train_encodings,face_encoding)
        if True in matches:
            image_index = matches.index(True)
            name = Names[image_index]
            email = "Log in by: "+name
            url = "http:// *Your computer's local IP address* :8000/"+name+".mp3"
            print(url)
            if name != nameOld:
                speak = True
            nameOld = name
            caster(url)
            top = int(top//scale_factor)
            left = int(left//scale_factor)
            bottom = int(bottom//scale_factor)
            right = int(right//scale_factor)
            cv2.rectangle(img,(top,left),(bottom,right),(0,255,0),2)
            cv2.putText(img,name,(left,top),font,0.75,(0,255,0),thickness=2)
    

    cv2.imwrite("login.jpg",img)
    email_alert("Update",email," *To email address* ")

Save data of faces

Python
import pickle
import face_recognition
import os
import pickle
import gtts

from face_recognition.api import face_locations

training_encodings = []
Names = []
image_dir =  r" *Your directory of known images* "

for root, dirs, files in os.walk(image_dir):
    for file in files:
        path = os.path.join(root,file)
        name = os.path.splitext(file)[0]
        person = face_recognition.load_image_file(path)
        encoding = face_recognition.face_encodings(person)[0]
        training_encodings.append(encoding)
        Names.append(name)
        print(name)
        print(encoding)

for name in Names:
    msg = name+"is at the door"
    tts = gtts.gTTS(msg,lang="en",tld = "co.uk")
    url = name+".mp3"
    tts.save(url)

with open('train.pkl','wb') as f:
    pickle.dump(Names,f)
    pickle.dump(training_encodings,f)

Credits

aman_a_shastry
5 projects • 6 followers
Contact

Comments

Please log in or sign up to comment.