File size: 2,296 Bytes
e187f2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Next Echo inference (NumPy only): find occurrences of an example sound in a recording.

Usage:
    python inference.py --query clap.wav --recording street.wav
    python inference.py --selftest
Audio must be 16 kHz mono 16-bit WAV for the CLI (resample elsewhere if needed).
"""
import argparse
import json
import os
import sys
import wave

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)

import numpy as np  # noqa: E402

import echo_core as ec  # noqa: E402
from nlrt import load_model as _load  # noqa: E402


def load(path=os.path.join(HERE, "model.json")):
    return ec.load_model(_load(path))


def read_wav(path):
    with wave.open(path, "rb") as w:
        assert w.getframerate() == 16000 and w.getnchannels() == 1 and w.getsampwidth() == 2, "need 16 kHz mono 16-bit"
        return np.frombuffer(w.readframes(w.getnframes()), dtype="<i2") / 32768.0


def find(model, query, recording, method="learned"):
    times, learned, base = ec.similarity_curves(model, query, recording)
    nxt, bl = ec.final_scores(learned, base, model["score"])
    sims = nxt if method == "learned" else bl
    th = model["thresholds"]["learned" if method == "learned" else "baseline"]
    return [{"start": round(a, 3), "end": round(b, 3), "score": round(s, 4)} for a, b, s in ec.detect(times, sims, th)]


def _selftest():
    model = load()
    with open(os.path.join(HERE, "selftest.json"), encoding="utf-8") as f:
        t = json.load(f)
    q, r = np.asarray(t["query"]) / 32768.0, np.asarray(t["recording"]) / 32768.0
    _, learned, base = ec.similarity_curves(model, q, r)
    diff = max(float(np.abs(learned - np.asarray(t["learned"])).max()), float(np.abs(base - np.asarray(t["base"])).max()))
    ok = diff < 1e-6
    print(json.dumps({"passed": ok, "max_abs_diff": diff, "windows": len(learned)}))
    return ok


if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("--selftest", action="store_true")
    ap.add_argument("--query")
    ap.add_argument("--recording")
    ap.add_argument("--method", default="learned", choices=["learned", "logmel"])
    a = ap.parse_args()
    if a.selftest:
        sys.exit(0 if _selftest() else 1)
    print(json.dumps(find(load(), read_wav(a.query), read_wav(a.recording), a.method), indent=1))