Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,19 @@
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
4 |
-
import
|
5 |
import os
|
6 |
import tempfile
|
7 |
import shutil
|
8 |
|
9 |
# Coba Import YOLO
|
10 |
-
|
11 |
try:
|
12 |
from ultralytics import YOLO
|
13 |
YOLO_AVAILABLE = True
|
14 |
except ImportError:
|
15 |
YOLO_AVAILABLE = False
|
16 |
|
17 |
-
st.set_page_config(page_title="Pengenalan Gambar
|
18 |
|
19 |
# Periksa apakah library YOLO tersedia
|
20 |
def cek_library():
|
@@ -34,54 +33,55 @@ st.markdown("""
|
|
34 |
# Pastikan library sudah terpasang sebelum melanjutkan
|
35 |
if cek_library():
|
36 |
# upload gambar
|
37 |
-
|
38 |
|
39 |
-
|
40 |
# Simpan sementara
|
41 |
temp_dir = tempfile.mkdtemp()
|
42 |
temp_file = os.path.join(temp_dir, "gambar.jpg")
|
43 |
image = Image.open(uploaded_file)
|
44 |
-
|
45 |
-
#Ubah Ukuran Gambar
|
46 |
-
image = image.resize((300,300))
|
47 |
image.save(temp_file)
|
48 |
|
49 |
-
#Tampilkan
|
50 |
-
st.markdown("<div style='text-align: center;'>",unsafe_allow_html=True)
|
51 |
st.image(image, caption="Gambar yang diupload")
|
52 |
st.markdown("</div>", unsafe_allow_html=True)
|
53 |
-
|
54 |
-
#Deteksi Gambar
|
55 |
if st.button("Deteksi Gambar"):
|
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 |
st.markdown(
|
83 |
-
"<div style='text-align: center;' class='footer'>Program Aplikasi deteksi batuan @2025</div>",
|
84 |
-
unsafe_allow_html=True
|
85 |
)
|
86 |
-
|
87 |
-
|
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
import os
|
6 |
import tempfile
|
7 |
import shutil
|
8 |
|
9 |
# Coba Import YOLO
|
|
|
10 |
try:
|
11 |
from ultralytics import YOLO
|
12 |
YOLO_AVAILABLE = True
|
13 |
except ImportError:
|
14 |
YOLO_AVAILABLE = False
|
15 |
|
16 |
+
st.set_page_config(page_title="Pengenalan Gambar Mineral")
|
17 |
|
18 |
# Periksa apakah library YOLO tersedia
|
19 |
def cek_library():
|
|
|
33 |
# Pastikan library sudah terpasang sebelum melanjutkan
|
34 |
if cek_library():
|
35 |
# upload gambar
|
36 |
+
uploaded_file = st.file_uploader("Upload gambar mineral", type=['jpg', 'jpeg', 'png'])
|
37 |
|
38 |
+
if uploaded_file:
|
39 |
# Simpan sementara
|
40 |
temp_dir = tempfile.mkdtemp()
|
41 |
temp_file = os.path.join(temp_dir, "gambar.jpg")
|
42 |
image = Image.open(uploaded_file)
|
43 |
+
|
44 |
+
# Ubah Ukuran Gambar
|
45 |
+
image = image.resize((300, 300))
|
46 |
image.save(temp_file)
|
47 |
|
48 |
+
# Tampilkan gambar
|
49 |
+
st.markdown("<div style='text-align: center;'>", unsafe_allow_html=True)
|
50 |
st.image(image, caption="Gambar yang diupload")
|
51 |
st.markdown("</div>", unsafe_allow_html=True)
|
52 |
+
|
53 |
+
# Deteksi Gambar
|
54 |
if st.button("Deteksi Gambar"):
|
55 |
+
with st.spinner("Sedang diproses"):
|
56 |
+
try:
|
57 |
+
model = YOLO('best(1).pt')
|
58 |
+
hasil = model(temp_file)
|
59 |
+
|
60 |
+
# Ambil Hasil Prediksi
|
61 |
+
nama_objek = hasil[0].names
|
62 |
+
nilai_prediksi = hasil[0].probs.data.numpy().tolist()
|
63 |
+
objek_terdeteksi = nama_objek[np.argmax(nilai_prediksi)]
|
64 |
+
|
65 |
+
# buat grafik pakai Matplotlib
|
66 |
+
fig, ax = plt.subplots()
|
67 |
+
ax.bar(list(nama_objek.values()), nilai_prediksi)
|
68 |
+
ax.set_title('Tingkat Keyakinan Prediksi')
|
69 |
+
ax.set_xlabel('Mineral')
|
70 |
+
ax.set_ylabel('Keyakinan')
|
71 |
+
plt.xticks(rotation=45)
|
72 |
+
|
73 |
+
# Tampilkan hasil
|
74 |
+
st.success(f"Mineral terdeteksi: {objek_terdeteksi}")
|
75 |
+
st.pyplot(fig)
|
76 |
+
|
77 |
+
except Exception as e:
|
78 |
+
st.error("Gambar tidak dapat terdeteksi")
|
79 |
+
st.error(f"Error: {e}")
|
80 |
+
|
81 |
+
# Hapus file sementara
|
82 |
+
shutil.rmtree(temp_dir, ignore_errors=True)
|
83 |
|
84 |
st.markdown(
|
85 |
+
"<div style='text-align: center;' class='footer'>Program Aplikasi deteksi batuan @2025</div>",
|
86 |
+
unsafe_allow_html=True
|
87 |
)
|
|
|
|