-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.py
145 lines (113 loc) · 4.44 KB
/
main.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# -*- coding:utf-8 -*-
import cv2
import os
import random
import time
import ctypes
from train import Model
from input import resize_with_pad, write_image
from input import IMAGE_SIZE, GRAY_MODE
DEBUG_OUTPUT = True # Output captured images
CropPadding = 10 # Padding when cropping faces from frames
StrictMode = False
MaxPromptDelay = 1000 # in microsecond
MaxFailDelay = 5000 # in microsecond
SampleInterval = 400 # in microsecond
cascade_path = "F:/Software/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml"
def extendFaceRect(rect):
[x, y, w, h] = rect
if y > CropPadding: y = y - CropPadding
else: y = 0
h += 2*CropPadding
if x > CropPadding: x = x - CropPadding
else: x = 0
w += 2*CropPadding
return [x, y, w, h]
def timestamp():
return '[' + time.asctime() + ']'
if __name__ == '__main__':
# Change working directory
os.chdir(os.path.dirname(os.path.realpath(__file__)))
cap = cv2.VideoCapture(0)
model = Model()
model.load()
# Get Cascade Classifier
cascade = cv2.CascadeClassifier(cascade_path)
isme=0
notme=0
nDelay = 0
# Run window in other thread
cv2.startWindowThread()
while True:
_, frame = cap.read()
# To gray image
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Recognize faces
facerect = cascade.detectMultiScale(
frame_gray,
scaleFactor=1.1,
minNeighbors=3,
minSize=(85, 85),
flags=cv2.CASCADE_SCALE_IMAGE
)
recStatus = 0
if len(facerect) > 0:
print(timestamp(), 'Face detected.')
color = (255, 255, 255) # 白
if nDelay >= MaxPromptDelay: # Show the recognize windows
for (x, y, w, h) in facerect:
[x, y, w, h] = extendFaceRect([x, y, w, h])
buffer = frame.copy()
cv2.rectangle(buffer, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(buffer, "Count down: " + str(MaxFailDelay-nDelay) + " ms",
(10, 50), cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 0))
cv2.imshow('Recognizing', buffer)
# cv2.setWindowProperty("Recognizing", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
#cv2.namedWindow('Recognizing', cv2.WINDOW_AUTOSIZE | cv2.WINDOW_KEEPRATIO | cv2.WINDOW_GUI_EXPANDED)
for rect in facerect:
[x, y, width, height] = extendFaceRect(rect)
# Crop the face
if GRAY_MODE == True:
img_predict = frame_gray[y: y + height, x: x + width]
else:
img_predict = frame[y: y + height, x: x + width]
# Predict face
if GRAY_MODE == True:
result = model.predict(img_predict, img_channels=1)
else:
result = model.predict(img_predict)
if DEBUG_OUTPUT == True:
outimg = frame[y: y + height, x: x + width]
if result == 0:
write_image('./output/isme/' + str(random.randint(1,999999)) + '.jpg', outimg)
else:
write_image('./output/notme/' + str(random.randint(1,999999)) + '.jpg', outimg)
if result == 0: # Is me
print(timestamp(), "It's you! Donny!")
isme+=1
recStatus = 1
else:
print(timestamp(), 'Not Donny.')
notme+=1
if recStatus == 0:
recStatus = -1
print(timestamp(), 'isme', isme, 'notme', notme)
# End if Face Detected
if recStatus == -1 or (recStatus == 0 and (StrictMode or nDelay >= MaxPromptDelay)):
nDelay += SampleInterval
print(timestamp(), 'Last notme:', nDelay, 'ago')
elif recStatus == 1:
nDelay = 0
cv2.destroyWindow('Recognizing')
if nDelay >= MaxFailDelay: # Lock Windows
print(timestamp(), "Locking computer.")
ctypes.windll.user32.LockWorkStation()
nDelay = 0
cv2.destroyWindow('Recognizing')
break
cv2.waitKey(1)
time.sleep(SampleInterval/1000)
# End while True
# Stop recognize
cap.release()
cv2.destroyAllWindows()