Spaces:
Sleeping
Sleeping
create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
from tensorflow.keras.models import load_model
|
| 6 |
+
import os
|
| 7 |
+
# Ensure the 'upload' directory exists
|
| 8 |
+
upload_folder = 'uploads'
|
| 9 |
+
if not os.path.exists(upload_folder):
|
| 10 |
+
os.makedirs(upload_folder)
|
| 11 |
+
|
| 12 |
+
# Load the pre-trained model
|
| 13 |
+
model = load_model("gender_detector.keras")
|
| 14 |
+
|
| 15 |
+
def get_result(img_path):
|
| 16 |
+
img = cv2.imread(img_path)
|
| 17 |
+
img_resize = cv2.resize(img, (224, 224))
|
| 18 |
+
img_resize = np.array(img_resize, dtype=np.float32)
|
| 19 |
+
img_resize /= 255.0
|
| 20 |
+
img_input = img_resize.reshape(1, 224, 224, 3)
|
| 21 |
+
prediction = model.predict(img_input)
|
| 22 |
+
|
| 23 |
+
if prediction[0][0] < 0.5:
|
| 24 |
+
return "He is a Men."
|
| 25 |
+
else:
|
| 26 |
+
return "She is a Women."
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# Set the title of the app
|
| 30 |
+
st.title('Image Input and Display')
|
| 31 |
+
|
| 32 |
+
# Upload image
|
| 33 |
+
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 34 |
+
|
| 35 |
+
# If an image is uploaded, display it along with text
|
| 36 |
+
if uploaded_image is not None:
|
| 37 |
+
# Open the image using PIL
|
| 38 |
+
|
| 39 |
+
# output = get_result(uploaded_image)
|
| 40 |
+
|
| 41 |
+
image = Image.open(uploaded_image)
|
| 42 |
+
|
| 43 |
+
image_path = os.path.join(upload_folder, uploaded_image.name)
|
| 44 |
+
image.save(image_path)
|
| 45 |
+
output = get_result(image_path)
|
| 46 |
+
# Display the image
|
| 47 |
+
st.image(image, caption= output, use_container_width=True)
|