/
homeautomation_fast.py
74 lines (68 loc) · 2.5 KB
/
homeautomation_fast.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
import numpy as np
import face_recognition as fr
import cv2
import logging
from multiprocessing import Process
from time import sleep
from mailsender import MailSender
from gpiozero import LED, MotionSensor
video_capture = cv2.VideoCapture(0)
image = fr.load_image_file("person.jpg")
face_encoding = fr.face_encodings(image)[0]
known_face_encondings = [face_encoding]
known_face_names = ["Harish"]
led = LED(14)
led.on()
sleep(1)
led.off()
pir = MotionSensor(4)
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',level=logging.INFO)
logger = logging.getLogger(__name__)
username = 'piharishkumar@gmail.com'
password = 'Sivahari9801@rit'
sender = username
mail_sender = MailSender(username, password)
state = 0
process_this_frame = True
while True:
ret, frame = video_capture.read()
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_small_frame = small_frame[:, :, ::-1]
if process_this_frame:
face_locations = fr.face_locations(rgb_small_frame)
face_encodings = fr.face_encodings(rgb_small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
matches = fr.compare_faces(known_face_encondings, face_encoding)
name = "Unknown"
face_distances = fr.face_distance(known_face_encondings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
face_names.append(name)
process_this_frame = not process_this_frame
if face_names == []:
if not state == 1:
state =1
logger.info('No face found')
led.off()
elif name == "Unknown":
if not state == 2:
state = 2
logger.info('Unknown face found')
led.off()
elif not state == 3:
state = 3
logger.info('Matching face found')
for face_name in face_names:
logger.info('Welcome %s',face_name)
led.on()
cv2.imwrite('image.jpg', frame)
if pir.motion_detected:
try:
images = [{'id': 'Person', 'path': 'image.jpg'}]
logger.info('Sending photo via email')
mail_sender.send(sender, ['piharishkumar@gmail.com'], 'Person alert!', images=images)
logger.info('Photo send to piharishkumar@gmail.com')
except:
logger.error('Photo send via email failed')