import numpy as np import cv2 import gradio as gr from tensorflow.keras.utils import img_to_array from tensorflow.keras.models import load_model # Load your pre-trained model model = load_model(r'model.h5') # Define the prediction function that takes an image as input and returns the predicted label def predict_image(img): # Preprocess the image if needed x = img_to_array(img) x = cv2.resize(x, (299, 299), interpolation=cv2.INTER_AREA) x /= 255 x = np.expand_dims(x, axis=0) image = np.vstack([x]) # Make a prediction using your model prediction = model.predict(image) # Assuming your model returns probabilities, get the label with the highest probability predicted_label = "dog" if prediction > 0.5 else "cat" return predicted_label # Define the Gradio Interface with the desired title and description description_html = """
This model was trained by Moaz Eldsouky You can find more about me here:
GitHub: GitHub Profile
LinkedIn: LinkedIn Profile
Kaggle: Kaggle Profile
This model was trained to predict whether an image contains a cat or a dog.
You can see how this model was trained on the following Kaggle Notebook:
Upload a photo to see how the model predicts!
""" # Example images for a dog and a cat example_dog_image = "dog_.jpeg" example_cat_image = "FELV-cat.jpg" gr.Interface( fn=predict_image, inputs="image", outputs="text", title="Dogs vs Cats classification with Xception 🐶vs 😺", description=description_html, allow_flagging='never', examples=[ [example_dog_image], # Example image for a dog [example_cat_image], # Example image for a cat ] ).launch()