如何在TensorFlow对象检测API中计数对象?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (138)

我要执行此示例(https://github.com/tensorflow/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)
      # 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 = load_image_into_numpy_array(image)
      # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
      image_np_expanded = np.expand_dims(image_np, axis=0)
      image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
      # Each box represents a part of the image where a particular object was detected.
      boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
      # Each score represent how level of confidence for each of the objects.
      # Score is shown on the result image, together with the class label.
      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=1)
      plt.figure(figsize=IMAGE_SIZE)
      plt.imshow(image_np)

这是给出实际对象检测的代码块,如下图所示:

我怎样才能得到对象计数?

提问于
用户回答回答于

值得注意的是,方框的数量总是100个。

如果你查看实际绘制框的代码,即vis_util.visualize_boxes_and_labels_on_image_array函数,你将看到它们定义了一个threshold-min_score_thresh=.5-仅限于大小大于0.5的探测框。你可以认为这只是绘图框,准确检测的概率大于50%。可以向上或向下调整此threshold以增加绘制的框数。但是,如果你把它降低得太低,你会得到很多不准确的方框。

用户回答回答于

你应该手动检查分数和统计对象。代码在这里:

#code to test image start

    (boxes, scores, classes, num) = sess.run(
        [detection_boxes, detection_scores, detection_classes, num_detections],
        feed_dict={image_tensor: image_np_expanded})

#code to test image finish

#add this part to count objects

    final_score = np.squeeze(scores)    
        count = 0
        for i in range(100):
            if scores is None or final_score[i] > 0.5:
                    count = count + 1

#count is the number of objects detected

扫码关注云+社区

领取腾讯云代金券