Spaces:
Runtime error
Runtime error
| import os | |
| import numpy as np | |
| import tensorflow as tf | |
| from tensorflow.keras.preprocessing.image import ImageDataGenerator | |
| from tensorflow.keras.models import load_model | |
| from PIL import Image | |
| from flask import Flask, request, jsonify | |
| app = Flask(__name__) | |
| model = None | |
| class_dict = None | |
| def load_saved_model(): | |
| global model | |
| global class_dict | |
| model = load_model("path/to/your/saved/model.h5") # Update with the actual path to your saved model | |
| class_dict = {0: "Class 1", 1: "Class 2", 2: "Class 3"} # Update with the actual class names | |
| def predict(): | |
| if "image" not in request.files: | |
| return jsonify({"error": "No image found in the request."}), 400 | |
| image = request.files["image"] | |
| image = Image.open(image) | |
| image = image.resize((75, 75)) | |
| image = np.array(image) / 255.0 | |
| image = np.expand_dims(image, axis=0) | |
| prediction = model.predict(image) | |
| class_id = np.argmax(prediction) | |
| class_name = class_dict.get(class_id, "Unknown") | |
| return jsonify({"class_id": class_id, "class_name": class_name}), 200 | |
| if __name__ == "__main__": | |
| load_saved_model() | |
| app.run() | |