forked from gilbeckers/MultiPersonMatching
-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw_humans.py
152 lines (109 loc) · 4.69 KB
/
draw_humans.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import numpy as np
from enum import Enum
import cv2
import parse_openpose_json
import matplotlib.pyplot as plt
def test():
json_data_path = 'data/json_data/'
images_data_path = 'data/image_data/'
model = "foto1"
input = "kleuter2"
model_json = json_data_path + model + '.json'
input_json = json_data_path + input + '_keypoints.json'
model_image = images_data_path + model + '.jpg'
input_image = images_data_path + input + '.jpg'
model_features = parse_openpose_json.parse_JSON_multi_person(model_json)
assert type(model_features) is list
img = draw_humans(model_image, model_features, True)
plt.figure()
plt.imshow(img)
plt.show()
class CocoPart(Enum):
Nose = 0
Neck = 1
RShoulder = 2
RElbow = 3
RWrist = 4
LShoulder = 5
LElbow = 6
LWrist = 7
RHip = 8
RKnee = 9
RAnkle = 10
LHip = 11
LKnee = 12
LAnkle = 13
REye = 14
LEye = 15
REar = 16
LEar = 17
Background = 18
CocoColors = [[255, 0, 0], [255, 85, 0], [255, 170, 0], [255, 255, 0], [170, 255, 0], [85, 255, 0], [0, 255, 0],
[0, 255, 85], [0, 255, 170], [0, 255, 255], [0, 170, 255], [0, 85, 255], [0, 0, 255], [85, 0, 255],
[170, 0, 255], [255, 0, 255], [255, 0, 170], [255, 0, 85]]
CocoPairs = [
(1, 2), (1, 5), (2, 3), (3, 4), (5, 6), (6, 7), (1, 8), (8, 9), (9, 10), (1, 11),
(11, 12), (12, 13), (1, 0), (0, 14), (14, 16), (0, 15), (15, 17), (2, 16), (5, 17)
] # = 19
CocoPairsRender = CocoPairs[:-2]
thickness= 4
# source: https://github.com/ildoonet/tf-pose-estimation/blob/master/src/estimator.py
def draw_humans(npimg, humans, imgcopy=False):
#npimg = plt.imread(img_path)
if imgcopy:
npimg = np.copy(npimg)
#image_h, image_w = npimg.shape[:2]
centers = {}
# if is type list => openpose.json van multiple persons
if type(humans) is list:
for human in humans:
# draw point
for i in range(CocoPart.Background.value):
body_part = human[i]
if body_part[0] == 0 and body_part[1] ==0:
continue
print("laaaaaaaaaaaaaaaaa")
center = (int(body_part[0]), int(body_part[1]))
centers[i] = center
cv2.circle(npimg, center, thickness, CocoColors[i], thickness=thickness, lineType=8, shift=0)
#draw line
for pair_order, pair in enumerate(CocoPairsRender):
# if pair[0] not in human.body_parts.keys() or pair[1] not in human.body_parts.keys():
# continue
# npimg = cv2.line(npimg, centers[pair[0]], centers[pair[1]], common.CocoColors[pair_order], 3)
cv2.line(npimg, centers[pair[0]], centers[pair[1]], CocoColors[pair_order], thickness=thickness)
return npimg
else: # single-person pose (no list, but plain np.array)
# draw point
for i in range(CocoPart.Background.value):
body_part = humans[i]
if body_part[0] == 0 and body_part[1] == 0:
continue
center = (int(body_part[0]), int(body_part[1]))
centers[i] = center
vers = 7
#cv2.rectangle(npimg, (center[0]-vers,center[1]-vers), (center[0]+vers,center[1]+vers) ,CocoColors[i], thickness=cv2.FILLED)
cv2.circle(npimg, center, thickness + 1, CocoColors[i], thickness=thickness + 2, lineType=8, shift=0)
# draw line
for pair_order, pair in enumerate(CocoPairsRender):
# if pair[0] not in human.body_parts.keys() or pair[1] not in human.body_parts.keys():
# continue
# npimg = cv2.line(npimg, centers[pair[0]], centers[pair[1]], common.CocoColors[pair_order], 3)
cv2.line(npimg, centers[pair[0]], centers[pair[1]], CocoColors[pair_order], thickness=thickness-1)
return npimg
def draw_square(npimg, features):
centers = {}
# draw point
for i in range(CocoPart.Background.value):
body_part = features[i]
if body_part[0] == 0 and body_part[1] == 0:
continue
center = (int(body_part[0]), int(body_part[1]))
centers[i] = center
vers = 7
cv2.rectangle(npimg, (center[0] - vers, center[1] - vers), (center[0] + vers, center[1] + vers),
color=[abs(CocoColors[i][0]-255),abs(CocoColors[i][1]-255),abs(CocoColors[i][2]-255)], thickness=3)
# cv2.circle(npimg, center, thickness + 1, color=[255, 0, 170], thickness=thickness + 2, lineType=8, shift=0)
# cv2.rectangle(npimg, (center[0] - vers, center[1] - vers), (center[0] + vers, center[1] + vers),
# color=[255, 0, 170], thickness=3)
return npimg