3
3
import cv2
4
4
from glob import glob
5
5
import os
6
+ import argparse
6
7
7
8
# openpose setup
8
9
from src import model
13
14
body_estimation = Body ('model/body_pose_model.pth' )
14
15
hand_estimation = Hand ('model/hand_pose_model.pth' )
15
16
16
- def process_frame (frame ):
17
- candidate , subset = body_estimation (frame )
17
+ def process_frame (frame , body = True , hands = True ):
18
18
canvas = copy .deepcopy (frame )
19
- canvas = util .draw_bodypose (canvas , candidate , subset )
20
- # detect hand
21
- hands_list = util .handDetect (candidate , subset , frame )
22
-
23
- all_hand_peaks = []
24
- for x , y , w , is_left in hands_list :
25
- peaks = hand_estimation (frame [y :y + w , x :x + w , :])
26
- peaks [:, 0 ] = np .where (peaks [:, 0 ]== 0 , peaks [:, 0 ], peaks [:, 0 ]+ x )
27
- peaks [:, 1 ] = np .where (peaks [:, 1 ]== 0 , peaks [:, 1 ], peaks [:, 1 ]+ y )
28
- all_hand_peaks .append (peaks )
29
-
30
- return util .draw_handpose (canvas , all_hand_peaks )
19
+ if body :
20
+ candidate , subset = body_estimation (frame )
21
+ canvas = util .draw_bodypose (canvas , candidate , subset )
22
+ if hands :
23
+ hands_list = util .handDetect (candidate , subset , frame )
24
+ all_hand_peaks = []
25
+ for x , y , w , is_left in hands_list :
26
+ peaks = hand_estimation (frame [y :y + w , x :x + w , :])
27
+ peaks [:, 0 ] = np .where (peaks [:, 0 ]== 0 , peaks [:, 0 ], peaks [:, 0 ]+ x )
28
+ peaks [:, 1 ] = np .where (peaks [:, 1 ]== 0 , peaks [:, 1 ], peaks [:, 1 ]+ y )
29
+ all_hand_peaks .append (peaks )
30
+ canvas = util .draw_handpose (canvas , all_hand_peaks )
31
+ return canvas
31
32
32
33
# writing video with ffmpeg
33
34
# https://stackoverflow.com/questions/61036822/opencv-videowriter-produces-cant-find-starting-number-error
@@ -36,8 +37,14 @@ def process_frame(frame):
36
37
def to8 (img ):
37
38
return (img / 256 ).astype ('uint8' )
38
39
39
- # open the first video file found in .videos/
40
- video_file = next (iter (glob ("videos/*" )))
40
+ # open specified video
41
+ parser = argparse .ArgumentParser (
42
+ description = "Process a video annotating poses detected." )
43
+ parser .add_argument ('file' , type = str , help = 'Video file location to process.' )
44
+ parser .add_argument ('--no_hands' , action = 'store_true' , help = 'No hand pose' )
45
+ parser .add_argument ('--no_body' , action = 'store_true' , help = 'No body pose' )
46
+ args = parser .parse_args ()
47
+ video_file = args .file
41
48
cap = cv2 .VideoCapture (video_file )
42
49
43
50
# pull video file info
@@ -73,7 +80,6 @@ def close(self):
73
80
self .ff_proc .wait ()
74
81
75
82
76
- # why isn't this a with statement??
77
83
writer = None
78
84
while (cap .isOpened ()):
79
85
ret , frame = cap .read ()
@@ -83,12 +89,12 @@ def close(self):
83
89
if writer is None :
84
90
input_framesize = frame .shape [:2 ]
85
91
writer = Writer (output_file , input_fps , input_framesize )
86
- posed_frame = process_frame (frame )
92
+ posed_frame = process_frame (frame , body = not args .no_body ,
93
+ hands = not args .no_hands )
87
94
88
- # cv2.imshow('frame', gray )
95
+ cv2 .imshow ('frame' , posed_frame )
89
96
90
97
# write the frame
91
- # writer(frame)
92
98
writer (posed_frame )
93
99
94
100
if cv2 .waitKey (1 ) & 0xFF == ord ('q' ):
0 commit comments