예제 #1
0
def draw_side_by_side_evaluation_image(eval_dict,
                                       category_index,
                                       max_boxes_to_draw=20,
                                       min_score_thresh=0.2,
                                       use_normalized_coordinates=True):
    """Creates a side-by-side image with detections and groundtruth.

  Bounding boxes (and instance masks, if available) are visualized on both
  subimages.

  Args:
    eval_dict: The evaluation dictionary returned by
      eval_util.result_dict_for_batched_example() or
      eval_util.result_dict_for_single_example().
    category_index: A category index (dictionary) produced from a labelmap.
    max_boxes_to_draw: The maximum number of boxes to draw for detections.
    min_score_thresh: The minimum score threshold for showing detections.
    use_normalized_coordinates: Whether to assume boxes and kepoints are in
      normalized coordinates (as opposed to absolute coordiantes).
      Default is True.

  Returns:
    A list of [1, H, 2 * W, C] uint8 tensor. The subimage on the left
      corresponds to detections, while the subimage on the right corresponds to
      groundtruth.
  """
    detection_fields = fields.DetectionResultFields()
    input_data_fields = fields.InputDataFields()

    images_with_detections_list = []

    # Add the batch dimension if the eval_dict is for single example.
    if len(eval_dict[detection_fields.detection_classes].shape) == 1:
        for key in eval_dict:
            if key != input_data_fields.original_image:
                eval_dict[key] = tf.expand_dims(eval_dict[key], 0)

    for indx in range(eval_dict[input_data_fields.original_image].shape[0]):
        instance_masks = None
        if detection_fields.detection_masks in eval_dict:
            instance_masks = tf.cast(
                tf.expand_dims(
                    eval_dict[detection_fields.detection_masks][indx], axis=0),
                tf.uint8)
        keypoints = None
        if detection_fields.detection_keypoints in eval_dict:
            keypoints = tf.expand_dims(
                eval_dict[detection_fields.detection_keypoints][indx], axis=0)
        groundtruth_instance_masks = None
        if input_data_fields.groundtruth_instance_masks in eval_dict:
            groundtruth_instance_masks = tf.cast(
                tf.expand_dims(eval_dict[
                    input_data_fields.groundtruth_instance_masks][indx],
                               axis=0), tf.uint8)

        images_with_detections = draw_bounding_boxes_on_image_tensors(
            tf.expand_dims(eval_dict[input_data_fields.original_image][indx],
                           axis=0),
            tf.expand_dims(eval_dict[detection_fields.detection_boxes][indx],
                           axis=0),
            tf.expand_dims(eval_dict[detection_fields.detection_classes][indx],
                           axis=0),
            tf.expand_dims(eval_dict[detection_fields.detection_scores][indx],
                           axis=0),
            category_index,
            original_image_spatial_shape=tf.expand_dims(eval_dict[
                input_data_fields.original_image_spatial_shape][indx],
                                                        axis=0),
            true_image_shape=tf.expand_dims(
                eval_dict[input_data_fields.true_image_shape][indx], axis=0),
            instance_masks=instance_masks,
            keypoints=keypoints,
            max_boxes_to_draw=max_boxes_to_draw,
            min_score_thresh=min_score_thresh,
            use_normalized_coordinates=use_normalized_coordinates)
        images_with_groundtruth = draw_bounding_boxes_on_image_tensors(
            tf.expand_dims(eval_dict[input_data_fields.original_image][indx],
                           axis=0),
            tf.expand_dims(
                eval_dict[input_data_fields.groundtruth_boxes][indx], axis=0),
            tf.expand_dims(
                eval_dict[input_data_fields.groundtruth_classes][indx],
                axis=0),
            tf.expand_dims(tf.ones_like(
                eval_dict[input_data_fields.groundtruth_classes][indx],
                dtype=tf.float32),
                           axis=0),
            category_index,
            original_image_spatial_shape=tf.expand_dims(eval_dict[
                input_data_fields.original_image_spatial_shape][indx],
                                                        axis=0),
            true_image_shape=tf.expand_dims(
                eval_dict[input_data_fields.true_image_shape][indx], axis=0),
            instance_masks=groundtruth_instance_masks,
            keypoints=None,
            max_boxes_to_draw=None,
            min_score_thresh=0.0,
            use_normalized_coordinates=use_normalized_coordinates)
        images_with_detections_list.append(
            tf.concat([images_with_detections, images_with_groundtruth],
                      axis=2))
    return images_with_detections_list
def draw_side_by_side_evaluation_image(eval_dict,
                                       category_index,
                                       max_boxes_to_draw=20,
                                       min_score_thresh=0.2):
    """Creates a side-by-side image with detections and groundtruth.

  Bounding boxes (and instance masks, if available) are visualized on both
  subimages.

  Args:
    eval_dict: The evaluation dictionary returned by
      eval_util.result_dict_for_single_example().
    category_index: A category index (dictionary) produced from a labelmap.
    max_boxes_to_draw: The maximum number of boxes to draw for detections.
    min_score_thresh: The minimum score threshold for showing detections.

  Returns:
    A [1, H, 2 * W, C] uint8 tensor. The subimage on the left corresponds to
      detections, while the subimage on the right corresponds to groundtruth.
  """
    detection_fields = fields.DetectionResultFields()
    input_data_fields = fields.InputDataFields()
    instance_masks = None
    if detection_fields.detection_masks in eval_dict:
        instance_masks = tf.cast(
            tf.expand_dims(eval_dict[detection_fields.detection_masks],
                           axis=0), tf.uint8)
    keypoints = None
    if detection_fields.detection_keypoints in eval_dict:
        keypoints = tf.expand_dims(
            eval_dict[detection_fields.detection_keypoints], axis=0)
    groundtruth_instance_masks = None
    if input_data_fields.groundtruth_instance_masks in eval_dict:
        groundtruth_instance_masks = tf.cast(
            tf.expand_dims(
                eval_dict[input_data_fields.groundtruth_instance_masks],
                axis=0), tf.uint8)
    images_with_detections = draw_bounding_boxes_on_image_tensors(
        eval_dict[input_data_fields.original_image],
        tf.expand_dims(eval_dict[detection_fields.detection_boxes], axis=0),
        tf.expand_dims(eval_dict[detection_fields.detection_classes], axis=0),
        tf.expand_dims(eval_dict[detection_fields.detection_scores], axis=0),
        category_index,
        instance_masks=instance_masks,
        keypoints=keypoints,
        max_boxes_to_draw=max_boxes_to_draw,
        min_score_thresh=min_score_thresh)
    images_with_groundtruth = draw_bounding_boxes_on_image_tensors(
        eval_dict[input_data_fields.original_image],
        tf.expand_dims(eval_dict[input_data_fields.groundtruth_boxes], axis=0),
        tf.expand_dims(eval_dict[input_data_fields.groundtruth_classes],
                       axis=0),
        tf.expand_dims(
            tf.ones_like(eval_dict[input_data_fields.groundtruth_classes],
                         dtype=tf.float32),
            axis=0),
        category_index,
        instance_masks=groundtruth_instance_masks,
        keypoints=None,
        max_boxes_to_draw=None,
        min_score_thresh=0.0)
    return tf.concat([images_with_detections, images_with_groundtruth], axis=2)