Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Load models
|
| 7 |
+
vgg16_model = tf.keras.models.load_model(
|
| 8 |
+
"/content/drive/MyDrive/Deep Learning Project/vgg16_best_model.keras"
|
| 9 |
+
)
|
| 10 |
+
xception_model = tf.keras.models.load_model(
|
| 11 |
+
"/content/drive/MyDrive/Deep Learning Project/Tri Classification/xception_best.keras"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def predict_fire(image):
|
| 16 |
+
img = Image.fromarray(image).convert("RGB")
|
| 17 |
+
img = img.resize((224, 224)) # Match model input size
|
| 18 |
+
img_array = np.array(img) / 255.0
|
| 19 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 20 |
+
|
| 21 |
+
fire_pred = vgg16_model.predict(img_array)
|
| 22 |
+
fire_status = "Fire Detected" if fire_pred[0][0] > 0.5 else "No Fire Detected"
|
| 23 |
+
|
| 24 |
+
if fire_status == "Fire Detected":
|
| 25 |
+
severity_pred = xception_model.predict(img_array)
|
| 26 |
+
severity_level = np.argmax(severity_pred[0])
|
| 27 |
+
severity = ["Mild", "Moderate", "Severe"][severity_level]
|
| 28 |
+
else:
|
| 29 |
+
severity = "N/A"
|
| 30 |
+
|
| 31 |
+
return fire_status, severity
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# Gradio interface
|
| 35 |
+
interface = gr.Interface(
|
| 36 |
+
fn=predict_fire,
|
| 37 |
+
inputs=gr.Image(type="numpy", label="Upload Image"),
|
| 38 |
+
outputs=[
|
| 39 |
+
gr.Textbox(label="Fire Status"),
|
| 40 |
+
gr.Textbox(label="Severity Level"),
|
| 41 |
+
],
|
| 42 |
+
title="Fire Prediction and Severity Classification",
|
| 43 |
+
description="Upload an image to predict fire and its severity level (Mild, Moderate, Severe).",
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
interface.launch()
|