|
a |
|
b/vis/processor.py |
|
|
1 |
import base64 |
|
|
2 |
import io |
|
|
3 |
import time |
|
|
4 |
|
|
|
5 |
import openpifpaf |
|
|
6 |
import PIL |
|
|
7 |
import torch |
|
|
8 |
import numpy as np |
|
|
9 |
|
|
|
10 |
|
|
|
11 |
class Processor(object): |
|
|
12 |
def __init__(self, width_height, args): |
|
|
13 |
self.width_height = width_height |
|
|
14 |
|
|
|
15 |
# Load model |
|
|
16 |
self.model_cpu, _ = openpifpaf.network.Factory().factory() |
|
|
17 |
self.model = self.model_cpu.to(args.device) |
|
|
18 |
self.processor = openpifpaf.decoder.factory(self.model_cpu.head_metas) |
|
|
19 |
# print(self.processor.device) |
|
|
20 |
self.device = args.device |
|
|
21 |
|
|
|
22 |
def get_bb(self, kp_set, score=None): |
|
|
23 |
bb_list = [] |
|
|
24 |
for i in range(kp_set.shape[0]): |
|
|
25 |
x = kp_set[i, :15, 0] |
|
|
26 |
y = kp_set[i, :15, 1] |
|
|
27 |
v = kp_set[i, :15, 2] |
|
|
28 |
assert np.any(v > 0) |
|
|
29 |
if not np.any(v > 0): |
|
|
30 |
return None |
|
|
31 |
|
|
|
32 |
# keypoint bounding box |
|
|
33 |
x1, x2 = np.min(x[v > 0]), np.max(x[v > 0]) |
|
|
34 |
y1, y2 = np.min(y[v > 0]), np.max(y[v > 0]) |
|
|
35 |
if x2 - x1 < 5.0/self.width_height[0]: |
|
|
36 |
x1 -= 2.0/self.width_height[0] |
|
|
37 |
x2 += 2.0/self.width_height[0] |
|
|
38 |
if y2 - y1 < 5.0/self.width_height[1]: |
|
|
39 |
y1 -= 2.0/self.width_height[1] |
|
|
40 |
y2 += 2.0/self.width_height[1] |
|
|
41 |
|
|
|
42 |
bb_list.append(((x1, y1), (x2, y2))) |
|
|
43 |
|
|
|
44 |
# ax.add_patch( |
|
|
45 |
# matplotlib.patches.Rectangle( |
|
|
46 |
# (x1, y1), x2s - x1, y2 - y1, fill=False, color=color)) |
|
|
47 |
# |
|
|
48 |
# if score: |
|
|
49 |
# ax.text(x1, y1, '{:.4f}'.format(score), fontsize=8, color=color) |
|
|
50 |
return bb_list |
|
|
51 |
|
|
|
52 |
@staticmethod |
|
|
53 |
def keypoint_sets(annotations): |
|
|
54 |
keypoint_sets = [ann.data for ann in annotations] |
|
|
55 |
# scores = [ann.score() for ann in annotations] |
|
|
56 |
# assert len(scores) == len(keypoint_sets) |
|
|
57 |
if not keypoint_sets: |
|
|
58 |
return np.zeros((0, 17, 3)) |
|
|
59 |
keypoint_sets = np.array(keypoint_sets) |
|
|
60 |
# scores = np.array(scores) |
|
|
61 |
|
|
|
62 |
return keypoint_sets |
|
|
63 |
|
|
|
64 |
def single_image(self, image): |
|
|
65 |
# image_bytes = io.BytesIO(base64.b64decode(b64image)) |
|
|
66 |
# im = PIL.Image.open(image_bytes).convert('RGB') |
|
|
67 |
im = PIL.Image.fromarray(image) |
|
|
68 |
|
|
|
69 |
target_wh = self.width_height |
|
|
70 |
if (im.size[0] > im.size[1]) != (target_wh[0] > target_wh[1]): |
|
|
71 |
target_wh = (target_wh[1], target_wh[0]) |
|
|
72 |
if im.size[0] != target_wh[0] or im.size[1] != target_wh[1]: |
|
|
73 |
# print(f'!!! have to resize image to {target_wh} from {im.size}') |
|
|
74 |
im = im.resize(target_wh, PIL.Image.BICUBIC) |
|
|
75 |
width_height = im.size |
|
|
76 |
|
|
|
77 |
start = time.time() |
|
|
78 |
preprocess = openpifpaf.transforms.Compose([ |
|
|
79 |
openpifpaf.transforms.NormalizeAnnotations(), |
|
|
80 |
openpifpaf.transforms.CenterPadTight(16), |
|
|
81 |
openpifpaf.transforms.EVAL_TRANSFORM, |
|
|
82 |
]) |
|
|
83 |
# processed_image, _, __ = preprocess(im, [], None) |
|
|
84 |
processed_image = openpifpaf.datasets.PilImageList([im], preprocess=preprocess)[0][0] |
|
|
85 |
# processed_image = processed_image_cpu.contiguous().to(self.device, non_blocking=True) |
|
|
86 |
# print(f'preprocessing time {time.time() - start}') |
|
|
87 |
|
|
|
88 |
all_fields = self.processor.batch(self.model, torch.unsqueeze( |
|
|
89 |
processed_image.float(), 0), device=self.device)[0] |
|
|
90 |
keypoint_sets = self.keypoint_sets(all_fields) |
|
|
91 |
|
|
|
92 |
# Normalize scale |
|
|
93 |
keypoint_sets[:, :, 0] /= processed_image.shape[2] |
|
|
94 |
keypoint_sets[:, :, 1] /= processed_image.shape[1] |
|
|
95 |
|
|
|
96 |
bboxes = self.get_bb(keypoint_sets) |
|
|
97 |
return keypoint_sets, bboxes, width_height |