|
import gradio as gr |
|
import tensorflow as tf |
|
import tensorflow_hub as hub |
|
from PIL import Image |
|
import numpy as np |
|
from tensorflow.keras.preprocessing.image import img_to_array, load_img |
|
|
|
|
|
model = tf.keras.models.load_model('cats_vs_dogs.h5', custom_objects={'KerasLayer': hub.KerasLayer}) |
|
|
|
def predict(input_image): |
|
try: |
|
|
|
input_image = img_to_array(input_image) |
|
|
|
|
|
input_image = tf.image.resize(input_image, (224, 224)).numpy() |
|
input_image = np.array(input_image).astype(np.float32) / 255.0 |
|
input_image = np.expand_dims(input_image, axis=0) |
|
|
|
|
|
|
|
prediction = model.predict(input_image) |
|
|
|
|
|
labels = ['Cat', 'Dog'] |
|
threshold = 0.5 |
|
predicted_class = 'Dog' if prediction[0][0] > threshold else 'Cat' |
|
prediction_probability = prediction[0][0] if predicted_class == 'Dog' else 1 - prediction[0][0] |
|
|
|
cat_emoji = "\U0001F408" |
|
dog_emoji = "\U0001F415" |
|
|
|
selected_emoji = dog_emoji if predicted_class == 'Dog' else cat_emoji |
|
|
|
|
|
output = f"{selected_emoji} {predicted_class}" |
|
|
|
return output |
|
except Exception as e: |
|
return str(e) |
|
|
|
examples = ["dog.jpg", |
|
"cat.jpg"] |
|
|
|
|
|
iface = gr.Interface( |
|
fn=predict, |
|
inputs=gr.Image(shape=(224, 224)), |
|
outputs="text", |
|
title = 'π± x πΆ Image Recognition Application', |
|
description="""<br> This model was trained to predict whether an image contains a cat or a dog. <br> |
|
<br> You can see how this model was trained on the following <a href = "https://github.com/ayushaggarwalx/Cats-vs-Dogs-Image-Recognition-App">GitHub</a>. |
|
<br>Upload a photo to see the how the model predicts!""", |
|
examples = examples |
|
) |
|
|
|
iface.launch() |