forked from open-mmlab/mmpose
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathface_video_demo.py
140 lines (109 loc) · 4.03 KB
/
face_video_demo.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
import os
from argparse import ArgumentParser
import cv2
from mmpose.apis import (inference_top_down_pose_model, init_pose_model,
vis_pose_result)
try:
import face_recognition
has_face_det = True
except (ImportError, ModuleNotFoundError):
has_face_det = False
def process_face_det_results(face_det_results):
"""Process det results, and return a list of bboxes.
:param face_det_results: (top, right, bottom and left)
:return: a list of detected bounding boxes (x,y,x,y)-format
"""
person_results = []
for bbox in face_det_results:
person = {}
# left, top, right, bottom
person['bbox'] = [bbox[3], bbox[0], bbox[1], bbox[2]]
person_results.append(person)
return person_results
def main():
"""Visualize the demo images.
Using mmdet to detect the human.
"""
parser = ArgumentParser()
parser.add_argument('pose_config', help='Config file for pose')
parser.add_argument('pose_checkpoint', help='Checkpoint file for pose')
parser.add_argument('--video-path', type=str, help='Video path')
parser.add_argument(
'--show',
action='store_true',
default=False,
help='whether to show visualizations.')
parser.add_argument(
'--out-video-root',
default='',
help='Root of the output video file. '
'Default not saving the visualization video.')
parser.add_argument(
'--device', default='cuda:0', help='Device used for inference')
parser.add_argument(
'--kpt-thr', type=float, default=0.3, help='Keypoint score threshold')
assert has_face_det, 'Please install face_recognition to run the demo. '\
'"pip install face_recognition", For more details, '\
'see https://github.com/ageitgey/face_recognition'
args = parser.parse_args()
assert args.show or (args.out_video_root != '')
# build the pose model from a config file and a checkpoint file
pose_model = init_pose_model(
args.pose_config, args.pose_checkpoint, device=args.device.lower())
dataset = pose_model.cfg.data['test']['type']
cap = cv2.VideoCapture(args.video_path)
if args.out_video_root == '':
save_out_video = False
else:
os.makedirs(args.out_video_root, exist_ok=True)
save_out_video = True
if save_out_video:
fps = cap.get(cv2.CAP_PROP_FPS)
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
videoWriter = cv2.VideoWriter(
os.path.join(args.out_video_root,
f'vis_{os.path.basename(args.video_path)}'), fourcc,
fps, size)
# optional
return_heatmap = False
# e.g. use ('backbone', ) to return backbone feature
output_layer_names = None
while (cap.isOpened()):
flag, img = cap.read()
if not flag:
break
face_det_results = face_recognition.face_locations(
cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
face_results = process_face_det_results(face_det_results)
# test a single image, with a list of bboxes.
pose_results, returned_outputs = inference_top_down_pose_model(
pose_model,
img,
face_results,
bbox_thr=None,
format='xyxy',
dataset=dataset,
return_heatmap=return_heatmap,
outputs=output_layer_names)
# show the results
vis_img = vis_pose_result(
pose_model,
img,
pose_results,
dataset=dataset,
kpt_score_thr=args.kpt_thr,
show=False)
if args.show:
cv2.imshow('Image', vis_img)
if save_out_video:
videoWriter.write(vis_img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
if save_out_video:
videoWriter.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()