Spaces:
Sleeping
Sleeping
Docummentation
Browse files- Dockerfile +27 -0
- app.py +64 -0
- model/day_night_model.h5 +3 -0
- model/scaler.pkl +3 -0
Dockerfile
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Base Image Python 3.9 Slim
|
| 2 |
+
FROM python:3.9-slim
|
| 3 |
+
|
| 4 |
+
# Install Library Sistem untuk OpenCV (Debian Bookworm/Trixie Compatible)
|
| 5 |
+
RUN apt-get update && apt-get install -y \
|
| 6 |
+
libgl1 \
|
| 7 |
+
libglib2.0-0 \
|
| 8 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 9 |
+
|
| 10 |
+
# Setup User Non-Root (Standar Keamanan HF)
|
| 11 |
+
RUN useradd -m -u 1000 user
|
| 12 |
+
USER user
|
| 13 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 14 |
+
|
| 15 |
+
# Setup Direktori Kerja
|
| 16 |
+
WORKDIR /app
|
| 17 |
+
|
| 18 |
+
# Install Dependencies
|
| 19 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
| 20 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 21 |
+
|
| 22 |
+
# Copy File Aplikasi
|
| 23 |
+
COPY --chown=user . /app
|
| 24 |
+
|
| 25 |
+
# Expose Port & Jalankan
|
| 26 |
+
EXPOSE 7860
|
| 27 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
+
import pickle
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
from flask import Flask, request, render_template_string
|
| 7 |
+
from skimage.feature import hog
|
| 8 |
+
|
| 9 |
+
app = Flask(__name__)
|
| 10 |
+
|
| 11 |
+
# Load Model & Scaler
|
| 12 |
+
MODEL_PATH = 'model/day_night_model.h5'
|
| 13 |
+
SCALER_PATH = 'model/scaler.pkl'
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
model = tf.keras.models.load_model(MODEL_PATH)
|
| 17 |
+
with open(SCALER_PATH, 'rb') as f:
|
| 18 |
+
scaler = pickle.load(f)
|
| 19 |
+
print("✅ System Loaded Successfully")
|
| 20 |
+
except Exception as e:
|
| 21 |
+
print(f"❌ Error loading system: {e}")
|
| 22 |
+
|
| 23 |
+
def preprocess_image(image_bytes):
|
| 24 |
+
# Decode gambar
|
| 25 |
+
nparr = np.frombuffer(image_bytes, np.uint8)
|
| 26 |
+
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 27 |
+
|
| 28 |
+
# Preprocessing (Harus sama persis dengan Training)
|
| 29 |
+
img = cv2.resize(img, (256, 256))
|
| 30 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 31 |
+
|
| 32 |
+
hog_feat = hog(gray, orientations=9, pixels_per_cell=(8,8),
|
| 33 |
+
cells_per_block=(2,2), block_norm='L2-Hys',
|
| 34 |
+
visualize=False, feature_vector=True)
|
| 35 |
+
|
| 36 |
+
return scaler.transform(hog_feat.reshape(1, -1))
|
| 37 |
+
|
| 38 |
+
@app.route('/', methods=['GET'])
|
| 39 |
+
def home():
|
| 40 |
+
return render_template_string('''
|
| 41 |
+
<div style="text-align:center; padding:50px;">
|
| 42 |
+
<h1>Day vs Night Classifier</h1>
|
| 43 |
+
<form action="/predict" method="post" enctype="multipart/form-data">
|
| 44 |
+
<input type="file" name="file" required><br><br>
|
| 45 |
+
<button type="submit">Prediksi</button>
|
| 46 |
+
</form>
|
| 47 |
+
</div>
|
| 48 |
+
''')
|
| 49 |
+
|
| 50 |
+
@app.route('/predict', methods=['POST'])
|
| 51 |
+
def predict():
|
| 52 |
+
try:
|
| 53 |
+
file = request.files['file']
|
| 54 |
+
data = preprocess_image(file.read())
|
| 55 |
+
prediction = model.predict(data)[0][0]
|
| 56 |
+
|
| 57 |
+
label = "Day (Siang)" if prediction > 0.5 else "Night (Malam)"
|
| 58 |
+
return f"<h2 style='text-align:center'>Hasil: {label}</h2><center><a href='/'>Kembali</a></center>"
|
| 59 |
+
except Exception as e:
|
| 60 |
+
return f"Error: {e}"
|
| 61 |
+
|
| 62 |
+
if __name__ == '__main__':
|
| 63 |
+
# Port 7860 wajib untuk Hugging Face Spaces
|
| 64 |
+
app.run(host='0.0.0.0', port=7860)
|
model/day_night_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5b845f658b285f31dc40377a751fb818fb92d4d43406b37b2fbe89a3ef77e2bc
|
| 3 |
+
size 53273656
|
model/scaler.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:453f1b2ef91392936efaab507118f1fd35274372a1eb6e22dafbe6e4235063b8
|
| 3 |
+
size 830760
|