Python 统计使用tensorflow对象检测api检测到的对象数

Python 统计使用tensorflow对象检测api检测到的对象数,python,numpy,tensorflow,object-detection,Python,Numpy,Tensorflow,Object Detection,我是tensorflow的新手。我不知道如何计算使用TensorFlow对象检测API检测到的对象数 # 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

我是tensorflow的新手。我不知道如何计算使用TensorFlow对象检测API检测到的对象数

# 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')

if image_np_expanded is not None:
    # Actual detection.
    (boxes, scores, classes, num_detections) = sess.run(
        [boxes, scores, classes, num_detections],
        feed_dict={image_tensor: image_np_expanded}
    )
由于num\u检测是一个numpy.ndarray,我尝试检索它的长度,但不起作用

num_detections.size
>> 1
无论是使用张量长度:

tf.size(num_detections, out_type=tf.int32)
>> 1

在我的例子中,检测到的对象的数量不止一个。

我一直在执行类似的过程,但工作流不同。我在object_detection笔记本中重写了教程代码,如下所示。它捕获检测框、类和概率的输出,并忽略任何经过推断的图像处理。在我的情况下,我将结果转储到一个JSON文件中,并将其导入另一个程序以完成我的分析(确定检测数量),但如果python是你的东西,我认为你可以处理“myResults”以获得答案

myResults = collections.defaultdict(list)
for image_path in TEST_IMAGE_PATHS:
  if os.path.exists(image_path):  
    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)
    # Actual detection.
    output_dict = run_inference_for_single_image(image_np, detection_graph)
    # Visualization of the results of a detection.
    op=get_scores(
      output_dict['detection_boxes'],
      output_dict['detection_classes'],
      output_dict['detection_scores'],
      category_index,
      min_score_thresh=.2)
    myResults[image_path].append(op)  

您能否说出写入
num\u detections.size
时出现的错误?它返回
1
,但检测到的对象不止一个。请尝试num\u detections[0]?num\u detections始终返回
1
作为大小。为了获得检测到的对象数量,我使用了box.shape[0],条件是分数精度。感谢您的帮助在通过非最大抑制函数后,您似乎实际上需要计算边界框的数量。我不知道
num\u detections
应该代表什么。