File size: 3,596 Bytes
9afcee2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import torch
import matplotlib.pyplot as plt
import matplotlib as mpl
import os
import cv2
from PIL import Image

from .visualizer import Visualizer


movable_imap = {
    0: 'one_hand',
    1: 'two_hands',
    2: 'fixture',
    -100: 'n/a',
}

rigid_imap = {
    1: 'yes',
    0: 'no',
    2: 'bad',
    -100: 'n/a',
}

kinematic_imap = {
    0: 'freeform',
    1: 'rotation',
    2: 'translation',
    -100: 'n/a'
}

action_imap = {
    0: 'free',
    1: 'pull',
    2: 'push',
    -100: 'n/a',
}


def draw_properties(
    output_path,
    movable,
    rigid,
    kinematic,
    action,
):
    fig = plt.figure()
    vis_phy = np.ones((80, 60, 3), dtype=np.uint8) * 255
    plt.imshow(vis_phy)
    fig.axes[0].add_patch(
        mpl.patches.FancyBboxPatch(
            (10, 10), 
            40, 
            60, 
            ec=np.array([79, 129, 189]) / 255.0, 
            boxstyle=mpl.patches.BoxStyle("Round", pad=5),
            fill=False,
            linewidth=5,
        )
    )
    fontsize = 20
    if rigid_imap[rigid] == 'yes':
        rigid_text = 'Rigid: Yes'
    else:
        rigid_text = 'Rigid: No'
    plt.text(30, 20, rigid_text, fontsize=fontsize, horizontalalignment='center')

    if movable_imap[movable] == 'one_hand':
        movable_text = "Move: 1 hand"
    elif movable_imap[movable] == 'two_hands':
        movable_text = "Move: 2 hands"
    else:
        movable_text = 'Move: Fixture'
    plt.text(30, 35, movable_text, fontsize=fontsize, horizontalalignment='center')

    if kinematic_imap[kinematic] == 'rotation':
        kinematic_text = 'Motion: Rot'
    elif kinematic_imap[kinematic] == 'translation':
        kinematic_text = 'Motion: Trans'
    else:
        kinematic_text = 'Motion: Free'
    plt.text(30, 50, kinematic_text, fontsize=fontsize, horizontalalignment='center')

    if action_imap[action] == 'pull':
        action_text = 'Action: Pull'
    elif action_imap[action] == 'push':
        action_text = 'Action: Push'
    else:
        action_text = 'Action: Free'
    plt.text(30, 65, action_text, fontsize=fontsize, horizontalalignment='center')

    plt.axis('off')
    plt.savefig(output_path, bbox_inches='tight', pad_inches=0)
    plt.close(fig)


def draw_localization(rgb, output_path, bbox, mask, axis, colors=None, alpha=0.5):
    if colors is None:
        # colors = np.array([[246, 95, 16], ]) / 255.0  # orange
        #colors = np.array([[227, 13, 255], ]) / 255.0  # purple
        colors = np.array([[178, 13, 255], ]) / 255.0  # purple


    pred_entry = {
        'keypoint': None,
        'bbox': bbox,
        'mask': mask,
        'affordance': None,
        'move': None,
        'rigid': None,
        'kinematic': None,
        'pull_or_push': None,
        'axis': axis,
    }
    instances = [pred_entry]

    vis = Visualizer(rgb)
    vis.overlay_instances(instances, assigned_colors=colors, alpha=alpha)
    vis.output.save(output_path)


def draw_affordance(rgb, output_path, affordance, alpha=0.5):
    # normalize affordance
    aff_min = affordance.min()
    aff_max = affordance.max()
    affordance = (affordance - aff_min) / (aff_max - aff_min)

    # convert rgb to gray scale for better visualization
    rgb_gray = rgb.copy()
    # rgb_gray[:, :, 0] = rgb.mean(axis=2)
    # rgb_gray[:, :, 1] = rgb.mean(axis=2)
    # rgb_gray[:, :, 2] = rgb.mean(axis=2)

    heatmap_img = cv2.applyColorMap((affordance * 255.0).astype(np.uint8), cv2.COLORMAP_HOT)[:, :, ::-1]
    vis = cv2.addWeighted(heatmap_img, alpha, rgb_gray, 1-alpha, 0)
    return Image.fromarray(vis) #.save(output_path)