Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from tensorflow.keras.models import load_model | |
| from tensorflow.keras.preprocessing.image import img_to_array | |
| from PIL import Image | |
| import numpy as np | |
| # Load your model | |
| model = load_model('cats_and_dogs_classifier.h5') | |
| # Define prediction function | |
| def predict(image): | |
| # Ensure the image is a PIL Image object | |
| if isinstance(image, np.ndarray): | |
| image = Image.fromarray(image) | |
| # Resize image to match model's expected sizing (150x150 pixels) | |
| img = image.resize((150, 150)) | |
| # Convert image to array and expand dimensions to fit model input requirements | |
| img_array = img_to_array(img) | |
| img_array = np.expand_dims(img_array, axis=0) | |
| # Predict using loaded model | |
| prediction = model.predict(img_array) | |
| # Convert prediction to label | |
| class_label = 'Cat' if prediction[0][0] < 0.5 else 'Dog' | |
| return class_label | |
| # Create a Gradio interface | |
| interface = gr.Interface(fn=predict, inputs="image", outputs="text") | |
| interface.launch() | |