trs's picture
Update app.py
923bb8f verified
Raw History Blame
1.62 kB
import gradio as gr
import cv2
import numpy as np
from tensorflow.keras.models import load_model
class_folders = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"]
# Define paths for the HDF5 model
model_paths = {
"InceptionV3": './inceptionv3_-saved-model-59-val_accuracy-0.94.hdf5',
"VGGNet": './vgg_16_-saved-model-89-acc-0.93.hdf5',
"ResNet": './inceptionv3_-saved-model-59-val_accuracy-0.94.hdf5',
"EfficientNet": './efficientnet-saved-model-06-val_acc-0.98.hdf5'
}
def classify_selected_model(image, model_name):
try:
img = cv2.imread(image)
img = cv2.resize(img, (150, 150))
img = np.expand_dims(img, axis=0)
img= cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#img = img.astype(np.float32) / 255.0
keras_model = load_model(model_paths[model_name])
predictions = keras_model.predict(img)
class_index = np.argmax(predictions[0])
class_name = class_folders[class_index]
probability = max(predictions[0])
print(f'Predicted class index: {class_index}')
print(f'Predicted class name: {class_name}')
print(f'Probability: {probability}')
return {"class_name": class_name, "probability": probability}
except Exception as e:
return {"error": str(e)}
# Create Gradio interface
image_input = gr.Image(type='filepath', label="Input Image")
model_dropdown = gr.Dropdown(model_paths.keys(), label="Select Model")
gr.Interface(
fn=classify_selected_model,
inputs=[image_input, model_dropdown],
outputs="json",
live=True
).launch(debug=True)