import os import numpy as np import cv2 import pickle import tensorflow as tf from flask import Flask, request, render_template_string from skimage.feature import hog app = Flask(__name__) # Load Model & Scaler MODEL_PATH = 'model/day_night_model.h5' SCALER_PATH = 'model/scaler.pkl' try: model = tf.keras.models.load_model(MODEL_PATH) with open(SCALER_PATH, 'rb') as f: scaler = pickle.load(f) print("✅ System Loaded Successfully") except Exception as e: print(f"❌ Error loading system: {e}") def preprocess_image(image_bytes): # Decode gambar nparr = np.frombuffer(image_bytes, np.uint8) img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) # Preprocessing (Harus sama persis dengan Training) img = cv2.resize(img, (256, 256)) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) hog_feat = hog(gray, orientations=9, pixels_per_cell=(8,8), cells_per_block=(2,2), block_norm='L2-Hys', visualize=False, feature_vector=True) return scaler.transform(hog_feat.reshape(1, -1)) @app.route('/', methods=['GET']) def home(): return render_template_string('''

Day vs Night Classifier



''') @app.route('/predict', methods=['POST']) def predict(): try: file = request.files['file'] data = preprocess_image(file.read()) prediction = model.predict(data)[0][0] label = "Day (Siang)" if prediction > 0.5 else "Night (Malam)" return f"

Hasil: {label}

Kembali
" except Exception as e: return f"Error: {e}" if __name__ == '__main__': # Port 7860 wajib untuk Hugging Face Spaces app.run(host='0.0.0.0', port=7860)