import numpy as np import tensorflow as tf from tensorflow.keras.models import load_model from tensorflow.keras.preprocessing.image import load_img, img_to_array import gradio as gr # Define class names for the model class_names = ["Girl", "Boy"] # Adjusted to represent binary classification # Load the pre-trained model model_path = 'best_model.h5' # Update this path model = load_model(model_path) # Function to preprocess the image and make predictions def classify_image(image): # Load and preprocess the image img = load_img(image, target_size=(150, 150)) # Ensure this matches the input size of your model img_array = img_to_array(img) / 255.0 # Normalize pixel values img_array = img_array.reshape((1, 150, 150, 3)) # Reshape for the model # Log the shape of the image array for debugging print(f"Image shape for prediction: {img_array.shape}") # Get prediction prediction = model.predict(img_array) predicted_class = class_names[1] if prediction[0][0] > 0.5 else class_names[0] # Interpret output return predicted_class # Create a Gradio interface iface = gr.Interface(fn=classify_image, inputs=gr.Image(type="filepath"), outputs="text", title="Boy or Girl Classifier", description="Upload an image to classify as either Boy or Girl.") # Run the Gradio app iface.launch(share=True) # Set share=True if you want to share the link