Skip to content

Commit 02174f5

Browse files
committed
process arbitrary video, with options
1 parent 7c983f5 commit 02174f5

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

demo_video.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import cv2
44
from glob import glob
55
import os
6+
import argparse
67

78
# openpose setup
89
from src import model
@@ -13,21 +14,21 @@
1314
body_estimation = Body('model/body_pose_model.pth')
1415
hand_estimation = Hand('model/hand_pose_model.pth')
1516

16-
def process_frame(frame):
17-
candidate, subset = body_estimation(frame)
17+
def process_frame(frame, body=True, hands=True):
1818
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
3132

3233
# writing video with ffmpeg
3334
# https://stackoverflow.com/questions/61036822/opencv-videowriter-produces-cant-find-starting-number-error
@@ -36,8 +37,14 @@ def process_frame(frame):
3637
def to8(img):
3738
return (img/256).astype('uint8')
3839

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
4148
cap = cv2.VideoCapture(video_file)
4249

4350
# pull video file info
@@ -73,7 +80,6 @@ def close(self):
7380
self.ff_proc.wait()
7481

7582

76-
# why isn't this a with statement??
7783
writer = None
7884
while(cap.isOpened()):
7985
ret, frame = cap.read()
@@ -83,12 +89,12 @@ def close(self):
8389
if writer is None:
8490
input_framesize = frame.shape[:2]
8591
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)
8794

88-
# cv2.imshow('frame', gray)
95+
cv2.imshow('frame', posed_frame)
8996

9097
# write the frame
91-
# writer(frame)
9298
writer(posed_frame)
9399

94100
if cv2.waitKey(1) & 0xFF == ord('q'):

0 commit comments

Comments
 (0)