bertani / app.py
huha3's picture
Update app.py
7cf4d1c verified
Raw History Blame Contribute Delete
1.17 kB
from flask import Flask, request, jsonify
from flask_cors import CORS
from tensorflow.keras.models import load_model
from PIL import Image
import numpy as np
import requests
from io import BytesIO
app = Flask(__name__)
CORS(app) # biar bisa diakses dari frontend
# Load model
model = load_model("model/Model_CNN_256px.keras")
class_labels = {
0: "Bercak Daun (Leaf Spot)",
1: "Hawar Daun (Blight)",
2: "Karat Daun (Rust)",
3: "Sehat (Healthy)"
}
@app.route("/api/model", methods=["POST"])
def predict():
data = request.json
image_url = data.get("image_url")
if not image_url:
return jsonify({"error": "image_url missing"}), 400
# Preprocess gambar
response = requests.get(image_url)
img = Image.open(BytesIO(response.content)).convert("RGB")
img = img.resize((256, 256))
img = np.array(img) / 255.0
img = np.expand_dims(img, axis=0)
preds = model.predict(img)
idx = int(np.argmax(preds[0]))
return jsonify({
"nama_penyakit": class_labels[idx],
"akurasi": round(float(preds[0][idx]) * 100, 2)
})
@app.route("/health")
def health():
return jsonify({"status": "ok"})