完成语音识别功能(win测试,arm未知)

This commit is contained in:
HP
2026-06-14 20:31:04 +08:00
parent de85a8d41d
commit e0946fbf36
900 changed files with 272942 additions and 11214 deletions
+45
View File
@@ -0,0 +1,45 @@
import argparse
import numpy as np
import wave
def read_wav_int16(path: str):
with wave.open(path, "rb") as wav:
sample_width = wav.getsampwidth()
if sample_width != 2:
raise ValueError(f"{path} is {sample_width * 8}-bit PCM; expected 16-bit")
sample_rate = wav.getframerate()
channels = wav.getnchannels()
frame_count = wav.getnframes()
raw = wav.readframes(frame_count)
samples = np.frombuffer(raw, dtype="<i2").astype(np.int16, copy=False)
return samples, sample_rate, channels
def write_wav_int16(path: str, samples: np.ndarray, sample_rate: int, channels: int):
with wave.open(path, "wb") as wav:
wav.setnchannels(channels)
wav.setsampwidth(2)
wav.setframerate(sample_rate)
wav.writeframes(np.asarray(samples, dtype="<i2").tobytes())
def main():
parser = argparse.ArgumentParser(description="Amplify a WAV file.")
parser.add_argument("input", type=str, help="Input WAV path")
parser.add_argument("output", type=str, help="Output WAV path")
parser.add_argument("--gain", type=float, default=5.0, help="Gain factor")
args = parser.parse_args()
data, sr, channels = read_wav_int16(args.input)
print(f"Original max abs: {np.max(np.abs(data))}")
amplified = np.clip(data.astype(np.float32) * args.gain, -32768, 32767).astype(np.int16)
print(f"Amplified max abs: {np.max(np.abs(amplified))}")
write_wav_int16(args.output, amplified, sr, channels)
print(f"Saved: {args.output}")
if __name__ == "__main__":
main()
+72
View File
@@ -0,0 +1,72 @@
import sys
import wave
import math
import numpy as np
# Insert project paths to import the test script's functions
sys.path.insert(0, "src/Apps/Game/tools")
import kws_python_test as kws
def load_wav(path):
with wave.open(path, "rb") as wav:
sample_rate = wav.getframerate()
channels = wav.getnchannels()
frames = wav.readframes(wav.getnframes())
samples = np.frombuffer(frames, dtype="<i2").astype(np.int16, copy=False)
return samples, sample_rate, channels
def main():
model_path = "E:/kws_model/kws_ref_model_float32.tflite"
wav_path = "E:/kws_model/my_up_gain5.wav"
print(f"Loading model: {model_path}")
backend, interpreter = kws.load_interpreter(model_path)
print(f"Backend: {backend}")
print(f"Loading WAV: {wav_path}")
samples, sample_rate, channels = load_wav(wav_path)
print(f" samples: {samples.size}, rate: {sample_rate}, channels: {channels}")
print(f" raw max abs: {np.max(np.abs(samples))}")
print("Preparing audio...")
prepared = kws.prepare_audio(samples, sample_rate, channels, trim=True)
print(f" prepared size: {prepared.size}")
print(f" prepared max abs: {np.max(np.abs(prepared))}")
if prepared.size == 0:
print("ERROR: prepared audio is empty!")
return
print("Extracting features for first window...")
window = kws.normalize_window(prepared, 0)
print(f" window max: {np.max(window)}")
print(f" window min: {np.min(window)}")
print(f" window non-zero count: {np.count_nonzero(window)}")
features = kws.extract_mfcc_features(window)
print(f" features shape: {features.shape}")
print(f" features max: {np.max(features)}")
print(f" features min: {np.min(features)}")
print(f" features mean: {np.mean(features)}")
print("Running inference...")
input_details = interpreter.get_input_details()[0]
output_details = interpreter.get_output_details()[0]
tensor = kws.tensor_from_features(features, input_details)
print(f" tensor shape: {tensor.shape}")
print(f" tensor max: {np.max(tensor)}")
interpreter.set_tensor(input_details["index"], tensor)
interpreter.invoke()
raw_output = interpreter.get_tensor(output_details["index"])
print(f" raw output shape: {raw_output.shape}")
print(f" raw output dtype: {raw_output.dtype}")
print(f" raw output: {raw_output}")
print(f" raw output max: {np.max(raw_output)}")
print(f" raw output min: {np.min(raw_output)}")
print(f" softmax: {np.exp(raw_output - np.max(raw_output)) / np.sum(np.exp(raw_output - np.max(raw_output)))}")
if __name__ == "__main__":
main()