Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
import numpy as np
|
4 |
+
import plotly.graph_objects as go
|
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 mineral")
|
18 |
+
|
19 |
+
# Periksa apakah library YOLO tersedia
|
20 |
+
def cek_library():
|
21 |
+
if not YOLO_AVAILABLE:
|
22 |
+
st.error("Ultralytics tidak terpasang. Silakan instal dengan perintah berikut:")
|
23 |
+
st.code("pip install ultralytics")
|
24 |
+
return False
|
25 |
+
return True
|
26 |
+
|
27 |
+
st.markdown("""
|
28 |
+
<div style="background-color:#0984e3; padding: 20px; text-align: center;">
|
29 |
+
<h1 style="color: white;"> Program Pengenalan Gambar </h1>
|
30 |
+
<h5 style="color: white;">Deteksi Gambar Mineral</h5>
|
31 |
+
</div>
|
32 |
+
""", unsafe_allow_html=True)
|
33 |
+
|
34 |
+
# Pastikan library sudah terpasang sebelum melanjutkan
|
35 |
+
if cek_library():
|
36 |
+
# upload gambar
|
37 |
+
uploaded_file = st.file_uploader("upload gambar mineral", type=['jpg', 'jpeg', 'png'])
|
38 |
+
|
39 |
+
if uploaded_file:
|
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 gamabar
|
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 |
+
with st.spinner("Sedang diproses"):
|
57 |
+
try:
|
58 |
+
model = YOLO('best.pt')
|
59 |
+
hasil = model(temp_file)
|
60 |
+
|
61 |
+
#Ambil Hasil Prediksi
|
62 |
+
nama_objek = hasil[0].names
|
63 |
+
nilai_prediksi= hasil[0].probs.data.numpy().tolist()
|
64 |
+
objek_terdeteksi = nama_objek[np.argmax(nilai_prediksi)]
|
65 |
+
|
66 |
+
#buat grafik
|
67 |
+
grafik = go.Figure([go.Bar(x=list(nama_objek.values()), y=nilai_prediksi)])
|
68 |
+
grafik.update_layout(title='Tingkat Keyakinan Prediksi', xaxis_title='Mineral',
|
69 |
+
yaxis_title='Keyakinan')
|
70 |
+
|
71 |
+
#Tampilkan hasil
|
72 |
+
st.write(f"mineral terdeteksi:{objek_terdeteksi}")
|
73 |
+
st.plotly_chart(grafik)
|
74 |
+
|
75 |
+
except Exception as e :
|
76 |
+
st.error("Gambar tidak dapat terdeteksi")
|
77 |
+
st.error(f"Error:{e}")
|
78 |
+
|
79 |
+
#Hapus file sementara
|
80 |
+
shutil.rmtree(temp_dir,ignore_errors=True)
|
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 |
+
|