Store Tensorflow object detection API image output with boxes in CSV format

前端 未结 1 1381
梦如初夏
梦如初夏 2020-12-17 03:34

I am referring to Google\'s Tensor-Flow object detection API. I have successfully trained and tested the objects. My question is after testing I get output image with box dr

相关标签:
1条回答
  • 2020-12-17 03:42

    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
    

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

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

    Edit:

    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.

    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=',')
    
    0 讨论(0)
提交回复
热议问题