Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,72 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
import
|
| 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 |
-
app.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import numpy as np
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from flask import Flask, request, jsonify
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
app = Flask(__name__)
|
| 9 |
+
|
| 10 |
+
# β
Correct Paths (IMPORTANT FIX)
|
| 11 |
+
MODEL_PATH = "animal_cnn.keras"
|
| 12 |
+
CLASS_NAMES_PATH = "class_names.json"
|
| 13 |
+
|
| 14 |
+
# β
Load Model
|
| 15 |
+
print("β³ Loading model...")
|
| 16 |
+
model = tf.keras.models.load_model(MODEL_PATH)
|
| 17 |
+
|
| 18 |
+
# β
Load Class Names
|
| 19 |
+
with open(CLASS_NAMES_PATH, "r") as f:
|
| 20 |
+
class_names = json.load(f)
|
| 21 |
+
|
| 22 |
+
print("β
Model loaded successfully!")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# β
Image Preprocessing Function
|
| 26 |
+
def preprocess_image(image):
|
| 27 |
+
image = image.resize((224, 224)) # same as training
|
| 28 |
+
image = np.array(image) / 255.0
|
| 29 |
+
image = np.expand_dims(image, axis=0)
|
| 30 |
+
return image
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
# β
Home Route (Test)
|
| 34 |
+
@app.route("/")
|
| 35 |
+
def home():
|
| 36 |
+
return "πΎ Animal Classifier API is Running!"
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# β
Prediction Route
|
| 40 |
+
@app.route("/predict", methods=["POST"])
|
| 41 |
+
def predict():
|
| 42 |
+
try:
|
| 43 |
+
if "file" not in request.files:
|
| 44 |
+
return jsonify({"error": "No file uploaded"}), 400
|
| 45 |
+
|
| 46 |
+
file = request.files["file"]
|
| 47 |
+
|
| 48 |
+
# Open image
|
| 49 |
+
image = Image.open(file).convert("RGB")
|
| 50 |
+
|
| 51 |
+
# Preprocess
|
| 52 |
+
processed_image = preprocess_image(image)
|
| 53 |
+
|
| 54 |
+
# Predict
|
| 55 |
+
predictions = model.predict(processed_image)[0]
|
| 56 |
+
top_index = np.argmax(predictions)
|
| 57 |
+
confidence = float(predictions[top_index])
|
| 58 |
+
|
| 59 |
+
result = {
|
| 60 |
+
"prediction": class_names[top_index],
|
| 61 |
+
"confidence": round(confidence * 100, 2)
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
return jsonify(result)
|
| 65 |
+
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return jsonify({"error": str(e)}), 500
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# β
Run App
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
app.run(host="0.0.0.0", port=7860)
|
|
|