male-or-female / app.py
a-guy-from-burma's picture
Update app.py
91ac8e2 verified
Raw
History Blame Contribute Delete
734 Bytes
import gradio as gr
import numpy as np
import tensorflow as tf
from PIL import Image
import io
# Load the trained model
model = tf.keras.models.load_model('gender_classification_model.h5')
# Define the prediction function
def predict_gender(img_data):
# Convert input image to PIL Image
img = Image.open(io.BytesIO(img_data))
# Resize and preprocess the image
img = img.resize((150, 150))
img = np.array(img)
img = np.expand_dims(img, axis=0)
img = img / 255.0
# Predict gender
prediction = model.predict(img)
return "Male" if prediction[0] > 0.5 else "Female"
# Create the Gradio interface
iface = gr.Interface(fn=predict_gender, inputs=gr.Image(), outputs="text")
iface.launch()