Skin_Types / prediction.py
dini15's picture
Update prediction.py
71b762e verified
import streamlit as st
import numpy as np
from PIL import Image
import tensorflow as tf
from tensorflow.keras.models import load_model
import tensorflow_hub as hub
# Load model sekali saat aplikasi di-start
@st.cache_resource
def load_skin_model():
return load_model('model_aug.keras', custom_objects={'KerasLayer': hub.KerasLayer})
model = load_skin_model()
# Kelas target
CLASS_NAMES = ['oily', 'dry', 'normal']
def preprocess_image(image):
"""Preprocess image to match model input."""
img_resized = tf.image.resize(image, [220, 220]) # Resize gambar
img_normalized = img_resized / 255.0 # Normalisasi
return tf.expand_dims(img_normalized, axis=0) # Tambah batch dimension
def run():
st.title('Skin Type Classification')
st.write('---')
st.write('Upload an image of skin, and this app will predict the skin type.')
link_gambar = 'https://belomed.com/wp-content/uploads/2024/01/Know-your-skin-type.jpg'
st.image(link_gambar, caption='Know your skin type!', use_container_width=True)
uploaded_file = st.file_uploader('Upload an image:', type=['jpg', 'png', 'jpeg'])
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption='Uploaded Image', use_column_width=True)
img_array = np.array(image)
img_tensor = preprocess_image(img_array)
# Prediksi menggunakan model
prediction = model.predict(img_tensor)
predicted_class = CLASS_NAMES[np.argmax(prediction)]
confidence = np.max(prediction) * 100
st.write(f"### Predicted Skin Type: {predicted_class}")
st.write(f"### Confidence: {confidence:.2f}%")
else:
st.write('Please upload an image to get a prediction.')
link_gambar = 'https://www.wendygriffith.co.uk/wp-content/uploads/sites/14118/2024/07/Healthy-Skin-1-1-1024x1024.png'
st.image(link_gambar, use_container_width=True)
if __name__ == '__main__':
run()