|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import os |
|
|
from flask import Flask, request, jsonify |
|
|
import numpy as np |
|
|
import tensorflow as tf |
|
|
from sklearn.preprocessing import StandardScaler |
|
|
import joblib |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
|
|
|
scaler = StandardScaler() |
|
|
scaler.mean_ = np.loadtxt('scaler_mean.csv', delimiter=',') |
|
|
scaler.scale_ = np.loadtxt('scaler_std.csv', delimiter=',') |
|
|
|
|
|
|
|
|
interpreter = tf.lite.Interpreter(model_path="walking_classifier.tflite") |
|
|
interpreter.allocate_tensors() |
|
|
|
|
|
input_details = interpreter.get_input_details() |
|
|
output_details = interpreter.get_output_details() |
|
|
|
|
|
@app.route('/predict', methods=['POST']) |
|
|
def predict(): |
|
|
try: |
|
|
|
|
|
input_data = request.json['data'] |
|
|
|
|
|
|
|
|
input_data = np.array(input_data, dtype=np.float32) |
|
|
|
|
|
|
|
|
input_data = scaler.transform(input_data) |
|
|
|
|
|
|
|
|
if input_data.shape[1] != input_details[0]['shape'][1]: |
|
|
return jsonify({"error": "Input shape does not match model expected shape."}) |
|
|
|
|
|
|
|
|
predictions = [] |
|
|
|
|
|
|
|
|
for i in range(input_data.shape[0]): |
|
|
single_input_data = input_data[i].reshape(1, -1) |
|
|
interpreter.set_tensor(input_details[0]['index'], single_input_data) |
|
|
interpreter.invoke() |
|
|
output_data = interpreter.get_tensor(output_details[0]['index'])[0] |
|
|
predictions.append(float(output_data)) |
|
|
|
|
|
|
|
|
threshold = 0.5 |
|
|
predicted_labels = (np.array(predictions) > threshold).astype(int).tolist() |
|
|
|
|
|
|
|
|
return jsonify({"predictions": predicted_labels}) |
|
|
except Exception as e: |
|
|
return jsonify({"error": str(e)}) |
|
|
|
|
|
if __name__ == '__main__': |
|
|
port = int(os.environ.get('PORT', 8080)) |
|
|
app.run(host='0.0.0.0', port=port) |
|
|
|