Skip to content

Commit 333b03a

Browse files
Merge pull request #27 from renan-siqueira/develop
Merge develop into main
2 parents 66e6300 + 1c0401e commit 333b03a

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

copy_randomic_files.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,48 @@
22
import shutil
33
import random
44

5-
from settings.settings import (
6-
COPY_DESTINATION_FOLDER,
7-
COPY_SOURCE_FOLDER,
8-
COPY_PERCENTAGE_TO_COPY,
9-
COPY_RANDOM_MODE,
10-
COPY_FIXED_NUMBER_TO_COPY
11-
)
5+
from settings.settings import COPY_SOURCE_FOLDER, DATA_PATH, COPY_FIXED_NUMBER_TO_COPY, TRAIN_PERCENTAGE
126

137

148
def copy_files(
159
source_folder,
16-
destination_folder,
17-
percentage=None,
18-
fixed_number=None,
10+
dataset_folder,
11+
fixed_number,
1912
random_mode=True
2013
):
2114

2215
files = [file_name for file_name in os.listdir(source_folder)
2316
if os.path.isfile(os.path.join(source_folder, file_name))]
2417

25-
if percentage is not None:
26-
total_to_copy = int(len(files) * percentage / 100)
27-
elif fixed_number is not None:
28-
total_to_copy = min(fixed_number, len(files))
29-
else:
30-
raise ValueError("Either percentage or fixed_number must be provided!")
18+
total_to_copy = min(fixed_number, len(files))
3119

3220
if random_mode:
3321
chosen_files = random.sample(files, total_to_copy)
3422
else:
3523
chosen_files = files[:total_to_copy]
3624

37-
for file_name in chosen_files:
38-
shutil.copy2(os.path.join(source_folder, file_name), destination_folder)
25+
# Splitting the chosen files for train and validation
26+
num_train = int(TRAIN_PERCENTAGE * total_to_copy)
27+
train_files = chosen_files[:num_train]
28+
valid_files = chosen_files[num_train:]
29+
30+
# Define train and valid destination folders
31+
train_destination_folder = os.path.join(dataset_folder, 'train')
32+
valid_destination_folder = os.path.join(dataset_folder, 'valid')
3933

40-
print(f"{total_to_copy} files have been copied from {source_folder} to {destination_folder}.")
34+
for file_name in train_files:
35+
shutil.copy2(os.path.join(source_folder, file_name), train_destination_folder)
36+
for file_name in valid_files:
37+
shutil.copy2(os.path.join(source_folder, file_name), valid_destination_folder)
38+
39+
print(f"{num_train} files have been copied from {source_folder} to {train_destination_folder}.")
40+
print(f"{total_to_copy - num_train} files have been copied from {source_folder} to {valid_destination_folder}.")
4141

4242

4343
if __name__ == '__main__':
4444
copy_files(
4545
COPY_SOURCE_FOLDER,
46-
COPY_DESTINATION_FOLDER,
47-
percentage=COPY_PERCENTAGE_TO_COPY,
46+
DATA_PATH,
4847
fixed_number=COPY_FIXED_NUMBER_TO_COPY,
49-
random_mode=COPY_RANDOM_MODE
48+
random_mode=True
5049
)

settings/settings.example.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,4 @@
88
COPY_SOURCE_FOLDER = '__source_images_folder_path__'
99

1010
TRAIN_PERCENTAGE = 0.7
11-
VALID_PERCENTAGE = 0.3
12-
1311
COPY_FIXED_NUMBER_TO_COPY = 32

0 commit comments

Comments
 (0)