Liusuthu commited on
Commit
46390ae
·
verified ·
1 Parent(s): 7892708

Update app_utils.py

Browse files
Files changed (1) hide show
  1. app_utils.py +325 -164
app_utils.py CHANGED
@@ -87,7 +87,7 @@ def preprocess_video_and_predict(video):
87
 
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))
@@ -246,173 +246,334 @@ def preprocess_video_and_rank(video):
246
  return stat,scores_str,"data/audio.wav"
247
 
248
  ###########################################################################################################################
249
- def video_score(video):
250
- cap = cv2.VideoCapture(video)
251
- w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
252
- h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
253
- fps = np.round(cap.get(cv2.CAP_PROP_FPS))
254
-
255
- path_save_video_face = 'result_face.mp4'
256
- vid_writer_face = cv2.VideoWriter(path_save_video_face, cv2.VideoWriter_fourcc(*'mp4v'), fps, (224, 224))
257
-
258
- # path_save_video_hm = 'result_hm.mp4'
259
- # vid_writer_hm = cv2.VideoWriter(path_save_video_hm, cv2.VideoWriter_fourcc(*'mp4v'), fps, (224, 224))
260
-
261
- lstm_features = []
262
- count_frame = 1
263
- count_face = 0
264
- probs = []
265
- frames = []
266
- last_output = None
267
- last_heatmap = None
268
- cur_face = None
269
-
270
- with mp_face_mesh.FaceMesh(
271
- max_num_faces=1,
272
- refine_landmarks=False,
273
- min_detection_confidence=0.5,
274
- min_tracking_confidence=0.5) as face_mesh:
275
-
276
- while cap.isOpened():
277
- _, frame = cap.read()
278
- if frame is None: break
279
-
280
- frame_copy = frame.copy()
281
- frame_copy.flags.writeable = False
282
- frame_copy = cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
283
- results = face_mesh.process(frame_copy)
284
- frame_copy.flags.writeable = True
285
-
286
- if results.multi_face_landmarks:
287
- for fl in results.multi_face_landmarks:
288
- startX, startY, endX, endY = get_box(fl, w, h)
289
- cur_face = frame_copy[startY:endY, startX: endX]
290
-
291
- if count_face%config_data.FRAME_DOWNSAMPLING == 0:
292
- cur_face_copy = pth_processing(Image.fromarray(cur_face))
293
- with torch.no_grad():
294
- features = torch.nn.functional.relu(pth_model_static.extract_features(cur_face_copy)).detach().numpy()
295
-
296
- # grayscale_cam = cam(input_tensor=cur_face_copy)
297
- # grayscale_cam = grayscale_cam[0, :]
298
- # cur_face_hm = cv2.resize(cur_face,(224,224), interpolation = cv2.INTER_AREA)
299
- # cur_face_hm = np.float32(cur_face_hm) / 255
300
- # heatmap = show_cam_on_image(cur_face_hm, grayscale_cam, use_rgb=False)
301
- # last_heatmap = heatmap
302
 
303
- if len(lstm_features) == 0:
304
- lstm_features = [features]*10
305
- else:
306
- lstm_features = lstm_features[1:] + [features]
307
-
308
- lstm_f = torch.from_numpy(np.vstack(lstm_features))
309
- lstm_f = torch.unsqueeze(lstm_f, 0)
310
- with torch.no_grad():
311
- output = pth_model_dynamic(lstm_f).detach().numpy()
312
- last_output = output
313
-
314
- if count_face == 0:
315
- count_face += 1
316
-
317
- else:
318
- if last_output is not None:
319
- output = last_output
320
- # heatmap = last_heatmap
321
-
322
- elif last_output is None:
323
- output = np.empty((1, 7))
324
- output[:] = np.nan
325
 
326
- probs.append(output[0])
327
- frames.append(count_frame)
328
- else:
329
- if last_output is not None:
330
- lstm_features = []
331
- empty = np.empty((7))
332
- empty[:] = np.nan
333
- probs.append(empty)
334
- frames.append(count_frame)
335
-
336
- if cur_face is not None:
337
- # heatmap_f = display_info(heatmap, 'Frame: {}'.format(count_frame), box_scale=.3)
338
-
339
- cur_face = cv2.cvtColor(cur_face, cv2.COLOR_RGB2BGR)
340
- cur_face = cv2.resize(cur_face, (224,224), interpolation = cv2.INTER_AREA)
341
- cur_face = display_info(cur_face, 'Frame: {}'.format(count_frame), box_scale=.3)
342
- vid_writer_face.write(cur_face)
343
- # vid_writer_hm.write(heatmap_f)
344
-
345
- count_frame += 1
346
- if count_face != 0:
347
- count_face += 1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
348
 
349
- vid_writer_face.release()
350
- # vid_writer_hm.release()
351
 
352
- stat = statistics_plot(frames, probs)
353
 
354
- if not stat:
355
- return None, None
356
-
357
- #for debug
358
- print(type(frames))
359
- print(frames)
360
- print(type(probs))
361
- print(probs)
362
- # to calculate scores
363
- nan=float('nan')
364
- s1 = 0
365
- s2 = 0
366
- s3 = 0
367
- s4 = 0
368
- s5 = 0
369
- s6 = 0
370
- s7 = 0
371
- frames_len=len(frames)
372
- for i in range(frames_len):
373
- if np.isnan(probs[i][0]):
374
- frames_len=frames_len-1
375
- else:
376
- s1=s1+probs[i][0]
377
- s2=s2+probs[i][1]
378
- s3=s3+probs[i][2]
379
- s4=s4+probs[i][3]
380
- s5=s5+probs[i][4]
381
- s6=s6+probs[i][5]
382
- s7=s7+probs[i][6]
383
- s1=s1/frames_len
384
- s2=s2/frames_len
385
- s3=s3/frames_len
386
- s4=s4/frames_len
387
- s5=s5/frames_len
388
- s6=s6/frames_len
389
- s7=s7/frames_len
390
- prob=[s1,s2,s3,s4,s5,s6,s7]
391
- prob_str=str(prob)
392
- with open("local_data/data.txt",'a', encoding="utf8") as f:
393
- f.write(prob_str+'\n')
394
-
395
- with open("local_data/data.txt",'r', encoding="utf8") as f:
396
- for i in f:
397
- print(i)
398
- #平衡点值为零,越正越负面
399
- score1=0*prob[0]-8*prob[1]+4*prob[2]+0*prob[3]+2*prob[4]+2*prob[5]+4*prob[6]
400
- print("score1=",score1)
401
 
402
- #trans the audio file
403
- my_audio_clip = AudioFileClip(video)
404
- my_audio_clip.write_audiofile("newaudio.wav",ffmpeg_params=["-ac","1"])
405
-
406
- # Audio="data/audio.wav"
407
- # text,prob2,label,path=classify_continuous(Audio)
408
- #0是抑郁概率,1是非抑郁概率
409
- # score2=10*prob2[0]-10*prob[1]
410
- # print(prob2,label)
411
- # score2=0
412
- # print("score2=",score2)
413
-
414
- # text_result="demo"
415
- # # text_result=text_api(text)
416
- # print(text_result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
417
 
418
- return score1
 
87
 
88
 
89
  #to return scores
90
+ def video_score(video):
91
 
92
  cap = cv2.VideoCapture(video)
93
  w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
 
246
  return stat,scores_str,"data/audio.wav"
247
 
248
  ###########################################################################################################################
249
+ # def video_score(video):
250
+ # cap = cv2.VideoCapture(video)
251
+ # w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
252
+ # h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
253
+ # fps = np.round(cap.get(cv2.CAP_PROP_FPS))
254
+
255
+ # path_save_video_face = 'result_face.mp4'
256
+ # vid_writer_face = cv2.VideoWriter(path_save_video_face, cv2.VideoWriter_fourcc(*'mp4v'), fps, (224, 224))
257
+
258
+ # # path_save_video_hm = 'result_hm.mp4'
259
+ # # vid_writer_hm = cv2.VideoWriter(path_save_video_hm, cv2.VideoWriter_fourcc(*'mp4v'), fps, (224, 224))
260
+
261
+ # lstm_features = []
262
+ # count_frame = 1
263
+ # count_face = 0
264
+ # probs = []
265
+ # frames = []
266
+ # last_output = None
267
+ # last_heatmap = None
268
+ # cur_face = None
269
+
270
+ # with mp_face_mesh.FaceMesh(
271
+ # max_num_faces=1,
272
+ # refine_landmarks=False,
273
+ # min_detection_confidence=0.5,
274
+ # min_tracking_confidence=0.5) as face_mesh:
275
+
276
+ # while cap.isOpened():
277
+ # _, frame = cap.read()
278
+ # if frame is None: break
279
+
280
+ # frame_copy = frame.copy()
281
+ # frame_copy.flags.writeable = False
282
+ # frame_copy = cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
283
+ # results = face_mesh.process(frame_copy)
284
+ # frame_copy.flags.writeable = True
285
+
286
+ # if results.multi_face_landmarks:
287
+ # for fl in results.multi_face_landmarks:
288
+ # startX, startY, endX, endY = get_box(fl, w, h)
289
+ # cur_face = frame_copy[startY:endY, startX: endX]
290
+
291
+ # if count_face%config_data.FRAME_DOWNSAMPLING == 0:
292
+ # cur_face_copy = pth_processing(Image.fromarray(cur_face))
293
+ # with torch.no_grad():
294
+ # features = torch.nn.functional.relu(pth_model_static.extract_features(cur_face_copy)).detach().numpy()
295
+
296
+ # # grayscale_cam = cam(input_tensor=cur_face_copy)
297
+ # # grayscale_cam = grayscale_cam[0, :]
298
+ # # cur_face_hm = cv2.resize(cur_face,(224,224), interpolation = cv2.INTER_AREA)
299
+ # # cur_face_hm = np.float32(cur_face_hm) / 255
300
+ # # heatmap = show_cam_on_image(cur_face_hm, grayscale_cam, use_rgb=False)
301
+ # # last_heatmap = heatmap
302
 
303
+ # if len(lstm_features) == 0:
304
+ # lstm_features = [features]*10
305
+ # else:
306
+ # lstm_features = lstm_features[1:] + [features]
307
+
308
+ # lstm_f = torch.from_numpy(np.vstack(lstm_features))
309
+ # lstm_f = torch.unsqueeze(lstm_f, 0)
310
+ # with torch.no_grad():
311
+ # output = pth_model_dynamic(lstm_f).detach().numpy()
312
+ # last_output = output
313
+
314
+ # if count_face == 0:
315
+ # count_face += 1
316
+
317
+ # else:
318
+ # if last_output is not None:
319
+ # output = last_output
320
+ # # heatmap = last_heatmap
321
+
322
+ # elif last_output is None:
323
+ # output = np.empty((1, 7))
324
+ # output[:] = np.nan
325
 
326
+ # probs.append(output[0])
327
+ # frames.append(count_frame)
328
+ # else:
329
+ # if last_output is not None:
330
+ # lstm_features = []
331
+ # empty = np.empty((7))
332
+ # empty[:] = np.nan
333
+ # probs.append(empty)
334
+ # frames.append(count_frame)
335
+
336
+ # if cur_face is not None:
337
+ # # heatmap_f = display_info(heatmap, 'Frame: {}'.format(count_frame), box_scale=.3)
338
+
339
+ # cur_face = cv2.cvtColor(cur_face, cv2.COLOR_RGB2BGR)
340
+ # cur_face = cv2.resize(cur_face, (224,224), interpolation = cv2.INTER_AREA)
341
+ # cur_face = display_info(cur_face, 'Frame: {}'.format(count_frame), box_scale=.3)
342
+ # vid_writer_face.write(cur_face)
343
+ # # vid_writer_hm.write(heatmap_f)
344
+
345
+ # count_frame += 1
346
+ # if count_face != 0:
347
+ # count_face += 1
348
+
349
+ # vid_writer_face.release()
350
+ # # vid_writer_hm.release()
351
+
352
+ # stat = statistics_plot(frames, probs)
353
+
354
+ # if not stat:
355
+ # return None, None
356
+
357
+ # #for debug
358
+ # print(type(frames))
359
+ # print(frames)
360
+ # print(type(probs))
361
+ # print(probs)
362
+ # # to calculate scores
363
+ # nan=float('nan')
364
+ # s1 = 0
365
+ # s2 = 0
366
+ # s3 = 0
367
+ # s4 = 0
368
+ # s5 = 0
369
+ # s6 = 0
370
+ # s7 = 0
371
+ # frames_len=len(frames)
372
+ # for i in range(frames_len):
373
+ # if np.isnan(probs[i][0]):
374
+ # frames_len=frames_len-1
375
+ # else:
376
+ # s1=s1+probs[i][0]
377
+ # s2=s2+probs[i][1]
378
+ # s3=s3+probs[i][2]
379
+ # s4=s4+probs[i][3]
380
+ # s5=s5+probs[i][4]
381
+ # s6=s6+probs[i][5]
382
+ # s7=s7+probs[i][6]
383
+ # s1=s1/frames_len
384
+ # s2=s2/frames_len
385
+ # s3=s3/frames_len
386
+ # s4=s4/frames_len
387
+ # s5=s5/frames_len
388
+ # s6=s6/frames_len
389
+ # s7=s7/frames_len
390
+ # prob=[s1,s2,s3,s4,s5,s6,s7]
391
+ # prob_str=str(prob)
392
+ # with open("local_data/data.txt",'a', encoding="utf8") as f:
393
+ # f.write(prob_str+'\n')
394
+
395
+ # with open("local_data/data.txt",'r', encoding="utf8") as f:
396
+ # for i in f:
397
+ # print(i)
398
+ # #平衡点值为零,越正越负面
399
+ # score1=0*prob[0]-8*prob[1]+4*prob[2]+0*prob[3]+2*prob[4]+2*prob[5]+4*prob[6]
400
+ # print("score1=",score1)
401
+
402
+ # #trans the audio file
403
+ # my_audio_clip = AudioFileClip(video)
404
+ # my_audio_clip.write_audiofile("newaudio.wav",ffmpeg_params=["-ac","1"])
405
+
406
+ # # Audio="data/audio.wav"
407
+ # # text,prob2,label,path=classify_continuous(Audio)
408
+ # #0是抑郁概率,1是非抑郁概率
409
+ # # score2=10*prob2[0]-10*prob[1]
410
+ # # print(prob2,label)
411
+ # # score2=0
412
+ # # print("score2=",score2)
413
+
414
+ # # text_result="demo"
415
+ # # # text_result=text_api(text)
416
+ # # print(text_result)
417
+
418
+ # return score1
419
 
 
 
420
 
 
421
 
422
+ #可以运行的代码,誊抄用
423
+ # def preprocess_video_and_rank(video):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
424
 
425
+ # cap = cv2.VideoCapture(video)
426
+ # w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
427
+ # h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
428
+ # fps = np.round(cap.get(cv2.CAP_PROP_FPS))
429
+
430
+ # path_save_video_face = 'result_face.mp4'
431
+ # vid_writer_face = cv2.VideoWriter(path_save_video_face, cv2.VideoWriter_fourcc(*'mp4v'), fps, (224, 224))
432
+
433
+ # # path_save_video_hm = 'result_hm.mp4'
434
+ # # vid_writer_hm = cv2.VideoWriter(path_save_video_hm, cv2.VideoWriter_fourcc(*'mp4v'), fps, (224, 224))
435
+
436
+ # lstm_features = []
437
+ # count_frame = 1
438
+ # count_face = 0
439
+ # probs = []
440
+ # frames = []
441
+ # last_output = None
442
+ # last_heatmap = None
443
+ # cur_face = None
444
+
445
+ # with mp_face_mesh.FaceMesh(
446
+ # max_num_faces=1,
447
+ # refine_landmarks=False,
448
+ # min_detection_confidence=0.5,
449
+ # min_tracking_confidence=0.5) as face_mesh:
450
+
451
+ # while cap.isOpened():
452
+ # _, frame = cap.read()
453
+ # if frame is None: break
454
+
455
+ # frame_copy = frame.copy()
456
+ # frame_copy.flags.writeable = False
457
+ # frame_copy = cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
458
+ # results = face_mesh.process(frame_copy)
459
+ # frame_copy.flags.writeable = True
460
+
461
+ # if results.multi_face_landmarks:
462
+ # for fl in results.multi_face_landmarks:
463
+ # startX, startY, endX, endY = get_box(fl, w, h)
464
+ # cur_face = frame_copy[startY:endY, startX: endX]
465
+
466
+ # if count_face%config_data.FRAME_DOWNSAMPLING == 0:
467
+ # cur_face_copy = pth_processing(Image.fromarray(cur_face))
468
+ # with torch.no_grad():
469
+ # features = torch.nn.functional.relu(pth_model_static.extract_features(cur_face_copy)).detach().numpy()
470
+
471
+ # # grayscale_cam = cam(input_tensor=cur_face_copy)
472
+ # # grayscale_cam = grayscale_cam[0, :]
473
+ # # cur_face_hm = cv2.resize(cur_face,(224,224), interpolation = cv2.INTER_AREA)
474
+ # # cur_face_hm = np.float32(cur_face_hm) / 255
475
+ # # heatmap = show_cam_on_image(cur_face_hm, grayscale_cam, use_rgb=False)
476
+ # # last_heatmap = heatmap
477
+
478
+ # if len(lstm_features) == 0:
479
+ # lstm_features = [features]*10
480
+ # else:
481
+ # lstm_features = lstm_features[1:] + [features]
482
+
483
+ # lstm_f = torch.from_numpy(np.vstack(lstm_features))
484
+ # lstm_f = torch.unsqueeze(lstm_f, 0)
485
+ # with torch.no_grad():
486
+ # output = pth_model_dynamic(lstm_f).detach().numpy()
487
+ # last_output = output
488
+
489
+ # if count_face == 0:
490
+ # count_face += 1
491
+
492
+ # else:
493
+ # if last_output is not None:
494
+ # output = last_output
495
+ # # heatmap = last_heatmap
496
+
497
+ # elif last_output is None:
498
+ # output = np.empty((1, 7))
499
+ # output[:] = np.nan
500
+
501
+ # probs.append(output[0])
502
+ # frames.append(count_frame)
503
+ # else:
504
+ # if last_output is not None:
505
+ # lstm_features = []
506
+ # empty = np.empty((7))
507
+ # empty[:] = np.nan
508
+ # probs.append(empty)
509
+ # frames.append(count_frame)
510
+
511
+ # if cur_face is not None:
512
+ # # heatmap_f = display_info(heatmap, 'Frame: {}'.format(count_frame), box_scale=.3)
513
+
514
+ # cur_face = cv2.cvtColor(cur_face, cv2.COLOR_RGB2BGR)
515
+ # cur_face = cv2.resize(cur_face, (224,224), interpolation = cv2.INTER_AREA)
516
+ # cur_face = display_info(cur_face, 'Frame: {}'.format(count_frame), box_scale=.3)
517
+ # vid_writer_face.write(cur_face)
518
+ # # vid_writer_hm.write(heatmap_f)
519
+
520
+ # count_frame += 1
521
+ # if count_face != 0:
522
+ # count_face += 1
523
+
524
+ # vid_writer_face.release()
525
+ # # vid_writer_hm.release()
526
+
527
+ # stat = statistics_plot(frames, probs)
528
+
529
+ # if not stat:
530
+ # return None, None
531
+
532
+ # #for debug
533
+ # print(type(frames))
534
+ # print(frames)
535
+ # print(type(probs))
536
+ # print(probs)
537
+ # # to calculate scores
538
+ # nan=float('nan')
539
+ # s1 = 0
540
+ # s2 = 0
541
+ # s3 = 0
542
+ # s4 = 0
543
+ # s5 = 0
544
+ # s6 = 0
545
+ # s7 = 0
546
+ # frames_len=len(frames)
547
+ # for i in range(frames_len):
548
+ # if np.isnan(probs[i][0]):
549
+ # frames_len=frames_len-1
550
+ # else:
551
+ # s1=s1+probs[i][0]
552
+ # s2=s2+probs[i][1]
553
+ # s3=s3+probs[i][2]
554
+ # s4=s4+probs[i][3]
555
+ # s5=s5+probs[i][4]
556
+ # s6=s6+probs[i][5]
557
+ # s7=s7+probs[i][6]
558
+ # s1=s1/frames_len
559
+ # s2=s2/frames_len
560
+ # s3=s3/frames_len
561
+ # s4=s4/frames_len
562
+ # s5=s5/frames_len
563
+ # s6=s6/frames_len
564
+ # s7=s7/frames_len
565
+ # scores=[s1,s2,s3,s4,s5,s6,s7]
566
+ # scores_str=str(scores)
567
+ # with open("local_data/data.txt",'a', encoding="utf8") as f:
568
+ # f.write(scores_str+'\n')
569
+
570
+ # with open("local_data/data.txt",'r', encoding="utf8") as f:
571
+ # for i in f:
572
+ # print(i)
573
+
574
+
575
+ # #trans the audio file
576
+ # my_audio_clip = AudioFileClip(video)
577
+ # my_audio_clip.write_audiofile("data/audio.wav",ffmpeg_params=["-ac","1"])
578
 
579
+ # return stat,scores_str,"data/audio.wav"