asrcoddeploy commited on
Commit
eeaaec3
·
verified ·
1 Parent(s): 01d768d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import torch.nn as nn
4
+ import cv2
5
+ import numpy as np
6
+
7
+ # --------------------
8
+ # Model Definition
9
+ # --------------------
10
+ class FireCNN(nn.Module):
11
+ def __init__(self, num_classes=3):
12
+ super(FireCNN, self).__init__()
13
+
14
+ self.features = nn.Sequential(
15
+ nn.Conv2d(3, 16, 3, padding=1),
16
+ nn.BatchNorm2d(16),
17
+ nn.ReLU(),
18
+ nn.MaxPool2d(2),
19
+
20
+ nn.Conv2d(16, 32, 3, padding=1),
21
+ nn.BatchNorm2d(32),
22
+ nn.ReLU(),
23
+ nn.MaxPool2d(2),
24
+
25
+ nn.Conv2d(32, 64, 3, padding=1),
26
+ nn.BatchNorm2d(64),
27
+ nn.ReLU(),
28
+ nn.MaxPool2d(2),
29
+
30
+ nn.Conv2d(64, 128, 3, padding=1),
31
+ nn.BatchNorm2d(128),
32
+ nn.ReLU(),
33
+ nn.MaxPool2d(2),
34
+ )
35
+
36
+ self.classifier = nn.Sequential(
37
+ nn.Flatten(),
38
+ nn.Linear(128 * 8 * 8, 128),
39
+ nn.ReLU(),
40
+ nn.Dropout(0.3),
41
+ nn.Linear(128, num_classes)
42
+ )
43
+
44
+ def forward(self, x):
45
+ x = self.features(x)
46
+ x = self.classifier(x)
47
+ return x
48
+
49
+
50
+ # --------------------
51
+ # Load Model
52
+ # --------------------
53
+ checkpoint = torch.load("fire_model.pth", map_location="cpu")
54
+
55
+ model = FireCNN()
56
+ model.load_state_dict(checkpoint["model_state_dict"])
57
+ model.eval()
58
+
59
+ IMG_SIZE = checkpoint["img_size"]
60
+
61
+ # --------------------
62
+ # Prediction Function
63
+ # --------------------
64
+ def predict(image):
65
+ img = cv2.resize(image, (IMG_SIZE, IMG_SIZE))
66
+ img = img / 255.0
67
+ img = np.transpose(img, (2, 0, 1))
68
+ img = torch.tensor(img, dtype=torch.float32).unsqueeze(0)
69
+
70
+ with torch.no_grad():
71
+ outputs = model(img)
72
+ probs = torch.softmax(outputs, dim=1).squeeze().numpy()
73
+
74
+ return {
75
+ "fire": float(probs[0]),
76
+ "smoke": float(probs[1]),
77
+ "non_fire": float(probs[2])
78
+ }
79
+
80
+
81
+ # --------------------
82
+ # Gradio Interface
83
+ # --------------------
84
+ demo = gr.Interface(
85
+ fn=predict,
86
+ inputs=gr.Image(type="numpy"),
87
+ outputs=gr.Label(num_top_classes=3),
88
+ title="🔥 Fire / Smoke Detection",
89
+ description="Upload an image to detect Fire, Smoke, or Non-Fire"
90
+ )
91
+
92
+ demo.launch()