|
import gradio as gr |
|
import tensorflow as tf |
|
from tensorflow.keras.preprocessing.image import load_img, img_to_array |
|
from tensorflow.keras.applications.efficientnet_v2 import preprocess_input |
|
import numpy as np |
|
|
|
|
|
model = tf.keras.models.load_model("setosys_dogs_model.h5") |
|
|
|
|
|
class_labels = {v: k for k, v in model.class_indices.items()} |
|
|
|
|
|
def preprocess_image(image_path): |
|
img = load_img(image_path, target_size=(224, 224)) |
|
img_array = img_to_array(img) |
|
img_array = preprocess_input(img_array) |
|
img_array = np.expand_dims(img_array, axis=0) |
|
return img_array |
|
|
|
|
|
def predict_dog_breed(image): |
|
img_array = preprocess_image(image) |
|
predictions = model.predict(img_array) |
|
class_idx = np.argmax(predictions) |
|
breed = class_labels[class_idx] |
|
confidence = predictions[0][class_idx] |
|
return breed, confidence |
|
|
|
|
|
iface = gr.Interface(fn=predict_dog_breed, |
|
inputs=gr.inputs.Image(type="filepath"), |
|
outputs=["text", "number"], |
|
live=True) |
|
|
|
|
|
iface.launch(share=True) |
|
|