File size: 2,906 Bytes
327b68f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#! /usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright 2020 Imperial College London (Pingchuan Ma)
# Apache 2.0  (http://www.apache.org/licenses/LICENSE-2.0)


"""Transforms mp4 audio to npz. Code has strong assumptions on the dataset organization!"""

import os
import librosa  # μŒμ› 데이터 뢄석 라이브러리
import argparse  # λͺ…λ Ήν–‰ 인자λ₯Ό νŒŒμ‹±ν•΄μ£ΌλŠ” λͺ¨λ“ˆ

from utils import *  # utils.py λͺ¨λ“ˆμ— μžˆλŠ” λͺ¨λ“  ν•¨μˆ˜(read_txt_lines(), save2npz(), read_video()) 뢈러였기


# μΈμžκ°’μ„ λ°›μ•„μ„œ μ²˜λ¦¬ν•˜λŠ” ν•¨μˆ˜
def load_args(default_config=None):
    # μΈμžκ°’μ„ 받을 수 μžˆλŠ” μΈμŠ€ν„΄μŠ€ 생성
    parser = argparse.ArgumentParser(description='Extract Audio Waveforms')
    
    # μž…λ ₯받을 μΈμžκ°’ 등둝
    # -- utils
    parser.add_argument('--video-direc', default=None, help='raw video directory')
    parser.add_argument('--filename-path', default='./vietnamese_detected_face_30.csv', help='list of detected video and its subject ID')
    parser.add_argument('--save-direc', default=None, help='the directory of saving audio waveforms (.npz)')
    # -- test set only
    parser.add_argument('--testset-only', default=False, action='store_true', help='process testing set only')

    # μž…λ ₯받은 μΈμžκ°’μ„ args에 μ €μž₯ (type: namespace)
    args = parser.parse_args()
    return args

args = load_args()  # args νŒŒμ‹± 및 λ‘œλ“œ

lines = open(args.filename_path).read().splitlines()  # λ¬Έμžμ—΄μ„ '\m' κΈ°μ€€μœΌλ‘œ μͺΌκ°  ν›„ list 생성
lines = list(filter(lambda x: 'test' == x.split('/')[-2], lines)) if args.testset_only else lines   # args.testset_only 값이 μžˆλ‹€λ©΄ test 폴더 속 파일λͺ…λ§Œ λΆˆλŸ¬μ™€μ„œ list 생성, μ•„λ‹ˆλΌλ©΄ μ›λž˜ lines κ·ΈλŒ€λ‘œ κ°’ μœ μ§€

# lines 개수만큼 반볡문 μ‹€ν–‰
for filename_idx, line in enumerate(lines):

    # 파일λͺ…, μ‚¬λžŒid
    filename, person_id = line.split(',')
    print('idx: {} \tProcessing.\t{}'.format(filename_idx, filename))  # 파일 인덱슀번호, 파일λͺ… 좜λ ₯

    video_pathname = os.path.join(args.video_direc, filename+'.mp4')  # μ˜μƒλ””λ ‰ν† λ¦¬ + 파일λͺ….mp4 
    dst_pathname = os.path.join( args.save_direc, filename+'.npz')  # μ €μž₯디렉토리 + 파일λͺ….npz

    # 파일이 μžˆλŠ”μ§€ 확인, μ—†μœΌλ©΄ AssertionError λ©”μ‹œμ§€λ₯Ό 띄움
    assert os.path.isfile(video_pathname), "File does not exist. Path input: {}".format(video_pathname)  # μ›ν•˜λŠ” 쑰건의 λ³€μˆ˜κ°’μ„ λ³΄μ¦ν•˜κΈ° μœ„ν•΄ μ‚¬μš©

    # wav 파일 μ½λŠ” 라이브러리: librosa
    # librosa 둜 데이터λ₯Ό 읽으면 데이터 λ²”μœ„κ°€ [-1,1]둜 μ •κ·œν™”λ¨
    # librosa μž…λ ₯μ—μ„œ sr=None 으둜 μ§€μ •ν•˜μ§€ μ•Šκ³  μž„μ˜μ˜ sample_rateλ₯Ό μ„€μ •ν•˜λ©΄ loadν•  λ•Œ resampling μˆ˜ν–‰ν•¨
    data = librosa.load(video_pathname, sr=16000)[0][-19456:]
    save2npz(dst_pathname, data=data)  # librosa 둜 읽은 데이터λ₯Ό npz ν˜•μ‹μœΌλ‘œ μ €μž₯