Spaces:
Sleeping
Sleeping
Commit
·
4838a1d
1
Parent(s):
a61e6d6
Made Modifications and added requirements.txt
Browse files
app.py
CHANGED
|
@@ -1,7 +1,94 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from tensorflow.keras.models import load_model
|
| 5 |
+
from tensorflow.keras.preprocessing import image
|
| 6 |
+
from PIL import Image
|
| 7 |
|
| 8 |
+
# --- CONFIGURATION (MUST MATCH YOUR TRAINING) ---
|
| 9 |
+
# The model file is automatically available in the Space's file system
|
| 10 |
+
MODEL_PATH = 'weapon_classifier_final_tuned.keras'
|
| 11 |
+
IMG_SIZE = (224, 224)
|
| 12 |
+
# The names of your classes, corresponding to the order they were generated (0=Not_Weapon, 1=Weapon)
|
| 13 |
+
CLASS_NAMES = ['Not a Weapon', 'Weapon']
|
| 14 |
+
# --- END CONFIGURATION ---
|
| 15 |
|
| 16 |
+
# 1. LOAD MODEL GLOBALLY
|
| 17 |
+
# Loading the model here ensures it only happens once when the app starts.
|
| 18 |
+
try:
|
| 19 |
+
classifier_model = load_model(MODEL_PATH)
|
| 20 |
+
print("Model loaded successfully for Gradio interface.")
|
| 21 |
+
except Exception as e:
|
| 22 |
+
print(f"Error loading model: {e}")
|
| 23 |
+
# Fallback in case of model load failure
|
| 24 |
+
classifier_model = None
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# 2. DEFINE THE PREDICTION FUNCTION
|
| 28 |
+
# Gradio automatically passes the uploaded image as a NumPy array (or PIL Image, depending on 'type').
|
| 29 |
+
def classify_weapon(input_img_array):
|
| 30 |
+
"""
|
| 31 |
+
Takes a NumPy image array, preprocesses it, and returns the classification.
|
| 32 |
+
"""
|
| 33 |
+
if classifier_model is None:
|
| 34 |
+
return "Error: Model failed to load."
|
| 35 |
+
|
| 36 |
+
# Gradio passes the image as a NumPy array with shape (H, W, 3) and values 0-255.
|
| 37 |
+
|
| 38 |
+
# a) Resize: Must resize to the model's required input size (224x224 for VGG16).
|
| 39 |
+
# We use PIL/Image.fromarray and resize before conversion to prevent distortion
|
| 40 |
+
img = Image.fromarray(input_img_array.astype('uint8'))
|
| 41 |
+
img = img.resize(IMG_SIZE)
|
| 42 |
+
|
| 43 |
+
# b) Convert to array and add batch dimension (1, 224, 224, 3)
|
| 44 |
+
img_array = np.array(img).astype('float32')
|
| 45 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 46 |
+
|
| 47 |
+
# c) Normalize (Rescale): Must match the 1./255 scaling used during training
|
| 48 |
+
processed_image = img_array / 255.0
|
| 49 |
+
|
| 50 |
+
# d) Make Prediction
|
| 51 |
+
# Prediction returns a 1x1 array, e.g., [[0.95]]
|
| 52 |
+
prediction = classifier_model.predict(processed_image)
|
| 53 |
+
|
| 54 |
+
# e) Interpret Output for Gradio Label Component
|
| 55 |
+
probability = prediction[0][0]
|
| 56 |
+
|
| 57 |
+
# Gradio's Label component expects a dictionary mapping labels to probabilities.
|
| 58 |
+
# We calculate the confidence for both classes based on the Sigmoid output.
|
| 59 |
+
confidences = {
|
| 60 |
+
CLASS_NAMES[1]: float(probability), # Weapon confidence
|
| 61 |
+
CLASS_NAMES[0]: float(1 - probability) # Not a Weapon confidence
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
return confidences
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
# 3. CREATE THE GRADIO INTERFACE
|
| 68 |
+
# Gradio will handle the 'Submit' button click automatically.
|
| 69 |
+
gr_interface = gr.Interface(
|
| 70 |
+
# The function to run when the user submits an image
|
| 71 |
+
fn=classify_weapon,
|
| 72 |
+
|
| 73 |
+
# Input component: Image component set to resize input to 224x224 for display
|
| 74 |
+
# Gradio automatically converts the image to a NumPy array for the function.
|
| 75 |
+
inputs=gr.Image(
|
| 76 |
+
shape=IMG_SIZE,
|
| 77 |
+
label="Upload Image for Classification",
|
| 78 |
+
type="numpy" # Pass NumPy array to the function
|
| 79 |
+
),
|
| 80 |
+
|
| 81 |
+
# Output component: Label displays the class names and confidence scores.
|
| 82 |
+
outputs=gr.Label(
|
| 83 |
+
num_top_classes=2,
|
| 84 |
+
label="Classification Result"
|
| 85 |
+
),
|
| 86 |
+
|
| 87 |
+
title="🔫 VGG16 Weapon Detector",
|
| 88 |
+
description="Upload an image to classify it as 'Weapon' or 'Not a Weapon'."
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
# 4. LAUNCH THE INTERFACE
|
| 92 |
+
# In a Hugging Face Space, the launch() call is automatically handled by the environment.
|
| 93 |
+
# If running locally, you would use: demo.launch()
|
| 94 |
+
gr_interface.launch()
|