使用TensorFlow对象检测输出分数,类别和ID提取

时间:2018-07-06 15:09:32

标签: tensorflow object-detection object-detection-api

如何提取由Tensorflow模型进行检测的图像中检测到的对象,对象类,对象ID的输出分数?

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

使用与此链接中相同的代码 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]))

这可以工作,并给出可能性最高的类的对象ID和得分。但是我也想提取类名以及图像中存在的所有对象的分数,Ids和名称

输出示例: 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

3 个答案:

答案 0 :(得分:1)

您可能需要一些有关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)

希望这会有所帮助。

答案 1 :(得分:0)

它为您提供了得分最高的课程,因为输出张量从最高得分到最低得分排序,并且您通过索引第一个元素[0]来要求最高得分。

查看object_detection / inference / detection_inference以获得灵感。

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

答案 2 :(得分:0)

获取班级名称 您的标签图应该可以在这里提供帮助。

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中只有一个框