trs's picture
Update app.py
7de6278 verified
Raw History Blame
2.17 kB
import numpy as np
import os
import cv2
import gradio as gr
import onnxruntime as ort
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):
# Resize the input image based on the selected model's expected input shape
input_size = (150, 150) # Assuming a common input size for all models
img = tf.image.resize(img, input_size)
img = image.img_to_array(img)
# Preprocess input based on the selected model
tokenizer = AutoTokenizer.from_pretrained(model_choice)
inputs = tokenizer(images=img, return_tensors="tf", padding=True)
# Make predictions based on the selected model
model = AutoModelForImageClassification.from_pretrained(model_choice)
predictions = model(**inputs).logits
class_idx = tf.argmax(predictions[0])
# Map class index to class name
class_name = tokenizer.convert_ids_to_tokens(class_idx.numpy())
# Return the predicted class name as a string
return f"Predicted class using {model_choice}: {class_name}"
# Example usage
img = tf.random.normal(shape=(224, 224, 3)) # Replace with your image data
model_choice = "facebook/vit-base-patch16-224-in21k-finetuned-image-coco" # Replace with your desired model
result = classify_image(img, model_choice)
print(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(["ResNet", "EfficientNet", "VGGNet", "InceptionV3"], label="Select Model")],
outputs=["json"],
live=True
).launch(debug=True)