Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
from tensorflow import keras
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
class_names = ['drive', 'legglance_flick', 'pullshot', 'sweep']
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# Load models
|
| 11 |
+
def load_models():
|
| 12 |
+
try:
|
| 13 |
+
vgg16_model = keras.models.load_model('vgg16_finetuned.pth')
|
| 14 |
+
custom_cnn_model = keras.models.load_model('cricket_model.pth')
|
| 15 |
+
return vgg16_model, custom_cnn_model
|
| 16 |
+
except Exception as e:
|
| 17 |
+
print(f"Error loading models: {e}")
|
| 18 |
+
return None, None
|
| 19 |
+
|
| 20 |
+
vgg16_model, custom_cnn_model = load_models()
|
| 21 |
+
|
| 22 |
+
def preprocess_image(image, target_size=(224, 224)):
|
| 23 |
+
"""Preprocess image for model prediction"""
|
| 24 |
+
if image is None:
|
| 25 |
+
return None
|
| 26 |
+
|
| 27 |
+
# Convert to PIL Image if needed
|
| 28 |
+
if not isinstance(image, Image.Image):
|
| 29 |
+
image = Image.fromarray(image)
|
| 30 |
+
|
| 31 |
+
# Resize image
|
| 32 |
+
image = image.resize(target_size)
|
| 33 |
+
|
| 34 |
+
# Convert to array and normalize
|
| 35 |
+
img_array = np.array(image)
|
| 36 |
+
|
| 37 |
+
# Handle grayscale images
|
| 38 |
+
if len(img_array.shape) == 2:
|
| 39 |
+
img_array = np.stack([img_array] * 3, axis=-1)
|
| 40 |
+
|
| 41 |
+
# Add batch dimension
|
| 42 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 43 |
+
|
| 44 |
+
# Normalize to [0, 1]
|
| 45 |
+
img_array = img_array.astype('float32') / 255.0
|
| 46 |
+
|
| 47 |
+
return img_array
|
| 48 |
+
|
| 49 |
+
def predict(image):
|
| 50 |
+
"""Make predictions with both models"""
|
| 51 |
+
if image is None:
|
| 52 |
+
return None, None
|
| 53 |
+
|
| 54 |
+
if vgg16_model is None or custom_cnn_model is None:
|
| 55 |
+
return "Models not loaded properly", "Models not loaded properly"
|
| 56 |
+
|
| 57 |
+
# Preprocess image
|
| 58 |
+
processed_img = preprocess_image(image)
|
| 59 |
+
|
| 60 |
+
# Get predictions from both models
|
| 61 |
+
vgg16_pred = vgg16_model.predict(processed_img, verbose=0)[0]
|
| 62 |
+
custom_cnn_pred = custom_cnn_model.predict(processed_img, verbose=0)[0]
|
| 63 |
+
|
| 64 |
+
# Create confidence dictionaries
|
| 65 |
+
vgg16_confidence = {CLASS_NAMES[i]: float(vgg16_pred[i]) for i in range(len(CLASS_NAMES))}
|
| 66 |
+
custom_cnn_confidence = {CLASS_NAMES[i]: float(custom_cnn_pred[i]) for i in range(len(CLASS_NAMES))}
|
| 67 |
+
|
| 68 |
+
return vgg16_confidence, custom_cnn_confidence
|
| 69 |
+
|
| 70 |
+
# Create Gradio interface
|
| 71 |
+
with gr.Blocks(title="Dual Model Comparison") as demo:
|
| 72 |
+
gr.Markdown(
|
| 73 |
+
"""
|
| 74 |
+
# 🔍 Dual Model Image Classification
|
| 75 |
+
|
| 76 |
+
Compare predictions from two models trained on the same dataset:
|
| 77 |
+
- **VGG16 Fine-tuned**: Transfer learning model based on VGG16
|
| 78 |
+
- **Custom CNN**: CNN trained from scratch
|
| 79 |
+
|
| 80 |
+
Upload an image to see predictions and confidence scores from both models.
|
| 81 |
+
"""
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
with gr.Row():
|
| 85 |
+
with gr.Column():
|
| 86 |
+
input_image = gr.Image(label="Upload Image", type="numpy")
|
| 87 |
+
predict_btn = gr.Button("Predict", variant="primary")
|
| 88 |
+
|
| 89 |
+
with gr.Row():
|
| 90 |
+
with gr.Column():
|
| 91 |
+
gr.Markdown("### VGG16 Fine-tuned Model")
|
| 92 |
+
vgg16_output = gr.Label(label="Predictions", num_top_classes=4)
|
| 93 |
+
|
| 94 |
+
with gr.Column():
|
| 95 |
+
gr.Markdown("### Custom CNN Model")
|
| 96 |
+
custom_cnn_output = gr.Label(label="Predictions", num_top_classes=4)
|
| 97 |
+
|
| 98 |
+
# Examples section (optional - add your example images)
|
| 99 |
+
gr.Markdown("### Examples")
|
| 100 |
+
gr.Examples(
|
| 101 |
+
examples=[
|
| 102 |
+
# Add paths to example images here
|
| 103 |
+
# ["example1.jpg"],
|
| 104 |
+
# ["example2.jpg"],
|
| 105 |
+
],
|
| 106 |
+
inputs=input_image,
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
# Connect the prediction function
|
| 110 |
+
predict_btn.click(
|
| 111 |
+
fn=predict,
|
| 112 |
+
inputs=input_image,
|
| 113 |
+
outputs=[vgg16_output, custom_cnn_output]
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
# Launch the app
|
| 117 |
+
if __name__ == "__main__":
|
| 118 |
+
demo.launch()
|