Face recognition with Python
An easy way to get into AI with facial recognition and Google Home integration!!
Components and supplies
1
Google Home
1
Webcam, Logitech® HD Pro
Apps and platforms
1
VS Code
1
Python 64 bit
1
Visual Studio 2015
Project description
Code
Face recognition code
python
1import smtplib 2from email.message import EmailMessage 3import face_recognition 4import cv2 5import pickle 6import pychromecast 7import http.server 8import multiprocessing 9 10train_encodings = [] 11Names = [] 12scale_factor = 0.25 13 14cast = pychromecast.Chromecast(" *Your Google home's local IP address* ") 15cast.wait() 16print(cast.name) 17mc = cast.media_controller 18speak = True 19mail = True 20nameOld = '' 21 22def caster(url): 23 global speak 24 if speak == True: 25 mc.play_media(url, 'audio/mp3') 26 mc.block_until_active() 27 mc.pause() 28 mc.play() 29 speak = False 30 31def email_alert(subject, body, to): 32 global mail 33 if mail == True: 34 msg = EmailMessage() 35 msg.set_content(body) 36 msg['subject'] = subject 37 msg['to'] = to 38 39 user = " *Your email ID* " 40 msg['from'] = user 41 password = " *Your Google account's double authentication password* " 42 43 server = smtplib.SMTP('smtp.gmail.com',587) 44 server.starttls() 45 server.login(user,password) 46 with open(r'login.jpg', 'rb') as f: 47 image_data = f.read() 48 image_name = f.name 49 image_type = image_name.split(".")[1] 50 msg.add_attachment(image_data, maintype='image', subtype=image_type, filename=image_name) 51 52 server.send_message(msg) 53 server.quit() 54 mail = False 55 56def web_server(): 57 httpd = http.server.HTTPServer(server_address=('',8000),RequestHandlerClass=http.server.SimpleHTTPRequestHandler) 58 httpd.serve_forever(poll_interval=0.5) 59 60with open ('train.pkl','rb') as f: 61 Names = pickle.load(f) 62 train_encodings = pickle.load(f) 63 64print(Names) 65 66cam = cv2.VideoCapture(0) 67font = cv2.FONT_HERSHEY_SIMPLEX 68 69while __name__ == '__main__': 70 p = multiprocessing.Process(target=web_server, args=()) 71 p.daemon = True 72 p.start() 73 74 r,img = cam.read() 75 img_small = cv2.resize(img,(0,0),fx = scale_factor,fy = scale_factor) 76 img_rgb = cv2.cvtColor(img_small,cv2.COLOR_BGR2RGB) 77 face_positions = face_recognition.face_locations(img_rgb,model = 'cnn') 78 79 if not face_positions: 80 continue 81 82 all_encodings = face_recognition.face_encodings(img_rgb,face_positions) 83 84 for (top,right,bottom,left),face_encoding in zip(face_positions,all_encodings): 85 name = "Unknown person" 86 matches = face_recognition.compare_faces(train_encodings,face_encoding) 87 if True in matches: 88 image_index = matches.index(True) 89 name = Names[image_index] 90 email = "Log in by: "+name 91 url = "http:// *Your computer's local IP address* :8000/"+name+".mp3" 92 print(url) 93 if name != nameOld: 94 speak = True 95 nameOld = name 96 caster(url) 97 top = int(top//scale_factor) 98 left = int(left//scale_factor) 99 bottom = int(bottom//scale_factor) 100 right = int(right//scale_factor) 101 cv2.rectangle(img,(top,left),(bottom,right),(0,255,0),2) 102 cv2.putText(img,name,(left,top),font,0.75,(0,255,0),thickness=2) 103 104 105 cv2.imwrite("login.jpg",img) 106 email_alert("Update",email," *To email address* ")
Save data of faces
python
1import pickle 2import face_recognition 3import os 4import pickle 5import gtts 6 7from face_recognition.api import face_locations 8 9training_encodings = [] 10Names = [] 11image_dir = r" *Your directory of known images* " 12 13for root, dirs, files in os.walk(image_dir): 14 for file in files: 15 path = os.path.join(root,file) 16 name = os.path.splitext(file)[0] 17 person = face_recognition.load_image_file(path) 18 encoding = face_recognition.face_encodings(person)[0] 19 training_encodings.append(encoding) 20 Names.append(name) 21 print(name) 22 print(encoding) 23 24for name in Names: 25 msg = name+"is at the door" 26 tts = gtts.gTTS(msg,lang="en",tld = "co.uk") 27 url = name+".mp3" 28 tts.save(url) 29 30with open('train.pkl','wb') as f: 31 pickle.dump(Names,f) 32 pickle.dump(training_encodings,f)
Face recognition code
python
1import smtplib 2from email.message import EmailMessage 3import face_recognition 4import cv2 5import pickle 6import pychromecast 7import http.server 8import multiprocessing 9 10train_encodings = [] 11Names = [] 12scale_factor = 0.25 13 14cast = pychromecast.Chromecast(" *Your Google home's local IP address* ") 15cast.wait() 16print(cast.name) 17mc = cast.media_controller 18speak = True 19mail = True 20nameOld = '' 21 22def caster(url): 23 global speak 24 if speak == True: 25 mc.play_media(url, 'audio/mp3') 26 mc.block_until_active() 27 mc.pause() 28 mc.play() 29 speak = False 30 31def email_alert(subject, body, to): 32 global mail 33 if mail == True: 34 msg = EmailMessage() 35 msg.set_content(body) 36 msg['subject'] = subject 37 msg['to'] = to 38 39 user = " *Your email ID* " 40 msg['from'] = user 41 password = " *Your Google account's double authentication password* " 42 43 server = smtplib.SMTP('smtp.gmail.com',587) 44 server.starttls() 45 server.login(user,password) 46 with open(r'login.jpg', 'rb') as f: 47 image_data = f.read() 48 image_name = f.name 49 image_type = image_name.split(".")[1] 50 msg.add_attachment(image_data, maintype='image', subtype=image_type, filename=image_name) 51 52 server.send_message(msg) 53 server.quit() 54 mail = False 55 56def web_server(): 57 httpd = http.server.HTTPServer(server_address=('',8000),RequestHandlerClass=http.server.SimpleHTTPRequestHandler) 58 httpd.serve_forever(poll_interval=0.5) 59 60with open ('train.pkl','rb') as f: 61 Names = pickle.load(f) 62 train_encodings = pickle.load(f) 63 64print(Names) 65 66cam = cv2.VideoCapture(0) 67font = cv2.FONT_HERSHEY_SIMPLEX 68 69while __name__ == '__main__': 70 p = multiprocessing.Process(target=web_server, args=()) 71 p.daemon = True 72 p.start() 73 74 r,img = cam.read() 75 img_small = cv2.resize(img,(0,0),fx = scale_factor,fy = scale_factor) 76 img_rgb = cv2.cvtColor(img_small,cv2.COLOR_BGR2RGB) 77 face_positions = face_recognition.face_locations(img_rgb,model = 'cnn') 78 79 if not face_positions: 80 continue 81 82 all_encodings = face_recognition.face_encodings(img_rgb,face_positions) 83 84 for (top,right,bottom,left),face_encoding in zip(face_positions,all_encodings): 85 name = "Unknown person" 86 matches = face_recognition.compare_faces(train_encodings,face_encoding) 87 if True in matches: 88 image_index = matches.index(True) 89 name = Names[image_index] 90 email = "Log in by: "+name 91 url = "http:// *Your computer's local IP address* :8000/"+name+".mp3" 92 print(url) 93 if name != nameOld: 94 speak = True 95 nameOld = name 96 caster(url) 97 top = int(top//scale_factor) 98 left = int(left//scale_factor) 99 bottom = int(bottom//scale_factor) 100 right = int(right//scale_factor) 101 cv2.rectangle(img,(top,left),(bottom,right),(0,255,0),2) 102 cv2.putText(img,name,(left,top),font,0.75,(0,255,0),thickness=2) 103 104 105 cv2.imwrite("login.jpg",img) 106 email_alert("Update",email," *To email address* ")
Comments
Only logged in users can leave comments