-
Notifications
You must be signed in to change notification settings - Fork 0
/
rec.py
262 lines (240 loc) · 11.8 KB
/
rec.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
import cv2
import numpy as np
import csv
import pandas as pd
import dlib
import sys
import imutils
from screeninfo import get_monitors
from math import ceil
def resized(frame):
screen = get_monitors()[0]
screen_width = screen.width
screen_height = screen.height
frame_width = frame.shape[1]
frame_height = frame.shape[0]
scale_width = 9999999
scale_height = 9999999
scale = 100
if frame_width > screen_width:
scale_width = 100 / (frame_width/screen_width)
if frame_height > screen_height:
scale_height = 100 / (frame_height/screen_height)
if scale_width < scale_height:
scale = scale_width
elif scale_height < scale_width:
scale = scale_height
scale_percent = scale # percent of original size
width = int(frame.shape[1] * scale_percent / 100)
height = int(frame.shape[0] * scale_percent / 100)
dim = (width, height)
return cv2.resize(frame, dim, interpolation=cv2.INTER_AREA)
def process_frame(frame, frame_num):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detector(gray)
face_detected = False
for face in faces:
#region the normal stuff
face_detected = True
x1 = face.left()
y1 = face.top()
x2 = face.right()
y2 = face.bottom()
# cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), 1)
#Just making the box bigger so it fit better
x1 -= int(x1/25)
y1 -= int(y1/25)
x2 += int(x2/25)
y2 += int(y2/25)
cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
my_data['face_left'] = x1
my_data['face_top'] = y1
my_data['face_right'] = x2
my_data['face_bottom'] = y2
landmarks = predictor(gray, face)
#My 68 landmarks XD
for i in range(0, 68):
x = landmarks.part(i).x
y = landmarks.part(i).y
#my_data[f'my_landmarks_{i}_x'] = x
#my_data[f'my_landmarks_{i}_y'] = y
cv2.circle(frame, (x, y), 2, color, -1)
"""
Jawline 0:16
Right eyebrow 17:21
Left eyebrow 22:26
Nose line 27:30
Lower nose line 31:35
Right eye 36:41
Left eye 42:47
Ouuter lips 48:59
Inner lips 60:67
"""
#Jawline
start_x = landmarks.part(0).x
start_y = landmarks.part(0).y
my_data['jawline_0_x'] = start_x
my_data['jawline_0_y'] = start_y
for point in range(1, 17):
end_x = landmarks.part(point).x
end_y = landmarks.part(point).y
my_data[f'jawline_{point}_x'] = start_x
my_data[f'jawline_{point}_y'] = start_y
cv2.line(frame, (start_x, start_y), (end_x, end_y), color, 2)
start_x = end_x
start_y = end_y
#Right eyebrow
start_x = landmarks.part(17).x
start_y = landmarks.part(17).y
my_data['right_eyebrow_17_x'] = start_x
my_data['right_eyebrow_17_y'] = start_y
for point in range(18, 22):
end_x = landmarks.part(point).x
end_y = landmarks.part(point).y
my_data[f'right_eyebrow_{point}_x'] = start_x
my_data[f'right_eyebrow_{point}_y'] = start_y
cv2.line(frame, (start_x, start_y), (end_x, end_y), color, 2)
start_x = end_x
start_y = end_y
#Left eyebrow
start_x = landmarks.part(22).x
start_y = landmarks.part(22).y
my_data['left_eyebrow_17_x'] = start_x
my_data['left_eyebrow_17_y'] = start_y
for point in range(23, 27):
end_x = landmarks.part(point).x
end_y = landmarks.part(point).y
my_data[f'left_eyebrow_{point}_x'] = start_x
my_data[f'left_eyebrow_{point}_y'] = start_y
cv2.line(frame, (start_x, start_y), (end_x, end_y), color, 2)
start_x = end_x
start_y = end_y
#Nose line
start_x = landmarks.part(27).x
start_y = landmarks.part(27).y
my_data['nose_line_27_x'] = start_x
my_data['nose_line_27_y'] = start_y
for point in range(28, 31):
end_x = landmarks.part(point).x
end_y = landmarks.part(point).y
my_data[f'nose_line_{point}_x'] = start_x
my_data[f'nose_line_{point}_y'] = start_y
cv2.line(frame, (start_x, start_y), (end_x, end_y), color, 2)
start_x = end_x
start_y = end_y
#Lower nose line
start_x = landmarks.part(31).x
start_y = landmarks.part(31).y
my_data['lower_nose_line_31_x'] = start_x
my_data['lower_nose_line_31_y'] = start_y
for point in range(32, 36):
end_x = landmarks.part(point).x
end_y = landmarks.part(point).y
my_data[f'lower_nose_line_{point}_x'] = start_x
my_data[f'lower_nose_line_{point}_y'] = start_y
cv2.line(frame, (start_x, start_y), (end_x, end_y), color, 2)
start_x = end_x
start_y = end_y
#Right eye
start_x = landmarks.part(36).x
start_y = landmarks.part(36).y
my_data['right_eye_36_x'] = start_x
my_data['right_eye_36_y'] = start_y
for point in range(37, 42):
end_x = landmarks.part(point).x
end_y = landmarks.part(point).y
my_data[f'right_eye_{point}_x'] = start_x
my_data[f'right_eye_{point}_y'] = start_y
cv2.line(frame, (start_x, start_y), (end_x, end_y), color, 2)
start_x = end_x
start_y = end_y
cv2.line(frame, (start_x, start_y), (landmarks.part(36).x, landmarks.part(36).y), color, 2)
#Left eye
start_x = landmarks.part(42).x
start_y = landmarks.part(42).y
my_data['left_eye_42_x'] = start_x
my_data['left_eye_42_y'] = start_y
for point in range(43, 48):
end_x = landmarks.part(point).x
end_y = landmarks.part(point).y
my_data[f'left_eye_{point}_x'] = start_x
my_data[f'left_eye_{point}_y'] = start_y
cv2.line(frame, (start_x, start_y), (end_x, end_y), color, 2)
start_x = end_x
start_y = end_y
cv2.line(frame, (start_x, start_y), (landmarks.part(42).x, landmarks.part(42).y), color, 2)
#Outter lips
start_x = landmarks.part(48).x
start_y = landmarks.part(48).y
my_data['outter_lips_48_x'] = start_x
my_data['outter_lips_48_y'] = start_y
for point in range(49, 60):
end_x = landmarks.part(point).x
end_y = landmarks.part(point).y
my_data[f'outter_lips_{point}_x'] = start_x
my_data[f'outter_lips_{point}_y'] = start_y
cv2.line(frame, (start_x, start_y), (end_x, end_y), color, 2)
start_x = end_x
start_y = end_y
cv2.line(frame, (start_x, start_y), (landmarks.part(48).x, landmarks.part(48).y), color, 2)
#Inner lips
start_x = landmarks.part(60).x
start_y = landmarks.part(60).y
my_data['inner_lips_60_x'] = start_x
my_data['inner_lips_60_y'] = start_y
for point in range(61, 68):
end_x = landmarks .part(point).x
end_y = landmarks.part(point).y
my_data[f'inner_lips_{point}_x'] = start_x
my_data[f'inner_lips_{point}_y'] = start_y
cv2.line(frame, (start_x, start_y), (end_x, end_y), color, 2)
start_x = end_x
start_y = end_y
cv2.line(frame, (start_x, start_y), (landmarks.part(60).x, landmarks.part(60).y), color, 2)
#Connecting the jaw line with the eyebrows
jawline_startpoints = (landmarks.part(0).x, landmarks.part(0).y)
jawline_endpoints = (landmarks.part(16).x, landmarks.part(16).y)
left_eyebrow_startpoints = (landmarks.part(17).x, landmarks.part(17).y)
cv2.line(frame, jawline_startpoints, left_eyebrow_startpoints, color, 2)
right_eyebrow_startpoints = (landmarks.part(26).x, landmarks.part(26).y)
cv2.line(frame, jawline_endpoints, right_eyebrow_startpoints, color, 2)
#endregion
time = f"{int((frame_num/fps)//60)}:{int(ceil(frame_num/fps)%60)}"
if face_detected == False:
for key in keys:
my_data[key] = 0
log_file = open("log_video.txt", 'a', newline='')
log_file.write(f"ERROR: Did't find a face in {sys.argv[1]} with {sys.argv[2]} at {time}\n")
if int(sys.argv[3]) == 0:
with open(csv_file, 'a', newline='') as output_file:
thewriter = csv.DictWriter(output_file, fieldnames=keys, quoting=csv.QUOTE_NONE, escapechar=' ')
thewriter.writerow(my_data)
else:
with open(csv_file, 'a', newline='') as output_file:
thewriter = csv.DictWriter(output_file, fieldnames=keys, quoting=csv.QUOTE_NONE, escapechar=' ')
thewriter.writerow(my_data)
return frame
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("trained_models/shape_predictor_68_face_landmarks.dat")
keys = list("face_left,face_top,face_right,face_bottom,jawline_0_x,jawline_0_y,jawline_1_x,jawline_1_y,jawline_2_x,jawline_2_y,jawline_3_x,jawline_3_y,jawline_4_x,jawline_4_y,jawline_5_x,jawline_5_y,jawline_6_x,jawline_6_y,jawline_7_x,jawline_7_y,jawline_8_x,jawline_8_y,jawline_9_x,jawline_9_y,jawline_10_x,jawline_10_y,jawline_11_x,jawline_11_y,jawline_12_x,jawline_12_y,jawline_13_x,jawline_13_y,jawline_14_x,jawline_14_y,jawline_15_x,jawline_15_y,jawline_16_x,jawline_16_y,right_eyebrow_17_x,right_eyebrow_17_y,right_eyebrow_18_x,right_eyebrow_18_y,right_eyebrow_19_x,right_eyebrow_19_y,right_eyebrow_20_x,right_eyebrow_20_y,right_eyebrow_21_x,right_eyebrow_21_y,left_eyebrow_17_x,left_eyebrow_17_y,left_eyebrow_23_x,left_eyebrow_23_y,left_eyebrow_24_x,left_eyebrow_24_y,left_eyebrow_25_x,left_eyebrow_25_y,left_eyebrow_26_x,left_eyebrow_26_y,nose_line_27_x,nose_line_27_y,nose_line_28_x,nose_line_28_y,nose_line_29_x,nose_line_29_y,nose_line_30_x,nose_line_30_y,lower_nose_line_31_x,lower_nose_line_31_y,lower_nose_line_32_x,lower_nose_line_32_y,lower_nose_line_33_x,lower_nose_line_33_y,lower_nose_line_34_x,lower_nose_line_34_y,lower_nose_line_35_x,lower_nose_line_35_y,right_eye_36_x,right_eye_36_y,right_eye_37_x,right_eye_37_y,right_eye_38_x,right_eye_38_y,right_eye_39_x,right_eye_39_y,right_eye_40_x,right_eye_40_y,right_eye_41_x,right_eye_41_y,left_eye_42_x,left_eye_42_y,left_eye_43_x,left_eye_43_y,left_eye_44_x,left_eye_44_y,left_eye_45_x,left_eye_45_y,left_eye_46_x,left_eye_46_y,left_eye_47_x,left_eye_47_y,outter_lips_48_x,outter_lips_48_y,outter_lips_49_x,outter_lips_49_y,outter_lips_50_x,outter_lips_50_y,outter_lips_51_x,outter_lips_51_y,outter_lips_52_x,outter_lips_52_y,outter_lips_53_x,outter_lips_53_y,outter_lips_54_x,outter_lips_54_y,outter_lips_55_x,outter_lips_55_y,outter_lips_56_x,outter_lips_56_y,outter_lips_57_x,outter_lips_57_y,outter_lips_58_x,outter_lips_58_y,outter_lips_59_x,outter_lips_59_y,inner_lips_60_x,inner_lips_60_y,inner_lips_61_x,inner_lips_61_y,inner_lips_62_x,inner_lips_62_y,inner_lips_63_x,inner_lips_63_y,inner_lips_64_x,inner_lips_64_y,inner_lips_65_x,inner_lips_65_y,inner_lips_66_x,inner_lips_66_y,inner_lips_67_x,inner_lips_67_y".split(","))
csv_file = "data/testing.csv"
my_data = {}
color = (0, 255, 0)
v = sys.argv[1]
if v == "0" or v == "1":
v = int(v)
cap = cv2.VideoCapture(v)
fps = cap.get(cv2.CAP_PROP_FPS)
frame_num = 0
while True:
res, frame = cap.read()
if res == False:
break
frame = resized(frame)
frame = imutils.rotate_bound(frame, int(sys.argv[2]))
frame = process_frame(frame, frame_num)
cv2.imshow("frames", frame)
key = cv2.waitKey(1)
if key == 27:
break
frame_num += 1