123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- import json
- import os
- from tqdm import tqdm
- import multiprocessing as mp
- from multiprocessing import Pool
- import soundfile as sf
- import numpy as np
- silence, sr = sf.read('/root/nas/data/broadcast/whitenoise_500ms.wav')
- root_dir = '/root/nas/data/broadcast/'
- output_dir = '/root/sata/broadcast/'
- def work_func(path):
- json2txt_broadcast( path, root_dir, output_dir, silence )
- def handle_broadcast():
-
- files_txt = 'files.txt'
- json_files = []
- if os.path.exists(root_dir + files_txt):
- with open(root_dir + files_txt, 'r') as f:
- for line in f:
- json_files.append(line.split('\n')[0])
-
- num_cores = mp.cpu_count()
- try:
- pool = Pool(processes = num_cores)
- with tqdm(total=len(json_files)) as pbar:
- for _ in tqdm(pool.imap_unordered(work_func, json_files)):
- pbar.update()
- pool.close()
- pool.join()
- print("Script complete!")
- except KeyboardInterrupt:
- pool.close()
- pool.join()
- ## wav_path : wav 파일 경로
- ## root_path : input dir 경로
- ## output_path : output dir 경로
- ## silence : 앞뒤에 추가할 묵음
- def json2txt_broadcast(wav_path, root_path, output_path, silence):
- json_path = wav_path.replace("원천데이터", "라벨링데이터").replace("wav", "json")
- split_root_path = output_dir + "split/" + json_path.split(root_path)[1].rsplit('/', 1)[0] + "/" + json_path.rsplit('/', 1)[1].split('.json')[0] + "/"
- # print(split_root_path)
- # print(json_path)
- # return
- with open(json_path, 'r') as f:
- json_data = json.load(f)
- os.makedirs(split_root_path, exist_ok=True)
- # original_sound, sr = sf.read(wav_path)
- for utter in json_data["utterance"]:
- data = utter["original_form"]
- start = float(utter['start'])
- end = float(utter['end'])
- utter_id = utter['id'].replace(".", "_")
- with open(f"{split_root_path}{utter_id}.txt", 'w+') as f:
- f.write(f'{data}\n')
- # if not os.path.isfile(f"{split_root_path}{utter_id}.txt"):
- # with open(f"{split_root_path}{utter_id}.txt", 'w+') as f:
- # f.write(f'{data}\n')
- # if not os.path.isfile(f"{split_root_path}{utter_id}.ogg"):
- # # sound = original_sound[int(sr * start) : int(sr * end)+1]
- # sound = np.append(silence, original_sound[int(sr * start) : int(sr * end)+1])
- # sound = np.append(original_sound[int(sr * start) : int(sr * end)+1], silence)
- # sf.write(f"{split_root_path}{utter_id}.ogg", sound, sr, format='ogg')
-
- if __name__ == '__main__':
- handle_broadcast()
|