Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import numpy as np
|
| 4 |
+
from tensorflow.keras.models import load_model
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
# Load the saved models
|
| 8 |
+
cnn_model = load_model('cnn_image_classifier.h5')
|
| 9 |
+
resnet_model = load_model('resnet_image_classifier.h5')
|
| 10 |
+
|
| 11 |
+
# Load label map (assuming you have it defined or loaded elsewhere)
|
| 12 |
+
# Replace with your actual loading method
|
| 13 |
+
with open('EuroSAT/label_map.json', 'r') as f:
|
| 14 |
+
label_map = json.load(f)
|
| 15 |
+
label_map_inv = {v: k for k, v in label_map.items()}
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def predict_image(image):
|
| 19 |
+
# Preprocess the image (resize, normalize, etc.)
|
| 20 |
+
image = cv2.resize(image, (64, 64))
|
| 21 |
+
image = image / 255.0
|
| 22 |
+
image = np.expand_dims(image, axis=0)
|
| 23 |
+
|
| 24 |
+
# Make predictions with both models
|
| 25 |
+
cnn_pred = cnn_model.predict(image)[0]
|
| 26 |
+
resnet_pred = resnet_model.predict(image)[0]
|
| 27 |
+
|
| 28 |
+
# Get top 5 predictions for CNN
|
| 29 |
+
cnn_top5_indices = np.argsort(cnn_pred)[::-1][:5]
|
| 30 |
+
cnn_top5 = {
|
| 31 |
+
label_map_inv[idx]: float(cnn_pred[idx]) for idx in cnn_top5_indices
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
# Get top 5 predictions for ResNet
|
| 35 |
+
resnet_top5_indices = np.argsort(resnet_pred)[::-1][:5]
|
| 36 |
+
resnet_top5 = {
|
| 37 |
+
label_map_inv[idx]: float(resnet_pred[idx]) for idx in resnet_top5_indices
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
# Final predictions
|
| 41 |
+
cnn_final_prediction = label_map_inv[np.argmax(cnn_pred)]
|
| 42 |
+
resnet_final_prediction = label_map_inv[np.argmax(resnet_pred)]
|
| 43 |
+
|
| 44 |
+
return cnn_top5, cnn_final_prediction, resnet_top5, resnet_final_prediction
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
iface = gr.Interface(
|
| 48 |
+
fn=predict_image,
|
| 49 |
+
inputs=gr.Image(type="numpy"),
|
| 50 |
+
outputs=[
|
| 51 |
+
gr.Label(num_top_classes=5, label="CNN Top 5 Predictions"),
|
| 52 |
+
gr.Textbox(label="CNN Final Prediction"),
|
| 53 |
+
gr.Label(num_top_classes=5, label="ResNet Top 5 Predictions"),
|
| 54 |
+
gr.Textbox(label="ResNet Final Prediction"),
|
| 55 |
+
],
|
| 56 |
+
title="Image Classification with CNN and ResNet",
|
| 57 |
+
description="Upload an image to classify using two different models.",
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
iface.launch(debug=True)
|