-
Notifications
You must be signed in to change notification settings - Fork 5
/
060_split_file.py
84 lines (64 loc) · 2.87 KB
/
060_split_file.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2018 Luca Clissa, Marco Dalla, Roberto Morelli
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Created on Wed Jan 9 19:45:22 2019
@author: Marco Dalla
"""
from tqdm import tqdm
from shutil import move, copy
from pathlib import Path
import argparse
import shutil
from config import *
def split_images_in_folder(folder, fol_type):
file_per_folder = 1000
images = os.listdir(str(folder))
images.sort()
if len(images) % file_per_folder != 0:
n_folder = len(images) // file_per_folder + 1
else:
n_folder = len(images) // file_per_folder
for i in range(n_folder):
path = str(folder.parent) + '_splitted_{}/{}/{}{}'.format(fol_type, fol_type, fol_type, str(i))
os.makedirs(path , exist_ok=True)
for i, name in tqdm(enumerate(images)):
fol = i // file_per_folder
path = str(folder.parent) + '_splitted_{}/{}/{}{}'.format(fol_type, fol_type, fol_type, str(fol))
dest_name = path + '/' + name
copy(str(folder / name), str(dest_name))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='weighting masks')
parser.add_argument('--images', nargs="?", default = AugCropImages, help='path including images for training')
parser.add_argument('--start_from_zero', action='store_const', const=True, default=True, help='remove previous file in the destination folder')
args = parser.parse_args()
if args.images == 'AugCropImagesBasic':
images_path = AugCropImagesBasic
masks_path = AugCropMasksBasic
else:
images_path = AugCropImages
masks_path = AugCropMasks
if args.start_from_zero:
print('deleting existing files in destination folder')
try:
shutil.rmtree(str(Path(images_path).parent) + '_splitted_images')
except:
pass
os.makedirs(str(Path(images_path).parent) + '_splitted_images',exist_ok=True)
try:
shutil.rmtree(str(Path(masks_path).parent) + '_splitted_masks')
except:
pass
os.makedirs(str(Path(masks_path).parent) + '_splitted_masks',exist_ok=True)
print('Splitting image in train_val and test')
split_images_in_folder(Path(images_path), 'images')
split_images_in_folder(Path(masks_path), 'masks')