(图像名) (bounding_box_coordinates) (类名)
有没有其他方法可以使 COWC 数据集中的注释更容易?
提前致谢:)
(图像名) (bounding_box_coordinates) (类名)
有没有其他方法可以使 COWC 数据集中的注释更容易?
提前致谢:)
COWC 数据集带有注释,其中每辆车都标有一个点。PNG 文件包含注释。这是我在 PNG 文件中找到注释位置的方法。
import numpy as np
from PIL import Image
annotation_path = 'cowc/datasets/ground_truth_sets/Toronto_ISPRS/03553_Annotated_Cars.png'
im = Image.open(annotation_path)
data = np.asarray(im)
这里的问题是这两个值都将被索引为非零,但我们只需要其中一个。COWC 数据集用红点标记汽车,用蓝点标记负数,我们不需要 alpha 通道,因此需要对新数组进行切片,这样我们就不会计算 alpha 通道并获得重复的索引值。
data = data[:,:,0:3]
y_ind, x_ind, rgba_ind = data.nonzero()
您现在有了注释文件中所有点的索引。y_ind
对应于高度尺寸,x_ind
宽度。这意味着在第一个 x,y 位置,我们应该看到一个看起来像这样的数组[255, 0, 0]
。这是我从索引中查找第一个 x、y 位置时得到的
>>> data[y_ind[0], x_ind[0]]
array([255, 0, 0], dtype=uint8)
在这里,作者决定在以数据集中提供的注释为中心的一侧创建一个 20 像素的边界框。要为此图像中的第一个注释创建单个边界框,您可以尝试这样做。
# define bbox given x, y and ensure bbox is within image bounds
def get_bbox(x, y, x_max, y_max):
x1 = max(0, x - 20) # returns zero if x-20 is negative
x2 = min(x_max, x + 20) # returns x_max if x+20 is greater than x_max
y1 = max(0, y - 20)
y2 = min(y_max, y + 20)
return x1, y1, x2, y2
x1, y1, x2, y2 = get_bbox(x_ind[0], y_ind[0], im.width, im.height)
您必须遍历所有 x、y 值以制作图像的所有边界框。这是为单个图像创建 csv 文件的粗略而肮脏的方法。
img_path = 'cowc/datasets/ground_truth_sets/Toronto_ISPRS/03553.png'
with open('anno.csv', 'w') as f:
for x, y in zip(x_ind, y_ind):
x1, y1, x2, y2 = get_bbox(x, y, im.width, im.height)
line = f'{img_path},{x1},{y1},{x2},{y2},car\n'
f.write(line)
我计划将一个巨大的图像分解成更小的图像,这将改变边界框的值。我希望你觉得这很有帮助,并且喜欢一个好的开始。