iot-logic/src/preprocessor/soundpreprocessor.py

47 lines
1.5 KiB
Python

#!/usr/bin/env python3
import requests
from pyAudioAnalysis import audioTrainTest as aT
from pyAudioAnalysis.audioTrainTest import load_model, load_model_knn
from utils import config
from .abcpreprocessor import AbcPreProcessor
"""
Abstract base class for Sender
"""
__author__ = "@tormakris"
__copyright__ = "Copyright 2020, Birbnetes Team"
__module_name__ = "soundpreprocessor"
__version__text__ = "1"
class SoundPreProcessor(AbcPreProcessor):
"""
SoundPreProcessor class, responsible for detecting birb chirps in sound sample.
"""
def preprocesssignal(self, signal: str) -> bool:
"""
Classify a sound sample.
:param signal: Access path of the sound sample up for processing.
:return:
"""
# TODO: Dynamic model injection?
r = requests.get(f"http://model-service/model/{config.MODEL_ID}/details")
r.raise_for_status()
model_details = r.json()
if model_details['type'] == 'knn':
classifier, mean, std, classes, mid_window, mid_step, short_window, short_step, compute_beat \
= load_model_knn(config.MODEL_ID + "MEANS")
else:
classifier, mean, std, classes, mid_window, mid_step, short_window, short_step, compute_beat \
= load_model(config.MODEL_ID + "MEANS")
target_id = classes.index(config.TARGET_NAME)
class_id, probability = aT.file_classification(signal, config.MODEL_ID, "svm")
return class_id == target_id