| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- import sounddevice as sd
- from scipy.io.wavfile import write
- import librosa
- import numpy as np
- from tensorflow.keras.models import load_model
- import pyaudio
- class HotwordDetector:
- def __init__(self, model_path):
- self.model_path = model_path
- self.LoadModel()
- self.rec_duration = 3
-
- def LoadModel(self):
- #self.interpreter = tf.lite.Interpreter(model_path=self.model_path)
- #self.interpreter.allocate_tensors()
- #self.input_details = self.interpreter.get_input_details()
- #self.output_details = self.interpreter.get_output_details()
- self.audio = pyaudio.PyAudio()
- self.model = load_model(self.model_path)
- self.mic_stream = self.audio.open(
- format=pyaudio.paInt16,
- channels=1,
- rate=44100,
- input=True,
- output=True,
- frames_per_buffer=4096)
-
- #def record_audio(self):
- # p = pyaudio.PyAudio()
- # stream = p.open(format=pyaudio.paInt16,
- # channels=1,
- # rate=16000,
- # input=True,
- # frames_per_buffer=self.chunk_size)
- # frames = []
- # for _ in range(int(16000 / self.chunk_size * 1)):
- # data = stream.read(self.chunk_size)
- # frames.append(np.frombuffer(data, dtype=np.int16))
- # stream.stop_stream()
- # stream.close()
- # p.terminate()
- # return np.concatenate(frames)
- #def predict(self, audio_data):
- # input_data = np.expand_dims(audio_data, axis=0).astype(np.float32)
- # self.interpreter.set_tensor(self.input_details[0]['index'], input_data)
- # self.interpreter.invoke()
- # output_data = self.interpreter.get_tensor(self.output_details[0]['index'])
- # return output_data[0][0]
- def detect_hotword(self):
- if True:
- myrecording = sd.rec(int(3 * 44100), samplerate=44100, channels=2)
- sd.wait()
- write("tmp.wav", 44100, myrecording)
- audio, sample_rate = librosa.load("tmp.wav")
- mfcc = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40)
- mfcc_processed = np.mean(mfcc.T, axis=0)
- prediction = self.model.predict(np.expand_dims(mfcc_processed, axis=0))
- else:
- data = self.mic_stream.read(4096)
- data_float = np.fromstring(data , dtype=np.float16)
- data_np = np.array(data_float , dtype='d')
- mfcc = librosa.feature.mfcc(data_np.flatten(), 44100, n_mfcc=40)
- mfcc_processed = np.mean(mfcc.T, axis=0)
- prediction = self.model.predict(np.expand_dims(mfcc_processed, axis=0))
- print(prediction[0])
- print(prediction[0][1])
- print(prediction[0][2])
- if prediction[0][1] > 0.90:
- print(f"Wake Word 'Trixy' Detected")
- print("Confidence:", prediction[0][1])
- elif prediction[0][2] > 0.90:
- print(f"Wake Word 'SystemCommand' Detected")
- print("Confidence:", prediction[0][2])
- else:
- print("Wake Word NOT Detected")
- print("Confidence:", prediction[0][0])
|