Fire detection based on HSV color model and CNN

HSV color model

HSV is also been call HexconeModel. The parameters of this model are: Hue (H), Saturation (S), and Value (V). Through experiments, we can obtain the range of HSV about the fire characteristics:
在这里插入图片描述
HSV space transformation in opencv-python:

hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
"""
frame => Image matrix
"""

Compared with LAB color model, although LAB is more sensitive to brightness, HSV is more accurate in fire positioning. Of course, it is also possible that the number of experiments is less why has a bad result of the LAB model.
在这里插入图片描述

The flow chart

PS:Refer to reference[1]
在这里插入图片描述

Detect and locate fire and make CNN network training data

The steps in this section are mostly the same as in the previous one. The difference is:

  1. Remove the GaussianFilter. Because what we need is actually a detection of the area of the fire, we don’t need to figure out the outline of the flame.
  2. Use CNN to identify the fire. Because our flame positioning is based on HSV filtering and image morphology, false detection of non-fire targets is inevitable. Therefore, we used ANN to identify the target. Here, we use VGG to train it.

Code:

import cv2
import numpy as np
from keras.models import load_model
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'


def make_prediction(image, model, class_dictionary):
    img = image / 255.

    # convert to 4D tensor
    image = np.expand_dims(img, axis=0)

    # train
    class_predicted = model.predict(image)
    inID = np.argmax(class_predicted[0])
    label = class_dictionary[inID]
    return label


def keras_model(weights_path):
    model = load_model(weights_path)
    return model


weights_path = 'fire1.h5'
# Define a dichotomous dictionary
class_dictionary = {}
class_dictionary[0] = 'fire'
class_dictionary[1] = 'not a fire'
model = keras_model(weights_path)


## And then the "#" which has code represents the video input ##
# cap = cv2.VideoCapture(1)

while True:
    # _, frame = cap.read()
    frame = cv2.imread('picture/exp/fire_6.jpg')
    frame = cv2.resize(frame, (400, 400))
    frame = cv2.GaussianBlur(frame, (3, 3), 1)

    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

	# Mask making
    l_m = np.array([0, 120, 200])
    u_m = np.array([50, 250, 250])
    mask = cv2.inRange(hsv, l_m, u_m)

	# Image morphology operation
    kernel1 = np.ones((15, 15), np.uint8)
    mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel1)

    res = cv2.bitwise_and(frame, frame, mask=mask)

    img = frame.copy()
    ret, thresh = cv2.threshold(mask, 0, 255, cv2.THRESH_BINARY)
    binary, contours, hierarchy = cv2.findContours(
    	thresh, 
    	cv2.RETR_EXTERNAL, 
    	cv2.CHAIN_APPROX_NONE
    )
    for cnt in contours:
        l = cv2.arcLength(cnt, True)
        if l > 50:
            x, y, w, h = cv2.boundingRect(cnt)

			# CNN data input
            img_test = frame[y:y + h, x:x + w]
            img_test = cv2.resize(img_test, (48, 48))
            label = make_prediction(img_test, model, class_dictionary)
            
            if label == 'fire':
                img = cv2.rectangle(
	                img, 
	                (x, y), 
	                (x + w, y + h), 
	                (0, 0, 255), 2)
                cv2.putText(
                    img,
                    "Fire", (x, y),
                    cv2.FONT_HERSHEY_SIMPLEX,
                    0.7, (0, 0, 255), 2)
            else:
                img = cv2.rectangle(
                	img, 
                	(x, y), 
                	(x + w, y + h), 
                	(0, 255, 0), 2)
                cv2.putText(
                    img,
                    "Not a Fire", (x, y),
                    cv2.FONT_HERSHEY_SIMPLEX,
                    0.7, (0, 255, 0), 2)

    cv2.imshow("img", img)

    key = cv2.waitKey(1)
    if key == 27:
        break
# cap.release()
cv2.destroyAllWindows()

Result: Here we use the sodium lamp as a contrast. Of course, during the experiment, it was found that some smaller signal lights(yellow) and brighter objects could also be judged as flames. Therefore, both data sets and algorithms need to improve.
在这里插入图片描述
So what’s the effect of video? We did an experiment and the results were as follows. However, during the inspection, it is found that the effect varies with the fluctuation of the fire. The bigger the flame fluctuation is, the worse the detection effect is. Therefore, the processing of the images can be greatly improved in this algorithm.
在这里插入图片描述

Conclusion

  • This paper is to use ANN to realize the fire recognition mentioned in the last paper. Although CNN is used in this paper, it is expected that RNN LSTM can be implemented, but the effect is not good in the experiment. In the future, I’m going to find a better way to solve it.
  • Although my algorithm implementation is not perfect, I think this implementation process is a good way.
  • The mathematical understanding of the algorithm is relatively important, especially in the field of AI. The algorithm described in the literature cannot be fully realized in this paper. The fundamental reason is that the knowledge of CNN is not fully understood, so the learning of neural network needs to be strengthened.

Reference

[1]李莹,李忠,李海洋,孙可可.结合颜色空间和CNN的火焰检测[J].计算机时代,2019(12):67-70.
LI Ying,LI Zhong,LI Haiyang,SUN Keke.Flame detecting with the combination of color space and CNN[J].Computer Era,2019(12):67-70.

[2]刘金利,张培玲.改进LeNet-5网络在图像分类中的应用[J].计算机工程与应用,2019,55(15):32-37+95.
LIU Jinli,ZHANG Peiling.Application of LeNet-5 neural network in image classification[J].Computer Engineering and Applications,2019,55(15):32-37.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure! Here's an example Python code for a fire identification camera based on OpenCV: ```python import cv2 import numpy as np # define the video capture device cap = cv2.VideoCapture(0) # define the lower and upper boundaries of the "fire" color in the HSV color space lower_fire = np.array([0, 50, 50]) upper_fire = np.array([10, 255, 255]) # loop over frames from the video stream while True: # grab the frame from the video stream ret, frame = cap.read() # convert the frame to the HSV color space hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # construct a mask for the "fire" color, then perform a series of dilations and erosions mask = cv2.inRange(hsv, lower_fire, upper_fire) mask = cv2.erode(mask, None, iterations=2) mask = cv2.dilate(mask, None, iterations=4) # find contours in the mask and initialize the current (x, y) center contours, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) center = None # only proceed if at least one contour was found if len(contours) > 0: # find the largest contour in the mask, then use it to compute the minimum enclosing circle and centroid c = max(contours, key=cv2.contourArea) ((x, y), radius) = cv2.minEnclosingCircle(c) M = cv2.moments(c) center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) # if the radius meets a minimum size, draw the circle and centroid on the frame if radius > 10: cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2) cv2.circle(frame, center, 5, (0, 0, 255), -1) # add a text label to the frame cv2.putText(frame, "Fire Detected", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) # show the frame cv2.imshow("Fire Detection Camera", frame) key = cv2.waitKey(1) & 0xFF # if the 'q' key is pressed, stop the loop if key == ord("q"): break # release the video capture device and close all windows cap.release() cv2.destroyAllWindows() ``` This code uses HSV color space to isolate the "fire" color in the video stream, and then performs a series of image processing techniques to identify and locate any fire within the frame. If a fire is detected, the code draws a circle and centroid around it and adds a text label to the frame. You can customize the code to suit your specific needs, such as adjusting the color thresholds or adding additional processing steps.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值