【问题标题】:Print the detected classes and scores when using TF Object Detection API使用 TF Object Detection API 时打印检测到的类和分数
【发布时间】:2020-09-16 00:33:45
【问题描述】:

您好,我正在寻找一种方法来打印检测到的类别和分数,同时使用 object_detection_tutorial 进行对象检测。 这里的大多数解决方案都适用于 Tensorflow 1,不再适用。

我在 StackOverflow 上找到了一个 Solution,但遗憾的是它只打印出检测到的对象之一。我不知道如何修改代码以获取图像中所有检测到的对象的分数。

这是我在这里找到的解决方案中给出的代码

def get_classes_name_and_scores(
    boxes,
    classes,
    scores,
    category_index,
    max_boxes_to_draw=20,
    min_score_thresh=.9): # returns bigger than 90% precision
display_str = {}
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:
        if classes[i] in six.viewkeys(category_index):
            display_str['name'] = category_index[classes[i]]['name']
            display_str['score'] = '{}%'.format(int(100 * scores[i]))

return display_str

我用这段代码打印出来

def show_inference(model, image_path):
  # the array based representation of the image will be used later in order to prepare the
  # result image with boxes and labels on it.
  image_np = np.array(Image.open(image_path))
  # Actual detection.
  output_dict = run_inference_for_single_image(model, image_np)
  # Visualization of the results of a detection.
  vis_util.visualize_boxes_and_labels_on_image_array(
    image_np,
    output_dict['detection_boxes'],
    output_dict['detection_classes'],
    output_dict['detection_scores'],
    category_index,
    instance_masks=output_dict.get('detection_masks_reframed', None),
    use_normalized_coordinates=True,
    line_thickness=8)

  # Print the Name and Score of each detected Object
  print(get_classes_name_and_scores(
    output_dict['detection_boxes'],
    output_dict['detection_classes'],
    output_dict['detection_scores'],
    category_index))

  display(Image.fromarray(image_np))

【问题讨论】:

    标签: python object-detection tensorflow2.0 object-detection-api tensorflow2.x


    【解决方案1】:

    每次循环时,这两行都会被覆盖,所以之前的内容就消失了:

    display_str['name'] = category_index[classes[i]]['name']
    display_str['score'] = '{}%'.format(int(100 * scores[i]))
    

    我假设您想记录循环触发这些行的每个实例。就个人而言,为此我会将结果添加到列表中并返回:

    display_str_list = []
        ### your loop code
        display_str_dict = {
            'name': category_index[classes[i]]['name'],
            'score': '{}%'.format(int(100 * scores[i])),
        }
        display_str_list.append(display_str_dict)
    return display_str_list
    

    【讨论】:

    • 非常感谢!!唯一的问题是列表中的分数与检测方块中的分数不同。就像我得到的结果一样:{'name': 'Dog', 'score': '98%'} 但图片中有一个正方形:Dog 99.0% 你知道是什么原因造成的吗?
    猜你喜欢
    • 2022-10-08
    • 2018-08-16
    • 2019-06-21
    • 2018-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 2018-01-22
    相关资源
    最近更新 更多