728x90
반응형
다른 형식으로된 label txt파일을 내가 원하는 형식 (이미지 경로 + '\t' + Label)으로 바꾸고, 기존 large dataset 중 내가 원하는 일부 이미지만을 다른 폴더에 옮기기 위한 코드
핵심은 shutil package에서 임포트한 copyfile함수
from shutil import copyfile
gt_file = open('./new_gt.txt', 'r')
label_file = open('./train_label.txt', 'w')
lines = gt_file.readlines()
new_folder = './Train/mlt_jp/images/'
for idx, line in enumerate(lines):
print('[%d/%d]'%(idx,len(lines)))
file_name_lst = line.split(',')[0]
file_name = ''.join(file_name_lst)
language_lst = line.split(',')[1]
language = ''.join(language_lst)
label_lst = line.split(',')[2:]
label = ''.join(label_lst)
copyfile('./mlt_jp/'+file_name, new_folder+file_name)
delimeter = '\t'
one_l = 'images/'+file_name+delimeter+label
label_file.write(one_l)
label_file.close()
다른 코드
from shutil import copyfile
import glob
from tqdm import tqdm
import os
train_path = "./results_train"
val_path = "./results_val"
test_path = "./results_test"
ntrain_path = "./train"
nval_path = "./dev"
ntest_path = "./test"
def move_file(path, dst):
os.makedirs(dst+"/gts", exist_ok=True)
os.makedirs(dst+"/images", exist_ok=True)
files = glob.glob(path+"/*/*/*/*")
file_list_txt = [file for file in files if file.endswith(".txt")]
file_list_jpg = [file for file in files if file.endswith(".jpg")]
for txt in tqdm(file_list_txt):
f_name = txt.split("/")[-1]
copyfile(txt, dst+"/gts/"+f_name)
for jpg in tqdm(file_list_jpg):
f_name = jpg.split("/")[-1]
copyfile(jpg, dst+"/images/"+f_name)
print("Train Files")
move_file(train_path, ntrain_path)
print("Val Files")
move_file(val_path, nval_path)
print("Test Files")
move_file(test_path, ntest_path)
파일목록(list.txt) 읽어서 복사 붙여넣기
origin_path = "./original/"
file_list = "./filelist.txt"
next_path = "./next/"
def get_file_list(path):
file = open(path)
return [nm.rstrip("\n") for nm in file.readlines()]
def move_file(path, dst):
os.makedirs(next_path, exist_ok=True)
files = get_file_list(file_list)
for file in tqdm(files):
f_name = file.split("/")[-1]
copyfile(os.path.join(origin_path, f_name), dst+f_name)
print("Move Files")
move_file(origin_path, next_path)
728x90
반응형
'사소한 Tip . 오류 해결법 > python' 카테고리의 다른 글
[python] 인터넷 크롤링(지마켓 상품명, 가격) / Python Crawling product name, price from online shopping mall (0) | 2022.03.15 |
---|---|
[python/pytorch] Model GPU 메모리 사용량 체크 (0) | 2022.03.11 |
[python, layout-parser] pdf에서 정보 뽑기/ Extracting information from PDF file (0) | 2022.01.11 |
[python] pdb command / pdb 명령어 / python debugger 사용법 (0) | 2021.01.15 |
[python]TypeError: 'module' object is not callable python (0) | 2020.11.18 |