Spaces:
Runtime error
Runtime error
File size: 2,782 Bytes
cf37736 a3ec0e3 7155e61 cf37736 93dd3e5 cf37736 685fa67 9e1a7b8 685fa67 9e1a7b8 685fa67 cf37736 685fa67 cf37736 685fa67 cf37736 685fa67 cf37736 685fa67 cf37736 685fa67 cf37736 685fa67 cf37736 abba1d9 685fa67 abba1d9 685fa67 cf37736 685fa67 7fd4e5e abba1d9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | 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) |