且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 CSV 格式的框存储 Tensorflow 对象检测 API 图像输出

更新时间:2023-12-02 18:33:34

boxes数组中的坐标([ymin, xmin, ymax, xmax])被归一化.因此,您必须将它们与图像的宽度/高度相乘才能获得原始值.

The coordinates in the boxes array ([ymin, xmin, ymax, xmax]) are normalized. Therefore, you have to multiply them with the images width / height to obtain the original values.

为此,您可以执行以下操作:

To achieve this, you can do something like the following:

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 中:

Then you can save the boxes to your csv using the numpy.savetxt() method:

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

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

As pointed out in the comments, the approach above gives a list of box coordinates. This is due to the fact, that the boxes tensor holds the coordinates of every detected region. One quick fix for me is the following, assuming that you use the default confidence acceptance threshold of 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))

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

This should print you the four values, and not four boxes. Each of the values represents one corner of the bounding box.

如果您使用其他置信度接受阈值,则必须调整此值.也许你可以解析这个参数的模型配置.

If you use another confidence acceptance threshold you have to adjust this value. Maybe you can parse the model configuration for this parameter.

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

To store the coordinates as CSV, you can do something like:

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=',')