File size: 1,581 Bytes
0e00e06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50cdc70
0e00e06
 
 
 
ce1f2c2
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
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
from tensorflow.keras.preprocessing.image import ImageDataGenerator

# Load the trained model
model = tf.keras.models.load_model("MobileNet_model.h5")  # Ensure the model file is in the same directory

# Define class names from your dataset
class_names = ["Fake", "Low", "Medium", "High"]  # Update based on test_generator.class_indices.keys()

# Image Preprocessing
img_size = (128, 128)  # Same as used in test_generator

def preprocess_image(image):
    image = image.resize(img_size)  # Resize to (128,128)
    image = np.array(image) / 255.0  # Normalize as done in ImageDataGenerator (rescale=1./255)
    image = np.expand_dims(image, axis=0)  # Add batch dimension
    return image

# Prediction Function
def predict(image):
    image = preprocess_image(image)
    predictions = model.predict(image)
    predicted_class = np.argmax(predictions, axis=1)[0]  # Get the predicted class index
    confidence_scores = {class_names[i]: float(predictions[0][i]) for i in range(len(class_names))}  # Get probability scores
    
    return {"Predicted Class": class_names[predicted_class], "Confidence Scores": confidence_scores}

# Gradio Interface
interface = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil"),
    outputs=gr.JSON(),  # Returns class and confidence scores
    title="Fire Severity Detection",
    description="Upload an image to classify it into one of four categories: Fake, Low, Medium, or High."
)

if __name__ == "__main__":
    interface.launch(show_error=True)