hpratapsingh's picture
Update app.py
29f6004
import streamlit as st
from PIL import Image
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import img_to_array, load_img
import keras
# Streamlit app
st.title("Age and Gender Prediction App (InceptionV3) ❤️")
# Display a message before the app starts
with st.spinner("Loading the app..."):
# Simulate app loading time
import time
time.sleep(2) # Adjust the sleep time as needed
# File uploader
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
# Create an empty element for displaying messages
message_placeholder = st.empty()
# Load saved models lazily
@st.cache_resource
def load_models():
age_model_inception = load_model('age_model_inception.h5')
gender_model_inception = load_model('gender_model_inception.h5')
return age_model_inception, gender_model_inception
# Process and predict function for InceptionV3 models
def process_and_predict_inception(file, age_model, gender_model):
# Load and resize the image to (299, 299)
img = load_img(file, target_size=(299, 299))
img_array = img_to_array(img)
img_array = img_array.reshape((1, 299, 299, 3))
img_array /= 255.0 # Normalize the image
# Predict age and gender
age = age_model.predict(img_array)
gender = np.round(gender_model.predict(img_array))[0][0]
gender = 'female' if gender == 1 else 'male'
return int(age[0]), gender
if uploaded_file is not None:
# Display the uploaded image
st.image(uploaded_file, caption="Uploaded Image.", use_column_width=True)
st.write("")
# Display "Please wait" message
with st.spinner("Classifying... Please wait."):
# Load models
age_model_inception, gender_model_inception = load_models()
# Process and predict
age, gender = process_and_predict_inception(uploaded_file, age_model_inception, gender_model_inception)
# Clear the "Please wait" spinner
st.spinner("")
# Stylish presentation using markdown and CSS
st.markdown(f"**Predicted Age:** {age} years")
# Use HTML and CSS for styling
if gender == 'female':
st.markdown('<p style="color:#FF69B4;font-size:20px;">Predicted Gender: Female ❤️</p>', unsafe_allow_html=True)
else:
st.markdown('<p style="color:#6495ED;font-size:20px;">Predicted Gender: Male ❤️</p>', unsafe_allow_html=True)