-
Notifications
You must be signed in to change notification settings - Fork 2
/
dataTxt.py
38 lines (31 loc) · 1.21 KB
/
dataTxt.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
# -*- coding: utf-8 -*-
"""
@Author : wyl
@Email : [email protected]
"""
import os
'''
为数据集生成对应的txt文件
'''
train_txt_path = os.path.join( "./", "train.txt")
train_dir = os.path.join( "./", "data")
# valid_txt_path = os.path.join( "./images", "test.txt")
# valid_dir = os.path.join("./images", "test")
def gen_txt(txt_path, img_dir):
f = open(txt_path, 'w')
for root, s_dirs, _ in os.walk(img_dir, topdown=True): # 获取 train文件下各文件夹名称
for index,sub_dir in enumerate(s_dirs):
i_dir = os.path.join(root, sub_dir) # 获取各类的文件夹 绝对路径
img_list = os.listdir(i_dir) # 获取类别文件夹下所有png图片的路径
for i in range(len(img_list)):
if not img_list[i].endswith('jpg'): # 若不是png文件,跳过
continue
# label = img_list[i].split('/')[0]
label = str(index)
img_path = os.path.join(i_dir, img_list[i])
line = img_path + ' ' + label + '\n'
f.write(line)
f.close()
if __name__ == '__main__':
gen_txt(train_txt_path, train_dir)
# gen_txt(valid_txt_path, valid_dir)