PASCAL VOC 格式的RSOD
PASCAL VOC 格式的 RSOD 数据集:下载地址(所需积分/C币: 0)(原始数据集 playground.zip 中的图片有189张,而标注只有149张,PASCAL VOC 格式的数据集中,只选用了有标注的149张图片)
原
1、下载原数据集 https://github.com/RSIA-LIESMARS-WHU/RSOD-Dataset-
2、将四个类别中的Annotation\xml\
和JPEGImages
分别放在Annotations
和JPEGImages
里(类似 VOC 的文件顺序)
3、重命名文件
import os
print(os.getcwd())
ann_filepath = 'F:/VOC2007/VOC2007/Annotations/'
img_filepath = 'F:/VOC2007/VOC2007/JPEGImages/'
ann_num = 0
img_num = 0
for file in os.listdir(ann_filepath):
new_name = '%06d' % int(ann_num)+'.xml'
ann_num += 1
print(file, new_name)
os.rename(os.path.join(ann_filepath, file),
os.path.join(ann_filepath, new_name))
for file in os.listdir(img_filepath):
new_name = '%06d' % int(img_num)+'.jpg'
img_num += 1
print(file, new_name)
os.rename(os.path.join(img_filepath, file),
os.path.join(img_filepath, new_name))
4、最后,自己制作 trainval.txt、test.txt 等文件,里面放的是图片名(可能会报“没有这个文件”的错误,需要手动新建……能力有限)。
from sklearn.model_selection import train_test_split
import os
name_path = r'F:/VOC2007/VOC2007/JPEGImages'
name_list = os.listdir(name_path)
names = []
for i in name_list:
names.append(i.split('.')[0])
trainval, test = train_test_split(names, test_size=0.5, shuffle=10)
val, train = train_test_split(trainval, test_size=0.5, shuffle=10)
with open('F:/VOC2007/VOC2007/ImageSets/Main/trainval.txt', 'w') as fw:
for i in trainval:
fw.write(i + '\n')
with open('F:/VOC2007/VOC2007/ImageSets/Main/test.txt', 'w') as fw:
for i in test:
fw.write(i + '\n')
with open('F:/VOC2007/VOC2007/ImageSets/Main/val.txt', 'w') as fw:
for i in val:
fw.write(i + '\n')
with open('F:/VOC2007/VOC2007/ImageSets/Main/train.txt', 'w') as fw:
for i in train:
fw.write(i + '\n')
print('done!')
2021.07.10 改
多谢评论大佬提醒,按上述3、重命名文件时,私以为 Annotations
和 JPEGImages
中的文件名都是同一排序方式,其实并不是……
将重命名后的 Annotations
标注在 JPEGImages
上时,发现只有aircraft
能够一一对应,oiltank
、playground
和 overpass
则对应不上,如下:
aircraft | oiltank | overpass | playground |
---|---|---|---|
修改后的3、重命名文件如下:
import os
print(os.getcwd())
ann_filepath = 'F:/VOC2007/VOC2007/Annotations/'
img_filepath = 'F:/VOC2007/VOC2007/JPEGImages/'
ann_num = 0
img_num = 0
for old_file in os.listdir(ann_filepath):
new_file_name = '%06d' % int(ann_num) + '.xml'
new_img_name = '%06d' % int(ann_num) + '.jpg'
ann_num += 1
os.rename(os.path.join(ann_filepath, old_file),
os.path.join(ann_filepath, new_file_name))
old_img = old_file.split('.')[0] + '.jpg'
os.rename(os.path.join(img_filepath, old_img),
os.path.join(img_filepath, new_img_name))
每个类别如下:
aircraft | oiltank | overpass | playground |
---|---|---|---|
附:对比代码
# 对比 RSOD 数据集变为 PASCAL VOC 前后是否一一对应
import numpy as np
import cv2
import os
import xml.etree.ElementTree as ET
color_gt = (255, 0, 0)
color_md = (0, 255, 0)
thickness = 1
img_path = 'F:/VOC2007/VOC2007/JPEGImages/'
file_path = 'F:/VOC2007/VOC2007/Annotations/'
for f in os.listdir(file_path):
file = os.path.join(file_path, f)
tree = ET.parse(file)
root = tree.getroot()
size = tree.find('size')
width = float(size.find('width').text)
height = float(size.find('height').text)
objs = root.findall('object')
file_name = f.split('.')
img_source = os.path.join(img_path, file_name[0] + '.jpg')
img = cv2.imread(img_source, 0)
for ix, obj in enumerate(objs):
bbox = obj.find('bndbox')
obj_name = str(obj.find('name').text)
y_max = float(bbox.find('ymax').text) - 1
y_min = float(bbox.find('ymin').text) - 1
x_max = float(bbox.find('xmax').text) - 1
x_min = float(bbox.find('xmin').text) - 1
ptLeftTop = (int(x_min), int(y_min))
ptRightBottom = (int(x_max), int(y_max))
cv2.rectangle(img, ptLeftTop, ptRightBottom, color_md)
cv2.putText(img, obj_name, (int(x_min), int(y_min)), cv2.FONT_HERSHEY_COMPLEX, thickness, color_md)
image = np.expand_dims(img, axis=2)
image = np.concatenate((image, image, image), axis=-1)
if obj_name == 'aircraft':
# ['aircraft', 'oiltank', 'overpass', 'playground']
cv2.imshow('image', img)
k = cv2.waitKey(0)
if k == 27: # wait for ESC key to exit
cv2.destroyAllWindows()
elif k == ord('s'): # wait for 's' key to save and exit
cv2.imwrite(file_name[0] + '.png', img)
cv2.destroyAllWindows()
# 对比依次取 VOC 数据集与原数据集标注文件中的内容,查看是否一致
img_path = 'F:/VOC2007/VOC2007/JPEGImages/'
file_path = 'F:/VOC2007/VOC2007/Annotations/'
origin_path = "F:/RSOD"
origin_name_list = ['aircraft', 'oiltank', 'overpass', 'playground']
voc_list = []
origin_list = []
for f in os.listdir(file_path):
file = os.path.join(file_path, f)
tree = ET.parse(file)
root = tree.getroot()
size = tree.find('size')
width = float(size.find('width').text)
height = float(size.find('height').text)
objs = root.findall('object')
for ix, obj in enumerate(objs):
obj_name = str(obj.find('name').text)
bbox = obj.find('bndbox')
y_max = float(bbox.find('ymax').text)
y_min = float(bbox.find('ymin').text)
x_max = float(bbox.find('xmax').text)
x_min = float(bbox.find('xmin').text)
voc_list.append([y_max, y_min, x_max, x_min, obj_name])
for name in origin_name_list:
origin_class_path = origin_path + '\\' + name + '\Annotation\\xml'
for f in os.listdir(origin_class_path):
file = os.path.join(origin_class_path, f)
tree = ET.parse(file)
root = tree.getroot()
size = tree.find('size')
width = float(size.find('width').text)
height = float(size.find('height').text)
objs = root.findall('object')
for ix, obj in enumerate(objs):
obj_name = str(obj.find('name').text)
bbox = obj.find('bndbox')
y_max = float(bbox.find('ymax').text)
y_min = float(bbox.find('ymin').text)
x_max = float(bbox.find('xmax').text)
x_min = float(bbox.find('xmin').text)
origin_list.append([y_max, y_min, x_max, x_min, obj_name])
print(voc_list)
print(origin_list)
print(voc_list == origin_list)