Himanshu2003's picture
update app.py
0e0a8ca verified
raw
history blame
1.37 kB
pip install -r requirements.txt
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 Men."
else:
return "She is a Women."
# Set the title of the app
st.title('Image Input and Display')
# Upload image
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
# If an image is uploaded, display it along with text
if uploaded_image is not None:
# Open the image using PIL
# output = get_result(uploaded_image)
image = Image.open(uploaded_image)
image_path = os.path.join(upload_folder, uploaded_image.name)
image.save(image_path)
output = get_result(image_path)
# Display the image
st.image(image, caption= output, use_container_width=True)