Created
July 11, 2012 13:07
-
-
Save breuderink/3090287 to your computer and use it in GitHub Desktop.
Proof of concept of using Flask to serve EEG.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import numpy as np | |
import requests | |
for i in range(1000): | |
# get some EEG samples | |
r = requests.get('http://localhost:5000/eeg/%d' % i) | |
S = np.fromstring(r.json['samples'].decode('base64'), np.float32) | |
S.shape = r.json['shape'] | |
print 'Received:', r.json.keys(), S.shape | |
# publish classification results | |
probabilities = np.random.rand(3) | |
r = requests.post('http://localhost:5000/prediction/%d' % i, | |
data=dict(probs=json.dumps(probabilities.tolist()))) | |
print 'Sent:', r |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import numpy as np | |
from flask import Flask, request | |
app = Flask(__name__) | |
state = {} | |
@app.route('/') | |
def index(): | |
return r'Welcome. Try some <a href="eeg/1">JSON encoded EEG</a>.' | |
@app.route('/eeg/<int:offset>') | |
def data(offset): | |
samples = np.random.randn(16, 10).astype(np.float32) | |
return json.dumps(dict( | |
offset=offset, format='float32', | |
samples=samples.tostring().encode('base64'), | |
shape=samples.shape)) | |
@app.route('/prediction/<int:offset>', methods=['GET', 'POST']) | |
def prediction(offset): | |
if request.method == 'POST': | |
state[offset] = request.form['probs'] | |
return 'done.' | |
else: | |
return repr(state.get(offset, '?')) | |
if __name__ == '__main__': | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment