forked from WTFHCN/ImageSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataHelper.py
More file actions
84 lines (69 loc) · 2.83 KB
/
Copy pathDataHelper.py
File metadata and controls
84 lines (69 loc) · 2.83 KB
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
import os
import shutil
from tqdm import tqdm
date_path = "data"
train_path = "train"
test_path = "test"
trainpercentage = 0.7 # 训练集合与测试百分比
def RName(str):
l = 0
for i in range(len(str)):
if str[i] <= 'Z' and str[i] >= 'A':
return str[i:]
if str[i] <= 'z' and str[i] >= 'a':
return str[i:]
def DirRename():
print("修改文件夹")
imagelist_name = os.listdir(date_path)
for name in tqdm(imagelist_name):
if os.path.isdir(os.path.join(date_path, name)):
os.rename(os.path.join(date_path, name),
os.path.join(date_path, RName(name)))
def PictureRename():
print("批量重命名")
imagelist_name = os.listdir(date_path)
for name in tqdm(imagelist_name):
dir_data = os.path.join(date_path, name)
if os.path.isdir(dir_data) == True:
image_list = os.listdir(dir_data)
i = 0
for image in image_list:
if image.find('.png') != -1 or image.find('.jpg') != -1:
lastname = os.path.join(dir_data, image)
# print(lastname)
filename = os.path.join(
dir_data, "image_" + str(i).zfill(4) + ".jpg")
os.renames(lastname, filename)
i += 1
def Make_tarin_test():
print("训练和测试分离")
imagelist_name = os.listdir(date_path)
for name in tqdm(imagelist_name):
dir_data = os.path.join(date_path, name)
if os.path.isdir(dir_data) == True:
image_list = os.listdir(dir_data)
tarinimage_list = os.path.join(train_path, name)
if os.path.exists(tarinimage_list):
shutil.rmtree(tarinimage_list)
os.mkdir(tarinimage_list)
testimage_list = os.path.join(test_path, name)
if os.path.exists(testimage_list):
shutil.rmtree(testimage_list)
os.mkdir(testimage_list)
train_num = int(len(image_list)*trainpercentage)
for image in image_list[:train_num]:
if image.find('.png') != -1 or image.find('.jpg') != -1:
old_name = os.path.join(dir_data, image)
new_name = os.path.join(
os.path.join(train_path, name), image)
shutil.copyfile(old_name, new_name)
for image in image_list[train_num:]:
if image.find('.png') != -1 or image.find('.jpg') != -1:
old_name = os.path.join(dir_data, image)
new_name = os.path.join(
os.path.join(test_path, name), image)
shutil.copyfile(old_name, new_name)
if __name__ == '__main__':
#PictureRename()
Make_tarin_test()
# DirRename()