首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Tensorflow对象检测API中计数对象

如何在Tensorflow对象检测API中计数对象
EN

Stack Overflow用户
提问于 2017-08-07 17:06:06
回答 4查看 21K关注 0票数 10

我正在执行https://github.com/tensorflow/tensorflow,这是一个检测图像中物体的例子。

我想要获得检测到的物体的计数,下面是给我在图像中绘制的检测到的物体的代码。但是我不能得到检测到的物体的数量。

代码语言:javascript
复制
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)

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

如何获取对象计数?

EN

回答 4

Stack Overflow用户

发布于 2017-08-07 18:08:39

简单解决boxes.shape打印长度问题

代码语言:javascript
复制
print(len(boxes.shape))
票数 4
EN

Stack Overflow用户

发布于 2017-10-02 21:49:15

您应该手动检查分数和清点对象。代码如下:

代码语言:javascript
复制
#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
票数 2
EN

Stack Overflow用户

发布于 2017-08-20 02:54:22

需要注意的是,框的数量始终是100。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45543154

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档