trs's picture
Update app.py
f90154c verified
Raw History Blame
1.61 kB
import gradio as gr
import cv2
import onnxruntime as ort
import numpy as np
class_folders = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"]
# Define paths for the ONNX models
model_paths = {
"InceptionV3": './Models_onnx/inceptionv3.onnx',
"VGGNet": './Models_onnx/vgg_16.onnx',
"ResNet": './Models_onnx/resnet50.onnx',
"EfficientNet": './Models_onnx/efficientnet.onnx'
}
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 = img.astype(np.float32) / 255.0
onnx_model = ort.InferenceSession(model_paths[model_name])
input_name = onnx_model.get_inputs()[0].name
output_name = onnx_model.get_outputs()[0].name
predictions = onnx_model.run(None, {input_name: 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)