输出 score , class 和 id Extraction using TensorFlow object detection

Output score , class and id Extraction using TensorFlow object detection

如何提取由用于对象检测的 Tensorflow 模型生成的对象、对象 class、图像中检测到的对象 ID 的输出分数?

我想将所有这些详细信息存储到单独的变量中,以便以后可以将它们存储在数据库中。

使用与此 link 中相同的代码 https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb

请帮我解决这个问题。

我试过了

print(str(output_dict['detection_classes'][0]), ":", str(output_dict['detection_scores'][0]))

这行得通,并以最高的概率给出 class 的对象 ID 和分数。但我也想提取 class 名称以及图像中存在的所有对象的分数、ID 和名称

输出示例: There are two dogs in the image . When I print out the result I get the id and score for the object with the highest probability[94% in this case] i want to print the object name too and also similar details for all other objects in the images

它给你 class 最高分,因为输出张量是从最高分到最低分排序的,你通过索引到第一个元素 [0] 来要求最高分。

查看 object_detection/inference/detection_inference 寻找灵感。

至于 class 名称,您可以使用 label map 创建类别索引字典以将 class id 转换为名称。

获取class名字, 您的标签地图应该能够在这里提供帮助。

from object_detection.utils import label_map_util

label_map_path = os.path.join(annotations_dir, 'label_map.pbtxt')
label_map_dict = label_map_util.get_label_map_dict(label_map_path)
label_map_dict_number_to_name = {v: k for k, v in label_map_dict.iteritems()}
class_number = output_dict['detection_classes'][index]
class_name = label_map_dict_number_to_name[class_number]

请粘贴您的代码,以便我们找出为什么 y 中只有一个框

您可能需要一些有关 tensorflow 对象检测的知识背景,这里的简短快速解决方案可能就是您所期望的方式:

with detection_graph.as_default():
  with tf.Session(graph=detection_graph) as sess:
    for image_path in TEST_IMAGE_PATHS:
      image = Image.open(image_path)
      image_np = load_image_into_numpy_array(image)
      image_np_expanded = np.expand_dims(image_np, axis=0)
      image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
      boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
      scores = detection_graph.get_tensor_by_name('detection_scores:0')
      classes = detection_graph.get_tensor_by_name('detection_classes:0')
      num_detections = detection_graph.get_tensor_by_name('num_detections:0')
      # Actual detection.
      (boxes, scores, classes, num_detections) = sess.run(
          [boxes, scores, classes, num_detections],
          feed_dict={image_tensor: image_np_expanded})
      # Visualization of the results of a detection.
      vis_util.visualize_boxes_and_labels_on_image_array(
          image_np,
          np.squeeze(boxes),
          np.squeeze(classes).astype(np.int32),
          np.squeeze(scores),
          category_index,
          use_normalized_coordinates=True,
          line_thickness=8)
      objects = []
      threshold = 0.5 # in order to get higher percentages you need to lower this number; usually at 0.01 you get 100% predicted objects
      for index, value in enumerate(classes[0]):
          object_dict = {}
          if scores[0, index] > threshold:
              object_dict[(category_index.get(value)).get('name').encode('utf8')] = \
                        scores[0, index]
              objects.append(object_dict)
      print (objects)
      print(len(np.where(scores[0] > threshold)[0])/num_detections[0])
      plt.figure(figsize=IMAGE_SIZE)
      plt.imshow(image_np)

希望这对您有所帮助。