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

change activity interface

parent 6b30a2aa
......@@ -6,7 +6,7 @@
<excludeFolder url="file://$MODULE_DIR$/API/venv" />
<excludeFolder url="file://$MODULE_DIR$/backend/IT18257632/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.9" jdkType="Python SDK" />
<orderEntry type="jdk" jdkName="Python 3.9 (21_22j-38)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (21_22j-38)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
from flask import Flask, redirect, url_for, render_template, request, jsonify, make_response
import random
import os
from API.model.colorModel import get_color_activities1, get_color_activities2, get_color_activity_result
from API.model.readModel import get_reading_activities
from API.keyword_spotting_service import Keyword_Spotting_service
from API.model.colorModel import get_color_activities1, get_color_activities2
from API.routers.router import funtion_one
from backend.IT18218640.keyword_spotting_service import Keyword_Spotting_service
app = Flask(__name__)
# Initial Route
@app.route("/")
def home():
return render_template('home.html')
# Color Function Route (IT18218640)
# Get Color activities route
@app.route("/getColorActivities1")
def getColorActivities1():
return get_color_activities1()
......@@ -24,63 +24,71 @@ def getColorActivities1():
def getColorActivities2():
return get_color_activities2()
@app.route("/getColorActivitiesResult")
def getColorActivitiesResult():
# print("data_dic")
return get_color_activity_result()
@app.route("/predict", methods=["POST"])
def predict():
# get audio file and save it
audio_file = request.files["file"]
print(f"{request.files['file']}")
# print(f"{request.files['name']}")
data = request.get_json()
print((f"{data}"))
######################################
print(f"{request.form['name']}")
#####################################
# print(f"{request.json}")
fileitem = data['uri']
file_name = str(random.randint(0, 100000))
# audio_file.save((file_name))
# if fileitem.filename:
# # strip the leading path from the file name
fn = os.path.basename(fileitem)
#
# # open read and write the file into the server
open(fn, 'wb').write(fileitem.file.read())
resp = make_response(request.get_json())
resp.headers['Access-Control-Allow-Origin'] = '*'
resp.headers['Content-Type'] = 'application/json'
return resp
# get file name
# get audio file and save it
# audio_file = request.files["file"]
# audio_file = request.files["name"]
# print((f"{request.data}"))
# print((f"{request.form}"))
# print((f"{request.values}"))
# print((f"{request.json}"))
# print(f"{request.form['name']}")
# file_name = str(random.randint(0, 100000))
# audio_file.save((file_name))
#
# # get file name
# predict_file_name = audio_file.filename
# predict_file_name = predict_file_name.split("/")
# new_predict_file_name = predict_file_name[1]
# new_predict_file_name = new_predict_file_name.split(".")
# FPFN = new_predict_file_name[0]
# print(f"{FPFN}")
# # print(f"{FPFN}")
#
# # 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 keword in json format
# data = {"Keyword" : predicted_keyword}
# return jsonify("print")
# return "Print"
# invoke keyword spotting service
kss = Keyword_Spotting_service()
# make a prediction
predicted_keyword = kss.predict(file_name, request.form['name'])
# remove the audio file
os.remove(file_name)
# send back the predicted keword in json format
data = {"Keyword": predicted_keyword}
return jsonify(data)
# return "audio_file"
# Read Function Route (IT)
@app.route("/testings")
def checkothers():
return "testing funtions"
# Color Function Route (IT18218640)
@app.route("/reading")
def getReadActivities():
return get_reading_activities()
@app.route("/ru")
def abc():
response_val = funtion_one()
......@@ -91,9 +99,7 @@ def abc():
# return make_response(d, 200)
return response_val
if __name__ == "__main__":
app.run(host='192.168.8.101')
# app.run(host='192.168.8.100,port='5000', debug=True)
# app.run(debug=True)
app.run(host='192.168.8.102')
#app.run(host='192.168.8.100,port='5000', debug=True)
# app.run(debug=True)
\ 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
File added
import {
useNavigation
} from "@react-navigation/native";
import { useNavigation } from "@react-navigation/native";
import Orientation from 'react-native-orientation-locker';
import React, {
useEffect,
useState
} from "react";
import {
Text,
TouchableOpacity,
StyleSheet,
View,
Dimensions,
SafeAreaView,
ImageBackground,
Button,
Image,
StatusBar
} from 'react-native'
import React, { useEffect, useState } from "react";
import { Text,TouchableOpacity, StyleSheet, View, Dimensions, SafeAreaView, ImageBackground, Button, Image, StatusBar} from 'react-native'
import Permissions from 'react-native-permissions';
import AudioRecord from 'react-native-audio-record';
import axios from "axios";
import client from "../client/Client";
import BackButton from "../../component/BackButton"
export default function Blue(color) {
const navigation = useNavigation();
......@@ -46,7 +31,6 @@ export default function Blue(color) {
return unsubscribe;
}, [navigation]);
// audioInit
function audioInit() {
// console.log('audioInit');
......@@ -61,10 +45,9 @@ export default function Blue(color) {
AudioRecord.init(colorAudio);
}
// checkPermission
async function checkPermission() {
// console.log('checkPermission');
const p = await Permissions.check('microphone');
// console.log(p);
// console.log('permission check', p);
......@@ -79,13 +62,11 @@ export default function Blue(color) {
}
// requestPermission
async function requestPermission() {
const p = await Permissions.request('microphone');
// console.log('permission request', p);
}
// audioStart
async function audioStart() {
// console.log('audioStart');
......@@ -96,7 +77,6 @@ export default function Blue(color) {
}, 2000);
}
// audioStop
async function audioStop() {
// console.log('audioStop');
......@@ -104,12 +84,66 @@ export default function Blue(color) {
let audioFile = await AudioRecord.stop();
console.log('userFile', audioFile);
//call sendAudio funtion
sendAudio(audioFile);
// sendAudio(audioFile);
getAudio(audioFile);
}
// send audio to server function
function getAudio(audioFile) {
const formData = new FormData();
var obj = {
uri: `file://${audioFile}`,
type: 'audio/wav',
name: 'color.wav',
colorNmae: backColor
}
// formData.append(
// 'file',
// {
// uri: `file://${audioFile}`,
// type: 'audio/wav',
// name: 'color.wav',
// }
// );
// formData.append(
// 'name', backColor
// );
var data = JSON.stringify(obj)
console.log(data);
axios
.post("http://192.168.8.102:5000/predict", data, {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
})
.then((response) => {
// setNames(response.data);
console.log('Success part : ',response.data);
})
.catch(function (error) {
if (error.response) {
console.log('Error data : ', error.response.data);
console.log('Error status : ', error.response.status);
console.log('Error headers : ', error.response.headers);
} else if (error.request) {
console.log('Error request : ', error.request);
} else {
console.log('Error message : ', error.message);
}
console.log('Error else part : ', error.config);
});
}
async function sendAudio(audioFile) {
// append form data and upload to api
......@@ -193,16 +227,34 @@ export default function Blue(color) {
</View>
<View style={{ flexDirection: "row", marginLeft: 480 }}>
<Button style={styles.button} title="Recode" onPress={() => { audioStart(); }} />
<Button title="Return" color="#1DCE92" />
<Button title="Exit" color="#841584" />
<View style={{ marginTop: 10 }}>
<BackButton path="Color" />
</View>
<View style={{ flexDirection: "row", marginLeft: 420, marginTop: -80 }}>
<View style={{}}>
<TouchableOpacity onPress={() => { audioStart(); }}
style={{ width: "30%", height: "40%", borderRadius: 50 }}>
<Image source={require('../../assets/game/mic2.png')} resizeMode='contain' style={{ flex: 1, marginLeft: -190 }} />
</TouchableOpacity>
</View>
<View style={{ marginLeft: -100 }}>
<TouchableOpacity onPress={() => { }}
style={{ width: "60%", height: "40%", borderRadius: 50 }}>
<Image source={require('../../assets/game/next.png')} resizeMode='contain' style={{ flex: .9, marginLeft: -90 }} />
</TouchableOpacity>
</View>
</View>
</ImageBackground>
</View>
</SafeAreaView>
)
}
const styles = StyleSheet.create({
......@@ -244,7 +296,8 @@ const styles = StyleSheet.create({
button: {
padding: 10,
marginRight: 50,
color: "#000000"
color: "#000000",
marginLeft: 50,
}
})
\ No newline at end of file
......@@ -93,13 +93,13 @@ const Login = () => {
</View>
<View style={styles.form_input}>
<TouchableOpacity onPress={submitForm} style={styles.btn}>
<TouchableOpacity onPress={()=> { navigation.navigate("Home")}} style={styles.btn}>
<Text style={styles.btn_text}>
Sign In
</Text>
</TouchableOpacity>
</View>
{/* onPress={submitForm} */}
<View style={styles.text_if}>
<TouchableOpacity onPress={()=> { navigation.navigate("Register")}}>
<Text style={styles.btn_text2}>
......
......@@ -86,6 +86,8 @@ const Register = () => {
}
}
// import client from "../client/Client";
return (
<SafeAreaView>
<ScrollView>
......
import axios from 'axios';
export default axios.create({ baseURL: 'http://192.168.8.101:5000/',timeout: 15000, });
\ No newline at end of file
export default axios.create({ baseURL: 'http://192.168.8.102:5000/',timeout: 15000, });
\ 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