This repository has been archived by the owner on Jul 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
visualize.py
88 lines (75 loc) · 3.08 KB
/
visualize.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
'''
Adapted from DeeperCut by Eldar Insafutdinov
https://github.com/eldar/pose-tensorflow
'''
import math, os
import numpy as np
#from scipy.misc import imresize
from deeplabcut.utils.auxfun_videos import imresize
import matplotlib
import platform
if os.environ.get('DLClight', default=False) == 'True':
matplotlib.use('AGG') #anti-grain geometry engine #https://matplotlib.org/faq/usage_faq.html
elif platform.system() == 'Darwin':
matplotlib.use('WxAgg') #TkAgg
else:
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
def _npcircle(image, cx, cy, radius, color, transparency=0.0):
"""Draw a circle on an image using only numpy methods."""
radius = int(radius)
cx = int(cx)
cy = int(cy)
y, x = np.ogrid[-radius: radius, -radius: radius]
index = x**2 + y**2 <= radius**2
image[cy-radius:cy+radius, cx-radius:cx+radius][index] = (
image[cy-radius:cy+radius, cx-radius:cx+radius][index].astype('float32') * transparency +
np.array(color).astype('float32') * (1.0 - transparency)).astype('uint8')
def check_point(cur_x, cur_y, minx, miny, maxx, maxy):
return minx < cur_x < maxx and miny < cur_y < maxy
def visualize_joints(image, pose):
marker_size = 8
minx = 2 * marker_size
miny = 2 * marker_size
maxx = image.shape[1] - 2 * marker_size
maxy = image.shape[0] - 2 * marker_size
num_joints = pose.shape[0]
visim = image.copy()
colors = [[255, 0, 0], [0, 255, 0], [0, 0, 255], [0, 245, 255], [255, 131, 250], [255, 255, 0],
[255, 0, 0], [0, 255, 0], [0, 0, 255], [0, 245, 255], [255, 131, 250], [255, 255, 0],
[0, 0, 0], [255, 255, 255], [255, 0, 0], [0, 255, 0], [0, 0, 255]]
for p_idx in range(num_joints):
cur_x = pose[p_idx, 0]
cur_y = pose[p_idx, 1]
if check_point(cur_x, cur_y, minx, miny, maxx, maxy):
_npcircle(visim,
cur_x, cur_y,
marker_size,
colors[p_idx],
0.0)
return visim
def show_heatmaps(cfg, img, scmap, pose, cmap="jet"):
interp = "bilinear"
all_joints = cfg.all_joints
all_joints_names = cfg.all_joints_names
subplot_width = 3
subplot_height = math.ceil((len(all_joints) + 1) / subplot_width)
f, axarr = plt.subplots(subplot_height, subplot_width)
for pidx, part in enumerate(all_joints):
plot_j = (pidx + 1) // subplot_width
plot_i = (pidx + 1) % subplot_width
scmap_part = np.sum(scmap[:, :, part], axis=2)
scmap_part = imresize(scmap_part, 8.0, interp='bicubic')
scmap_part = np.lib.pad(scmap_part, ((4, 0), (4, 0)), 'minimum')
curr_plot = axarr[plot_j, plot_i]
curr_plot.set_title(all_joints_names[pidx])
curr_plot.axis('off')
curr_plot.imshow(img, interpolation=interp)
curr_plot.imshow(scmap_part, alpha=.5, cmap=cmap, interpolation=interp)
curr_plot = axarr[0, 0]
curr_plot.set_title('Pose')
curr_plot.axis('off')
curr_plot.imshow(visualize_joints(img, pose))
plt.show()
def waitforbuttonpress():
plt.waitforbuttonpress(timeout=1)