Spaces:
Runtime error
Runtime error
File size: 2,088 Bytes
7da5886 224c243 7da5886 36e402d 9afeced 224c243 36e402d 7da5886 224c243 7da5886 224c243 36e402d 224c243 7157a7a 224c243 0f44bf7 224c243 7157a7a 224c243 7157a7a 224c243 7da5886 224c243 7da5886 813c2fd 7da5886 224c243 7da5886 224c243 f3ff58b 310ac2f 7da5886 f3ff58b | 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 | import onnxruntime as ort
import numpy as np
import gradio as gr
import cv2
# Load ONNX model
onnx_inception_model = ort.InferenceSession('./Models_onnx/inceptionv3.onnx')
onnx_vggnet_model = ort.InferenceSession('./Models_onnx/vgg_16.onnx')
onnx_resnet_model = ort.InferenceSession('./Models_onnx/resnet50.onnx')
onnx_efficientnet_model = ort.InferenceSession('./Models_onnx/efficientnet.onnx')
class_names = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"]
def preprocess_input(img):
img = cv2.imread(img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (150, 150))
img = img.astype(np.float32) / 255.0
img = np.expand_dims(img, axis=0)
return img
def classify_image(img, model_choice):
# Run inference based on the selected model
model = {
"InceptionV3": onnx_inception_model,
"VGGNet": onnx_vggnet_model,
"ResNet": onnx_resnet_model,
"EfficientNet": onnx_efficientnet_model,
}.get(model_choice)
if model is None:
return {"Error": f"Model {model_choice} not found"}
img_preprocessed = preprocess_input(img)
onnx_file=onnx.load(model)
input_name = model.get_inputs()[0].name
output_name= model.get_output()[0].name
try:
predictions = model.run(None, {input_name: img_preprocessed})
class_idx = np.argmax(predictions[0][0])
class_name = class_names[class_idx]
confidence_score = round(predictions[0, class_idx] * 100, 2)
result = {"Predicted class": class_name,"Confidence Score": confidence_score }
except Exception as e:
result = {"Error": str(e)}
return result
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(["InceptionV3", "VGGNet", "ResNet", "EfficientNet"], label="Select Model")],
outputs=[gr.Label(name="Predicted class"), gr.Label(name="Confidence Score")],
title='Image Classification',
live=True
)
iface.launch(share=True) |