File size: 2,391 Bytes
7661f93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import argparse
from pathlib import Path

import cv2 as cv
from glob import glob
import pandas as pd
import numpy as np
from scipy.io import wavfile
from tqdm import tqdm

from project_settings import project_path
from toolbox.python_speech_features.misc import wave2spectrum_image


def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--template_dir",
        default=(project_path / "data/early_media/62/templates/").as_posix(),
        type=str
    )
    parser.add_argument(
        "--wav_dir",
        default=(project_path / "data/early_media/62/wav/").as_posix(),
        type=str
    )
    args = parser.parse_args()
    return args


def main():
    args = get_args()

    template_dir = Path(args.template_dir)
    wav_dir = Path(args.wav_dir)

    max_wave_value = 32768.0

    result = list()
    for template_file in template_dir.glob("*/*.wav"):
        template_label = template_file.parts[-2]
        _, template = wavfile.read(template_file)
        template = template / max_wave_value
        template = wave2spectrum_image(
            wave=template,
            sample_rate=8000,
            # n_low_freq=100
        )
        template = template.T

        for wav_file in tqdm(wav_dir.glob("*/*.wav")):
            wav_label = wav_file.parts[-2]
            _, signal = wavfile.read(wav_file)
            signal = signal[:12 * 8000]
            signal = signal / max_wave_value

            spectrum = wave2spectrum_image(
                wave=signal,
                sample_rate=8000,
                # n_low_freq=100
            )
            spectrum = spectrum.T

            sqdiff_normed = cv.matchTemplate(image=spectrum, templ=template, method=cv.TM_SQDIFF_NORMED)
            min_val, _, min_loc, _ = cv.minMaxLoc(sqdiff_normed)
            # msg = "label1: {}; label2: {}; min_val: {}".format(label1, label2, min_val)
            # print(msg)

            row = {
                "template_label": template_label,
                "template_file": template_file.as_posix(),
                "wav_label": wav_label,
                "wav_file": wav_file.as_posix(),
                "min_val": min_val,
            }
            result.append(row)

    result = pd.DataFrame(result)
    result.to_excel("result.xlsx", index=False)
    return


if __name__ == '__main__':
    main()