Himanshu2003's picture
Update app.py
d1a929b verified
import streamlit as st
from PIL import Image
import numpy as np
import cv2
from tensorflow.keras.models import load_model
import os
# Ensure the 'upload' directory exists
upload_folder = 'uploads'
if not os.path.exists(upload_folder):
os.makedirs(upload_folder)
# Load the pre-trained model
model = load_model("gender_detector.keras")
def get_result(img_path):
img = cv2.imread(img_path)
img_resize = cv2.resize(img, (224, 224))
img_resize = np.array(img_resize, dtype=np.float32)
img_resize /= 255.0
img_input = img_resize.reshape(1, 224, 224, 3)
prediction = model.predict(img_input)
if prediction[0][0] < 0.5:
return "He is a Man ๐Ÿšน"
else:
return "She is a Woman ๐Ÿšบ"
st.title("Let\'s detect the gender ๐Ÿšน๐Ÿšบ")
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_image is not None:
image = Image.open(uploaded_image)
image_path = os.path.join(upload_folder, uploaded_image.name)
image.save(image_path)
output = get_result(image_path)
st.markdown(f"<h1 style='text-align: center; color: white;'>{output}</h1>", unsafe_allow_html=True)
st.image(image, use_container_width=True)