dini15 commited on
Commit
c9e9625
·
verified ·
1 Parent(s): f69fe68

Update prediction.py

Browse files
Files changed (1) hide show
  1. prediction.py +18 -19
prediction.py CHANGED
@@ -1,51 +1,50 @@
1
  import streamlit as st
2
  import numpy as np
3
- import cv2
4
  from PIL import Image
5
  import tensorflow as tf
6
- import os
 
7
 
8
- # Load model yang telah dilatih (ganti 'model.h5' dengan file model kamu)
9
- model = load_model('model_aug.keras', custom_objects={'KerasLayer': KerasLayer})
 
 
10
 
11
- # Kelas target (ubah sesuai dengan model kamu)
 
 
12
  CLASS_NAMES = ['oily', 'dry', 'normal']
13
 
 
 
 
 
 
 
14
  def run():
15
- # Set judul aplikasi
16
  st.title('Skin Type Classification')
17
  st.write('---')
18
-
19
- # Tambahkan deskripsi
20
  st.write('Upload an image of skin, and this app will predict the skin type.')
21
 
22
- # Gambar ilustrasi (opsional)
23
- link_gambar = 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQntnqn33t_1jWqaFszEgEdMCQjGNNtWLxv8A&s' # Ganti dengan URL gambar
24
  st.image(link_gambar, caption='Know your skin type!', use_container_width=True)
25
 
26
- # Form untuk upload gambar
27
  uploaded_file = st.file_uploader('Upload an image:', type=['jpg', 'png', 'jpeg'])
28
 
29
  if uploaded_file is not None:
30
- # Tampilkan gambar yang di-upload
31
  image = Image.open(uploaded_file)
32
  st.image(image, caption='Uploaded Image', use_column_width=True)
33
 
34
- # Convert gambar ke format yang diterima model
35
  img_array = np.array(image)
36
- img_resized = cv2.resize(img_array, (220, 220)) # Ubah ukuran ke input model
37
- img_normalized = img_resized / 255.0 # Normalisasi
38
- img_expanded = np.expand_dims(img_normalized, axis=0) # Tambahkan batch dimension
39
 
40
  # Prediksi menggunakan model
41
- prediction = model.predict(img_expanded)
42
  predicted_class = CLASS_NAMES[np.argmax(prediction)]
43
  confidence = np.max(prediction) * 100
44
 
45
- # Tampilkan hasil prediksi
46
  st.write(f"### Predicted Skin Type: {predicted_class}")
47
  st.write(f"### Confidence: {confidence:.2f}%")
48
-
49
  else:
50
  st.write('Please upload an image to get a prediction.')
51
 
 
1
  import streamlit as st
2
  import numpy as np
 
3
  from PIL import Image
4
  import tensorflow as tf
5
+ from tensorflow.keras.models import load_model
6
+ from tensorflow.keras.layers import KerasLayer
7
 
8
+ # Load model sekali saat aplikasi di-start
9
+ @st.cache_resource
10
+ def load_skin_model():
11
+ return load_model('model_aug.keras', custom_objects={'KerasLayer': KerasLayer})
12
 
13
+ model = load_skin_model()
14
+
15
+ # Kelas target
16
  CLASS_NAMES = ['oily', 'dry', 'normal']
17
 
18
+ def preprocess_image(image):
19
+ """Preprocess image to match model input."""
20
+ img_resized = tf.image.resize(image, [220, 220]) # Resize gambar
21
+ img_normalized = img_resized / 255.0 # Normalisasi
22
+ return tf.expand_dims(img_normalized, axis=0) # Tambah batch dimension
23
+
24
  def run():
 
25
  st.title('Skin Type Classification')
26
  st.write('---')
 
 
27
  st.write('Upload an image of skin, and this app will predict the skin type.')
28
 
29
+ link_gambar = 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQntnqn33t_1jWqaFszEgEdMCQjGNNtWLxv8A&s'
 
30
  st.image(link_gambar, caption='Know your skin type!', use_container_width=True)
31
 
 
32
  uploaded_file = st.file_uploader('Upload an image:', type=['jpg', 'png', 'jpeg'])
33
 
34
  if uploaded_file is not None:
 
35
  image = Image.open(uploaded_file)
36
  st.image(image, caption='Uploaded Image', use_column_width=True)
37
 
 
38
  img_array = np.array(image)
39
+ img_tensor = preprocess_image(img_array)
 
 
40
 
41
  # Prediksi menggunakan model
42
+ prediction = model.predict(img_tensor)
43
  predicted_class = CLASS_NAMES[np.argmax(prediction)]
44
  confidence = np.max(prediction) * 100
45
 
 
46
  st.write(f"### Predicted Skin Type: {predicted_class}")
47
  st.write(f"### Confidence: {confidence:.2f}%")
 
48
  else:
49
  st.write('Please upload an image to get a prediction.')
50