File size: 2,517 Bytes
95b61df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# from flask import Flask, request, jsonify
# import tensorflow as tf
# import numpy as np

# app = Flask(__name__)

# # Load the model
# model = tf.keras.models.load_model('walking_classifier_tf.h5')

# @app.route('/predict', methods=['POST'])
# def predict():
#     data = request.get_json(force=True)
#     predictions = model.predict(np.array(data['features']))
#     return jsonify({'predictions': predictions.tolist()})

# if __name__ == '__main__':
#     app.run(host='0.0.0.0', port=5000)


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__)

# Load the saved scaler and model
scaler = StandardScaler()
scaler.mean_ = np.loadtxt('scaler_mean.csv', delimiter=',')
scaler.scale_ = np.loadtxt('scaler_std.csv', delimiter=',')

# Load the TFLite model and allocate tensors
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:
        # Get the data from the request
        input_data = request.json['data']

        # Convert to numpy array and reshape
        input_data = np.array(input_data, dtype=np.float32)

        # Normalize the data
        input_data = scaler.transform(input_data)

        # Check the input shape
        if input_data.shape[1] != input_details[0]['shape'][1]:
            return jsonify({"error": "Input shape does not match model expected shape."})

        # Prepare the prediction list
        predictions = []

        # Run the model for each input data
        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))

        # Convert to binary labels
        threshold = 0.5
        predicted_labels = (np.array(predictions) > threshold).astype(int).tolist()

        # Return the predictions as JSON
        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)