-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
250 lines (184 loc) · 7.22 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
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
import cv2
import mediapipe as mp
import numpy as np
def lerp(a, b, c):
return int((c * a) + ((1 - c) * b))
def largestBox(boxes):
"""
Given a list of boxes, return the box with the largest width
:param boxes: a list of 4-tuples (x, y, w, h)
:return: The largest box
"""
lrg_width = 0
lrg_box = None
for box in boxes:
if box[2] > lrg_width:
lrg_box = BoundingBox(box[0], box[1], box[2], box[3])
lrg_width = box[2]
if lrg_box is None:
# return original box
lrg_box = BoundingBox(0, 0, 0, 0)
return lrg_box
class BoundingBox:
def __init__(self, x, y, w, h):
self.dim = [x, y, w, h]
def lerpShape(self, newBox):
for i in range(2):
self.dim[i] = lerp(self.dim[i], newBox.dim[i], 0.4)
for i in range(2):
j = i + 2
self.dim[j] = lerp(self.dim[j], newBox.dim[j], 0.7)
class Frame:
boxIsVisible = False
def __init__(self, img, box):
self.zoom = 0.4
self.img = img
self.box = box
x, y, w, h = box.dim
self.postFilterBox = BoundingBox(x, y, w, h)
def setZoom(self, amount):
self.zoom = min(max(amount, 0.01), 0.99)
def filter(self):
# Declare basic variables
screenHeight = self.img.shape[0]
screenWidth = self.img.shape[1]
screenRatio = float(screenWidth) / screenHeight
(boxX, boxY, boxW, boxH) = self.box.dim
distX1 = boxX
# dist refers to the distances in front of and
distY1 = boxY
distX2 = screenWidth - distX1 - boxW # behind the face detection box
# EX: |---distX1----[ :) ]--distX2--|
distY2 = screenHeight - distY1 - boxH
# Equalize x's and y's to shortest length
if distX1 > distX2:
distX1 = distX2
if distY1 > distY2:
distY1 = distY2
distX = distX1 # Set to an equal distance value
distY = distY1
# Trim sides to match original aspect ratio
centerX = distX + (boxW / 2.0)
centerY = distY + (boxH / 2.0)
distsRatio = centerX / centerY
if screenRatio < distsRatio:
offset = centerX - (centerY * screenRatio)
distX -= offset
elif screenRatio > distsRatio:
offset = centerY - (centerX / screenRatio)
distY -= offset
# Make screen to box ratio constant
# (constant can be changed as ZOOM in main.py)
if screenWidth > screenHeight:
distX = min(0.5 * ((boxW / self.zoom) - boxW), distX)
distY = min(
((1.0 / screenRatio) * (distX + (boxW / 2.0))) - (boxH / 2.0), distY)
else:
distY = min(0.5 * ((boxH / self.zoom) - boxH), distY)
distX = min((screenRatio * (distY + (boxH / 2.0))) -
(boxW / 2.0), distX)
# Crop image to match distance values
newX = int(boxX - distX)
# This is a debugging tool that prints the values of the new bounding box.
newY = int(boxY - distY)
newW = int(2 * distX + boxW)
newH = int(2 * distY + boxH)
# print(newX, newY, newW, newH)
# if the new box is out of bounds, don't crop
if not (newX < 0 or newY < 0 or newW > screenWidth or newH > screenHeight):
self.crop([newX, newY, newW, newH])
# Resize image to fit original resolution
resizePercentage = float(screenWidth) / newW
self.img = cv2.resize(self.img, (screenWidth, screenHeight))
for i in range(4):
self.postFilterBox.dim[i] = int(self.postFilterBox.dim[i] * resizePercentage)
# Flip Filtered image on y-axis
self.img = cv2.flip(self.img, 2)
def drawBox(self):
(x, y, w, h) = self.postFilterBox.dim
if x > 0:
cv2.rectangle(self.img, (x, y), (x + w, y + h), (255, 255, 255), 2)
def crop(self, dim):
x, y, w, h = dim
self.img = self.img[y:y + h, x:x + w]
self.postFilterBox.dim[0] -= x
self.postFilterBox.dim[1] -= y
def show(self):
if self.boxIsVisible:
self.drawBox()
# reduce the size of the image by a factor of 2 for upsamling
upres = cv2.resize(self.img, (0, 0), fx=0.3, fy=0.3)
upres = sr.upsample(upres) # upsample image
upres = cv2.resize(upres, (1280, 720))
cv2.imshow("Face-Tracking", upres)
mp_face_detection = mp.solutions.face_detection
mp_drawing = mp.solutions.drawing_utils
ZOOM = 0.22 # Medium = 0.2 to 0.3, Close = 0.35 to 0.5
SHOW_BOX = False # Show face detection box around the largest detected face
SCALE_FACTOR = 1.17 # Medium = 1.2, Close = 1.14
MIN_NEIGHBORS = 8 # 8
MINSIZE = (60, 60) # Medium = (60, 60), Close = (120, 120)
cap = cv2.VideoCapture(0)
# Create global detection box for steady screen transformation
box = BoundingBox(-1, -1, -1, -1)
'''
Experimental Function - Super Resolution using FSRCNN
'''
sr = cv2.dnn_superres.DnnSuperResImpl_create()
path = "./models/FSRCNN_model.pb"
sr.readModel(path)
sr.setModel("fsrcnn", 3)
with mp_face_detection.FaceDetection(model_selection=0, min_detection_confidence=0.66) as face_detection:
prev_results = []
counter = 0
while cap.isOpened():
_, image = cap.read()
image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = face_detection.process(image)
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
FACES = []
if results.detections:
for detection in results.detections:
x1, y1, w, h = detection.location_data.relative_bounding_box.xmin, detection.location_data.relative_bounding_box.ymin, detection.location_data.relative_bounding_box.width, detection.location_data.relative_bounding_box.height
x1 = int(x1 * image.shape[1])
y1 = int(y1 * image.shape[0])
w = int(w * image.shape[1])
h = int(h * image.shape[0])
FACES.append([x1, y1, w, h])
RESULTS = np.array(FACES)
if RESULTS.size > 0:
if counter % 5 == 0:
prev_results.append(RESULTS)
boxLarge = largestBox(RESULTS)
if box.dim[0] == -1:
box = boxLarge
else:
box.lerpShape(boxLarge)
counter += 1
else:
# If no faces are detected, zoom out from prev_result to [-1, -1, -1, -1]
if len(prev_results) > 0:
box.lerpShape(largestBox(prev_results[-1]))
counter += 1
else:
box.dim = [-1, -1, -1, -1]
counter = 0
frame = Frame(image, box)
frame.boxIsVisible = SHOW_BOX
frame.setZoom(ZOOM)
frame.filter()
box = frame.box
frame.show()
# Stop if escape key is pressed
k = cv2.waitKey(30)
if k == 27:
break
if k == 49:
SHOW_BOX = not SHOW_BOX
if k == 50:
ZOOM = max(ZOOM - 0.05, 0.01)
if k == 51:
ZOOM = min(ZOOM + 0.05, 0.99)
cap.release()