script.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import json
  2. import os
  3. from tqdm import tqdm
  4. import multiprocessing as mp
  5. from multiprocessing import Pool
  6. import soundfile as sf
  7. import numpy as np
  8. silence, sr = sf.read('/root/nas/data/broadcast/whitenoise_500ms.wav')
  9. root_dir = '/root/nas/data/broadcast/'
  10. output_dir = '/root/sata/broadcast/'
  11. def work_func(path):
  12. json2txt_broadcast( path, root_dir, output_dir, silence )
  13. def handle_broadcast():
  14. files_txt = 'files.txt'
  15. json_files = []
  16. if os.path.exists(root_dir + files_txt):
  17. with open(root_dir + files_txt, 'r') as f:
  18. for line in f:
  19. json_files.append(line.split('\n')[0])
  20. num_cores = mp.cpu_count()
  21. try:
  22. pool = Pool(processes = num_cores)
  23. with tqdm(total=len(json_files)) as pbar:
  24. for _ in tqdm(pool.imap_unordered(work_func, json_files)):
  25. pbar.update()
  26. pool.close()
  27. pool.join()
  28. print("Script complete!")
  29. except KeyboardInterrupt:
  30. pool.close()
  31. pool.join()
  32. ## wav_path : wav 파일 경로
  33. ## root_path : input dir 경로
  34. ## output_path : output dir 경로
  35. ## silence : 앞뒤에 추가할 묵음
  36. def json2txt_broadcast(wav_path, root_path, output_path, silence):
  37. json_path = wav_path.replace("원천데이터", "라벨링데이터").replace("wav", "json")
  38. split_root_path = output_dir + "split/" + json_path.split(root_path)[1].rsplit('/', 1)[0] + "/" + json_path.rsplit('/', 1)[1].split('.json')[0] + "/"
  39. # print(split_root_path)
  40. # print(json_path)
  41. # return
  42. with open(json_path, 'r') as f:
  43. json_data = json.load(f)
  44. os.makedirs(split_root_path, exist_ok=True)
  45. # original_sound, sr = sf.read(wav_path)
  46. for utter in json_data["utterance"]:
  47. data = utter["original_form"]
  48. start = float(utter['start'])
  49. end = float(utter['end'])
  50. utter_id = utter['id'].replace(".", "_")
  51. with open(f"{split_root_path}{utter_id}.txt", 'w+') as f:
  52. f.write(f'{data}\n')
  53. # if not os.path.isfile(f"{split_root_path}{utter_id}.txt"):
  54. # with open(f"{split_root_path}{utter_id}.txt", 'w+') as f:
  55. # f.write(f'{data}\n')
  56. # if not os.path.isfile(f"{split_root_path}{utter_id}.ogg"):
  57. # # sound = original_sound[int(sr * start) : int(sr * end)+1]
  58. # sound = np.append(silence, original_sound[int(sr * start) : int(sr * end)+1])
  59. # sound = np.append(original_sound[int(sr * start) : int(sr * end)+1], silence)
  60. # sf.write(f"{split_root_path}{utter_id}.ogg", sound, sr, format='ogg')
  61. if __name__ == '__main__':
  62. handle_broadcast()