且构网

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

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

更新时间:2023-12-02 18:54:16

数组( [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=',')