Spaces:
Runtime error
Runtime error
Update app_utils.py
Browse files- app_utils.py +153 -153
app_utils.py
CHANGED
@@ -88,162 +88,162 @@ def preprocess_video_and_predict(video):
|
|
88 |
|
89 |
#to return scores
|
90 |
def preprocess_video_and_rank(video):
|
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 |
def video_score(video):
|
|
|
88 |
|
89 |
#to return scores
|
90 |
def preprocess_video_and_rank(video):
|
91 |
+
|
92 |
+
cap = cv2.VideoCapture(video)
|
93 |
+
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
94 |
+
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
95 |
+
fps = np.round(cap.get(cv2.CAP_PROP_FPS))
|
96 |
+
|
97 |
+
path_save_video_face = 'result_face.mp4'
|
98 |
+
vid_writer_face = cv2.VideoWriter(path_save_video_face, cv2.VideoWriter_fourcc(*'mp4v'), fps, (224, 224))
|
99 |
+
|
100 |
+
# path_save_video_hm = 'result_hm.mp4'
|
101 |
+
# vid_writer_hm = cv2.VideoWriter(path_save_video_hm, cv2.VideoWriter_fourcc(*'mp4v'), fps, (224, 224))
|
102 |
+
|
103 |
+
lstm_features = []
|
104 |
+
count_frame = 1
|
105 |
+
count_face = 0
|
106 |
+
probs = []
|
107 |
+
frames = []
|
108 |
+
last_output = None
|
109 |
+
last_heatmap = None
|
110 |
+
cur_face = None
|
111 |
+
|
112 |
+
with mp_face_mesh.FaceMesh(
|
113 |
+
max_num_faces=1,
|
114 |
+
refine_landmarks=False,
|
115 |
+
min_detection_confidence=0.5,
|
116 |
+
min_tracking_confidence=0.5) as face_mesh:
|
117 |
+
|
118 |
+
while cap.isOpened():
|
119 |
+
_, frame = cap.read()
|
120 |
+
if frame is None: break
|
121 |
+
|
122 |
+
frame_copy = frame.copy()
|
123 |
+
frame_copy.flags.writeable = False
|
124 |
+
frame_copy = cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
|
125 |
+
results = face_mesh.process(frame_copy)
|
126 |
+
frame_copy.flags.writeable = True
|
127 |
+
|
128 |
+
if results.multi_face_landmarks:
|
129 |
+
for fl in results.multi_face_landmarks:
|
130 |
+
startX, startY, endX, endY = get_box(fl, w, h)
|
131 |
+
cur_face = frame_copy[startY:endY, startX: endX]
|
132 |
+
|
133 |
+
if count_face%config_data.FRAME_DOWNSAMPLING == 0:
|
134 |
+
cur_face_copy = pth_processing(Image.fromarray(cur_face))
|
135 |
+
with torch.no_grad():
|
136 |
+
features = torch.nn.functional.relu(pth_model_static.extract_features(cur_face_copy)).detach().numpy()
|
137 |
+
|
138 |
+
# grayscale_cam = cam(input_tensor=cur_face_copy)
|
139 |
+
# grayscale_cam = grayscale_cam[0, :]
|
140 |
+
# cur_face_hm = cv2.resize(cur_face,(224,224), interpolation = cv2.INTER_AREA)
|
141 |
+
# cur_face_hm = np.float32(cur_face_hm) / 255
|
142 |
+
# heatmap = show_cam_on_image(cur_face_hm, grayscale_cam, use_rgb=False)
|
143 |
+
# last_heatmap = heatmap
|
144 |
|
145 |
+
if len(lstm_features) == 0:
|
146 |
+
lstm_features = [features]*10
|
147 |
+
else:
|
148 |
+
lstm_features = lstm_features[1:] + [features]
|
149 |
+
|
150 |
+
lstm_f = torch.from_numpy(np.vstack(lstm_features))
|
151 |
+
lstm_f = torch.unsqueeze(lstm_f, 0)
|
152 |
+
with torch.no_grad():
|
153 |
+
output = pth_model_dynamic(lstm_f).detach().numpy()
|
154 |
+
last_output = output
|
155 |
+
|
156 |
+
if count_face == 0:
|
157 |
+
count_face += 1
|
158 |
+
|
159 |
+
else:
|
160 |
+
if last_output is not None:
|
161 |
+
output = last_output
|
162 |
+
# heatmap = last_heatmap
|
163 |
+
|
164 |
+
elif last_output is None:
|
165 |
+
output = np.empty((1, 7))
|
166 |
+
output[:] = np.nan
|
167 |
|
168 |
+
probs.append(output[0])
|
169 |
+
frames.append(count_frame)
|
170 |
+
else:
|
171 |
+
if last_output is not None:
|
172 |
+
lstm_features = []
|
173 |
+
empty = np.empty((7))
|
174 |
+
empty[:] = np.nan
|
175 |
+
probs.append(empty)
|
176 |
+
frames.append(count_frame)
|
177 |
+
|
178 |
+
if cur_face is not None:
|
179 |
+
# heatmap_f = display_info(heatmap, 'Frame: {}'.format(count_frame), box_scale=.3)
|
180 |
+
|
181 |
+
cur_face = cv2.cvtColor(cur_face, cv2.COLOR_RGB2BGR)
|
182 |
+
cur_face = cv2.resize(cur_face, (224,224), interpolation = cv2.INTER_AREA)
|
183 |
+
cur_face = display_info(cur_face, 'Frame: {}'.format(count_frame), box_scale=.3)
|
184 |
+
vid_writer_face.write(cur_face)
|
185 |
+
# vid_writer_hm.write(heatmap_f)
|
186 |
+
|
187 |
+
count_frame += 1
|
188 |
+
if count_face != 0:
|
189 |
+
count_face += 1
|
190 |
+
|
191 |
+
vid_writer_face.release()
|
192 |
+
# vid_writer_hm.release()
|
193 |
+
|
194 |
+
stat = statistics_plot(frames, probs)
|
195 |
+
|
196 |
+
if not stat:
|
197 |
+
return None, None
|
198 |
+
|
199 |
+
#for debug
|
200 |
+
print(type(frames))
|
201 |
+
print(frames)
|
202 |
+
print(type(probs))
|
203 |
+
print(probs)
|
204 |
+
# to calculate scores
|
205 |
+
nan=float('nan')
|
206 |
+
s1 = 0
|
207 |
+
s2 = 0
|
208 |
+
s3 = 0
|
209 |
+
s4 = 0
|
210 |
+
s5 = 0
|
211 |
+
s6 = 0
|
212 |
+
s7 = 0
|
213 |
+
frames_len=len(frames)
|
214 |
+
for i in range(frames_len):
|
215 |
+
if np.isnan(probs[i][0]):
|
216 |
+
frames_len=frames_len-1
|
217 |
+
else:
|
218 |
+
s1=s1+probs[i][0]
|
219 |
+
s2=s2+probs[i][1]
|
220 |
+
s3=s3+probs[i][2]
|
221 |
+
s4=s4+probs[i][3]
|
222 |
+
s5=s5+probs[i][4]
|
223 |
+
s6=s6+probs[i][5]
|
224 |
+
s7=s7+probs[i][6]
|
225 |
+
s1=s1/frames_len
|
226 |
+
s2=s2/frames_len
|
227 |
+
s3=s3/frames_len
|
228 |
+
s4=s4/frames_len
|
229 |
+
s5=s5/frames_len
|
230 |
+
s6=s6/frames_len
|
231 |
+
s7=s7/frames_len
|
232 |
+
scores=[s1,s2,s3,s4,s5,s6,s7]
|
233 |
+
scores_str=str(scores)
|
234 |
+
with open("local_data/data.txt",'a', encoding="utf8") as f:
|
235 |
+
f.write(scores_str+'\n')
|
236 |
+
|
237 |
+
with open("local_data/data.txt",'r', encoding="utf8") as f:
|
238 |
+
for i in f:
|
239 |
+
print(i)
|
240 |
+
|
241 |
+
|
242 |
+
#trans the audio file
|
243 |
+
my_audio_clip = AudioFileClip(video)
|
244 |
+
my_audio_clip.write_audiofile("data/audio.wav",ffmpeg_params=["-ac","1"])
|
245 |
|
246 |
+
return stat,scores_str,"data/audio.wav"
|
247 |
|
248 |
###########################################################################################################################
|
249 |
def video_score(video):
|