|
| 1 | +# Copyright (C) 2024 Robotec.AI |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from typing import Any, Tuple |
| 16 | + |
| 17 | +from numpy.typing import NDArray |
| 18 | +from openwakeword.model import Model as OWWModel |
| 19 | +from openwakeword.utils import download_models |
| 20 | + |
| 21 | +from rai_asr.models import BaseVoiceDetectionModel |
| 22 | + |
| 23 | + |
| 24 | +class OpenWakeWord(BaseVoiceDetectionModel): |
| 25 | + def __init__(self, wake_word_model_path: str, threshold: float = 0.5): |
| 26 | + super(OpenWakeWord, self).__init__() |
| 27 | + self.model_name = "open_wake_word" |
| 28 | + download_models() |
| 29 | + self.model = OWWModel( |
| 30 | + wakeword_models=[ |
| 31 | + wake_word_model_path, |
| 32 | + ], |
| 33 | + inference_framework="onnx", |
| 34 | + ) |
| 35 | + self.threshold = threshold |
| 36 | + |
| 37 | + def detected( |
| 38 | + self, audio_data: NDArray, input_parameters: dict[str, Any] |
| 39 | + ) -> Tuple[bool, dict[str, Any]]: |
| 40 | + print(len(audio_data)) |
| 41 | + predictions = self.model.predict(audio_data) |
| 42 | + ret = input_parameters.copy() |
| 43 | + ret.update({self.model_name: {"predictions": predictions}}) |
| 44 | + for key, value in predictions.items(): |
| 45 | + if value > self.threshold: |
| 46 | + self.model.reset() |
| 47 | + return True, ret |
| 48 | + return False, ret |
0 commit comments