import numpy as np import gradio as gr from keras.models import load_model from keras.preprocessing.image import load_img, img_to_array # Load the pre-trained model model = load_model('best_model.h5') # Define class names for Boy or Girl classification class_names = ['Boy', 'Girl'] # 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 = np.expand_dims(img_array, axis=0) # Reshape for the model input (1, 150, 150, 3) # 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) # Determine the predicted class based on the model output predicted_class_index = np.argmax(prediction[0]) # Get index of the highest prediction score predicted_class = class_names[predicted_class_index] # Map index to class name return predicted_class # Gradio interface iface = gr.Interface(fn=classify_image, inputs=gr.Image(type="filepath"), outputs=gr.Textbox(), title="Boy or Girl Classifier", description="Upload an image to classify it as either a Boy or a Girl.") # Launch the app iface.launch()