如何在Tensorflow Object Detection API中计算对象

Roh*_*nke 9 python machine-learning tensorflow jupyter-notebook

我正在执行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)
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

我怎样才能获得对象数?

Ozl*_*zlu 5

您可以使用TensorFlow对象计数API,该API是在TensorFlow之上构建的开源框架,可轻松开发对象计数系统以计数任何对象!

使用TensorFlow进行行人计数(quicdemo)

使用TensorFlow进行车辆计数(quicdemo)

使用TensorFlow进行实时对象计数(quicdemo)

有关更多信息,请参见TensorFlow对象计数API。如果您觉得有用,请回购中加注星号以表示您对开源社区的支持!


Roh*_*nke 4

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

print(len(boxes.shape))
Run Code Online (Sandbox Code Playgroud)