File size: 2,757 Bytes
da19676
 
1f5d32e
da19676
e1abfbb
10a0153
 
 
 
9e1a7b8
da19676
 
 
 
 
d32d74c
da19676
9e1a7b8
1f5d32e
da19676
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d3a820
90946bd
fb7ebe0
 
685fa67
 
90946bd
da19676
cf37736
da19676
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import onnxruntime as ort
import numpy as np
import gradio as gr
import cv2

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):
    def preprocess_input(img, model):
        # Placeholder for preprocessing steps
        # Replace with actual preprocessing steps based on the model
        processed_img = img / 255.0
        return processed_img

    # Keep the input size as (150, 150) since your model expects that size
    input_size = (150, 150)
    img_resized = cv2.resize(img, input_size)

    # Preprocess the input image
    img_preprocessed = preprocess_input(img_resized, 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:
        predictions = model.run(None, {input_name: img_preprocessed})
        class_idx = np.argmax(predictions)
        class_name = class_names[class_idx]

        # Get the top 3 predictions
        top_predictions = sorted(enumerate(predictions[0]), key=lambda x: x[1], reverse=True)[:3]
        top_classes = [class_names[idx] for idx, _ in top_predictions]
        top_probs = [round(prob * 100, 2) for _, prob in top_predictions]

        result = {
            "Original Image": img.tolist(),  # Convert to list for compatibility with Gradio
            "Resized Image": img_resized.tolist(),
            f"Predicted class using {model_choice}": class_name,
            "Top 3 Predictions": list(zip(top_classes, top_probs)),
            "Confidence Scores": predictions[0].tolist()
        }
    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(["ResNet", "EfficientNet", "VGGNet", "InceptionV3"], label="Select Model")],
    outputs="json",
    live=True
)

iface.launch(share=True)