Abhijit Rai commited on
app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from flask import Flask, request, jsonify
|
| 5 |
+
from tensorflow.keras.models import load_model
|
| 6 |
+
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
| 7 |
+
from tensorflow.keras.applications.resnet50 import preprocess_input
|
| 8 |
+
from sklearn.preprocessing import StandardScaler
|
| 9 |
+
|
| 10 |
+
# Flask app setup
|
| 11 |
+
app = Flask(_name_)
|
| 12 |
+
|
| 13 |
+
# Load the model
|
| 14 |
+
MODEL_PATH = "fine_tuned_lake_event_prediction_model_with_xgb.keras"
|
| 15 |
+
model = load_model(MODEL_PATH)
|
| 16 |
+
|
| 17 |
+
# Parameters
|
| 18 |
+
time_steps_image = 4
|
| 19 |
+
target_img_size = (224, 224)
|
| 20 |
+
|
| 21 |
+
# Preprocessing functions
|
| 22 |
+
def preprocess_sensor_data(sensor_data):
|
| 23 |
+
"""Preprocess sensor data by normalizing."""
|
| 24 |
+
scaler = StandardScaler()
|
| 25 |
+
return scaler.fit_transform(sensor_data)
|
| 26 |
+
|
| 27 |
+
def preprocess_image(image_path):
|
| 28 |
+
"""Preprocess a single image."""
|
| 29 |
+
try:
|
| 30 |
+
image = load_img(image_path, target_size=target_img_size)
|
| 31 |
+
image_array = img_to_array(image)
|
| 32 |
+
return preprocess_input(image_array)
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print(f"Error processing image {image_path}: {e}")
|
| 35 |
+
return np.zeros((224, 224, 3), dtype=np.float32)
|
| 36 |
+
|
| 37 |
+
def preprocess_image_sequences(image_paths):
|
| 38 |
+
"""Preprocess a sequence of images."""
|
| 39 |
+
images = [preprocess_image(img_path) for img_path in image_paths]
|
| 40 |
+
return np.expand_dims(np.array(images), axis=0)
|
| 41 |
+
|
| 42 |
+
# Routes
|
| 43 |
+
@app.route('/predict', methods=['POST'])
|
| 44 |
+
def predict():
|
| 45 |
+
"""Predict based on sensor data and image sequences."""
|
| 46 |
+
try:
|
| 47 |
+
# Extract JSON payload
|
| 48 |
+
data = request.json
|
| 49 |
+
|
| 50 |
+
# Sensor data preprocessing
|
| 51 |
+
sensor_data = np.array(data['sensor_data']).reshape(1, -1)
|
| 52 |
+
sensor_data = preprocess_sensor_data(sensor_data)
|
| 53 |
+
|
| 54 |
+
# Image data preprocessing
|
| 55 |
+
image_paths = data['image_paths']
|
| 56 |
+
if len(image_paths) != time_steps_image:
|
| 57 |
+
return jsonify({"error": f"Exactly {time_steps_image} image paths are required."}), 400
|
| 58 |
+
|
| 59 |
+
image_sequences = preprocess_image_sequences(image_paths)
|
| 60 |
+
|
| 61 |
+
# Make prediction
|
| 62 |
+
prediction = model.predict([sensor_data, image_sequences])
|
| 63 |
+
result = float(prediction[0][0])
|
| 64 |
+
|
| 65 |
+
return jsonify({"prediction": result})
|
| 66 |
+
|
| 67 |
+
except Exception as e:
|
| 68 |
+
return jsonify({"error": str(e)}), 500
|
| 69 |
+
|
| 70 |
+
# Main entry point
|
| 71 |
+
if _name_ == "_main_":
|
| 72 |
+
app.run(debug=True)
|