143 lines
3.1 KiB
Python
143 lines
3.1 KiB
Python
from flask import Flask, request, jsonify, render_template
|
|
from werkzeug.utils import secure_filename
|
|
from flask_cors import CORS
|
|
from src.predict import predict_image
|
|
import os
|
|
|
|
# =========================
|
|
# KONFIGURASI PATH
|
|
# =========================
|
|
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
UPLOAD_FOLDER = os.path.join(BASE_DIR, "temp")
|
|
|
|
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
|
|
|
ALLOWED_EXTENSIONS = {"png", "jpg", "jpeg"}
|
|
|
|
# =========================
|
|
# INIT FLASK
|
|
# =========================
|
|
app = Flask(
|
|
__name__,
|
|
template_folder="templates",
|
|
static_folder="static"
|
|
)
|
|
|
|
CORS(app)
|
|
|
|
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
|
|
|
|
# =========================
|
|
# HELPER FUNCTION
|
|
# =========================
|
|
def allowed_file(filename):
|
|
|
|
return "." in filename and \
|
|
filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
|
|
|
|
# =========================
|
|
# ROUTE HALAMAN WEB
|
|
# =========================
|
|
@app.route("/")
|
|
def index():
|
|
return render_template("index.html")
|
|
|
|
@app.route("/deteksi")
|
|
def deteksi():
|
|
return render_template("deteksi.html")
|
|
|
|
@app.route("/edukasi")
|
|
def edukasi():
|
|
return render_template("edukasi.html")
|
|
|
|
@app.route("/tentang")
|
|
def tentang():
|
|
return render_template("tentang.html")
|
|
|
|
# =========================
|
|
# API PREDIKSI
|
|
# =========================
|
|
@app.route("/predict", methods=["POST"])
|
|
def predict():
|
|
|
|
# =====================
|
|
# VALIDASI FILE
|
|
# =====================
|
|
if "image" not in request.files:
|
|
return jsonify({
|
|
"error": "File gambar tidak ditemukan"
|
|
}), 400
|
|
|
|
image_file = request.files["image"]
|
|
|
|
if image_file.filename == "":
|
|
return jsonify({
|
|
"error": "Nama file kosong"
|
|
}), 400
|
|
|
|
if not allowed_file(image_file.filename):
|
|
return jsonify({
|
|
"error": "Format file tidak didukung"
|
|
}), 400
|
|
|
|
# =====================
|
|
# SIMPAN FILE
|
|
# =====================
|
|
filename = secure_filename(image_file.filename)
|
|
|
|
image_path = os.path.join(
|
|
app.config["UPLOAD_FOLDER"],
|
|
filename
|
|
)
|
|
|
|
try:
|
|
|
|
# =====================
|
|
# SAVE FILE
|
|
# =====================
|
|
image_file.save(image_path)
|
|
|
|
print("\n=========================")
|
|
print("FILE BERHASIL DIUPLOAD")
|
|
print(image_path)
|
|
print("=========================")
|
|
|
|
# =====================
|
|
# PREDIKSI MODEL
|
|
# =====================
|
|
result = predict_image(image_path)
|
|
|
|
print("\nHASIL PREDIKSI:")
|
|
print(result)
|
|
|
|
# =====================
|
|
# RETURN JSON
|
|
# =====================
|
|
return jsonify(result)
|
|
|
|
except Exception as e:
|
|
|
|
print("\n❌ ERROR:")
|
|
print(str(e))
|
|
|
|
return jsonify({
|
|
"error": str(e)
|
|
}), 500
|
|
|
|
finally:
|
|
|
|
# =====================
|
|
# HAPUS FILE SEMENTARA
|
|
# =====================
|
|
if os.path.exists(image_path):
|
|
os.remove(image_path)
|
|
|
|
# =========================
|
|
# MAIN
|
|
# =========================
|
|
if __name__ == "__main__":
|
|
|
|
app.run(
|
|
debug=True
|
|
) |