| 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 |
|
|
| |
| model = load_model(r'model.h5') |
|
|
| |
| def predict_image(img): |
| |
| 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]) |
| |
| prediction = model.predict(image) |
| |
| predicted_label = "dog" if prediction > 0.5 else "cat" |
| return predicted_label |
|
|
| |
| description_html = """ |
| <p>This model was trained by Moaz Eldsouky You can find more about me here:</p> |
| <p>GitHub: <a href="https://github.com/MoazEldsouky">GitHub Profile</a></p> |
| <p>LinkedIn: <a href="https://www.linkedin.com/in/moaz-eldesouky-762288251/">LinkedIn Profile</a></p> |
| <p>Kaggle: <a href="https://www.kaggle.com/moazeldsokyx">Kaggle Profile</a></p> |
| <p>This model was trained to predict whether an image contains a cat or a dog.</p> |
| <p>You can see how this model was trained on the following Kaggle Notebook:</p> |
| <p><a href="https://www.kaggle.com/code/moazeldsokyx/dogs-vs-cats-classification-with-xception">Kaggle Notebook</a></p> |
| <p>Upload a photo to see how the model predicts!</p> |
| """ |
|
|
| |
| 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_cat_image], |
| ] |
| ).launch() |
|
|