hotword_detection.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import sounddevice as sd
  4. from scipy.io.wavfile import write
  5. import librosa
  6. import numpy as np
  7. from tensorflow.keras.models import load_model
  8. import pyaudio
  9. class HotwordDetector:
  10. def __init__(self, model_path):
  11. self.model_path = model_path
  12. self.LoadModel()
  13. self.rec_duration = 3
  14. def LoadModel(self):
  15. #self.interpreter = tf.lite.Interpreter(model_path=self.model_path)
  16. #self.interpreter.allocate_tensors()
  17. #self.input_details = self.interpreter.get_input_details()
  18. #self.output_details = self.interpreter.get_output_details()
  19. self.audio = pyaudio.PyAudio()
  20. self.model = load_model(self.model_path)
  21. self.mic_stream = self.audio.open(
  22. format=pyaudio.paInt16,
  23. channels=1,
  24. rate=44100,
  25. input=True,
  26. output=True,
  27. frames_per_buffer=4096)
  28. #def record_audio(self):
  29. # p = pyaudio.PyAudio()
  30. # stream = p.open(format=pyaudio.paInt16,
  31. # channels=1,
  32. # rate=16000,
  33. # input=True,
  34. # frames_per_buffer=self.chunk_size)
  35. # frames = []
  36. # for _ in range(int(16000 / self.chunk_size * 1)):
  37. # data = stream.read(self.chunk_size)
  38. # frames.append(np.frombuffer(data, dtype=np.int16))
  39. # stream.stop_stream()
  40. # stream.close()
  41. # p.terminate()
  42. # return np.concatenate(frames)
  43. #def predict(self, audio_data):
  44. # input_data = np.expand_dims(audio_data, axis=0).astype(np.float32)
  45. # self.interpreter.set_tensor(self.input_details[0]['index'], input_data)
  46. # self.interpreter.invoke()
  47. # output_data = self.interpreter.get_tensor(self.output_details[0]['index'])
  48. # return output_data[0][0]
  49. def detect_hotword(self):
  50. if True:
  51. myrecording = sd.rec(int(3 * 44100), samplerate=44100, channels=2)
  52. sd.wait()
  53. write("tmp.wav", 44100, myrecording)
  54. audio, sample_rate = librosa.load("tmp.wav")
  55. mfcc = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40)
  56. mfcc_processed = np.mean(mfcc.T, axis=0)
  57. prediction = self.model.predict(np.expand_dims(mfcc_processed, axis=0))
  58. else:
  59. data = self.mic_stream.read(4096)
  60. data_float = np.fromstring(data , dtype=np.float16)
  61. data_np = np.array(data_float , dtype='d')
  62. mfcc = librosa.feature.mfcc(data_np.flatten(), 44100, n_mfcc=40)
  63. mfcc_processed = np.mean(mfcc.T, axis=0)
  64. prediction = self.model.predict(np.expand_dims(mfcc_processed, axis=0))
  65. print(prediction[0])
  66. print(prediction[0][1])
  67. print(prediction[0][2])
  68. if prediction[0][1] > 0.90:
  69. print(f"Wake Word 'Trixy' Detected")
  70. print("Confidence:", prediction[0][1])
  71. elif prediction[0][2] > 0.90:
  72. print(f"Wake Word 'SystemCommand' Detected")
  73. print("Confidence:", prediction[0][2])
  74. else:
  75. print("Wake Word NOT Detected")
  76. print("Confidence:", prediction[0][0])