-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurity.py
More file actions
117 lines (92 loc) · 3.31 KB
/
security.py
File metadata and controls
117 lines (92 loc) · 3.31 KB
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
107
108
109
110
111
112
113
114
115
116
117
import face_recognition
import cv2
from picamera import PiCamera
from picamera.array import PiRGBArray
import time
import numpy as np
from PIL import Image
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import os.path
email = ''
password = ''
send_to_email = 'asdf@gmail.com'
subject = 'Uh Oh'
message = 'Uh Oh!!!!'
msg = MIMEMultipart()
msg['From'] = email
msg['To'] = send_to_email
msg['Subject'] = subject
msg.attach(MIMEText(message, 'plain'))
cam = PiCamera()
cam.resolution = (640, 480)
cam.framerate = 16
rawCapture = PiRGBArray(cam, size=(640, 480))
time.sleep(1)
face_locations = []
names = []
frame_numbers = [0]
person1 = face_recognition.load_image_file('known/person1.jpg')
person1 = face_recognition.face_encodings(person1)[0]
known_encodings = [person1]
known_names = ['person1']
for frame in cam.capture_continuous(rawCapture, format="bgr", use_video_port=True):
#setup
image = frame.array
s_image = cv2.resize(image, (0, 0), fx=0.25, fy=0.25)
rgb = s_image[:, :, ::-1]
frame_number = 0
i = 0
if i % 3 == 0:
#box setup
face_locations = face_recognition.face_locations(rgb)[0]
face_encodings = face_recognition.face_encodings(rgb, face_locations)[0]
(top, right, bottom, left) = face_locations
picture = image[top:bottom, left:right]
matches = face_recognition.compare_faces(known_encodings, face_encodings)
global name = "unknown"
distances = face_recognition.face_distance(known_encodings, face_encodings)
best_match_index = np.argmin(distances)
if matches[best_match_index]:
name = known_names[best_match_index]
names.append(name)
if "unknown" in names and frame_number - frame_numbers[-1] > 960:
file = Image.fromarray(image)
file.save(f"badguy{frame_number}.jpg")
# Setup the attachment
file_location = f'C:\\Users\\You\\Desktop\\badguy{frame_number}.jpg'
filename = os.path.basename(file_location)
attachment = open(file_location, "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
# Attach the attachment to the MIMEMultipart object
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email, password)
text = msg.as_string()
server.sendmail(email, send_to_email, text)
server.quit()
frame_numbers.append(frame_number)
i += 1
top, right, bottom, left = face_locations
bob = names[0]
top *= 4
right *= 4
bottom *= 4
left *= 4
cv2.rectangle(image, (left, top), (right, bottom), (255, 0, 100), 4)
cv2.rectangle(image, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(image, bob, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 2)
cv2.imshow("Frame", image)
rawCapture.truncate(0)
key = cv2.waitKey(1) & 0xFF
frame_number += 1
if key == ord('q'):
break