| import gradio as gr |
| import tensorflow as tf |
| import numpy as np |
| from PIL import Image |
|
|
| |
| |
| |
| model = tf.keras.models.load_model("cnn_model_transfer.keras") |
|
|
| |
| class_names = ["Cat", "Dog"] |
|
|
| |
| |
| |
| def preprocess_image(img): |
| img = img.resize((224, 224)) |
| img = np.array(img) |
|
|
| |
| img = tf.keras.applications.mobilenet_v2.preprocess_input(img) |
|
|
| img = np.expand_dims(img, axis=0) |
| return img |
|
|
| |
| |
| |
| def predict(img): |
| img_array = preprocess_image(img) |
|
|
| pred = model.predict(img_array)[0][0] |
|
|
| if pred > 0.5: |
| label = class_names[1] |
| confidence = float(pred) |
| else: |
| label = class_names[0] |
| confidence = float(1 - pred) |
|
|
| return { |
| "Cat": 1 - pred, |
| "Dog": pred |
| } |
|
|
| |
| |
| |
| demo = gr.Interface( |
| fn=predict, |
| inputs=gr.Image(type="pil"), |
| outputs=gr.Label(num_top_classes=2), |
| title="🐱🐶 Cat vs Dog Classifier", |
| description="Upload an image and the model will predict whether it is a Cat or Dog using MobileNetV2 Transfer Learning." |
| ) |
|
|
| |
| |
| |
| demo.launch() |