Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import numpy as np
|
3 |
+
import tensorflow as tf
|
4 |
+
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
5 |
+
from tensorflow.keras.models import load_model
|
6 |
+
from PIL import Image
|
7 |
+
from flask import Flask, request, jsonify
|
8 |
+
|
9 |
+
app = Flask(__name__)
|
10 |
+
model = None
|
11 |
+
class_dict = None
|
12 |
+
|
13 |
+
def load_saved_model():
|
14 |
+
global model
|
15 |
+
global class_dict
|
16 |
+
model = load_model("path/to/your/saved/model.h5") # Update with the actual path to your saved model
|
17 |
+
class_dict = {0: "Class 1", 1: "Class 2", 2: "Class 3"} # Update with the actual class names
|
18 |
+
|
19 |
+
@app.route("/predict", methods=["POST"])
|
20 |
+
def predict():
|
21 |
+
if "image" not in request.files:
|
22 |
+
return jsonify({"error": "No image found in the request."}), 400
|
23 |
+
|
24 |
+
image = request.files["image"]
|
25 |
+
image = Image.open(image)
|
26 |
+
image = image.resize((75, 75))
|
27 |
+
image = np.array(image) / 255.0
|
28 |
+
image = np.expand_dims(image, axis=0)
|
29 |
+
|
30 |
+
prediction = model.predict(image)
|
31 |
+
class_id = np.argmax(prediction)
|
32 |
+
class_name = class_dict.get(class_id, "Unknown")
|
33 |
+
|
34 |
+
return jsonify({"class_id": class_id, "class_name": class_name}), 200
|
35 |
+
|
36 |
+
if __name__ == "__main__":
|
37 |
+
load_saved_model()
|
38 |
+
app.run()
|