Exemple #1
0
def visualize_image(image, boxes, classes, scores, id_mapping):
  """Visualizes a list of images.

  Args:
    image: a image with shape [H, W, C].
    boxes: a box prediction with shape [N, 4] ordered [ymin, xmin, ymax, xmax].
    classes: a class prediction with shape [N].
    scores: A list of float value with shape [N].
    id_mapping: a dictionary from class id to name.

  Returns:
    output_image: an output image with annotated boxes and classes.
  """
  category_index = {k: {'id': k, 'name': id_mapping[k]} for k in id_mapping}
  img = np.array(image)
  vis_utils.visualize_boxes_and_labels_on_image_array(
      img,
      boxes,
      classes,
      scores,
      category_index,
      min_score_thresh=0.5,
      instance_masks=None,
      use_normalized_coordinates=False,
      line_thickness=4)
  return img
Exemple #2
0
def visualize_image(image,
                    boxes,
                    classes,
                    scores,
                    id_mapping=None,
                    min_score_thresh=0.01,
                    max_boxes_to_draw=1000,
                    line_thickness=2,
                    **kwargs):
  """Visualizes a given image.

  Args:
    image: a image with shape [H, W, C].
    boxes: a box prediction with shape [N, 4] ordered [ymin, xmin, ymax, xmax].
    classes: a class prediction with shape [N].
    scores: A list of float value with shape [N].
    id_mapping: a dictionary from class id to name.
    min_score_thresh: minimal score for showing. If claass probability is below
      this threshold, then the object will not show up.
    max_boxes_to_draw: maximum bounding box to draw.
    line_thickness: how thick is the bounding box line.
    **kwargs: extra parameters.

  Returns:
    output_image: an output image with annotated boxes and classes.
  """
  id_mapping = parse_label_id_mapping(id_mapping)
  category_index = {k: {'id': k, 'name': id_mapping[k]} for k in id_mapping}
  img = np.array(image)

#changes
  indices = np.argwhere((classes ==31) | (classes==33) | (classes==44) |
  (classes==47) | (classes==48) | (classes==49) | (classes==50) | (classes==51)
  | (classes==74)| (classes==75)| (classes==77)| (classes==84)| (classes==86)
  | (classes==87)| (classes==88)| (classes==89)| (classes==90))
  boxes = np.squeeze(boxes[indices], axis=1) # to prevent errors made by nd.array of size 1 nd.array
  scores = np.squeeze(scores[indices], axis=1)
  classes = np.squeeze(classes[indices], axis=1)
#ends

  vis_utils.visualize_boxes_and_labels_on_image_array(
      img,
      boxes,
      classes,
      scores,
      category_index,
      min_score_thresh=min_score_thresh,
      max_boxes_to_draw=max_boxes_to_draw,
      line_thickness=line_thickness,
      **kwargs)
  return img
 def test_visualize_boxes_and_labels_on_image_array(self):
     ori_image = np.ones([360, 480, 3], dtype=np.int32) * 255
     test_image = np.ones([360, 480, 3], dtype=np.int32) * 255
     detections = np.array([[0.8, 0.1, 0.9, 0.1, 1., 0.1],
                            [0.1, 0.3, 0.8, 0.7, 1., 0.6]])
     labelmap = {1: {'id': 1, 'name': 'cat'}, 2: {'id': 2, 'name': 'dog'}}
     vis_utils.visualize_boxes_and_labels_on_image_array(
         test_image,
         detections[:, :4],
         detections[:, 4].astype(np.int32),
         detections[:, 5],
         labelmap,
         track_ids=None,
         use_normalized_coordinates=True,
         max_boxes_to_draw=1,
         min_score_thresh=0.2,
         agnostic_mode=False,
         line_thickness=8)
     self.assertGreater(np.abs(np.sum(test_image - ori_image)), 0)
    def visualize(self):
        """save tfrecords images with bounding boxes."""
        vis_ds = self.input_fn(params=self.params)
        data = next(iter(vis_ds))  # iterable.
        images = data[0]
        gt_data = data[1]['groundtruth_data']

        # scales
        scale_to_org = data[1]['image_scales']
        scales = 1.0 / scale_to_org
        offset = tf.constant([0.485, 0.456, 0.406])
        offset = tf.reshape(offset, (1, 1, -1))
        scale_image = tf.constant([0.229, 0.224, 0.225])
        scale_image = tf.reshape(scale_image, (1, 1, -1))

        logging.info('Visualizing TfRecords %s', FLAGS.file_pattern)
        for i, zip_data in enumerate(zip(gt_data, images, scales)):
            gt, image, scale = zip_data
            boxes = gt[:, :4]
            boxes = boxes[np.any(boxes > 0, axis=1)].numpy()
            if boxes.shape[0] > 0:
                classes = gt[:boxes.shape[0], -1].numpy()
                try:
                    category_index = {
                        idx: {
                            'id': idx,
                            'name': self.cls_to_label[idx]
                        }
                        for idx in np.asarray(classes, dtype=np.int)
                    }
                except Exception:  # pylint: disable=broad-except
                    category_index = {}

                # unnormalize image.
                image *= scale_image
                image += offset

                # 0-255. range
                image = np.asarray(image.numpy() * 255., dtype=np.uint8)

                # scale to image_size
                boxes *= scale.numpy()

                image = vis_utils.visualize_boxes_and_labels_on_image_array(
                    image,
                    boxes=boxes,
                    classes=classes.astype(int),
                    scores=np.ones(boxes.shape[0]),
                    category_index=category_index,
                    line_thickness=2,
                    skip_scores=True)
                image = Image.fromarray(image)
                image.save(
                    os.path.join(FLAGS.save_samples_dir, f'sample{i}.jpg'))
def visualize_image(image,
                    boxes,
                    classes,
                    scores,
                    label_map=None,
                    min_score_thresh=0.01,
                    max_boxes_to_draw=1000,
                    line_thickness=2,
                    **kwargs):
    """Visualizes a given image.

  Args:
    image: a image with shape [H, W, C].
    boxes: a box prediction with shape [N, 4] ordered [ymin, xmin, ymax, xmax].
    classes: a class prediction with shape [N].
    scores: A list of float value with shape [N].
    label_map: a dictionary from class id to name.
    min_score_thresh: minimal score for showing. If claass probability is below
      this threshold, then the object will not show up.
    max_boxes_to_draw: maximum bounding box to draw.
    line_thickness: how thick is the bounding box line.
    **kwargs: extra parameters.

  Returns:
    output_image: an output image with annotated boxes and classes.
  """
    label_map = label_util.get_label_map(label_map or 'coco')
    category_index = {k: {'id': k, 'name': label_map[k]} for k in label_map}
    img = np.array(image)
    vis_utils.visualize_boxes_and_labels_on_image_array(
        img,
        boxes,
        classes,
        scores,
        category_index,
        min_score_thresh=min_score_thresh,
        max_boxes_to_draw=max_boxes_to_draw,
        line_thickness=line_thickness,
        **kwargs)
    return img
Exemple #6
0
def visualize_image(image,
                    boxes,
                    classes,
                    scores,
                    id_mapping=None,
                    min_score_thresh=anchors.MIN_SCORE_THRESH,
                    max_boxes_to_draw=anchors.MAX_DETECTIONS_PER_IMAGE,
                    line_thickness=2,
                    **kwargs):
    """Visualizes a given image.

  Args:
    image: a image with shape [H, W, C].
    boxes: a box prediction with shape [N, 4] ordered [ymin, xmin, ymax, xmax].
    classes: a class prediction with shape [N].
    scores: A list of float value with shape [N].
    id_mapping: a dictionary from class id to name.
    min_score_thresh: minimal score for showing. If claass probability is below
      this threshold, then the object will not show up.
    max_boxes_to_draw: maximum bounding box to draw.
    line_thickness: how thick is the bounding box line.
    **kwargs: extra parameters.

  Returns:
    output_image: an output image with annotated boxes and classes.
  """
    id_mapping = parse_label_id_mapping(id_mapping)
    category_index = {k: {'id': k, 'name': id_mapping[k]} for k in id_mapping}
    img = np.array(image)
    vis_utils.visualize_boxes_and_labels_on_image_array(
        img,
        boxes,
        classes,
        scores,
        category_index,
        min_score_thresh=min_score_thresh,
        max_boxes_to_draw=max_boxes_to_draw,
        line_thickness=line_thickness,
        **kwargs)
    return img
Exemple #7
0
def visualize_image(image,
                    boxes,
                    classes,
                    scores,
                    id_mapping,
                    min_score_thresh=0.2,
                    max_boxes_to_draw=50,
                    line_thickness=4,
                    **kwargs):

    category_index = {k: {'id': k, 'name': id_mapping[k]} for k in id_mapping}
    img = np.array(image)
    vis_utils.visualize_boxes_and_labels_on_image_array(
        img,
        boxes,
        classes,
        scores,
        category_index,
        min_score_thresh=min_score_thresh,
        max_boxes_to_draw=max_boxes_to_draw,
        line_thickness=line_thickness,
        **kwargs)
    return img