-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_recognize_live_vid.py
106 lines (88 loc) · 3.21 KB
/
face_recognize_live_vid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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* ")