-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.py
263 lines (206 loc) · 7.92 KB
/
ui.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import cv2
import time
import requests
import numpy as np
import math
from screenresolution import screen_resolution
from puttext import put_text
from arguments import parsed_args
from imutils.video import VideoStream
#Load the face recognition model
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
global_time = None
def sw(message):
global global_time
if global_time is None:
global_time = time.time()
print(message)
return
print(message + " " + str(time.time() - global_time))
global_time = time.time()
def main():
args = parsed_args()
# initialize the video stream and allow the cammera sensor to warmup
try:
vs = VideoStream(usePiCamera=True,resolution=args.cameraresolution).start()
time.sleep(2.0)
except:
vs = VideoStream(usePiCamera=False,resolution=args.cameraresolution).start()
time.sleep(2.0)
#Set up a full sized window
cv2.namedWindow("Deep Grave", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("Deep Grave", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
screen_width, screen_height = screen_resolution()
display_height,display_width = rotate_size((screen_height,screen_width),opposite_angle(args.displayorientation))
# capture frames from the camera
while True:
#UI for capturing image
img = get_image(vs,args,display_width,display_height)
if img is None:
return
#Turn it into a zombie picture
zombie = deep_grave_me(img, args.webtimeout)
if zombie is None:
continue
#Show it
show_zombie(zombie,args,display_width,display_height)
def opposite_angle(angle):
return (360 - angle)%360
def show_zombie(zombie,args,display_width,display_height):
zombie = resize_without_data_loss(zombie,display_width,display_height)
zombie = rotate_img(zombie,opposite_angle(args.displayorientation))
cv2.imshow("Deep Grave",zombie)
zombie_start_time = time.time()
while time.time() - zombie_start_time < args.zombietime:
key = cv2.waitKey(1)
if key == ord('q'):
break
def get_image(vs,args,display_width,display_height):
text_queue = []
queue_length = 10
wait_start = None
to_return = None
# capture frames from the camera
while True:
img = vs.read()
if img is None:
print("Sleeping")
time.sleep(.1)
continue
#Correct for mirroring and camera orientation
if not args.mirrored:
img = cv2.flip(img,1)
img = rotate_img(img,args.cameraorientation)
img = clip_to_display_aspect_ratio(img,display_width,display_height)
#Get the biggest face to display
max_face = get_max_face(img)
if max_face is None:
display_text = "Look Into The Mirror"
else:
(x,y,w,h) = max_face
if w < args.minfacesize or h < args.minfacesize:
display_text = "Come closer"
else:
display_text = "Good"
to_return = img
text_queue.append(display_text)
if len(text_queue) > queue_length:
text_queue.pop(0)
smoothed_text = most_frequent(text_queue)
if smoothed_text == "Good":
if wait_start == None:
wait_start = time.time()
if time.time() - wait_start > 3:
return to_return
else:
wait_start = None
#Create the display image
display_image = cv2.resize(img,(display_width,display_height))
display_image = put_text(display_image,smoothed_text,args.fontsize)
#Rotate
display_image = rotate_img(display_image,opposite_angle(args.displayorientation))
#Display display image
cv2.imshow("Deep Grave", display_image)
key = cv2.waitKey(1)
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
return None
def rotate_size(size,rotation):
assert(rotation in [0,90,180,270])
if rotation in [0,180]:
return size
else:
return (size[1],size[0])
def rotate_img(img,rotation):
assert(rotation in [0,90,180,270])
if rotation == 0:
return img
else:
return np.rot90(img,rotation//90)
def most_frequent(List):
counter = 0
num = List[0]
for i in List:
curr_frequency = List.count(i)
if(curr_frequency> counter):
counter = curr_frequency
num = i
return num
def get_max_face(img):
to_return = None
width = img.shape[1]
height = img.shape[0]
n_pixels = 129600 #target number of pixels
scale = math.sqrt(width*height/n_pixels)
if scale > 1:
small_size = (round(width/scale),round(height/scale))
small_img = cv2.resize(img,small_size)
else:
scale = 1
small_img = img
gray = cv2.cvtColor(small_img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
if to_return is None or w * h > to_return[3]*to_return[2]:
to_return = (x,y,w,h)
# To draw a rectangle in a face
#cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,0),2)
if to_return is None:
return None
x,y,w,h = to_return
x = max(min(round(x * scale),width-1),0)
y = max(min(round(y * scale),height-1),0)
w = min(round(w * scale),width - x)
h = min(round(h * scale),height - y)
return (x,y,w,h)
def put_text_cv2(img,text):
font = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 1
fontColor = (0,255,0)
lineType = 2
textsize = cv2.getTextSize(text,font,1,2)[0]
textX = (img.shape[1] - textsize[0])//2
textY = (img.shape[0] + textsize[1])//2
bottomLeftCornerOfText = (textX,textY)
cv2.putText(img,text,
bottomLeftCornerOfText,
font,
fontScale,
fontColor,
lineType)
def deep_grave_me(img, webtimeout):
cv2.imwrite("currentPerson.png",img)
with open('currentPerson.png', 'rb') as f:
try:
r = requests.post('https://deepgrave.me/api/upload?dobinary=true', files={'currentPerson.png': f}, verify=False, timeout=webtimeout)
except:
return None
with open('currentZombie.png', 'wb') as zombie:
zombie.write(r.content)
return cv2.imread("currentZombie.png")
def resize_without_data_loss(img,new_width,new_height):
#Figure out the new size we'll resize to
old_height,old_width,_ = img.shape
scale = min(new_height/old_width,new_width/old_width)
new_height_no_border,new_width_no_border = (round(old_height * scale),round(old_width * scale))
new_size_without_border = (new_width_no_border,new_height_no_border)
#Resize it
to_return_without_border = cv2.resize(img,new_size_without_border)
top = (new_height - new_height_no_border)//2
bottom = (new_height - new_height_no_border)//2
left = (new_width - new_width_no_border)//2
right = (new_width - new_width_no_border)//2
return cv2.copyMakeBorder(to_return_without_border,top,bottom,left,right,cv2.BORDER_CONSTANT,value=[0,0,0])
def clip_to_display_aspect_ratio(img, new_width, new_height):
#Figure out the new size we'll resize to
old_height,old_width,_ = img.shape
scale = max(new_height/old_height,new_width/old_width)
original_roi_height = min(round(new_height/scale),old_height)
original_roi_width = min(round(new_width/scale),old_width)
top = max(0,old_height//2 - original_roi_height//2)
bottom = min(old_height,old_height//2 + original_roi_height//2)
left = max(0,old_width//2 - original_roi_width//2)
right = min(old_width,old_width//2 + original_roi_width//2)
return img[top:bottom,left:right,:]
main()