Datasets:
License:
#!/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() | |