python+keras识别火灾与烟雾报警

火灾如果发现得早的话可以及时扑灭,减少了损失,一般的大厦中都有火灾烟雾感应器,一旦检测到烟雾超过一定的量就会喷水灭火,那么可不可以通过摄像头和python来试试检测火灾呢,是可以的,今天小编来教大家如何用python识别火灾。

▊ 模型构建

我们今天使用专门检测火灾与烟雾的神经网络FireDetectionNet,该网络利用深度可分离卷积而不是标准卷积作为深度可分离卷积,它有以下优点:

★效率更高,因为Edge / IoT物联网设备将具有有限的CPU和功耗。

★需要更少的内存,同样,Edge / IoT物联网设备的RAM有限。

★由于我们的CPU能力有限,因此需要较少的计算。

★在某些情况下,其性能可能比标准卷积更好,更适合火灾/烟雾探测器。

下面是模型代码:

# import the necessary packages
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.layers import SeparableConv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import Activation
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Dense
class FireDetectionNet:
	@staticmethod
	def build(width, height, depth, classes):
		# initialize the model along with the input shape to be
		# "channels last" and the channels dimension itself
		model = Sequential()
		inputShape = (height, width, depth)
		chanDim = -1
		# CONV => RELU => POOL
		model.add(SeparableConv2D(16, (7, 7), padding="same",
			input_shape=inputShape))
		model.add(Activation("relu"))
		model.add(BatchNormalization(axis=chanDim))
		model.add(MaxPooling2D(pool_size=(2, 2)))
		# CONV => RELU => POOL
		model.add(SeparableConv2D(32, (3, 3), padding="same"))
		model.add(Activation("relu"))
		model.add(BatchNormalization(axis=chanDim))
		model.add(MaxPooling2D(pool_size=(2, 2)))
		# (CONV => RELU) * 2 => POOL
		model.add(SeparableConv2D(64, (3, 3), padding="same"))
		model.add(Activation("relu"))
		model.add(BatchNormalization(axis=chanDim))
		model.add(SeparableConv2D(64, (3, 3), padding="same"))
		model.add(Activation("relu"))
		model.add(BatchNormalization(axis=chanDim))
		model.add(MaxPooling2D(pool_size=(2, 2)))
		# first set of FC => RELU layers
		model.add(Flatten())
		model.add(Dense(128))
		model.add(Activation("relu"))
		model.add(BatchNormalization())
		model.add(Dropout(0.5))
		# second set of FC => RELU layers
		model.add(Dense(128))
		model.add(Activation("relu"))
		model.add(BatchNormalization())
		model.add(Dropout(0.5))
		# softmax classifier
		model.add(Dense(classes))
		model.add(Activation("softmax"))
		# return the constructed network architecture
		return model



▊ 数据集

我们从百度图片里获取到火灾和烟雾的一些图片数据放在文件件fire里。

python+keras识别火灾与烟雾报警
然后寻找8个场景的非火灾图片放在non-fire文件夹中。
python+keras识别火灾与烟雾报警

▊ 模型训练

现在我们开始训练模型
# set the matplotlib backend so figures can be saved in the background
import matplotlib
matplotlib.use("Agg")
# import the necessary packages
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from pyimagesearch.learningratefinder import LearningRateFinder
from pyimagesearch.firedetectionnet import FireDetectionNet
from pyimagesearch import config
from imutils import paths
import matplotlib.pyplot as plt
import numpy as np
import argparse
import cv2
import sys
def load_dataset(datasetPath):
	# grab the paths to all images in our dataset...

点击查看剩余70%

{{collectdata}}

网友评论0