以 CSV 格式存储带框的 Tensorflow 对象检测 API 图像输出
Store Tensorflow object detection API image output with boxes in CSV format
我指的是 Google 的 Tensor-Flow 对象检测 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 文件中file.What有办法吗?
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=',')
我指的是 Google 的 Tensor-Flow 对象检测 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 文件中file.What有办法吗?
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=',')