File size: 3,506 Bytes
89c5d90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
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
74
75
76
77
78
79
80
81
82
83
84
import os
import glob
import subprocess
import streamlit as st
from PIL import Image

# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
# 1) Setup direktori dan session state
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
DATA_DIR = "data"
FUSED_DIR = os.path.join("result", "BIPED2CLASSIC", "fused")

os.makedirs(DATA_DIR, exist_ok=True)
os.makedirs(FUSED_DIR, exist_ok=True)

# Inisialisasi flag supaya hasil hanya tampil setelah proses
if "processed" not in st.session_state:
    st.session_state.processed = False

st.title("Deteksi jalan berlubang menggunakan DexiNed Egde Detection")

# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
# 2) Upload input
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
upload = st.file_uploader("Unggah satu gambar jalan", type=["jpg","jpeg","png"])
if upload:
    st.session_state.uploaded_file = upload
else:
    # reset kalau user hapus input
    st.session_state.processed = False

# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
# 3) Tombol Proses
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
if st.button("Memproses Gambar"):
    
    # 3.0 Validasi apakah gambar sudah diupload
    if not upload and "uploaded_file" not in st.session_state:
        st.error("❌ Silakan upload gambar terlebih dahulu sebelum memproses!")
    elif not upload and st.session_state.get("uploaded_file") is None:
        st.error("❌ Silakan upload gambar terlebih dahulu sebelum memproses!")
    else:
        # 3.1 Hapus semua file lama di DATA_DIR
        for f in glob.glob(os.path.join(DATA_DIR, "*")):
            os.remove(f)

        # 3.2 Simpan ulang file input (seharusnya hanya 1)
        input_path = os.path.join(DATA_DIR, st.session_state.uploaded_file.name)
        with open(input_path, "wb") as f:
            f.write(st.session_state.uploaded_file.getvalue())

        # 3.3 Hapus semua file lama di FUSED_DIR
        for f in glob.glob(os.path.join(FUSED_DIR, "*")):
            os.remove(f)

        # 3.4 Jalankan main.py (sesuaikan args jika perlu)
        with st.spinner("Sedang memproses gambar..."):
            result = subprocess.run(
                ["python", "main.py"],
                capture_output=True, text=True
            )
        
        if result.returncode == 0:
            st.success("Proses selesai πŸŽ‰")
            st.session_state.processed = True
        else:
            st.error("Proses gagal:")
            st.code(result.stderr)
            st.session_state.processed = False

# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
# 4) Tampilkan hasil jika sudah diproses
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
if st.session_state.processed:
    # Cari file di fused (harusnya 1 file output)
    outputs = glob.glob(os.path.join(FUSED_DIR, "*.*"))
    if len(outputs) == 1:
        out_img = Image.open(outputs[0])
        st.subheader("Gambar Hasil")
        st.image(out_img, caption=os.path.basename(outputs[0]), use_container_width=True)
    elif len(outputs) > 1:
        st.warning(f"Ditemukan {len(outputs)} file, seharusnya hanya 1.")
    else:
        st.error("Tidak ada file output di folder fused.")