장규범

feat: add pose estimation video

1 +import argparse
2 +import logging
3 +import time
4 +
5 +import cv2
6 +import numpy as np
7 +
8 +from tf_pose.estimator import TfPoseEstimator
9 +from tf_pose.networks import get_graph_path, model_wh
10 +
11 +logger = logging.getLogger('TfPoseEstimator-WebCam')
12 +logger.setLevel(logging.DEBUG)
13 +ch = logging.StreamHandler()
14 +ch.setLevel(logging.DEBUG)
15 +formatter = logging.Formatter(
16 + '[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s')
17 +ch.setFormatter(formatter)
18 +logger.addHandler(ch)
19 +
20 +fps_time = 0
21 +
22 +
23 +def str2bool(v):
24 + return v.lower() in ("yes", "true", "t", "1")
25 +
26 +
27 +if __name__ == '__main__':
28 + parser = argparse.ArgumentParser(
29 + description='tf-pose-estimation motion Video')
30 + parser.add_argument('--video', type=str, default='')
31 + parser.add_argument('--resize', type=str, default='0x0',
32 + help='if provided, resize images before they are processed. default=0x0, Recommends : 432x368 or 656x368 or 1312x736 ')
33 + parser.add_argument('--resize-out-ratio', type=float, default=4.0,
34 + help='if provided, resize heatmaps before they are post-processed. default=1.0')
35 +
36 + parser.add_argument('--model', type=str, default='mobilenet_thin',
37 + help='cmu / mobilenet_thin / mobilenet_v2_large / mobilenet_v2_small')
38 + parser.add_argument('--show-process', type=bool, default=False,
39 + help='for debug purpose, if enabled, speed for inference is dropped.')
40 +
41 + parser.add_argument('--tensorrt', type=str, default="False",
42 + help='for tensorrt process.')
43 + args = parser.parse_args()
44 +
45 + logger.debug('initialization %s : %s' %
46 + (args.model, get_graph_path(args.model)))
47 + w, h = model_wh(args.resize)
48 + if w > 0 and h > 0:
49 + e = TfPoseEstimator(get_graph_path(args.model), target_size=(
50 + w, h), trt_bool=str2bool(args.tensorrt))
51 + else:
52 + e = TfPoseEstimator(get_graph_path(args.model), target_size=(
53 + 432, 368), trt_bool=str2bool(args.tensorrt))
54 + logger.debug('cam read+')
55 + cam = cv2.VideoCapture(args.video)
56 + ret_val, image = cam.read()
57 + logger.info('cam image=%dx%d' % (image.shape[1], image.shape[0]))
58 +
59 + while True:
60 + ret_val, image = cam.read()
61 +
62 + logger.debug('image process+')
63 + humans = e.inference(image, resize_to_default=(
64 + w > 0 and h > 0), upsample_size=args.resize_out_ratio)
65 +
66 + logger.debug('postprocess+')
67 + image = TfPoseEstimator.draw_humans(image, humans, imgcopy=False)
68 +
69 + logger.debug('show+')
70 + cv2.putText(image,
71 + "FPS: %f" % (1.0 / (time.time() - fps_time)),
72 + (10, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
73 + (0, 255, 0), 2)
74 + cv2.imshow('tf-pose-estimation result', image)
75 + fps_time = time.time()
76 + if cv2.waitKey(1) == 27:
77 + break
78 + logger.debug('finished+')
79 +
80 + cv2.destroyAllWindows()