Spaces:
Sleeping
Sleeping
M. Rifqi Akram
commited on
Commit
β’
5329e35
1
Parent(s):
54f9e08
Add files via upload
Browse files
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
import numpy as np
|
4 |
+
from tensorflow.keras.models import load_model
|
5 |
+
from tensorflow.keras.preprocessing import image
|
6 |
+
|
7 |
+
# Inisialisasi model Jankenpon dari Hugging Face
|
8 |
+
model_path = 'best_model.h5'
|
9 |
+
classifier = load_model(model_path)
|
10 |
+
|
11 |
+
# Fungsi untuk menampilkan gambar dengan judul
|
12 |
+
def display_image(image_path, title, width=None):
|
13 |
+
image = Image.open(image_path)
|
14 |
+
st.image(image, caption=title, use_column_width=width)
|
15 |
+
|
16 |
+
def preprocess_image(image_path):
|
17 |
+
img = image.load_img(image_path, target_size=(150, 150))
|
18 |
+
img_array = image.img_to_array(img)
|
19 |
+
img_array = np.expand_dims(img_array, axis=0)
|
20 |
+
img_array /= 255.0
|
21 |
+
return img_array
|
22 |
+
|
23 |
+
def main():
|
24 |
+
st.title("Let's Play Jankenpon βπβ")
|
25 |
+
st.subheader("Choose your jankenpon and see the image prediction!")
|
26 |
+
|
27 |
+
# Membuat session state
|
28 |
+
if 'state' not in st.session_state:
|
29 |
+
st.session_state.state = {
|
30 |
+
'image_choice': '',
|
31 |
+
'prediction_result': None
|
32 |
+
}
|
33 |
+
|
34 |
+
choice = [
|
35 |
+
'paper_1.png', 'paper_2.png', 'paper_3.png', 'paper_4.png', 'paper_5.png',
|
36 |
+
'rock_1.png', 'rock_2.png', 'rock_3.png', 'rock_4.png', 'rock_5.png',
|
37 |
+
'scissors_1.png', 'scissors_2.png', 'scissors_3.png', 'scissors_4.png', 'scissors_5.png'
|
38 |
+
]
|
39 |
+
|
40 |
+
# Meminta user untuk memilih gambar
|
41 |
+
image_choice = st.selectbox("Choose your choice:", ['', *choice])
|
42 |
+
|
43 |
+
enter_button, reset_button = st.columns(2)
|
44 |
+
|
45 |
+
if enter_button.button("Enter"):
|
46 |
+
if image_choice:
|
47 |
+
# Menampilkan gambar yang dipilih oleh user
|
48 |
+
col1, col2 = st.columns(2)
|
49 |
+
with col1:
|
50 |
+
display_image(image_choice, "Your Choice", width=150)
|
51 |
+
|
52 |
+
# Memproses gambar menjadi format yang sesuai untuk model
|
53 |
+
img_array = preprocess_image(image_choice)
|
54 |
+
|
55 |
+
# Memprediksi kelas gambar yang dipilih oleh user
|
56 |
+
prediction_result = classifier.predict(img_array)
|
57 |
+
predicted_class_index = np.argmax(prediction_result)
|
58 |
+
|
59 |
+
# Daftar kelas Jankenpon
|
60 |
+
classes = ['paper', 'rock', 'scissors']
|
61 |
+
classes_emoji = ['β Paper', 'π Rock', 'β Scissors']
|
62 |
+
|
63 |
+
# Menampilkan hasil prediksi dan probability
|
64 |
+
st.write(f"<h2 style='font-size: 20px;'>Image Prediction Probabilities:</h2>", unsafe_allow_html=True)
|
65 |
+
probabilities = prediction_result[0]
|
66 |
+
for i, prob in enumerate(probabilities):
|
67 |
+
st.text(f"{classes[i]}: {prob:.4f}")
|
68 |
+
|
69 |
+
st.write(f"<h2 style='font-size: 20px;'>Image Prediction Result: {classes_emoji[predicted_class_index]}</h2>", unsafe_allow_html=True)
|
70 |
+
|
71 |
+
# Menentukan kelas untuk menampilkan gambar 'my choice'
|
72 |
+
my_choice_class_index = (predicted_class_index + 2) % 3
|
73 |
+
|
74 |
+
# Menampilkan gambar 'my choice'
|
75 |
+
with col2:
|
76 |
+
display_image(f"{classes[my_choice_class_index]}_1.png", "My Choice", width=150)
|
77 |
+
|
78 |
+
# Memperbarui session state hanya ketika tombol "Enter" ditekan
|
79 |
+
st.session_state.state['image_choice'] = image_choice
|
80 |
+
st.session_state.state['prediction_result'] = prediction_result
|
81 |
+
|
82 |
+
if reset_button.button("Reset"):
|
83 |
+
# Mereset session state
|
84 |
+
st.session_state.state = {}
|
85 |
+
st.experimental_rerun()
|
86 |
+
|
87 |
+
if __name__ == "__main__":
|
88 |
+
main()
|