|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright 2025 Xiaomi Corp. (authors: Fangjun Kuang) |
| 3 | + |
| 4 | +# Please run this file on your rk3588 board |
| 5 | + |
| 6 | +try: |
| 7 | + from rknnlite.api import RKNNLite |
| 8 | +except: |
| 9 | + print("Please run this file on your board (linux + aarch64 + npu)") |
| 10 | + print("You need to install rknn_toolkit_lite2") |
| 11 | + print( |
| 12 | + " from https://github.com/airockchip/rknn-toolkit2/tree/master/rknn-toolkit-lite2/packages" |
| 13 | + ) |
| 14 | + print( |
| 15 | + "https://github.com/airockchip/rknn-toolkit2/blob/v2.1.0/rknn-toolkit-lite2/packages/rknn_toolkit_lite2-2.1.0-cp310-cp310-linux_aarch64.whl" |
| 16 | + ) |
| 17 | + print("is known to work") |
| 18 | + raise |
| 19 | + |
| 20 | +import time |
| 21 | +from pathlib import Path |
| 22 | +from typing import Tuple |
| 23 | + |
| 24 | +import numpy as np |
| 25 | +import soundfile as sf |
| 26 | + |
| 27 | + |
| 28 | +def load_audio(filename: str) -> Tuple[np.ndarray, int]: |
| 29 | + data, sample_rate = sf.read( |
| 30 | + filename, |
| 31 | + always_2d=True, |
| 32 | + dtype="float32", |
| 33 | + ) |
| 34 | + data = data[:, 0] # use only the first channel |
| 35 | + |
| 36 | + samples = np.ascontiguousarray(data) |
| 37 | + return samples, sample_rate |
| 38 | + |
| 39 | + |
| 40 | +def init_model(filename, target_platform="rk3588"): |
| 41 | + if not Path(filename).is_file(): |
| 42 | + exit(f"{filename} does not exist") |
| 43 | + |
| 44 | + rknn_lite = RKNNLite(verbose=False) |
| 45 | + ret = rknn_lite.load_rknn(path=filename) |
| 46 | + if ret != 0: |
| 47 | + exit(f"Load model {filename} failed!") |
| 48 | + |
| 49 | + ret = rknn_lite.init_runtime(core_mask=RKNNLite.NPU_CORE_0) |
| 50 | + if ret != 0: |
| 51 | + exit(f"Failed to init rknn runtime for {filename}") |
| 52 | + return rknn_lite |
| 53 | + |
| 54 | + |
| 55 | +class RKNNModel: |
| 56 | + def __init__(self, model: str, target_platform="rk3588"): |
| 57 | + self.model = init_model(model) |
| 58 | + |
| 59 | + def release(self): |
| 60 | + self.model.release() |
| 61 | + |
| 62 | + def __call__(self, x: np.ndarray, h: np.ndarray, c: np.ndarray): |
| 63 | + """ |
| 64 | + Args: |
| 65 | + x: (1, 512), np.float32 |
| 66 | + h: (2, 1, 64), np.float32 |
| 67 | + c: (2, 1, 64), np.float32 |
| 68 | + Returns: |
| 69 | + prob: |
| 70 | + next_h: |
| 71 | + next_c |
| 72 | + """ |
| 73 | + out, next_h, next_c = self.model.inference(inputs=[x, h, c]) |
| 74 | + return out.item(), next_h, next_c |
| 75 | + |
| 76 | + |
| 77 | +def main(): |
| 78 | + model = RKNNModel(model="./m.rknn") |
| 79 | + for i in range(1): |
| 80 | + test(model) |
| 81 | + |
| 82 | + |
| 83 | +def test(model): |
| 84 | + print("started") |
| 85 | + start = time.time() |
| 86 | + samples, sample_rate = load_audio("./lei-jun-test.wav") |
| 87 | + assert sample_rate == 16000, sample_rate |
| 88 | + |
| 89 | + window_size = 512 |
| 90 | + |
| 91 | + h = np.zeros((2, 1, 64), dtype=np.float32) |
| 92 | + c = np.zeros((2, 1, 64), dtype=np.float32) |
| 93 | + |
| 94 | + threshold = 0.5 |
| 95 | + num_windows = samples.shape[0] // window_size |
| 96 | + out = [] |
| 97 | + for i in range(num_windows): |
| 98 | + print(i, num_windows) |
| 99 | + this_samples = samples[i * window_size : (i + 1) * window_size] |
| 100 | + prob, h, c = model(this_samples[None], h, c) |
| 101 | + out.append(prob > threshold) |
| 102 | + |
| 103 | + min_speech_duration = 0.25 * sample_rate / window_size |
| 104 | + min_silence_duration = 0.25 * sample_rate / window_size |
| 105 | + |
| 106 | + result = [] |
| 107 | + last = -1 |
| 108 | + for k, f in enumerate(out): |
| 109 | + if f >= threshold: |
| 110 | + if last == -1: |
| 111 | + last = k |
| 112 | + elif last != -1: |
| 113 | + if k - last > min_speech_duration: |
| 114 | + result.append((last, k)) |
| 115 | + last = -1 |
| 116 | + |
| 117 | + if last != -1 and k - last > min_speech_duration: |
| 118 | + result.append((last, k)) |
| 119 | + |
| 120 | + if not result: |
| 121 | + print("Empty for ./lei-jun-test.wav") |
| 122 | + return |
| 123 | + |
| 124 | + print(result) |
| 125 | + |
| 126 | + final = [result[0]] |
| 127 | + for r in result[1:]: |
| 128 | + f = final[-1] |
| 129 | + if r[0] - f[1] < min_silence_duration: |
| 130 | + final[-1] = (f[0], r[1]) |
| 131 | + else: |
| 132 | + final.append(r) |
| 133 | + |
| 134 | + for f in final: |
| 135 | + start = f[0] * window_size / sample_rate |
| 136 | + end = f[1] * window_size / sample_rate |
| 137 | + print("{:.3f} -- {:.3f}".format(start, end)) |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + main() |
0 commit comments