-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateTxt.py
387 lines (344 loc) · 15.1 KB
/
createTxt.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import os
import numpy as np
import copy
# 文件夹下直接为类别文件夹
def createTxt(root,filename):
data_path = root
dirs = os.listdir(data_path)
# dirs.sort()
print(dirs)
with open(filename, "w", encoding="utf-8") as f:
label = 0
for c in dirs:
img_path = os.path.join(data_path, c)
if os.path.isdir(img_path):
imgs = os.listdir(img_path)
# print(imgs)
for img in imgs:
fullpath = os.path.join(img_path, img)
f.write("{} {}\n".format(fullpath, label))
label += 1
# 文件夹下按人分别建立子文件夹
def createTxt2(root,filename):
data_path = root
dirs = os.listdir(data_path)
dirs.sort()
print(dirs)
with open(filename, "w", encoding="utf-8") as f:
for p in dirs:
path = os.path.join(data_path, p)
print(path)
label = 0
for c in os.listdir(path):
if c =="delete":
continue
img_path = os.path.join(path, c)
if os.path.isdir(img_path):
imgs = os.listdir(img_path)
# print(imgs)
for img in imgs:
fullpath = os.path.join(img_path, img)
f.write("{} {}\n".format(fullpath, label))
label += 1
def convertName(root):
data_path = root
dirs = os.listdir(data_path)
dirs.sort()
print(dirs)
for c in dirs:
dir_path = os.path.join(data_path, c)
if os.path.isdir(dir_path):
imgs = os.listdir(dir_path)
# print(imgs)
for index, img in enumerate(imgs):
src_path = os.path.join(dir_path, img)
dest_path = os.path.join(dir_path, "{}.jpg".format(index))
# print(src_path, dest_path)
os.rename(src_path, dest_path)
def createTxt_100():
data_path = data_path = r"D:\IDM\imagenet2012\ILSVRC2012_img"
dirs = os.listdir(data_path)
dirs.sort()
print(dirs)
filename = r"./data/imagenet/imagenet2012_100.txt"
with open(filename, "w", encoding="utf-8") as f:
label = 0
for i, c in enumerate(dirs):
if i > 99:
break
img_path = os.path.join(data_path, c)
if os.path.isdir(img_path):
imgs = os.listdir(img_path)
# print(imgs)
for img in imgs:
fullpath = os.path.join(img_path, img)
f.write("{} {}\n".format(fullpath, label))
label += 1
def convert_imagenetVal():
train_path = r"D:\IDM\imagenet2012\ILSVRC2012_img"
dirs = os.listdir(train_path)
print(dirs)
print(len(dirs))
map_path = r"D:\IDM\imagenet2012\train_label _mapping.txt"
true_label =[]
with open(map_path, "r", encoding="utf-8") as f:
lines = f.readlines()
for line in lines:
# print(dirs.index(line.split()[1]))
true_label.append(dirs.index(line.split()[1]))
print(true_label)
label_path = r"D:\IDM\imagenet2012\ILSVRC2012_devkit_t12\data\ILSVRC2012_validation_ground_truth.txt"
img_label_list = []
with open(label_path, "r", encoding="utf-8") as f:
lines = f.readlines()
for line in lines:
index = int(line.split()[0])
# print(index)
img_label_list.append(true_label[index-1])
# print(img_label_list)
label_map_path = r"D:\IDM\imagenet2012\ILSVRC2012_devkit_t12\data\ILSVRC2012_validation_ground_truth_map.txt"
with open(label_map_path, "w", encoding="utf-8") as f:
for label in img_label_list:
f.write(str(label)+"\n")
def create_imagenet_val():
label_list=[]
label_map_path = r"D:\IDM\imagenet2012\ILSVRC2012_devkit_t12\data\ILSVRC2012_validation_ground_truth_map.txt"
with open(label_map_path, "r", encoding="utf-8") as f:
lines = f.readlines()
for line in lines:
label_list.append(line.split()[0])
print(label_list)
val_path = r"D:\IDM\imagenet2012\ILSVRC2012_img_val"
dirs = os.listdir(val_path)
print(dirs)
print(len(dirs))
val_txt = r"./data/imagenet/imagenet2012_val.txt"
base_path = r"D:\IDM\imagenet2012\ILSVRC2012_img_val"
with open(val_txt, "w", encoding="utf-8") as f:
for index, dir in enumerate(dirs):
fullpath = os.path.join(base_path, dir)
f.write("{} {}\n".format(fullpath, label_list[index]))
def shuffleTxt(source, dest):
with open(source, encoding='utf-8') as f:
lines = f.readlines()
np.random.seed(10101) # seed( ) 用于指定随机数生成时所用算法开始的整数值,如果使用相同的seed( )值,则每次生成的随即数都相同,如果不设置这个值,则系统根据时间来自己选择这个值,此时每次生成的随机数因时间差异而不同。
np.random.shuffle(lines) # 对X进行重排序,如果X为多维数组,只沿第一条轴洗牌,输出为None,改变原来的X
np.random.seed(None)
with open(dest, "w", encoding="utf-8") as f:
for i in range(len(lines)):
f.write(lines[i])
def createByLabel():
# source_path = r"D:\dataset\state-farm-distracted-driver-detection\imgs\train"
source_path = r"D:\dataset\AUC\AUC\trainVal224"
dirs = os.listdir(source_path)
dirs.sort()
# dest_path = r"./data/kg2my.txt"
dest_path = r"./data/AUCv2_my.txt"
labels = [0, 5, 8, 10, 10, 4, 2, 6, 7, 1]
with open(dest_path, "w", encoding="utf-8") as f:
index = 0
for c in dirs:
label = labels[index]
# print(c, label)
if label ==10:
index += 1
continue
print(c, label)
img_path = os.path.join(source_path, c)
if os.path.isdir(img_path):
imgs = os.listdir(img_path)
# print(imgs)
for img in imgs:
fullpath = os.path.join(img_path, img)
f.write("{} {}\n".format(fullpath, label))
index += 1
# c0: safe driving
# c1: texting - right
# c2: talking on the phone - right
# c3: texting - left
# c4: talking on the phone - left
# c5: operating the radio
# c6: drinking
# c7: reaching behind
# c8: hair and makeup
# c9: talking to passenger
# old labels = ["正常", "侧视", "喝水", "吸烟", "操作中控", "玩手机", "侧身拿东西", "整理仪容", "接电话"]
# new labels = ["正常", "喝水", "吸烟", "操作中控", "玩手机", "接电话"] 6 [0,-1,1,2,3,4,-1,-1,5]
# new labels = ["正常", "侧视", "喝水", "吸烟", "玩手机", "接电话"] 6 2
# new labels = ["正常", "喝水", "吸烟", "操作中控", "玩手机", "接电话","其他"] 7
# new labels = ["正常", "侧视", "喝水", "吸烟", "玩手机", "接电话", "其他] 7 2
# old labels = ["正常", "喝水", "吸烟", "玩手机", "侧身拿东西", "接电话",其他] 7 3
# new labels = ["正常", "侧视", "喝水", "吸烟", "操作中控", "玩手机", "侧身拿东西", "接电话"] 8
# 转换标签
def convert_Label():
# 9类转6类
# source_txt = r"data/txt/12_23_12_addpre_train224.txt"
# dest_txt = r"data/txt6/12_23_12_addpre_train224_6.txt"
# source_txt = r"data/txt/12_23_12_addpre_test224.txt"
# dest_txt = r"data/txt6/12_23_12_addpre_test224_6.txt"
# source_txt = r"data/txt/12_23_12_addpre_train224_addcrop.txt"
# dest_txt = r"data/txt6/12_23_12_addpre_train224_addcrop_6.txt"
# source_txt = r"data/txt/12_23_12_addpre_train224_kg2my_aucv2_my.txt"
# dest_txt = r"data/txt6/12_23_12_addpre_train224_kg2my_aucv2_my_6.txt"
# source_txt = r"data/txt/12_23_12_addpre_train224_kg2my_aucv2_my_addcrop.txt"
# dest_txt = r"data/txt6/12_23_12_addpre_train224_kg2my_aucv2_my_addcrop_6.txt"
# source_txt = r"data/txt_raw/total_train.txt"
# dest_txt = r"data/txt_raw/total_train_c6.txt"
# source_txt = r"data/txt_raw/total_test.txt"
# dest_txt = r"data/txt_raw/total_test_c6.txt"
# convert_index = [0, -1, 1, 2, 3, 4, -1, -1, 5]
# # 9类转6类 2 new labels = ["正常", "侧视", "喝水", "吸烟", "玩手机", "接电话"]
# source_txt = r"data/txt_raw/total_train.txt"
# dest_txt = r"data/txt_raw/total_train_62.txt"
# convert_index = [0, 1, 2, 3, -1, 4, -1, -1, 5]
# # 9类转7类
# source_txt = r"data/txt/12_23_12_addpre_train224.txt"
# dest_txt = r"data/txt7/12_23_12_addpre_train224_7.txt"
# source_txt = r"data/txt/12_23_12_addpre_test224.txt"
# dest_txt = r"data/txt7/12_23_12_addpre_test224_7.txt"
# source_txt = r"data/txt/12_23_12_addpre_train224_addcrop.txt"
# dest_txt = r"data/txt7/12_23_12_addpre_train224_addcrop_7.txt"
# source_txt = r"data/txt/12_23_12_addpre_test224_addcrop.txt"
# dest_txt = r"data/txt7/12_23_12_addpre_test224_addcrop_7.txt"
# source_txt = r"data/txt/12_23_12_addpre_train224_kg2my_aucv2_my.txt"
# dest_txt = r"data/txt7/12_23_12_addpre_train224_kg2my_aucv2_my_7.txt"
# source_txt = r"data/txt/12_23_12_addpre_train224_kg2my_aucv2_my_addcrop.txt"
# dest_txt = r"data/txt7/12_23_12_addpre_train224_kg2my_aucv2_my_addcrop_7.txt"
# source_txt = r"data/txt_raw/total_train.txt"
# dest_txt = r"data/txt_raw/total_train_c7.txt"
# # source_txt = r"data/txt_raw/total_test.txt"
# # dest_txt = r"data/txt_raw/total_test_c7.txt"
# convert_index = [0, 6, 1, 2, 3, 4, 6, 6, 5]
# 9类转7类2
# source_txt = r"data/txt_raw/total_train.txt"
# dest_txt = r"data/txt_raw/total_train_c7_2.txt"
# source_txt = r"data/txt_raw/total_test.txt"
# dest_txt = r"data/txt_raw/total_test_c7_2.txt"
# source_txt = r"data/txt_raw_crop/total_train_crop.txt"
# dest_txt = r"data/txt_raw_crop/total_train_crop_72.txt"
# source_txt = r"data/txt_raw_crop/total_test_crop.txt"
# dest_txt = r"data/txt_raw_crop/total_test_crop_72.txt"
# convert_index = [0, 1, 2, 3, 6, 4, 6, 6, 5]
# 9类转7类3
# old labels = ["正常", "喝水", "吸烟", "玩手机", "侧身拿东西", "接电话",其他] 7 3
# source_txt = r"data/txt_raw_crop/total_train_crop.txt"
# dest_txt = r"data/txt_raw_crop/total_train_crop_73.txt"
# source_txt = r"data/txt_raw/total_test.txt"
# dest_txt = r"data/txt_raw/total_test_73.txt"
# convert_index = [0, 6, 1, 2, 6, 3, 4, 6, 5]
# # 9类转8类
# # new labels = ["正常", "侧视", "喝水", "吸烟", "操作中控", "玩手机", "侧身拿东西", "接电话"] 8
# source_txt = r"data/bus/test224.txt"
# dest_txt = r"data/bus/test224_8.txt"
# # source_txt = r"data/bus/addcar_test224.txt"
# # dest_txt = r"data/bus/addcar_test224_8.txt"
# convert_index = [0, 1, 2, 3, 4, 5, 6, -1, 7]
# 9类转6类 new labels = ["正常", "喝水", "吸烟", "操作中控", "玩手机", "接电话"]
# source_txt = r"data/ours/224/train_crop224.txt"
# dest_txt = r"data/ours/224/train_crop224_6.txt"
source_txt = r"data/txt_raw_crop/total_test_crop.txt"
dest_txt = r"data/txt_raw_crop/total_test_crop_6.txt"
convert_index = [0, -1, 1, 2, 3, 4, -1, -1, 5]
with open(source_txt, encoding='utf-8') as f:
lines = f.readlines()
new_lines = []
for line in lines:
old_index = int(line.split()[1])
img_path = line.split()[0]
new_index = convert_index[old_index]
if new_index == -1:
continue
else:
# 需要转义\n
new_lines.append("{} {}\n".format(img_path, new_index))
print("old num:{}; new num:{}".format(len(lines),len(new_lines)))
with open(dest_txt, "w", encoding="utf-8") as f:
for i in range(len(new_lines)):
f.write(new_lines[i])
def concatTxt():
# source1 = r'./data/drive224.txt'
# source2 = r"./data/kg2my.txt"
# dest = r"./data/kgAddmy.txt"
source1 = r'data/total_train.txt'
source2 = r"./data/kg2my.txt"
dest = r"./data/kg_total.txt"
with open(source1, encoding='utf-8') as f:
lines1 = f.readlines()
with open(source2, encoding='utf-8') as f:
lines2 = f.readlines()
dest_lines = lines1+lines2
print(len(dest_lines))
np.random.seed(10101)
np.random.shuffle(dest_lines)
np.random.seed(None)
with open(dest, "w", encoding="utf-8") as f:
for i in range(len(dest_lines)):
f.write(dest_lines[i])
def countTxt(path=r"./data/kgAddmy.txt", class_num=9):
with open(path, encoding='utf-8') as f:
lines = f.readlines()
total = len(lines)
print("total:{}".format(total))
c_num = [0]*class_num
for line in lines:
# print(line.split()[1])
c_num[int(line.split()[1])] += 1
print(c_num)
def addClass():
# source = r"./data/kgAddmy.txt"
# dest_path = r"./data/kgAddmy_add.txt"
source = r"data/kg_total.txt"
dest_path = r"data/kg_total_add.txt"
add_num = 5
with open(source, encoding='utf-8') as f:
lines = f.readlines()
print(len(lines))
new_lines = copy.deepcopy(lines)
for line in lines:
if int(line.split()[1]) == 3:
for i in range(add_num):
new_lines.append(line)
print(len(new_lines))
np.random.seed(10101)
np.random.shuffle(new_lines)
np.random.seed(None)
with open(dest_path, "w", encoding="utf-8") as f:
for i in range(len(new_lines)):
f.write(new_lines[i])
if __name__ == '__main__':
# createTxt_100()
# convert_imagenetVal()
# create_imagenet_val()
# data_path = r"D:\dataset\state-farm-distracted-driver-detection\imgs\train224"
# createTxt(data_path, "data/stateFarm/stateFarm.txt")
# data_path = r"D:\datasets\11_16\dataset\test224"
# createTxt(data_path, "data/ours/cut224/11_16_test224.txt")
# data_path = r"D:\datasets\11_16\dataset\test_crop224"
# createTxt(data_path, "data/ours/cut224/11_16_test_crop224.txt")
# data_path = r"D:\datasets\11_16\dataset\train224"
# createTxt(data_path, "data/ours/cut224/11_16_train224.txt")
# data_path = r"D:\datasets\11_09\train_crop224"
# createTxt(data_path, "data/ours/cut224/11_9_train_crop224.txt")
# data_path = r"D:\datasets\3_23\cam_chen\dataset\test224"
# createTxt2(data_path, "data/bus/test224.txt")
# data_path = r"D:\datasets\3_23\cam_chen\dataset\train224"
# createTxt2(data_path, "data/bus/train224.txt")
# data_path = r"D:\datasets\3_25\cam_he\dataset\train224"
# createTxt2(data_path, "data/ours/cut224/3_25_train224.txt")
# data_path = r"D:\datasets\12_23_2\dataset\train_crop224"
# createTxt2(data_path, "data/ours/cut224/12_232_train_crop224.txt")
# data_path = r"D:\datasets\3_23\cam_chen\dataset\test"
# createTxt2(data_path, "data/bus/test.txt")
# data_path = r"D:\datasets\3_23\cam_chen\dataset\train"
# createTxt2(data_path, "data/bus/train.txt")
# data_path = r"D:\dataset\VLR-40\train"
# convertName(data_path)
# source = r"./data/imagenet/imagenet2012_trains.txt"
# dest = r"./data/imagenet/imagenet2012_trains.txt"
# shuffleTxt(source, dest)
# countTxt(path=r"data/kg_total_add.txt")
# createByLabel()
# concatTxt()
# countTxt()
# addClass()
convert_Label()