def image_cb(self, msg): img_orig = self.bridge.imgmsg_to_cv2(msg, desired_encoding='rgb8') img = np.asarray(img_orig) / 255 H, W = img.shape[:2] h_step = self.patch_height * self.subsample w_step = self.patch_width * self.subsample h_idxs = np.arange(0, H, h_step)[:-1] w_idxs = np.arange(0, W, h_step)[:-1] results = [] for y in h_idxs: for x in w_idxs: sub_img = img[y:y+h_step:self.subsample, x:x+w_step:self.subsample, :][np.newaxis, ...] self.interpreter.set_tensor(self.input_details[0]['index'], sub_img.astype(np.float32)) self.interpreter.invoke() cls = self.interpreter.get_tensor(self.output_details[0]['index']) cls = [np.argmax(cls), np.max(cls)] results.append(cls) labels = np.asarray([p[0] for p in results]) probs = np.asarray([p[1] for p in results]) cls_msg = ClassificationResult( header=msg.header, classifier=self.classifier_name, target_names=self.label_names, labels=labels, label_names=[self.label_names[l] for l in labels], label_proba=probs) self.pub_classification.publish(cls_msg) if self.visualize: fig = plt.figure(tight_layout={'pad': 0}) ax = plt.Axes(fig, [0., 0., 1., 1.]) ax.axis('off') fig.add_axes(ax) clr = np.asarray([[1,0,0], [0,1,0], [0,0,1], [0,0,0]]) idx = 0 ax = vis_image(img_orig.transpose((2, 0, 1)), ax=ax) for y in h_idxs: for x in w_idxs: ax.add_patch(plt.Rectangle( (x,y), self.patch_width * self.subsample, self.patch_height * self.subsample, fill=True, facecolor=clr[labels[idx]], linewidth=0, alpha=0.3)) idx += 1 fig.canvas.draw() w, h = fig.canvas.get_width_height() vis_img = np.fromstring( fig.canvas.tostring_rgb(), dtype=np.uint8) vis_img.shape = (h, w, 3) fig.clf() plt.close() vis_msg = self.bridge.cv2_to_imgmsg(vis_img, 'rgb8') # BUG: https://answers.ros.org/question/316362/sensor_msgsimage-generates-float-instead-of-int-with-python3/ # NOQA vis_msg.step = int(vis_msg.step) vis_msg.header = msg.header self.pub_image.publish(vis_msg)
def vis_bbox(img, bbox, label=None, score=None, label_names=None, instance_colors=None, alpha=1., linewidth=3., sort_by_score=True, ax=None): from matplotlib import pyplot as plt if label is not None and not len(bbox) == len(label): raise ValueError('The length of label must be same as that of bbox') if score is not None and not len(bbox) == len(score): raise ValueError('The length of score must be same as that of bbox') if sort_by_score and score is not None: order = np.argsort(score) bbox = bbox[order] score = score[order] if label is not None: label = label[order] if instance_colors is not None: instance_colors = np.array(instance_colors)[order] # Returns newly instantiated matplotlib.axes.Axes object if ax is None ax = vis_image(img, ax=ax) # If there is no bounding box to display, visualize the image and exit. if len(bbox) == 0: return ax if instance_colors is None: # Red instance_colors = np.zeros((len(bbox), 3), dtype=np.float32) instance_colors[:, 0] = 255 instance_colors = np.array(instance_colors) for i, bb in enumerate(bbox): xy = (bb[1], bb[0]) height = bb[2] - bb[0] width = bb[3] - bb[1] color = instance_colors[i % len(instance_colors)] / 255 ax.add_patch(plt.Rectangle( xy, width, height, fill=False, edgecolor=color, linewidth=linewidth, alpha=alpha)) caption = [] if label is not None and label_names is not None: lb = label[i] if not (0 <= lb < len(label_names)): raise ValueError('No corresponding name is given') caption.append(label_names[lb]) if score is not None: sc = score[i] caption.append(u"{:.2f}".format(sc)) if len(caption) > 0: ax.text(bb[1], bb[0], u": ".join(caption), style='italic', bbox={'facecolor': 'white', 'alpha': 0.7, }, fontsize=5) return ax
def vis_point(img, point, visible=None, ax=None): """Visualize points in an image. Example: >>> import chainercv >>> import matplotlib.pyplot as plt >>> dataset = chainercv.datasets.CUBKeypointDataset() >>> img, point, visible = dataset[0] >>> chainercv.visualizations.vis_point(img, point, visible) >>> plt.show() Args: img (~numpy.ndarray): See the table below. If this is :obj:`None`, no image is displayed. point (~numpy.ndarray or list of arrays): See the table below. visible (~numpy.ndarray or list of arrays): See the table below. ax (matplotlib.axes.Axes): If provided, plot on this axis. .. csv-table:: :header: name, shape, dtype, format :obj:`img`, ":math:`(3, H, W)`", :obj:`float32`, \ "RGB, :math:`[0, 255]`" :obj:`point`, ":math:`(R, K, 2)` or :math:`[(K, 2)]`", \ :obj:`float32`, ":math:`(y, x)`" :obj:`visible`, ":math:`(R, K)` or :math:`[(K,)]`", :obj:`bool`, -- Returns: ~matploblib.axes.Axes: Returns the Axes object with the plot for further tweaking. """ import matplotlib.pyplot as plt # Returns newly instantiated matplotlib.axes.Axes object if ax is None ax = vis_image(img, ax=ax) _, H, W = img.shape n_inst = len(point) cm = plt.get_cmap('gist_rainbow') for i in range(n_inst): pnt = point[i] n_point = len(pnt) if visible is not None: vsbl = visible[i] else: vsbl = np.ones((n_point, ), dtype=np.bool) colors = [cm(k / n_point) for k in six.moves.range(n_point)] for k in range(n_point): if vsbl[k]: ax.scatter(pnt[k][1], pnt[k][0], c=colors[k], s=100) ax.set_xlim(left=0, right=W) ax.set_ylim(bottom=H - 1, top=0) return ax
def vis_point(img, point, ax=None): # Returns newly instantiated matplotlib.axes.Axes object if ax is None ax = vis_image(img, ax=ax) for i in range(len(point)): for j in range(len(point[i])): ax.scatter(point[i][j][0], point[i][j][1]) return ax
def vis_keypoint(img, keypoint, kp_mask=None, ax=None): """Visualize keypoints in an image. Example: >>> import chainercv >>> import matplotlib.pyplot as plot >>> dataset = chainercv.datasets.CUBKeypointDataset() >>> img, keypoint, kp_mask = dataset[0] >>> chainercv.visualizations.vis_keypoint(img, keypoint, kp_mask) >>> plot.show() Args: img (~numpy.ndarray): An image of shape :math:`(3, height, width)`. This is in RGB format and the range of its value is :math:`[0, 255]`. This should be visualizable using :obj:`matplotlib.pyplot.imshow(img)` keypoint (~numpy.ndarray): An array with keypoint pairs whose shape is :math:`(K, 2)`, where :math:`K` is the number of keypoints in the array. The second axis corresponds to :math:`y` and :math:`x` coordinates of the keypoint. kp_mask (~numpy.ndarray, optional): A boolean array whose shape is :math:`(K,)`. If :math:`i` th index is :obj:`True`, the :math:`i` th keypoint is not displayed. If not specified, all keypoints in :obj:`keypoint` will be displayed. ax (matplotlib.axes.Axes, optional): If provided, plot on this axis. Returns: ~matploblib.axes.Axes: Returns the Axes object with the plot for further tweaking. """ import matplotlib.pyplot as plot # Returns newly instantiated matplotlib.axes.Axes object if ax is None ax = vis_image(img, ax=ax) _, H, W = img.shape n_kp = len(keypoint) if kp_mask is None: kp_mask = np.ones((n_kp,), dtype=np.bool) cm = plot.get_cmap('gist_rainbow') colors = [cm(1. * i / n_kp) for i in six.moves.range(n_kp)] for i in range(n_kp): if kp_mask[i]: ax.scatter(keypoint[i][1], keypoint[i][0], c=colors[i], s=100) ax.set_xlim(left=0, right=W) ax.set_ylim(bottom=H - 1, top=0) return ax
def vis_point(img, point, mask=None, ax=None): """Visualize points in an image. Example: >>> import chainercv >>> import matplotlib.pyplot as plot >>> dataset = chainercv.datasets.CUBKeypointDataset() >>> img, point, mask = dataset[0] >>> chainercv.visualizations.vis_point(img, point, mask) >>> plot.show() Args: img (~numpy.ndarray): An image of shape :math:`(3, height, width)`. This is in RGB format and the range of its value is :math:`[0, 255]`. This should be visualizable using :obj:`matplotlib.pyplot.imshow(img)` point (~numpy.ndarray): An array of point coordinates whose shape is :math:`(P, 2)`, where :math:`P` is the number of points. The second axis corresponds to :math:`y` and :math:`x` coordinates of the points. mask (~numpy.ndarray): A boolean array whose shape is :math:`(P,)`. If :math:`i` th element is :obj:`True`, the :math:`i` th point is not displayed. If not specified, all points in :obj:`point` will be displayed. ax (matplotlib.axes.Axes): If provided, plot on this axis. Returns: ~matploblib.axes.Axes: Returns the Axes object with the plot for further tweaking. """ import matplotlib.pyplot as plot # Returns newly instantiated matplotlib.axes.Axes object if ax is None ax = vis_image(img, ax=ax) _, H, W = img.shape n_point = len(point) if mask is None: mask = np.ones((n_point,), dtype=np.bool) cm = plot.get_cmap('gist_rainbow') colors = [cm(i / n_point) for i in six.moves.range(n_point)] for i in range(n_point): if mask[i]: ax.scatter(point[i][1], point[i][0], c=colors[i], s=100) ax.set_xlim(left=0, right=W) ax.set_ylim(bottom=H - 1, top=0) return ax
def vis_tracking_bbox(img, bbox, inst_id, label=None, score=None, label_names=None, alpha=1.0, ax=None): from matplotlib import pyplot as plot ax = vis_image(img, ax=ax) assert len(bbox) == len(inst_id) if len(bbox) == 0: return ax for i, (bb, inst_i) in enumerate(zip(bbox, inst_id)): bb = np.round(bb).astype(np.int32) y_min, x_min, y_max, x_max = bb color = np.array(_default_cmap(inst_i + 1)) / 255. ax.add_patch( plot.Rectangle((x_min, y_min), x_max - x_min, y_max - y_min, fill=False, edgecolor=color, linewidth=3)) caption = [] caption.append('{}'.format(inst_i)) if label is not None and label_names is not None: lb = label[i] if not (0 <= lb < len(label_names)): raise ValueError('No corresponding name is given') caption.append(label_names[lb]) if score is not None: sc = score[i] caption.append('{:.2f}'.format(sc)) ax.text((x_max + x_min) / 2, y_min, ': '.join(caption), style='italic', bbox={ 'facecolor': color, 'alpha': alpha }, fontsize=8, color='white')
def vis_bbox(img, bbox, label=None, score=None, label_names=None, instance_colors=None, alpha=1., linewidth=3., sort_by_score=True, ax=None): """Visualize bounding boxes inside image. Example: >>> from chainercv.datasets import VOCBboxDataset >>> from chainercv.datasets import voc_bbox_label_names >>> from chainercv.visualizations import vis_bbox >>> import matplotlib.pyplot as plt >>> dataset = VOCBboxDataset() >>> img, bbox, label = dataset[60] >>> vis_bbox(img, bbox, label, ... label_names=voc_bbox_label_names) >>> plt.show() This example visualizes by displaying the same colors for bounding boxes assigned to the same labels. >>> from chainercv.datasets import VOCBboxDataset >>> from chainercv.datasets import voc_bbox_label_names >>> from chainercv.visualizations import vis_bbox >>> from chainercv.visualizations.colormap import voc_colormap >>> import matplotlib.pyplot as plt >>> dataset = VOCBboxDataset() >>> img, bbox, label = dataset[61] >>> colors = voc_colormap(label + 1) >>> vis_bbox(img, bbox, label, ... label_names=voc_bbox_label_names, ... instance_colors=colors) >>> plt.show() Args: img (~numpy.ndarray): See the table below. If this is :obj:`None`, no image is displayed. bbox (~numpy.ndarray): See the table below. label (~numpy.ndarray): See the table below. This is optional. score (~numpy.ndarray): See the table below. This is optional. label_names (iterable of strings): Name of labels ordered according to label ids. If this is :obj:`None`, labels will be skipped. instance_colors (iterable of tuples): List of colors. Each color is RGB format and the range of its values is :math:`[0, 255]`. The :obj:`i`-th element is the color used to visualize the :obj:`i`-th instance. If :obj:`instance_colors` is :obj:`None`, the red is used for all boxes. alpha (float): The value which determines transparency of the bounding boxes. The range of this value is :math:`[0, 1]`. linewidth (float): The thickness of the edges of the bounding boxes. sort_by_score (bool): When :obj:`True`, instances with high scores are always visualized in front of instances with low scores. ax (matplotlib.axes.Axis): The visualization is displayed on this axis. If this is :obj:`None` (default), a new axis is created. .. csv-table:: :header: name, shape, dtype, format :obj:`img`, ":math:`(3, H, W)`", :obj:`float32`, \ "RGB, :math:`[0, 255]`" :obj:`bbox`, ":math:`(R, 4)`", :obj:`float32`, \ ":math:`(y_{min}, x_{min}, y_{max}, x_{max})`" :obj:`label`, ":math:`(R,)`", :obj:`int32`, \ ":math:`[0, \#fg\_class - 1]`" :obj:`score`, ":math:`(R,)`", :obj:`float32`, -- Returns: ~matploblib.axes.Axes: Returns the Axes object with the plot for further tweaking. """ from matplotlib import pyplot as plt if label is not None and not len(bbox) == len(label): raise ValueError('The length of label must be same as that of bbox') if score is not None and not len(bbox) == len(score): raise ValueError('The length of score must be same as that of bbox') if sort_by_score and score is not None: order = np.argsort(score) bbox = bbox[order] score = score[order] if label is not None: label = label[order] if instance_colors is not None: instance_colors = np.array(instance_colors)[order] # Returns newly instantiated matplotlib.axes.Axes object if ax is None ax = vis_image(img, ax=ax) # If there is no bounding box to display, visualize the image and exit. if len(bbox) == 0: return ax if instance_colors is None: # Red instance_colors = np.zeros((len(bbox), 3), dtype=np.float32) instance_colors[:, 0] = 255 instance_colors = np.array(instance_colors) for i, bb in enumerate(bbox): xy = (bb[1], bb[0]) height = bb[2] - bb[0] width = bb[3] - bb[1] color = instance_colors[i % len(instance_colors)] / 255 ax.add_patch( plt.Rectangle(xy, width, height, fill=False, edgecolor=color, linewidth=linewidth, alpha=alpha)) caption = [] if label is not None and label_names is not None: lb = label[i] if not (0 <= lb < len(label_names)): raise ValueError('No corresponding name is given') caption.append(label_names[lb]) if score is not None: sc = score[i] caption.append('{:.2f}'.format(sc)) if len(caption) > 0: ax.text(bb[1], bb[0], ': '.join(caption), style='italic', bbox={ 'facecolor': 'white', 'alpha': 0.7, 'pad': 10 }) return ax
def vis_bbox(img, bbox, label=None, score=None, label_names=None, instance_colors=None, alpha=0.9, linewidth=3.0, ax=None): """Visualize bounding boxes inside image. Example: >>> from chainercv.datasets import VOCBboxDataset >>> from chainercv.datasets import voc_bbox_label_names >>> from light_head_rcnn.visualizations import vis_bbox >>> import matplotlib.pyplot as plt >>> dataset = VOCBboxDataset() >>> img, bbox, label = dataset[60] >>> vis_bbox(img, bbox, label, ... label_names=voc_bbox_label_names) >>> plt.show() This example visualizes by displaying the same colors for bounding boxes assigned to the same labels. >>> from chainercv.datasets import VOCBboxDataset >>> from chainercv.datasets import voc_bbox_label_names >>> from chainercv.visualizations.colormap import voc_colormap >>> from light_head_rcnn.visualizations import vis_bbox >>> import matplotlib.pyplot as plt >>> dataset = VOCBboxDataset() >>> img, bbox, label = dataset[61] >>> colors = voc_colormap(label + 1) >>> vis_bbox(img, bbox, label, ... label_names=voc_bbox_label_names, ... instance_colors=colors) >>> plt.show() Args: img (~numpy.ndarray): An array of shape :math:`(3, height, width)`. This is in RGB format and the range of its value is :math:`[0, 255]`. If this is :obj:`None`, no image is displayed. bbox (~numpy.ndarray): An array of shape :math:`(R, 4)`, where :math:`R` is the number of bounding boxes in the image. Each element is organized by :math:`(y_{min}, x_{min}, y_{max}, x_{max})` in the second axis. label (~numpy.ndarray): An integer array of shape :math:`(R,)`. The values correspond to id for label names stored in :obj:`label_names`. This is optional. score (~numpy.ndarray): A float array of shape :math:`(R,)`. Each value indicates how confident the prediction is. This is optional. label_names (iterable of strings): Name of labels ordered according to label ids. If this is :obj:`None`, labels will be skipped. instance_colors (iterable of tuples): List of colors. Each color is RGB format and the range of its values is :math:`[0, 255]`. The :obj:`i`-th element is the color used to visualize the :obj:`i`-th instance. If :obj:`instance_colors` is :obj:`None`, the red is used for all boxes. alpha (float): The value which determines transparency of the bounding boxes. The range of this value is :math:`[0, 1]`. linewidth (float): The thickness of the edges of the bounding boxes. ax (matplotlib.axes.Axis): The visualization is displayed on this axis. If this is :obj:`None` (default), a new axis is created. Returns: ~matploblib.axes.Axes: Returns the Axes object with the plot for further tweaking. """ from matplotlib import pyplot as plt if label is not None and not len(bbox) == len(label): raise ValueError('The length of label must be same as that of bbox') if score is not None and not len(bbox) == len(score): raise ValueError('The length of score must be same as that of bbox') # Returns newly instantiated matplotlib.axes.Axes object if ax is None ax = vis_image(img, ax=ax) # If there is no bounding box to display, visualize the image and exit. if len(bbox) == 0: return ax n_inst = len(bbox) if instance_colors is None: instance_colors = voc_colormap(list(range(1, n_inst + 1))) instance_colors = np.array(instance_colors) for i, bb in enumerate(bbox): bb = np.round(bb).astype(np.int32) y_min, x_min, y_max, x_max = bb color = instance_colors[i % len(instance_colors)] / 255 ax.add_patch( plt.Rectangle((x_min, y_min), x_max - x_min, y_max - y_min, fill=False, edgecolor=color, linewidth=linewidth, alpha=alpha)) caption = [] if label is not None and label_names is not None: lb = label[i] if not (0 <= lb < len(label_names)): raise ValueError('No corresponding name is given') caption.append(label_names[lb]) if score is not None: sc = score[i] caption.append('{:.2f}'.format(sc)) if len(caption) > 0: ax.text(x_min, y_min, ': '.join(caption), style='italic', bbox={ 'facecolor': color, 'alpha': alpha }, fontsize=8, color='white') return ax
def vis_bbox(img, bbox, label=None, score=None, label_names=None, ax=None): """Visualize bounding boxes inside image. Example: >>> from chainercv.datasets import VOCDetectionDataset >>> from chainercv.datasets import voc_bbox_label_names >>> from chainercv.visualizations import vis_bbox >>> import matplotlib.pyplot as plot >>> dataset = VOCDetectionDataset() >>> img, bbox, label = dataset[60] >>> vis_bbox(img, bbox, label, ... label_names=voc_bbox_label_names) >>> plot.show() Args: img (~numpy.ndarray): An array of shape :math:`(3, height, width)`. This is in RGB format and the range of its value is :math:`[0, 255]`. bbox (~numpy.ndarray): An array of shape :math:`(R, 4)`, where :math:`R` is the number of bounding boxes in the image. Each element is organized by :math:`(y_{min}, x_{min}, y_{max}, x_{max})` in the second axis. label (~numpy.ndarray): An integer array of shape :math:`(R,)`. The values correspond to id for label names stored in :obj:`label_names`. This is optional. score (~numpy.ndarray): A float array of shape :math:`(R,)`. Each value indicates how confident the prediction is. This is optional. label_names (iterable of strings): Name of labels ordered according to label ids. If this is :obj:`None`, labels will be skipped. ax (matplotlib.axes.Axis): The visualization is displayed on this axis. If this is :obj:`None` (default), a new axis is created. Returns: ~matploblib.axes.Axes: Returns the Axes object with the plot for further tweaking. """ from matplotlib import pyplot as plot if label is not None and not len(bbox) == len(label): raise ValueError('The length of label must be same as that of bbox') if score is not None and not len(bbox) == len(score): raise ValueError('The length of score must be same as that of bbox') # Returns newly instantiated matplotlib.axes.Axes object if ax is None ax = vis_image(img, ax=ax) # If there is no bounding box to display, visualize the image and exit. if len(bbox) == 0: return ax for i, bb in enumerate(bbox): xy = (bb[1], bb[0]) height = bb[2] - bb[0] width = bb[3] - bb[1] ax.add_patch( plot.Rectangle(xy, width, height, fill=False, edgecolor='red', linewidth=3)) caption = [] if label is not None and label_names is not None: lb = label[i] if not (0 <= lb < len(label_names)): raise ValueError('No corresponding name is given') caption.append(label_names[lb]) if score is not None: sc = score[i] caption.append('{:.2f}'.format(sc)) if len(caption) > 0: ax.text(bb[1], bb[0], ': '.join(caption), style='italic', bbox={ 'facecolor': 'white', 'alpha': 0.7, 'pad': 10 }) return ax
def vis_bbox(img, bbox, roi, label=None, score=None, mask=None, label_names=None, ax=None, contour=False, labeldisplay=True): """Visualize bounding boxes inside image. Example: >>> from chainercv.datasets import VOCDetectionDataset >>> from chainercv.datasets import voc_bbox_label_names >>> from chainercv.visualizations import vis_bbox >>> import matplotlib.pyplot as plot >>> dataset = VOCDetectionDataset() >>> img, bbox, label = dataset[60] >>> vis_bbox(img, bbox, label, ... label_names=voc_bbox_label_names) >>> plot.show() Args: img (~numpy.ndarray): An array of shape :math:`(3, height, width)`. This is in RGB format and the range of its value is :math:`[0, 255]`. bbox (~numpy.ndarray): An array of shape :math:`(R, 4)`, where :math:`R` is the number of bounding boxes in the image. Each element is organized by :obj:`(y_min, x_min, y_max, x_max)` in the second axis. label (~numpy.ndarray): An integer array of shape :math:`(R,)`. The values correspond to id for label names stored in :obj:`label_names`. This is optional. score (~numpy.ndarray): A float array of shape :math:`(R,)`. Each value indicates how confident the prediction is. This is optional. label_names (iterable of strings): Name of labels ordered according to label ids. If this is :obj:`None`, labels will be skipped. ax (matplotlib.axes.Axis): The visualization is displayed on this axis. If this is :obj:`None` (default), a new axis is created. Returns: ~matploblib.axes.Axes: Returns the Axes object with the plot for further tweaking. """ from matplotlib import pyplot as plot if label is not None and not len(bbox) == len(label): raise ValueError('The length of label must be same as that of bbox') if score is not None and not len(bbox) == len(score): raise ValueError('The length of score must be same as that of bbox') # Returns newly instantiated matplotlib.axes.Axes object if ax is None ax = vis_image(img, ax=ax) # If there is no bounding box to display, visualize the image and exit. if len(bbox) == 0: return ax COLOR = [(1, 1, 0), (1, 0, 1), (0, 1, 1), (0, 0, 1), (0, 1, 0), (1, 0, 0), (0.1, 1, 0.2)] for i, (bb, r) in enumerate(zip(bbox, roi)): #print(label[i]) #if label[i] >1: # continue xy = (bb[1], bb[0]) height = int(bb[2]) - int(bb[0]) width = int(bb[3]) - int(bb[1]) ax.add_patch( plot.Rectangle(xy, width, height, fill=False, edgecolor='red', linewidth=1)) if mask is not None: M = mask[i] padded_mask = np.zeros((img.shape[2], img.shape[1]), dtype=np.uint8) resized_mask = cv2.resize(mask[i].T * 255, (height, width)) padded_mask[int(bb[1]):int(bb[3]), int(bb[0]):int(bb[2])] = resized_mask Mcontours = find_contours(padded_mask / 255, 0.3) for verts in Mcontours: p = Polygon(verts, facecolor="none", edgecolor=[1, 1, 1]) #print(M) caption = list() for my in range(14): for mx in range(14): mxy = (r[1] + (r[3] - r[1]) / 14 * mx, r[0] + (r[2] - r[0]) / 14 * my) Mcolor = np.clip((M[my, mx]) * 1, 0, 0.5) #print(Mcolor) ax.add_patch( plot.Rectangle(mxy, int((r[3] - r[1]) / 14) + 1, int((r[2] - r[0]) / 14) + 1, fill=True, linewidth=0, facecolor=COLOR[i % len(COLOR)], alpha=Mcolor)) if contour: ax.add_patch(p) if label is not None and label_names is not None: lb = label[i] print(lb) if not (0 <= lb < len(label_names)): raise ValueError('No corresponding name is given') caption.append(label_names[lb]) if score is not None: sc = score[i] caption.append('{:.2f}'.format(sc)) if len(caption) > 0 and labeldisplay: ax.text(bb[1], bb[0], ': '.join(caption), style='italic', fontsize=8, bbox={ 'facecolor': 'white', 'alpha': 0.7, 'pad': 10 }) return ax
def vis_keypoint_coco(img, point, visible=None, point_score=None, thresh=2, markersize=3, linewidth=1, ax=None): """Visualize keypoints organized as in COCO. Example: >>> from chainercv.datasets import COCOKeypointDataset >>> from chainercv.visualizations import vis_keypoint_coco >>> import matplotlib.pyplot as plt >>> data = COCOKeypointDataset(split='val') >>> img, point, visible = data[10][:3] >>> vis_keypoint_coco(img, point, visible) >>> plt.show() Args: img (~numpy.ndarray): See the table below. If this is :obj:`None`, no image is displayed. point (~numpy.ndarray): See the table below. visible (~numpy.ndarray): See the table below. If this is :obj:`None`, all points are assumed to be visible. point_score (~numpy.ndarray): See the table below. If this is :obj:`None`, the confidence of all points is infinitely large. thresh (float): Points with confidence below :obj:`thresh` are not visualized. markersize (float): The size of vertices. linewidth (float): The thickness of edges. ax (matplotlib.axes.Axis): The visualization is displayed on this axis. If this is :obj:`None` (default), a new axis is created. .. csv-table:: :header: name, shape, dtype, format :obj:`img`, ":math:`(3, H, W)`", :obj:`float32`, \ "RGB, :math:`[0, 255]`" :obj:`point`, ":math:`(R, K, 2)`", :obj:`float32`, \ ":math:`(y, x)`" :obj:`visible`, ":math:`(R, K)`", :obj:`bool`, \ "true when a keypoint is visible." :obj:`point_score`, ":math:`(R, K)`", :obj:`float32`, -- Returns: ~matploblib.axes.Axes: Returns the Axes object with the plot for further tweaking. """ from matplotlib import pyplot as plt # Returns newly instantiated matplotlib.axes.Axes object if ax is None ax = vis_image(img, ax=ax) cmap = plt.get_cmap('rainbow') colors = [cmap(i) for i in np.linspace(0, 1, len(coco_point_skeleton) + 2)] if point_score is None: point_score = np.inf * np.ones(point.shape[:2], dtype=np.float32) if point_score.shape != point.shape[:2]: raise ValueError('Mismatch in the number of instances or joints.') if point.shape[1:] != (len(coco_keypoint_names[human_id]), 2): raise ValueError('point has invisible shape') if visible is not None: if visible.dtype != np.bool: raise ValueError('The dtype of `visible` should be np.bool') if visible.shape != point.shape[:2]: raise ValueError('Mismatch in the number of instances or joints.') for i, vld in enumerate(visible): point_score[i, np.logical_not(vld)] = -np.inf for pnt, pnt_sc in zip(point, point_score): for l in range(len(coco_point_skeleton)): i0 = coco_point_skeleton[l][0] i1 = coco_point_skeleton[l][1] s0 = pnt_sc[i0] y0 = pnt[i0, 0] x0 = pnt[i0, 1] s1 = pnt_sc[i1] y1 = pnt[i1, 0] x1 = pnt[i1, 1] if s0 > thresh and s1 > thresh: line = ax.plot([x0, x1], [y0, y1]) plt.setp(line, color=colors[l], linewidth=linewidth, alpha=0.7) if s0 > thresh: ax.plot(x0, y0, '.', color=colors[l], markersize=markersize, alpha=0.7) if s1 > thresh: ax.plot(x1, y1, '.', color=colors[l], markersize=markersize, alpha=0.7) # for better visualization, add mid shoulder / mid hip mid_shoulder = ( pnt[coco_keypoint_names[human_id].index('right_shoulder'), :2] + pnt[coco_keypoint_names[human_id].index('left_shoulder'), :2]) / 2 mid_shoulder_sc = np.minimum( pnt_sc[coco_keypoint_names[human_id].index('right_shoulder')], pnt_sc[coco_keypoint_names[human_id].index('left_shoulder')]) mid_hip = ( pnt[coco_keypoint_names[human_id].index('right_hip'), :2] + pnt[coco_keypoint_names[human_id].index('left_hip'), :2]) / 2 mid_hip_sc = np.minimum( pnt_sc[coco_keypoint_names[human_id].index('right_hip')], pnt_sc[coco_keypoint_names[human_id].index('left_hip')]) if (mid_shoulder_sc > thresh and pnt_sc[coco_keypoint_names[human_id].index('nose')] > thresh): y = [ mid_shoulder[0], pnt[coco_keypoint_names[human_id].index('nose'), 0] ] x = [ mid_shoulder[1], pnt[coco_keypoint_names[human_id].index('nose'), 1] ] line = ax.plot(x, y) plt.setp(line, color=colors[len(coco_point_skeleton)], linewidth=linewidth, alpha=0.7) if (mid_shoulder_sc > thresh and mid_hip_sc > thresh): y = [mid_shoulder[0], mid_hip[0]] x = [mid_shoulder[1], mid_hip[1]] line = ax.plot(x, y) plt.setp(line, color=colors[len(coco_point_skeleton) + 1], linewidth=linewidth, alpha=0.7) return ax
def vis_bbox(img, bbox, label=None, score=None, label_names=None, ax=None): """Visualize bounding boxes inside image. Example: >>> from chainercv.datasets import VOCDetectionDataset >>> from chainercv.datasets import voc_bbox_label_names >>> from chainercv.visualizations import vis_bbox >>> import matplotlib.pyplot as plot >>> dataset = VOCDetectionDataset() >>> img, bbox, label = dataset[60] >>> vis_bbox(img, bbox, label, ... label_names=voc_bbox_label_names) >>> plot.show() Args: img (~numpy.ndarray): An array of shape :math:`(3, height, width)`. This is in RGB format and the range of its value is :math:`[0, 255]`. bbox (~numpy.ndarray): An array of shape :math:`(R, 4)`, where :math:`R` is the number of bounding boxes in the image. Each element is organized by :math:`(y_{min}, x_{min}, y_{max}, x_{max})` in the second axis. label (~numpy.ndarray): An integer array of shape :math:`(R,)`. The values correspond to id for label names stored in :obj:`label_names`. This is optional. score (~numpy.ndarray): A float array of shape :math:`(R,)`. Each value indicates how confident the prediction is. This is optional. label_names (iterable of strings): Name of labels ordered according to label ids. If this is :obj:`None`, labels will be skipped. ax (matplotlib.axes.Axis): The visualization is displayed on this axis. If this is :obj:`None` (default), a new axis is created. Returns: ~matploblib.axes.Axes: Returns the Axes object with the plot for further tweaking. """ from matplotlib import pyplot as plot if label is not None and not len(bbox) == len(label): raise ValueError('The length of label must be same as that of bbox') if score is not None and not len(bbox) == len(score): raise ValueError('The length of score must be same as that of bbox') # Returns newly instantiated matplotlib.axes.Axes object if ax is None ax = vis_image(img, ax=ax) # If there is no bounding box to display, visualize the image and exit. if len(bbox) == 0: return ax for i, bb in enumerate(bbox): xy = (bb[1], bb[0]) height = bb[2] - bb[0] width = bb[3] - bb[1] ax.add_patch(plot.Rectangle( xy, width, height, fill=False, edgecolor='red', linewidth=3)) caption = [] if label is not None and label_names is not None: lb = label[i] if not (0 <= lb < len(label_names)): raise ValueError('No corresponding name is given') caption.append(label_names[lb]) if score is not None: sc = score[i] caption.append('{:.2f}'.format(sc)) if len(caption) > 0: ax.text(bb[1], bb[0], ': '.join(caption), style='italic', bbox={'facecolor': 'white', 'alpha': 0.7, 'pad': 10}) return ax
def vis_bbox(img, bbox, label=None, score=None, mask=None, label_names=None, ax=None, contour=False, labeldisplay=True): """Visualize bounding boxes inside image. Example: >>> from chainercv.datasets import VOCDetectionDataset >>> from chainercv.datasets import voc_bbox_label_names >>> from chainercv.visualizations import vis_bbox >>> import matplotlib.pyplot as plot >>> dataset = VOCDetectionDataset() >>> img, bbox, label = dataset[60] >>> vis_bbox(img, bbox, label, ... label_names=voc_bbox_label_names) >>> plot.show() Args: img (~numpy.ndarray): An array of shape :math:`(3, height, width)`. This is in RGB format and the range of its value is :math:`[0, 255]`. bbox (~numpy.ndarray): An array of shape :math:`(R, 4)`, where :math:`R` is the number of bounding boxes in the image. Each element is organized by :obj:`(y_min, x_min, y_max, x_max)` in the second axis. label (~numpy.ndarray): An integer array of shape :math:`(R,)`. The values correspond to id for label names stored in :obj:`label_names`. This is optional. score (~numpy.ndarray): A float array of shape :math:`(R,)`. Each value indicates how confident the prediction is. This is optional. label_names (iterable of strings): Name of labels ordered according to label ids. If this is :obj:`None`, labels will be skipped. ax (matplotlib.axes.Axis): The visualization is displayed on this axis. If this is :obj:`None` (default), a new axis is created. Returns: ~matploblib.axes.Axes: Returns the Axes object with the plot for further tweaking. """ from matplotlib import pyplot as plot if label is not None and not len(bbox) == len(label): raise ValueError('The length of label must be same as that of bbox') if score is not None and not len(bbox) == len(score): raise ValueError('The length of score must be same as that of bbox') # alpha-blend the masks COLOR = [(1, 1, 0), (1, 0, 1), (0, 1, 1), (0, 0, 1), (0, 1, 0), (1, 0, 0), (0.1, 1, 0.2)] dst = img.astype(float) for i, m in enumerate(mask): alpha = np.tile(np.round(m), (3, 1, 1)).astype(float) * 0.4 src1 = np.ones(dst.shape).astype(float) for j, col in enumerate(COLOR[i % len(COLOR)]): src1[j] *= col * 255 dst = cv2.multiply(src1, alpha) + cv2.multiply(dst, 1 - alpha) # Returns newly instantiated matplotlib.axes.Axes object if ax is None ax = vis_image(dst, ax=ax) # If there is no bounding box to display, visualize the image and exit. if len(bbox) == 0: return ax # add boxes, contours and labels for i, bb in enumerate(bbox): # boxes xy = (bb[1], bb[0]) height = int(bb[2]) - int(bb[0]) width = int(bb[3]) - int(bb[1]) ax.add_patch( plot.Rectangle(xy, width, height, fill=False, edgecolor='red', linewidth=1)) # contours if contour: Mcontours = find_contours(mask[i].T, 0.5) for verts in Mcontours: p = Polygon(verts, facecolor="none", edgecolor=[0.5, 0.5, 0.5]) ax.add_patch(p) #labels caption = list() if label is not None and label_names is not None: lb = label[i] print(lb) if not (0 <= lb < len(label_names)): raise ValueError('No corresponding name is given') caption.append(label_names[lb]) if score is not None: sc = score[i] caption.append('{:.2f}'.format(sc)) if len(caption) > 0 and labeldisplay: ax.text(bb[1], bb[0], ': '.join(caption), style='italic', fontsize=8, color='white' ) #'facecolor': 'white', 'alpha': 0.7, 'pad': 10}) return ax