Spaces:
Runtime error
Runtime error
Update app.py
Browse files123456789101112131415161718192021222312523456789101112131415161718192021272829303132333435363738392241422324254 unmodified lines30313233343536372 unmodified lines40414261626364656667686970717273import onnxruntime as ortimport numpy as npimport gradio as grimport cv2
# Load ONNX modelsonnx_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 for classificationclass_names = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"]
def preprocess_input(img, target_size=(150, 150)): """Preprocess input image for model inference.""" img = cv2.imread(img) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.resize(img, target_size) img = img.astype(np.float32) / 255.0 img = np.expand_dims(img, axis=0) return img
def classify_image(img, model_choice): """Classify the input image using the selected ONNX model.""" def preprocess_input(img, model): 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
img_preprocessed = preprocess_input(img, model_choice)
# Run inference based on the selected model model = { "ResNet": onnx_resnet_model, "EfficientNet": onnx_efficientnet_model, "VGGNet": onnx_vggnet_model, # "InceptionV3": onnx_inception_model # Uncomment or remove based on your needs }[model_choice]
input_name = model.get_inputs()[0].name try: # Get the selected model model = { "ResNet": onnx_resnet_model, "EfficientNet": onnx_efficientnet_model, "VGGNet": onnx_vggnet_model, "InceptionV3": onnx_inception_model }[model_choice]
# Preprocess the input image img_preprocessed = preprocess_input(img)
# Run inference input_name = model.get_inputs()[0].name predictions = model.run(None, {input_name: img_preprocessed}) # Get the predicted class class_idx = np.argmax(predictions) class_name = class_names[class_idx]
4 unmodified lines
result = { "Original Image": img.tolist(), # Convert to list for compatibility with Gradio # Assuming img_resized was meant to be img_preprocessed "Resized Image": img_preprocessed.tolist(), f"Predicted class using {model_choice}": class_name, "Top 3 Predictions": list(zip(top_classes, top_probs)), "Confidence Scores": predictions[0].tolist()2 unmodified lines result = {"Error": str(e)}
return result
# Define Gradio inputs and interfaceimage_input = gr.Image()model_dropdown = gr.Dropdown(["ResNet", "EfficientNet", "VGGNet", "InceptionV3"], label="Select Model")iface = gr.Interface( fn=classify_image, inputs=[image_input, model_dropdown], outputs="json", live=True)
# Launch the Gradio interfaceiface.launch(share=True)app.py+21-52