-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunlock_it.wakeup
executable file
·115 lines (102 loc) · 3.06 KB
/
unlock_it.wakeup
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
#!/usr/bin/env python3
"""
This Module loads a trained model, takes a picture
and runs the picture against the trained model to predict
if the picture is of the user
"""
import sys
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
import keyring
from time import sleep
from subprocess import call
subjects = ['', 'User']
face_recognizer = cv2.face.LBPHFaceRecognizer_create()
face_recognizer.read("MacFaceID/support/saved_instance2.xml")
def detect_face(img):
"""
Method detects a face on a given picture.
returns a box around the face if found one
else, returns None
"""
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier('MacFaceID/support/opencv-files/haarcascade_frontalface_alt.xml')
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)
if len(faces) is 0:
return None, None
try:
for (x, y, w, h) in faces:
(x, y, w, h) = faces[0]
except:
return None
return gray[y:y+w, x:x+h], faces[0]
def draw_rectangle(img, rect):
"""
Method draws a rectangle
"""
(x, y, w, h) = rect
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
def draw_text(img, text, x, y):
"""
Method writes text on a picture
"""
cv2.putText(img, text, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 255, 0), 2)
def predict(test_img):
"""
Method predicts if face is of the user with trained model
"""
if test_img is not None:
img = test_img.copy()
else:
return
face, rect = detect_face(img)
try:
label, confidence = face_recognizer.predict(face)
if confidence >= 15.0:
return None
label_text = subjects[label]
draw_rectangle(img, rect)
draw_text(img, label_text, rect[0], rect[1]-5)
except:
return None
return img
def get_image():
"""
Helper method
"""
retval, im = camera.read()
return im
camera_port = 0
ramp_frames = 30
camera = cv2.VideoCapture(camera_port)
def get_image():
retval, im = camera.read()
return im
for i in range(ramp_frames):
temp = get_image()
camera_capture = get_image()
filename = "MacFaceID/support/test-data/test1.jpg"
cv2.imwrite(filename,camera_capture)
test_img1 = cv2.imread("MacFaceID/support/test-data/test1.jpg")
predicted_img1 = predict(test_img1)
if predicted_img1 is None:
password = "NOTAmatch"
cmd = """ osascript -e 'tell application "System Events"
keystroke "%s"
keystroke return
end tell' """ % (password)
os.system(cmd)
else:
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))
ax1.imshow(cv2.cvtColor(predicted_img1, cv2.COLOR_BGR2RGB))
password = keyring.get_password("MacFace", "Account-name")
cmd = """ osascript -e 'tell application "System Events"
keystroke "%s"
keystroke return
end tell' """ % (password)
os.system(cmd)
os.remove(filename)
del(camera)
cv2.destroyAllWindows()