我使用Tensorflow label_image示例https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/label_image来检测和定位image中的10个类对象。现在我想使用tensorflow :: ops :: NonmaxSuppression删除一个对象的多预测矩形。我不知道如何在我的代码中使用它。请帮我解决。 like this picture
ezeke123 回答:如何在label_image tensorflow cpp示例中使用tensorflow :: ops :: NonMaxSuppression删除为一个对象预测的多个矩形?
您可以使用以下功能绘制超出阈值的框,我从Tensorflow对象检测API中获取它。
def visualize_boxes_and_labels_on_image_array(
image,boxes,classes,scores,category_index,instance_masks=None,instance_boundaries=None,keypoints=None,track_ids=None,use_normalized_coordinates=False,max_boxes_to_draw=20,min_score_thresh=.5,agnostic_mode=False,line_thickness=4,groundtruth_box_visualization_color='black',skip_scores=False,skip_labels=False,skip_track_ids=False):
"""Overlay labeled boxes on an image with formatted scores and label names.
This function groups boxes that correspond to the same location
and creates a display string for each detection and overlays these
on the image. Note that this function modifies the image in place,and returns
that same image.
Args:
image: uint8 numpy array with shape (img_height,img_width,3)
boxes: a numpy array of shape [N,4]
classes: a numpy array of shape [N]. Note that class indices are 1-based,and match the keys in the label map.
scores: a numpy array of shape [N] or None. If scores=None,then
this function assumes that the boxes to be plotted are groundtruth
boxes and plot all boxes as black with no classes or scores.
category_index: a dict containing category dictionaries (each holding
category index `id` and category name `name`) keyed by category indices.
instance_masks: a numpy array of shape [N,image_height,image_width] with
values ranging between 0 and 1,can be None.
instance_boundaries: a numpy array of shape [N,image_width]
with values ranging between 0 and 1,can be None.
keypoints: a numpy array of shape [N,num_keypoints,2],can
be None
track_ids: a numpy array of shape [N] with unique track ids. If provided,color-coding of boxes will be determined by these ids,and not the class
indices.
use_normalized_coordinates: whether boxes is to be interpreted as
normalized coordinates or not.
max_boxes_to_draw: maximum number of boxes to visualize. If None,draw
all boxes.
min_score_thresh: minimum score threshold for a box to be visualized
agnostic_mode: boolean (default: False) controlling whether to evaluate in
class-agnostic mode or not. This mode will display scores but ignore
classes.
line_thickness: integer (default: 4) controlling line width of the boxes.
groundtruth_box_visualization_color: box color for visualizing groundtruth
boxes
skip_scores: whether to skip score when drawing a single detection
skip_labels: whether to skip label when drawing a single detection
skip_track_ids: whether to skip track id when drawing a single detection
Returns:
uint8 numpy array with shape (img_height,3) with overlaid boxes.
"""
# Create a display string (and color) for every box location,group any boxes
# that correspond to the same location.
box_to_display_str_map = collections.defaultdict(list)
box_to_color_map = collections.defaultdict(str)
box_to_instance_masks_map = {}
box_to_instance_boundaries_map = {}
box_to_keypoints_map = collections.defaultdict(list)
box_to_track_ids_map = {}
if not max_boxes_to_draw:
max_boxes_to_draw = boxes.shape[0]
for i in range(min(max_boxes_to_draw,boxes.shape[0])):
if scores is None or scores[i] > min_score_thresh:
box = tuple(boxes[i].tolist())
if instance_masks is not None:
box_to_instance_masks_map[box] = instance_masks[i]
if instance_boundaries is not None:
box_to_instance_boundaries_map[box] = instance_boundaries[i]
if keypoints is not None:
box_to_keypoints_map[box].extend(keypoints[i])
if track_ids is not None:
box_to_track_ids_map[box] = track_ids[i]
if scores is None:
box_to_color_map[box] = groundtruth_box_visualization_color
else:
display_str = ''
if not skip_labels:
if not agnostic_mode:
if classes[i] in six.viewkeys(category_index):
class_name = category_index[classes[i]]['name']
else:
class_name = 'N/A'
display_str = str(class_name)
if not skip_scores:
if not display_str:
display_str = '{}%'.format(int(100*scores[i]))
else:
display_str = '{}: {}%'.format(display_str,int(100*scores[i]))
if not skip_track_ids and track_ids is not None:
if not display_str:
display_str = 'ID {}'.format(track_ids[i])
else:
display_str = '{}: ID {}'.format(display_str,track_ids[i])
box_to_display_str_map[box].append(display_str)
if agnostic_mode:
box_to_color_map[box] = 'DarkOrange'
elif track_ids is not None:
prime_multipler = _get_multiplier_for_color_randomness()
box_to_color_map[box] = STANDARD_COLORS[
(prime_multipler * track_ids[i]) % len(STANDARD_COLORS)]
else:
box_to_color_map[box] = STANDARD_COLORS[
classes[i] % len(STANDARD_COLORS)]
# Draw all boxes onto image.
for box,color in box_to_color_map.items():
ymin,xmin,ymax,xmax = box
if instance_masks is not None:
draw_mask_on_image_array(
image,box_to_instance_masks_map[box],color=color
)
if instance_boundaries is not None:
draw_mask_on_image_array(
image,box_to_instance_boundaries_map[box],color='red',alpha=1.0
)
draw_bounding_box_on_image_array(
image,ymin,xmax,color=color,thickness=line_thickness,display_str_list=box_to_display_str_map[box],use_normalized_coordinates=use_normalized_coordinates)
if keypoints is not None:
draw_keypoints_on_image_array(
image,box_to_keypoints_map[box],radius=line_thickness / 2,use_normalized_coordinates=use_normalized_coordinates)
return image