使用CSV格式的框存储Tensorflow对象检测API图像输出 [英] Store Tensorflow object detection API image output with boxes in CSV format

查看:106
本文介绍了使用CSV格式的框存储Tensorflow对象检测API图像输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指的是Google的Tensor-Flow对象检测API。我已经成功地训练和测试了这些物体。我的问题是在测试后,我得到了带有围绕对象绘制的框的输出图像,如何获得这些框的csv坐标?可以在( https://github.com上找到测试代码。 com / tensorflow / models / blob / master / research / object_detection / object_detection_tutorial.ipynb

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 drawn around an object, how do I get csv coordinates of these boxes? code for testing can be found on (https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb)

如果看到帮助程序代码,它将图像加载到numpy数组中:

If I see the helper code it loads the image into numpy array:

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)

在检测中,它会使用此图像数组并使用如下所示的框给出输出

In detection it takes this array of images and give output with box drawn as follows

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文件中。有什么方法?

I want to store the coordinates of these green boxes in a csv file.What is a way to do it?

推荐答案

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

这篇关于使用CSV格式的框存储Tensorflow对象检测API图像输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆