如何在tensorflow对象检测器中将对象检测的(帧编号、边界框信息、置信度)打印到文本文件中?

如何在tensorflow对象检测器中将对象检测的(帧编号、边界框信息、置信度)打印到文本文件中?,tensorflow,object-detection-api,Tensorflow,Object Detection Api,我使用tensorflow对象检测api检测视频中的多个对象。然而,我一直在努力弄清楚如何将这些结果对象检测写入text/CSV/xml文件(基本上是边界框信息、图像序列的帧数、bbox的可信度) 我在stackoverflow和github中看到了几个答案,但大多数答案要么含糊不清,要么就是我找不到确切的答案 下面显示的是检测代码的最后一部分,我知道检测框和检测分数是我所需要的,但我不知道如何将它们写入文本文件,也不知道如何只写入图像上看到的最终bbox检测,而不是所有检测边界框 for im

我使用tensorflow对象检测api检测视频中的多个对象。然而,我一直在努力弄清楚如何将这些结果对象检测写入text/CSV/xml文件(基本上是边界框信息、图像序列的帧数、bbox的可信度)

我在stackoverflow和github中看到了几个答案,但大多数答案要么含糊不清,要么就是我找不到确切的答案

下面显示的是检测代码的最后一部分,我知道检测框和检测分数是我所需要的,但我不知道如何将它们写入文本文件,也不知道如何只写入图像上看到的最终bbox检测,而不是所有检测边界框

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)  # Actual detection.
    output_dict = run_inference_for_single_image(image_np_expanded, detection_graph) # Visualization of the results of a detection.
    vis_util.visualize_boxes_and_labels_on_image_array(
        image_np,
        output_dict['detection_boxes'],
        output_dict['detection_classes'],
        output_dict['detection_scores'],
        category_index,
        instance_masks=output_dict.get('detection_masks'),
        use_normalized_coordinates=True,
        line_thickness=8)   plt.figure(figsize=IMAGE_SIZE)   
    plt.imshow(image_np)

您可以尝试以下代码

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)

boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
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')    

(boxes, scores, classes, num_detections) = sess.run(
          [boxes, scores, classes, num_detections],
          feed_dict={image_tensor: image_np_expanded})

width = 1024
height = 600
threshold = 0.5

temp = [] # list to store scores greater than 0.5

# iterate through all scores and pick those having a score greater than 0.5

for index,value in enumerate(classes[0]):
    if scores[0,index] > threshold:      
        temp.append(scores[0,index])

# Similarly, classes[0,index] will give you the class of the bounding box detection

# Actual detection.
output_dict = run_inference_for_single_image(image_np, detection_graph)

# For printing the bounding box coordinates
for i,j in zip(output_dict['detection_boxes'],output_dict['detection_scores']):
        if(j>threshold):
            print(i[1]*width,i[0]*height,i[3]*width,i[2]*height)

上面的代码片段将为您提供边界框坐标和检测分数。您可以使用最小阈值来过滤不必要的检测。我希望这对你有帮助。另外,我不太明白你所说的帧号是什么意思。请你进一步解释一下你的意思


如果您遇到任何问题,请告诉我,您可以尝试以下代码

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)

boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
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')    

(boxes, scores, classes, num_detections) = sess.run(
          [boxes, scores, classes, num_detections],
          feed_dict={image_tensor: image_np_expanded})

width = 1024
height = 600
threshold = 0.5

temp = [] # list to store scores greater than 0.5

# iterate through all scores and pick those having a score greater than 0.5

for index,value in enumerate(classes[0]):
    if scores[0,index] > threshold:      
        temp.append(scores[0,index])

# Similarly, classes[0,index] will give you the class of the bounding box detection

# Actual detection.
output_dict = run_inference_for_single_image(image_np, detection_graph)

# For printing the bounding box coordinates
for i,j in zip(output_dict['detection_boxes'],output_dict['detection_scores']):
        if(j>threshold):
            print(i[1]*width,i[0]*height,i[3]*width,i[2]*height)

上面的代码片段将为您提供边界框坐标和检测分数。您可以使用最小阈值来过滤不必要的检测。我希望这对你有帮助。另外,我不太明白你所说的帧号是什么意思。请你进一步解释一下你的意思

如果您遇到任何问题,请告诉我