-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImagesmani.py
70 lines (49 loc) · 2.06 KB
/
Imagesmani.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
import os
from random import choice
import shutil
#arrays to store file names
imgs =[]
xmls =[]
#setup dir names
trainPath = 'C:/tensorflow1/models/research/object_detection/images/train/'
testPath = 'C:/tensorflow1/models/research/object_detection/images/test/'
crsPath = 'D:/copies/' #dir where images and annotations stored
#setup ratio (val ratio = rest of the files in origin dir after splitting into train and test)
train_ratio = 0.8
test_ratio = 0.2
#total count of imgs
totalImgCount = len(os.listdir(crsPath))
#soring files to corresponding arrays
for (dirname, dirs, files) in os.walk(crsPath):
for filename in files:
if filename.endswith('.xml'):
xmls.append(filename)
else:
imgs.append(filename)
#counting range for cycles
countForTrain = int(len(imgs)*train_ratio)
countForTest = int(len(imgs)*test_ratio)
#cycle for train dir
for x in range(countForTrain):
fileJpg = choice(imgs) # get name of random image from origin dir
fileXml = fileJpg[:-4] +'.xml' # get name of corresponding annotation file
#move both files into train dir
shutil.move(os.path.join(crsPath, fileJpg), os.path.join(trainPath, fileJpg))
shutil.move(os.path.join(crsPath, fileXml), os.path.join(trainPath, fileXml))
#remove files from arrays
imgs.remove(fileJpg)
xmls.remove(fileXml)
#cycle for test dir
for x in range(countForTest):
fileJpg = choice(imgs) # get name of random image from origin dir
fileXml = fileJpg[:-4] +'.xml' # get name of corresponding annotation file
#move both files into train dir
shutil.move(os.path.join(crsPath, fileJpg), os.path.join(testPath, fileJpg))
shutil.move(os.path.join(crsPath, fileXml), os.path.join(testPath, fileXml))
#remove files from arrays
imgs.remove(fileJpg)
xmls.remove(fileXml)
#summary information after splitting
print('Total images: ', totalImgCount)
print('Images in train dir:', len(os.listdir(trainPath)))
print('Images in test dir:', len(os.listdir(testPath)))