1
1
import os
2
2
import shutil
3
3
from tqdm import tqdm
4
+
4
5
def split_data ():
5
6
# 원본 데이터셋 폴더와 저장할 폴더 지정
6
7
original_dataset_folder = 'C:/cv_project/Recycling_trash/Separate_Collection/trash_dataset'
@@ -11,29 +12,28 @@ def split_data():
11
12
os .makedirs (train_folder )
12
13
13
14
# 각 batch 폴더를 돌면서 파일을 읽어와 train 폴더에 저장
14
- index = 0
15
+ start_index = 0
15
16
for batch_folder in tqdm (os .listdir (original_dataset_folder ), desc = "Processing batches" ):
16
- batch_folder_name = f'batch_{ batch_folder } '
17
- batch_folder_path = os .path .join (original_dataset_folder , batch_folder_name )
17
+ batch_folder_path = os .path .join (original_dataset_folder , batch_folder )
18
18
19
19
# batch 폴더가 존재하는지 확인
20
- if os .path .exists (batch_folder_path ):
20
+ if os .path .isdir (batch_folder_path ):
21
21
# batch 폴더 안에 있는 파일들을 정렬
22
22
file_list = sorted (os .listdir (batch_folder_path ))
23
23
24
24
# train 폴더에 파일을 복사하면서 이름을 변경
25
25
for i , file_name in enumerate (file_list ):
26
26
# 숫자 부분을 4자리로 맞추고 0을 추가
27
+ index = start_index + i
27
28
padded_number = f"{ index :04d} " if index < 1000 else str (index )
28
29
new_file_name = f"{ padded_number } .jpg"
29
30
30
31
source_path = os .path .join (batch_folder_path , file_name )
31
32
destination_path = os .path .join (train_folder , new_file_name )
32
33
shutil .copyfile (source_path , destination_path )
33
34
34
- # 다음 batch 폴더에서 시작할 인덱스 업데이트
35
- index += i
36
-
35
+ # 다음 batch 폴더에서 시작할 인덱스 업데이트
36
+ start_index += len (file_list )
37
37
38
38
if __name__ == '__main__' :
39
39
split_data ()
0 commit comments