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

2024-04-14

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

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

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

请帮我解决这个问题。

我试过了

打印(str(output_dict ['检测类'] [0]),“:”,str(output_dict ['检测_分数'] [0]))

这有效并给出了概率最高的类的对象 id 和分数。但我也想提取类名以及图像中存在的所有对象的分数、ID 和名称

输出示例:图像中有两只狗。当我打印结果时,我得到概率最高的对象的 ID 和分数 [在本例中为 94%] 我也想打印对象名称以及图像中所有其他对象的类似详细信息 https://i.stack.imgur.com/33x5U.png


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

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)

希望这有帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 TensorFlow 对象检测输出分数、类别和 ID 提取 的相关文章

随机推荐