Spaces:
Build error
Build error
File size: 8,425 Bytes
c7f0cc1 |
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
from typing import Tuple
import os.path as osp
import PIL
import mmcv
import numpy as np
from detectron2.utils.colormap import colormap
from detectron2.utils.visualizer import VisImage, Visualizer
from mmdet.datasets.coco_panoptic import INSTANCE_OFFSET
CLASSES = [
'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train',
'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign',
'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag',
'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite',
'baseball bat', 'baseball glove', 'skateboard', 'surfboard',
'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon',
'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot',
'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant',
'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',
'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink',
'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear',
'hair drier', 'toothbrush', 'banner', 'blanket', 'bridge', 'cardboard',
'counter', 'curtain', 'door-stuff', 'floor-wood', 'flower', 'fruit',
'gravel', 'house', 'light', 'mirror-stuff', 'net', 'pillow', 'platform',
'playingfield', 'railroad', 'river', 'road', 'roof', 'sand', 'sea',
'shelf', 'snow', 'stairs', 'tent', 'towel', 'wall-brick', 'wall-stone',
'wall-tile', 'wall-wood', 'water-other', 'window-blind', 'window-other',
'tree-merged', 'fence-merged', 'ceiling-merged', 'sky-other-merged',
'cabinet-merged', 'table-merged', 'floor-other-merged', 'pavement-merged',
'mountain-merged', 'grass-merged', 'dirt-merged', 'paper-merged',
'food-other-merged', 'building-other-merged', 'rock-merged',
'wall-other-merged', 'rug-merged', 'background'
]
PREDICATES = [
'over',
'in front of',
'beside',
'on',
'in',
'attached to',
'hanging from',
'on back of',
'falling off',
'going down',
'painted on',
'walking on',
'running on',
'crossing',
'standing on',
'lying on',
'sitting on',
'flying over',
'jumping over',
'jumping from',
'wearing',
'holding',
'carrying',
'looking at',
'guiding',
'kissing',
'eating',
'drinking',
'feeding',
'biting',
'catching',
'picking',
'playing with',
'chasing',
'climbing',
'cleaning',
'playing',
'touching',
'pushing',
'pulling',
'opening',
'cooking',
'talking to',
'throwing',
'slicing',
'driving',
'riding',
'parked on',
'driving on',
'about to hit',
'kicking',
'swinging',
'entering',
'exiting',
'enclosing',
'leaning on',
]
def get_colormap(num_colors: int):
return (np.resize(colormap(), (num_colors, 3))).tolist()
def draw_text(
viz_img: VisImage = None,
text: str = None,
x: float = None,
y: float = None,
color: Tuple[float, float, float] = [0, 0, 0],
size: float = 10,
padding: float = 5,
box_color: str = 'black',
font: str = None,
) -> float:
text_obj = viz_img.ax.text(
x,
y,
text,
size=size,
# family="sans-serif",
bbox={
'facecolor': box_color,
'alpha': 0.8,
'pad': padding,
'edgecolor': 'none',
},
verticalalignment='top',
horizontalalignment='left',
color=color,
zorder=10,
rotation=0,
)
viz_img.get_image()
text_dims = text_obj.get_bbox_patch().get_extents()
return text_dims.width
def show_result(img,
result,
is_one_stage,
num_rel=20,
show=False,
out_dir=None,
out_file=None):
# Load image
img = mmcv.imread(img)
img = img.copy() # (H, W, 3)
img_h, img_w = img.shape[:-1]
# Decrease contrast
img = PIL.Image.fromarray(img)
converter = PIL.ImageEnhance.Color(img)
img = converter.enhance(0.01)
if out_file is not None:
mmcv.imwrite(np.asarray(img), 'bw'+out_file)
# Draw masks
pan_results = result.pan_results
ids = np.unique(pan_results)[::-1]
num_classes = 133
legal_indices = (ids != num_classes) # for VOID label
ids = ids[legal_indices]
# Get predicted labels
labels = np.array([id % INSTANCE_OFFSET for id in ids], dtype=np.int64)
labels = [CLASSES[l] for l in labels]
#For psgtr
rel_obj_labels = result.labels
rel_obj_labels = [CLASSES[l - 1] for l in rel_obj_labels]
# (N_m, H, W)
segms = pan_results[None] == ids[:, None, None]
# Resize predicted masks
segms = [
mmcv.image.imresize(m.astype(float), (img_w, img_h)) for m in segms
]
# One stage segmentation
masks = result.masks
# Choose colors for each instance in coco
colormap_coco = get_colormap(len(masks)) if is_one_stage else get_colormap(len(segms))
colormap_coco = (np.array(colormap_coco) / 255).tolist()
# Viualize masks
viz = Visualizer(img)
viz.overlay_instances(
labels=rel_obj_labels if is_one_stage else labels,
masks=masks if is_one_stage else segms,
assigned_colors=colormap_coco,
)
viz_img = viz.get_output().get_image()
if out_file is not None:
mmcv.imwrite(viz_img, out_file)
# Draw relations
# Filter out relations
n_rel_topk = num_rel
# Exclude background class
rel_dists = result.rel_dists[:, 1:]
# rel_dists = result.rel_dists
rel_scores = rel_dists.max(1)
# rel_scores = result.triplet_scores
# Extract relations with top scores
rel_topk_idx = np.argpartition(rel_scores, -n_rel_topk)[-n_rel_topk:]
rel_labels_topk = rel_dists[rel_topk_idx].argmax(1)
rel_pair_idxes_topk = result.rel_pair_idxes[rel_topk_idx]
relations = np.concatenate(
[rel_pair_idxes_topk, rel_labels_topk[..., None]], axis=1)
n_rels = len(relations)
top_padding = 20
bottom_padding = 20
left_padding = 20
text_size = 10
text_padding = 5
text_height = text_size + 2 * text_padding
row_padding = 10
height = (top_padding + bottom_padding + n_rels *
(text_height + row_padding) - row_padding)
width = img_w
curr_x = left_padding
curr_y = top_padding
# # Adjust colormaps
# colormap_coco = [adjust_text_color(c, viz) for c in colormap_coco]
viz_graph = VisImage(np.full((height, width, 3), 255))
all_rel_vis = []
for i, r in enumerate(relations):
s_idx, o_idx, rel_id = r
s_label = rel_obj_labels[s_idx]
o_label = rel_obj_labels[o_idx]
rel_label = PREDICATES[rel_id]
viz = Visualizer(img)
viz.overlay_instances(
labels=[s_label, o_label],
masks=[masks[s_idx], masks[o_idx]],
assigned_colors=[colormap_coco[s_idx], colormap_coco[o_idx]],
)
viz_masked_img = viz.get_output().get_image()
viz_graph = VisImage(np.full((40, width, 3), 255))
curr_x = 2
curr_y = 2
text_size = 25
text_padding = 20
font = 36
text_width = draw_text(
viz_img=viz_graph,
text=s_label,
x=curr_x,
y=curr_y,
color=colormap_coco[s_idx],
size=text_size,
padding=text_padding,
font=font,
)
curr_x += text_width
# Draw relation text
text_width = draw_text(
viz_img=viz_graph,
text=rel_label,
x=curr_x,
y=curr_y,
size=text_size,
padding=text_padding,
box_color='gainsboro',
font=font,
)
curr_x += text_width
# Draw object text
text_width = draw_text(
viz_img=viz_graph,
text=o_label,
x=curr_x,
y=curr_y,
color=colormap_coco[o_idx],
size=text_size,
padding=text_padding,
font=font,
)
output_viz_graph = np.vstack([viz_masked_img, viz_graph.get_image()])
if show:
all_rel_vis.append(output_viz_graph)
return all_rel_vis |