Spaces:
Running
Running
File size: 1,461 Bytes
b6096d5 7640426 7ccec0f 7640426 75b4669 f46e6ef 7ccec0f 7640426 4255e30 7640426 e9dec91 4255e30 7ccec0f 4255e30 b6096d5 7ccec0f 7640426 e9dec91 75b4669 7ccec0f 7640426 7ccec0f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
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()
|