Spaces:
Runtime error
Runtime error
Update app.py
Browse files13 unmodified lines1415161717181920212223242526272829303132333435363738394041422021222324434426272829304546473248493435363738395051525354414243555645575859474860616250515253636465666768556970717213 unmodified linesresnet_model = tf.keras.models.load_model(resnet_model_path)efficientnet_model = tf.keras.models.load_model(efficientnet_model_path)
class_folders = ['Umm Qais', 'Jeresh', 'Petra',' Ajloun Castle', 'Wadi Rum', 'Roman amphitheater'] # Define class names corresponding to your datasetclass_names = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"] # Replace with your actual class names
# Define the classify_image functiondef 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"
def classify_selected_model(image, model_name): img = cv2.imread(image.name) img = cv2.resize(img, (150, 150)) img = np.expand_dims(img, axis=0) img = img / 255.0 img = tf.expand_dims(img, 0) # Add batch dimension
if model_name.lower() == 'inceptionv3': predictions = inception_model.predict(img) elif model_name.lower() == 'vggnet': predictions = vggnet_model.predict(img) elif model_name.lower() == 'resnet': # Make predictions based on the selected model if model_choice == "ResNet": predictions = resnet_model.predict(img) elif model_name.lower() == 'efficientnet': elif model_choice == "EfficientNet": predictions = efficientnet_model.predict(img) else: return {"error": "Invalid model selection"}
class_index = np.argmax(predictions) class_name = class_folders[class_index] probability = predictions[0, class_index] elif model_choice == "VGGNet": predictions = VGGNet_model.predict(img) elif model_choice == "InceptionV3": predictions = InceptionV3_model.predict(img)
print(f"Class Index: {class_index}") print(f"Class Name: {class_name}") print(f"Probability: {probability}") class_idx = tf.argmax(predictions[0])
return {class_name: probability} # Map class index to class name class_name = class_names[class_idx]
image_input = gr.Image()model_selection_input = gr.Dropdown(["InceptionV3", "VGGNet", "ResNet", "EfficientNet"], value="InceptionV3") # Return the predicted class name as a string return f"Predicted class using {model_choice}: {class_name}"
gr.Interface( fn=classify_selected_model, inputs=[image_input, model_selection_input], outputs="label",# Create a Gradio interface with a dropdown for model selectioniface = gr.Interface( fn=classify_image, inputs=["image", gr.Dropdown(["ResNet", "EfficientNet","VGGNet","InceptionV3"], label="Select Model")], outputs="text", live=True).launch(debug=True))
# Launch the Gradio interfaceiface.launch(debug=True,share=True)app.py+45-29