Commit c9ab2f6e authored by W.D.R.P. Sandeepa's avatar W.D.R.P. Sandeepa

changes

parent a3382838
from flask import jsonify
import os
import random
from flask import jsonify, Flask, request
from API.db.dbConnection import get_all_data, insert_data_json, insert_data, insert, get_data
from API.model.color.keyword_spotting_service import Keyword_Spotting_service
from API.util.util import getUUID
# get color activity 1 fun
......@@ -86,4 +89,43 @@ def save_color_session_details(userId, status):
qry = 'INSERT INTO colorsession (id,userId,token,status) VALUES (NULL, %s, %s, %s)'
args = (userId, token, status)
insert(qry, args)
return token
\ No newline at end of file
return token
#predict audio file
def sendPredictData(audioFile, name):
# print(f"{name}")
path = "color/test/green.wav"
# print(f"{path}")
audio_file = open(path, "rb")
values = {
"file": (path, audio_file, "audio/wav")
}
audio_file = values["file"]
file_name = str(random.randint(0, 100000))
audio_file.save((file_name))
# print(f"{path}")
# get file name
predict_file_name = audio_file.filename
new_predict_file_name = predict_file_name.split(".")
FPFN = new_predict_file_name[0]
# invoke keyword spotting service
kss = Keyword_Spotting_service()
# make a prediction
predicted_keyword = kss.predict(file_name, FPFN)
# remove the audio file
os.remove(file_name)
# send back the predicted keyword in json format
data = {"Keyword": predicted_keyword}
print(f"data: {data}")
return jsonify(data)
\ No newline at end of file
import tensorflow.keras as keras
import numpy as np
import librosa
MODEL_PATH = "model.h5"
NUM_SAMPLES_TO_CONSIDER = 22050 # 1 sec
class _Keyword_Spotting_Service:
model = None
_mappings = [
"black",
"blue",
"green",
"red",
"white",
"yellow"
]
_instance = None
# 78293
def predict(self, file_path, FPFN):
print(f"{file_path}")
# extract MFCCs
MFCCs = self.preprocess(file_path) # (# segment, # coefficients)
# convert 2d MFCCs array into 4d array -> (# samples, # segment, # coefficients, # channels)
MFCCs = MFCCs[np.newaxis, ..., np.newaxis]
# make prediction
predictions = self.model.predict(MFCCs)
predicted_index = np.argmax(predictions)
predicted_keyword = self._mappings[predicted_index]
# return predicted_keyword
if predicted_keyword == FPFN:
return predicted_keyword
else:
return "No Prediction"
def preprocess(self, file_path, n_mfcc=13, n_fft=2048, hop_length=512):
# load audio file
signal, sr = librosa.load(file_path)
# ensure consistency in the audio file length
if len(signal) > NUM_SAMPLES_TO_CONSIDER:
signal = signal[:NUM_SAMPLES_TO_CONSIDER]
# extract MFCCs
MFCCs = librosa.feature.mfcc(signal, n_mfcc=n_mfcc, n_fft=n_fft, hop_length=hop_length)
return MFCCs.T
def Keyword_Spotting_service():
# ensure that we only have 1 instance of KSS
if _Keyword_Spotting_Service._instance is None:
_Keyword_Spotting_Service._instance = _Keyword_Spotting_Service()
_Keyword_Spotting_Service.model = keras.models.load_model(MODEL_PATH)
return _Keyword_Spotting_Service._instance
if __name__ == "__main__":
kss = Keyword_Spotting_service()
keyword1 = kss.predict("test/black.wav")
keyword2 = kss.predict("test/blue.wav")
keyword3 = kss.predict("test/green.wav")
keyword4 = kss.predict("test/red.wav")
keyword5 = kss.predict("test/white.wav")
keyword6 = kss.predict("test/yellow.wav")
keyword7 = kss.predict("test/other.wav")
print(f"Predicted Keywords: {keyword1}")
print(f"Predicted Keywords: {keyword2}")
print(f"Predicted Keywords: {keyword3}")
print(f"Predicted Keywords: {keyword4}")
print(f"Predicted Keywords: {keyword5}")
print(f"Predicted Keywords: {keyword6}")
print(f"Predicted Keywords: {keyword7}")
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment