trs's picture
Update app.py
abba1d9 verified
Raw History Blame
2.78 kB
import numpy as np
import os
import cv2
import gradio as gr
import tensorflow as tf
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
inception_model_path = './inceptionv3_-saved-model-97-loss-0.00.hdf5'
vggnet_model_path = './resnet50-saved-model-79-val_acc-1.00.hdf5'
resnet_model_path = './vgg_16_-saved-model-89-acc-0.93.hdf5'
efficientnet_model_path = './efficientnet-saved-model-06-val_acc-0.98.hdf5'
inception_model = tf.keras.models.load_model(inception_model_path)
vggnet_model = tf.keras.models.load_model(vggnet_model_path)
resnet_model = tf.keras.models.load_model(resnet_model_path)
efficientnet_model = tf.keras.models.load_model(efficientnet_model_path)
# Define class names corresponding to your dataset
class_names = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"] # Replace with your actual class names
# Define the classify_image function
def classify_image(img, model_choice):
# Resize the input image based on the selected model's expected input shape
if model_choice == "ResNet":
img = tf.image.resize(img, (150, 150)) # ResNet50 input size
img = image.img_to_array(img)
img = img.copy() # Make a copy of the image array
img = resnet_preprocess_input(img)
elif model_choice == "EfficientNet":
img = tf.image.resize(img, (150, 150))
img = image.img_to_array(img)
img = preprocess_input(img)
elif model_choice == "VGGNet":
img = tf.image.resize(img, (150, 150))
img = image.img_to_array(img)
img = preprocess_input(img)
elif model_choice == "InceptionV3":
img = tf.image.resize(img, (150, 150))
img = image.img_to_array(img)
img = preprocess_input(img)
else:
return "Invalid model choice"
img = tf.expand_dims(img, 0) # Add batch dimension
# Make predictions based on the selected model
if model_choice == "ResNet":
predictions = resnet_model.predict(img)
elif model_choice == "EfficientNet":
predictions = efficientnet_model.predict(img)
elif model_choice == "VGGNet":
predictions = VGGNet_model.predict(img)
elif model_choice == "InceptionV3":
predictions = InceptionV3_model.predict(img)
class_idx = tf.argmax(predictions[0])
# Map class index to class name
class_name = class_names[class_idx]
# Return the predicted class name as a string
return f"Predicted class using {model_choice}: {class_name}"
image_input = gr.Image()
# Create a Gradio interface with a dropdown for model selection
iface = gr.Interface(
fn=classify_image,
inputs=[image_input, gr.Dropdown(["ResNet", "EfficientNet","VGGNet","InceptionV3"], label="Select Model")],
outputs="text",
live=True
)
.launch(debug=True)