forked from tobybreckon/fire-detection-cnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firenet.py
170 lines (108 loc) · 5.53 KB
/
firenet.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
################################################################################
# Example : perform live fire detection in video using FireNet CNN
# Copyright (c) 2017/18 - Andrew Dunnings / Toby Breckon, Durham University, UK
# License : https://github.com/tobybreckon/fire-detection-cnn/blob/master/LICENSE
################################################################################
import cv2
import os
import sys
import math
################################################################################
import tflearn
from tflearn.layers.core import *
from tflearn.layers.conv import *
from tflearn.layers.normalization import *
from tflearn.layers.estimator import regression
################################################################################
def construct_firenet (x,y, training=False):
# Build network as per architecture in [Dunnings/Breckon, 2018]
network = tflearn.input_data(shape=[None, y, x, 3], dtype=tf.float32)
network = conv_2d(network, 64, 5, strides=4, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)
network = conv_2d(network, 128, 4, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)
network = conv_2d(network, 256, 1, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)
network = fully_connected(network, 4096, activation='tanh')
if(training):
network = dropout(network, 0.5)
network = fully_connected(network, 4096, activation='tanh')
if(training):
network = dropout(network, 0.5)
network = fully_connected(network, 2, activation='softmax')
# if training then add training hyperparameters
if(training):
network = regression(network, optimizer='momentum',
loss='categorical_crossentropy',
learning_rate=0.001)
# constuct final model
model = tflearn.DNN(network, checkpoint_path='firenet',
max_checkpoints=1, tensorboard_verbose=2)
return model
################################################################################
if __name__ == '__main__':
################################################################################
# construct and display model
model = construct_firenet (224, 224, training=False)
print("Constructed FireNet ...")
model.load(os.path.join("models/FireNet", "firenet"),weights_only=True)
print("Loaded CNN network weights ...")
################################################################################
# network input sizes
rows = 224
cols = 224
# display and loop settings
windowName = "Live Fire Detection - FireNet CNN";
keepProcessing = True;
################################################################################
if len(sys.argv) == 2:
# load video file from first command line argument
video = cv2.VideoCapture(sys.argv[1])
print("Loaded video ...")
# create window
cv2.namedWindow(windowName, cv2.WINDOW_NORMAL);
# get video properties
width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH));
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = video.get(cv2.CAP_PROP_FPS)
frame_time = round(1000/fps);
while (keepProcessing):
# start a timer (to see how long processing and display takes)
start_t = cv2.getTickCount();
# get video frame from file, handle end of file
ret, frame = video.read()
if not ret:
print("... end of video file reached");
break;
# re-size image to network input size and perform prediction
small_frame = cv2.resize(frame, (rows, cols), cv2.INTER_AREA)
# perform prediction on the image frame which is:
# - an image (tensor) of dimension 224 x 224 x 3
# - a 3 channel colour image with channel ordering BGR (not RGB)
# - un-normalised (i.e. pixel range going into network is 0->255)
output = model.predict([small_frame])
# label image based on prediction
if round(output[0][0]) == 1:
cv2.rectangle(frame, (0,0), (width,height), (0,0,255), 50)
cv2.putText(frame,'FIRE',(int(width/16),int(height/4)),
cv2.FONT_HERSHEY_SIMPLEX, 4,(255,255,255),10,cv2.LINE_AA);
else:
cv2.rectangle(frame, (0,0), (width,height), (0,255,0), 50)
cv2.putText(frame,'CLEAR',(int(width/16),int(height/4)),
cv2.FONT_HERSHEY_SIMPLEX, 4,(255,255,255),10,cv2.LINE_AA);
# stop the timer and convert to ms. (to see how long processing and display takes)
stop_t = ((cv2.getTickCount() - start_t)/cv2.getTickFrequency()) * 1000;
# image display and key handling
cv2.imshow(windowName, frame);
# wait fps time or less depending on processing time taken (e.g. 1000ms / 25 fps = 40 ms)
key = cv2.waitKey(max(2, frame_time - int(math.ceil(stop_t)))) & 0xFF;
if (key == ord('x')):
keepProcessing = False;
elif (key == ord('f')):
cv2.setWindowProperty(windowName, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN);
else:
print("usage: python firenet.py videofile.ext");
################################################################################