forked from tryagainconcepts/tf-pose-estimation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
110 lines (93 loc) · 3.51 KB
/
run.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
import argparse
import logging
import sys
import time
from tf_pose import common
import cv2
import numpy as np
from tf_pose.estimator import TfPoseEstimator
from tf_pose.networks import get_graph_path, model_wh
logger = logging.getLogger("TfPoseEstimatorRun")
logger.handlers.clear()
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter("[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s")
ch.setFormatter(formatter)
logger.addHandler(ch)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="tf-pose-estimation run")
parser.add_argument("--image", type=str, default="./images/p1.jpg")
parser.add_argument(
"--model",
type=str,
default="cmu",
help="cmu / mobilenet_thin / mobilenet_v2_large / mobilenet_v2_small",
)
parser.add_argument(
"--resize",
type=str,
default="0x0",
help="if provided, resize images before they are processed. "
"default=0x0, Recommends : 432x368 or 656x368 or 1312x736 ",
)
parser.add_argument(
"--resize-out-ratio",
type=float,
default=4.0,
help="if provided, resize heatmaps before they are post-processed. default=1.0",
)
args = parser.parse_args()
w, h = model_wh(args.resize)
if w == 0 or h == 0:
e = TfPoseEstimator(get_graph_path(args.model), target_size=(432, 368))
else:
e = TfPoseEstimator(get_graph_path(args.model), target_size=(w, h))
# estimate human poses from a single image !
image = common.read_imgfile(args.image, None, None)
if image is None:
logger.error("Image can not be read, path=%s" % args.image)
sys.exit(-1)
t = time.time()
humans = e.inference(
image, resize_to_default=(w > 0 and h > 0), upsample_size=args.resize_out_ratio
)
elapsed = time.time() - t
logger.info("inference image: %s in %.4f seconds." % (args.image, elapsed))
image = TfPoseEstimator.draw_humans(image, humans, imgcopy=False)
try:
import matplotlib.pyplot as plt
fig = plt.figure()
a = fig.add_subplot(2, 2, 1)
a.set_title("Result")
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
bgimg = cv2.cvtColor(image.astype(np.uint8), cv2.COLOR_BGR2RGB)
bgimg = cv2.resize(
bgimg,
(e.heatMat.shape[1], e.heatMat.shape[0]),
interpolation=cv2.INTER_AREA,
)
# show network output
a = fig.add_subplot(2, 2, 2)
plt.imshow(bgimg, alpha=0.5)
tmp = np.amax(e.heatMat[:, :, :-1], axis=2)
plt.imshow(tmp, cmap=plt.cm.gray, alpha=0.5)
plt.colorbar()
tmp2 = e.pafMat.transpose((2, 0, 1))
tmp2_odd = np.amax(np.absolute(tmp2[::2, :, :]), axis=0)
tmp2_even = np.amax(np.absolute(tmp2[1::2, :, :]), axis=0)
a = fig.add_subplot(2, 2, 3)
a.set_title("Vectormap-x")
# plt.imshow(CocoPose.get_bgimg(inp, target_size=(vectmap.shape[1], vectmap.shape[0])), alpha=0.5)
plt.imshow(tmp2_odd, cmap=plt.cm.gray, alpha=0.5)
plt.colorbar()
a = fig.add_subplot(2, 2, 4)
a.set_title("Vectormap-y")
# plt.imshow(CocoPose.get_bgimg(inp, target_size=(vectmap.shape[1], vectmap.shape[0])), alpha=0.5)
plt.imshow(tmp2_even, cmap=plt.cm.gray, alpha=0.5)
plt.colorbar()
plt.show()
except Exception as e:
logger.warning("matplitlib error, %s" % e)
cv2.imshow("result", image)
cv2.waitKey()