-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitdata.py
More file actions
77 lines (60 loc) · 2.42 KB
/
splitdata.py
File metadata and controls
77 lines (60 loc) · 2.42 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
import os
import random
import shutil
from itertools import islice
outputFolderPath = "Dataset/SplitData"
inputFolderPath = "Dataset/all"
splitRatio = {"train": 0.7, "val": 0.2, "test": 0.1}
classes = ["tomato","apple","banana","cucumber","lemon","mpineapple","pear","carrot","pomegranate","orange","alligator pear"]
try:
shutil.rmtree(outputFolderPath)
except OSError as e:
os.mkdir(outputFolderPath)
# -------- Directories to Create -----------
os.makedirs(f"{outputFolderPath}/train/images", exist_ok=True)
os.makedirs(f"{outputFolderPath}/train/labels", exist_ok=True)
os.makedirs(f"{outputFolderPath}/val/images", exist_ok=True)
os.makedirs(f"{outputFolderPath}/val/labels", exist_ok=True)
os.makedirs(f"{outputFolderPath}/test/images", exist_ok=True)
os.makedirs(f"{outputFolderPath}/test/labels", exist_ok=True)
# -------- Get the Names -----------
listNames = os.listdir(inputFolderPath)
uniqueNames = []
for name in listNames:
uniqueNames.append(name.split('.')[0])
uniqueNames = list(set(uniqueNames))
# -------- Shuffle -----------
random.shuffle(uniqueNames)
# -------- Find the number of images for each folder -----------
lenData = len(uniqueNames)
lenTrain = int(lenData * splitRatio['train'])
lenVal = int(lenData * splitRatio['val'])
lenTest = int(lenData * splitRatio['test'])
# -------- Put remaining images in Training -----------
if lenData != lenTrain + lenTest + lenVal:
remaining = lenData - (lenTrain + lenTest + lenVal)
lenTrain += remaining
# -------- Split the list -----------
lengthToSplit = [lenTrain, lenVal, lenTest]
Input = iter(uniqueNames)
Output = [list(islice(Input, elem)) for elem in lengthToSplit]
print(f'Total Images:{lenData} \nSplit: {len(Output[0])} {len(Output[1])} {len(Output[2])}')
# -------- Copy the files -----------
sequence = ['train', 'val', 'test']
for i,out in enumerate(Output):
for fileName in out:
shutil.copy(f'{inputFolderPath}/{fileName}.jpg', f'{outputFolderPath}/{sequence[i]}/images/{fileName}.jpg')
shutil.copy(f'{inputFolderPath}/{fileName}.txt', f'{outputFolderPath}/{sequence[i]}/labels/{fileName}.txt')
print("Split Process Completed...")
# -------- Creating Data.yaml file -----------
dataYaml = f'path: ../Data\n\
train: ../train/images\n\
val: ../val/images\n\
test: ../test/images\n\
\n\
nc: {len(classes)}\n\
names: {classes}'
f = open(f"{outputFolderPath}/data.yaml", 'a')
f.write(dataYaml)
f.close()
print("Data.yaml file Created...")