使用CSV格式存储带有边界框的Tensorflow目标检测API图像输出

5
我是一名有用的助手,可以帮您进行文本翻译。
我正在提到谷歌的TensorFlow目标检测API。我已经成功地训练和测试了物体。我的问题是,在测试后,我得到了一个在物体周围画框的输出图像,如何获取这些框的CSV坐标?测试代码可以在以下链接中找到:(https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb)
如果我查看辅助代码,它将图像加载到numpy数组中:
def load_image_into_numpy_array(image):
  (im_width, im_height) = image.size
  return np.array(image.getdata()).reshape(
      (im_height, im_width, 3)).astype(np.uint8)

在检测中,它会使用这个图像数组,并输出带有框的结果,如下所示。
with detection_graph.as_default():
  with tf.Session(graph=detection_graph) as sess:
    # Definite input and output Tensors for detection_graph
    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.
    detection_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.
    detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
    detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
    num_detections = detection_graph.get_tensor_by_name('num_detections:0')
    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.
      (boxes, scores, classes, num) = sess.run(
          [detection_boxes, detection_scores, detection_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=8)
      plt.figure(figsize=IMAGE_SIZE)
      plt.imshow(image_np)

我想将这些绿色框的坐标存储在csv文件中。有什么方法可以做到这一点?

1
它在一个名为'boxes'的变量中存储值。我输出了变量'boxes'的结果,显示了以下数值:[[[2.44699568e-01 2.14029700e-02 3.81645471e-01 2.81205386e-01] [1.58572584e-01 4.91167933e-01 2.97784775e-01 7.96089888e-01] [3.64572904e-03 6.43181324e-01 7.87424743e-02 9.45716262e-01]],这些数值对我来说没有意义,我们怎么从这些数字中提取数据呢? - Ajinkya
1个回答

7
boxes数组中的坐标([ymin, xmin, ymax, xmax])已经进行了归一化,因此您需要将它们乘以图像的宽度/高度才能获得原始值。
为了实现这一点,您可以类似以下操作:
for box in np.squeeze(boxes):
    box[0] = box[0] * heigh
    box[1] = box[1] * width
    box[2] = box[2] * height
    box[3] = box[3] * width

接下来,您可以使用numpy.savetxt()方法将这些框保存到csv文件中:

import numpy as np
np.savetxt('yourfile.csv', boxes, delimiter=',')

编辑:

正如评论中指出的那样,上述方法给出了一个框框坐标列表。这是因为框框张量保存了每个检测到区域的坐标。对我来说一个快速修复方法是假设您使用默认的置信度接受阈值0.5:

  for i, box in enumerate(np.squeeze(boxes)):
      if(np.squeeze(scores)[i] > 0.5):
          print("ymin={}, xmin={}, ymax={}, xmax{}".format(box[0]*height,box[1]*width,box[2]*height,box[3]*width))

这应该会打印出四个值,而不是四个框。每个值代表一个边界框的一个角。

如果你使用另一个置信度接受阈值,你需要调整该值。也许你可以解析模型配置以获取此参数。

要将坐标存储为CSV,您可以执行以下操作:

new_boxes = []
for i, box in enumerate(np.squeeze(boxes)):
    if(np.squeeze(scores)[i] > 0.5):
        new_boxes.append(box)
np.savetxt('yourfile.csv', new_boxes, delimiter=',')

1
是的,我在自己的示例中也看到了相同的行为。我编辑了我的帖子并添加了修复方法。 - ITiger
有没有关于如何在导出的CSV文档中区分一张图片与另一张图片的建议?例如,我得到了4张图像的CSV值列表,然后输出4张带有框的图像,在查看CSV时很难将其与所属的图像相关联。 - Ajinkya
1
我的第一个建议是将每个图像的边界框存储在单独的CSV文件中。但您也可以创建一个图像ID(例如UUID),并将其存储在CSV的每一行中。在我看来,第一种选项更加简洁,如果CSV文件与所引用的图像具有相同的名称,则可以轻松访问CSV文件。 - ITiger
4
对于任何新手,盒子坐标的顺序已更改为: [ymin,xmin,ymax,xmax] - Gregory Saldanha
2
@GregorySaldanha 谢谢。我已经更新了帖子,将坐标的正确顺序放在了前面。 - ITiger
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接