【问题标题】:How to count objects in Tensorflow Object Detection API如何在 Tensorflow 对象检测 API 中计数对象
【发布时间】:2018-01-14 12:54:53
【问题描述】:

我正在执行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)

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

如何获取对象计数?

【问题讨论】:

  • 这给了我这些类的信息。我需要一个从 box.shape 的打印长度中得到的计数。任何方式谢谢你。
  • 这是一个用于 TensorFlow 的对象计数 API:github.com/ahmetozlu/tensorflow_object_counting_api,您可以使用它来计算对象以及更多用于开发基于对象检测和计数的项目..

标签: python tensorflow machine-learning jupyter-notebook


【解决方案1】:

解决它只需打印 box.shape 的长度

print(len(boxes.shape))

【讨论】:

    【解决方案2】:

    请务必注意,盒子的数量始终为 100。

    如果您查看实际绘制框的代码,即vis_util.visualize_boxes_and_labels_on_image_array 函数,您会发现它们定义了一个阈值——min_score_thresh=.5——将绘制的框限制为仅检测到那些其中得分> 0.5。您可以将其视为仅绘制准确检测概率> 50%的框。您可以向上或向下调整此阈值以增加绘制的框数。但是,如果将其减小得太低,则会得到很多不准确的框。

    【讨论】:

      【解决方案3】:

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

      #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
      

      【讨论】:

      • 如果 scores 是 None ,是否意味着它包含一个对象?
      【解决方案4】:

      添加这部分来统计对象

      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 是检测到的对象数

      这部分将打印计数,但会以连续方式打印它是否可以像最终计数一样只打印一次=某个值而不是重复打印

      【讨论】:

        【解决方案5】:

        您可以使用 TensorFlow Object Counting API,这是一个构建在 TensorFlow 之上的开源框架,可以轻松开发对象计数系统来计算任何对象!此外,它还提供了示例项目,因此您可以采用它们来开发您自己的特定案例研究!

        • 示例项目#1 是“行人计数”:

        • 示例项目#2 是“车辆计数”:

        • 示例项目#3 是“实时对象计数”:

        查看TensorFlow Object Counting API 了解更多信息,如果您觉得有用,请给that repo 打一个星号,以表示您对开源社区的支持!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-12-02
          • 1970-01-01
          • 1970-01-01
          • 2019-11-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-17
          相关资源
          最近更新 更多