legacy/SERVER_CODE/websocket_server.py

37 lines
1.2 KiB
Python
Raw Normal View History

2020-07-13 19:42:06 +02:00
import asyncio
import websockets
import cnn_classifier
import donwlink_message
import numpy as np
from scipy.io import wavfile
server_classifier=None
server_ip="192.168.1.71"
server_port=8765
model_struct = 'model_mukcso_batch256.json'
model_weights = "best_model_mukcso_batch256.h5"
def background(f):
def wrapped(*args, **kwargs):
return asyncio.get_event_loop().run_in_executor(None, f, *args, *kwargs)
return wrapped
async def service(websocket, path):
buf = await websocket.recv()
decoded = np.frombuffer(buf, dtype=np.int16)
print("Wav arrived!")
wavfile.write("arrived.wav", 44100, decoded.astype(np.int16))
await websocket.send("Wav arrived to Server.")
prediction=server_classifier.predict("arrived.wav")
if prediction[0] == 'sturnus':
alert()
@background
def alert():
donwlink_message.send_to_device("RequestAlert")
def start_websocket_server():
global server_classifier
server_classifier = cnn_classifier.classifier(model_struct,model_weights)
start_server = websockets.serve(service, server_ip, server_port)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()