forked from yeyupiaoling/PP-YOLOE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_data_list.py
49 lines (42 loc) · 1.73 KB
/
create_data_list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os
import random
import xml.etree.ElementTree
from tqdm import tqdm
# 打乱数据
def shuffle_data(data_list_path):
with open(data_list_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
random.shuffle(lines)
with open(data_list_path, 'w', encoding='utf-8') as f:
f.writelines(lines)
# 生成图像列表
def create(images_dir, annotations_dir, train_list_path, test_list_path, label_file):
f_train = open(train_list_path, 'w', encoding='utf-8')
f_test = open(test_list_path, 'w', encoding='utf-8')
f_label = open(label_file, 'w', encoding='utf-8')
label = set()
images = os.listdir(images_dir)
i = 0
for image in tqdm(images):
i += 1
annotation_path = os.path.join(annotations_dir, image[:-3] + 'xml').replace('\\', '/')
image_path = os.path.join(images_dir, image).replace('\\', '/')
if not os.path.exists(annotation_path):
continue
root = xml.etree.ElementTree.parse(annotation_path).getroot()
for object in root.findall('object'):
label.add(object.find('name').text)
if i % 20 == 0:
f_test.write("%s %s\n" % (image_path[image_path.find('/') + 1:], annotation_path[annotation_path.find('/') + 1:]))
else:
f_train.write("%s %s\n" % (image_path[image_path.find('/') + 1:], annotation_path[annotation_path.find('/') + 1:]))
for l in label:
f_label.write("%s\n" % l)
f_train.close()
f_test.close()
f_label.close()
# 打乱训练数据
shuffle_data(train_list_path)
print('create data list done!')
if __name__ == '__main__':
create('dataset/images', 'dataset/annotation', 'dataset/trainval.txt', 'dataset/test.txt', 'dataset/label_list.txt')