Skip to main content
 首页 » 编程设计

python之使用 tensorflow object detection api 计算检测到的对象数量

2025年02月15日27unruledboy

我是 tensorflow 的新手。我对如何计算使用 TensorFlow Object Detection 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_detections 是一个 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)