trs's picture
Update app.py
14a39e9 verified
Raw History Blame
2.58 kB
import numpy as np
import os
import cv2
import gradio as gr
import onnxruntime as ort
import tensorflow as tf
onnx_efficientnet_path = './Models_onnx/efficientnet.onnx'
# onnx_inception_path = './Models_onnx/inceptionv3.onnx'
onnx_vggnet_path = './Models_onnx/vgg_16.onnx'
onnx_resnet_path = './Models_onnx/resnet50.onnx'
# Uncomment or remove the following line based on your needs
onnx_inception_model = ort.InferenceSession(onnx_inception_path)
onnx_vggnet_model = ort.InferenceSession(onnx_vggnet_path)
onnx_resnet_model = ort.InferenceSession(onnx_resnet_path)
onnx_efficientnet_model = ort.InferenceSession(onnx_efficientnet_path)
class_names = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"]
def classify_image(img, model_choice):
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"
img = tf.expand_dims(img, 0) # Add batch dimension
# Make predictions based on the selected model
if model_choice == "ResNet":
predictions = onnx_resnet_model.predict(img)
elif model_choice == "EfficientNet":
predictions = onnx_efficientnet_model.predict(img)
elif model_choice == "VGGNet":
predictions = onnx_vggnet_model.predict(img)
elif model_choice == "InceptionV3":
predictions = onnx_inception_model.predict(img)
class_idx = tf.argmax(predictions[0])
# Map class index to class name
class_name = class_names[class_idx]
# Return the predicted class name as a string
return f"Predicted class using {model_choice}: {class_name}"
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(["ResNet", "EfficientNet", "VGGNet", "InceptionV3"], label="Select Model")],
outputs=["json"],
live=True
).launch(share=True)