File size: 6,126 Bytes
a0d454c
 
 
 
 
59dd57f
a0d454c
 
 
 
 
 
 
 
 
 
 
 
d9e6654
a0d454c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9e6654
a0d454c
649f58d
a0d454c
583efd1
 
 
a0d454c
583efd1
 
a0d454c
d9e6654
a0d454c
 
 
649f58d
a0d454c
 
 
d9e6654
a0d454c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59dd57f
 
68b3190
 
 
 
59dd57f
68b3190
59dd57f
68b3190
 
 
 
59dd57f
68b3190
 
 
 
59dd57f
68b3190
 
 
59dd57f
68b3190
 
 
 
 
 
 
 
 
 
 
59dd57f
 
 
 
 
68b3190
59dd57f
 
 
 
 
 
68b3190
 
 
59dd57f
68b3190
 
 
 
 
 
 
59dd57f
68b3190
 
 
 
 
 
 
 
59dd57f
 
 
68b3190
59dd57f
 
 
68b3190
59dd57f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68b3190
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import gradio as gr
import cv2
import numpy as np
from tensorflow.keras.models import load_model

"""
class_folders = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"]

Inception_path='./Models/inceptionv3_-saved-model-59-val_accuracy-0.94.hdf5'
VGGNet_path='./Models/new_vgg_16.hdf5'
ResNet_path= './Models/new_resnet50.hdf5'
EfficientNet_path= './Models/new_efficientnet.h5'

Inception_model = load_model(Inception_path)
VGGNet_model = load_model(VGGNet_path)
ResNet_model = load_model(ResNet_path)
Efficient_model = load_model(EfficientNet_path)



def classify_selected_model(image, model_name):
    try:
        img = cv2.imread(image)
        img = cv2.resize(img, (150, 150))
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        img = np.expand_dims(img, axis=0)

        models = {
            "ResNet": ResNet_model,
            "EfficientNet": Efficient_model,
            "VGGNet": VGGNet_model,
            "InceptionV3": Inception_model
        }

        all_predictions = {}
        for model_key, model in models.items():
            predictions = model.predict(img)
            class_index = np.argmax(predictions[0])
            class_name = class_folders[class_index]
            probability = round(max(predictions[0]) * 100, 2)
            all_predictions[model_key] = probability

        min_probability = min(all_predictions.values())

        if min_probability < 70:  # Adjust the threshold as needed
          
            result_str = "Can't detect this image. Please try another one."
            return None, None, result_str

        best_model = max(all_predictions, key=all_predictions.get)
        class_name = class_folders[np.argmax(models[best_model].predict(img)[0])]
        probability = max(probability)

        result_str = f"{best_model}: {class_name} {probability}%"
        result_str.append(result_str)

        return class_name, probability, None

    except Exception as e:
        error_message = f"Error: {str(e)}"
        return None, None, error_message

image_input = gr.Image(type='filepath', label="Input Image")
model_dropdown = gr.Dropdown(["ResNet", "EfficientNet", "VGGNet", "InceptionV3"], label="Select Model")
result_textbox = gr.Textbox("Error ", label="Error Message")

iface = gr.Interface(
    fn=classify_selected_model,
    inputs=[image_input, model_dropdown],
    outputs=[
        gr.Label(value="Archaeological Site"),
        gr.Label(value="Probability"),
        result_textbox
    ],
    examples=[
        ["Sites/Ajloun.png"],
        ["Sites/Jeresh.png"],
        ["Sites/Umm Qais.png"],
        ["Sites/Petra.png"],
        ["Sites/Roman Amphitheater.png"],
        ["Sites/Wadi Rum.png"]
    ],
    live=True,
    title="Image Classification for Archaeological Sites Interface"
)

iface.launch(debug=True, share=True)
"""

import gradio as gr
import cv2
import numpy as np
from tensorflow.keras.models import load_model

class_folders = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"]

Inception_path='./Models/new_efficientnet.h5'
VGGNet_path='./Models/new_vgg_16.hdf5'
ResNet_path= './Models/new_resnet50.hdf5'
EfficientNet_path= './Models/efficientnet-saved-model-06-val_acc-0.98.hdf5'

Inception_model = load_model(Inception_path)
VGGNet_model = load_model(VGGNet_path)
ResNet_model = load_model(ResNet_path)
Efficient_model = load_model(EfficientNet_path)

history=[]

def classify_selected_model(image, model_name):
    try:
        img = cv2.imread(image)
        img = cv2.resize(img, (150, 150))
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        img = np.expand_dims(img, axis=0)

        models = {
            "ResNet": ResNet_model,
            "EfficientNet": Efficient_model,
            "VGGNet": VGGNet_model,
            "InceptionV3": Inception_model
        }

        all_predictions = {}
        for model_key, model in models.items():
            predictions = model.predict(img)
            class_index = np.argmax(predictions[0])
            class_name = class_folders[class_index]
            probability = round(max(predictions[0]) * 100, 2)
            all_predictions[model_key] = probability

        min_probability = min(all_predictions.values())

        if min_probability < 70:  # Adjust the threshold as needed
            result_str = "Can't detect this image. Please try another one."
            history.append(result_str)
            return None, None, history[-3:-1]

        else:
            class_index = np.argmax(predictions[0])
            class_name = class_folders[class_index]
            max_probability = round(max(predictions[0]) * 100, 2)
            result_str = f"{model_name}: {class_name} {max_probability}%"
            history.append(result_str)
            return class_name, max_probability, history[-3:-1], result_str

        # best_model = max(all_predictions, key=all_predictions.get)
        # class_name = class_folders[np.argmax(models[best_model].predict(img)[0])]
        # probability = all_predictions[best_model]

        # result_str = f"{best_model}: {class_name} {probability}%"
        # history.append(result_str)

        # return class_name, probability, history[-3:-1]

    except Exception as e:
        error_message = f"Error: {str(e)}"
        return None, None, [error_message]

image_input = gr.Image(type='filepath', label="Input Image")
model_dropdown = gr.Dropdown(["ResNet", "EfficientNet", "VGGNet", "InceptionV3"], label="Select Model")
result_textbox = gr.Textbox("Results", label="History Results")

iface = gr.Interface(
    fn=classify_selected_model,
    inputs=[image_input, model_dropdown],
    outputs=[
        gr.Label(value="Archaeological Site"),
        gr.Label(value="Probability"),
        result_textbox
    ],
    examples=[
        ["Sites/Ajloun.png"],
        ["Sites/Jeresh.png"],
        ["Sites/Umm Qais.png"],
        ["Sites/Petra.png"],
        ["Sites/Roman Amphitheater.png"],
        ["Sites/Wadi Rum.png"]
    ],
    live=True,
    title="Image Classification for Archaeological Sites Interface"
)

iface.launch(debug=True, share=True)