|
火焰識別源碼,親測有用,下載后需要搭建opencv QT5的環(huán)境。不懂得話可以私聊我
- # -*- coding: cp936 -*-
- import sys
- import threading #線程模塊
- import cv2
- import numpy as np
- import time
- import os
- import sys
- from PyQt5 import Qt
- from PyQt5 import QtCore
- from PyQt5.QtGui import QImage, QPixmap,QFont,QIcon
- from PyQt5.QtWidgets import (QApplication,QDialog, QFileDialog, QGridLayout,
- QLabel, QPushButton,QHBoxLayout,QFrame,QWidget,QLineEdit)
-
- font = cv2.FONT_HERSHEY_SIMPLEX #設(shè)置字體
-
- class Work(threading.Thread):
- def __init__(self, caller):
- threading.Thread.__init__(self)
- self.caller = caller #父類調(diào)用者
- self.isHaveFire = False #全局變量是否檢測到火焰
-
- def run(self): #線程啟動后自動調(diào)用此函數(shù)
- cap = cv2.VideoCapture()#初始化VideoCapture類對象
- if(self.caller.video_flag == 1):#標(biāo)志位為1,對應(yīng)打開攝像頭
- cap = cv2.VideoCapture(0) #打開攝像頭
- print("打開攝像頭")
- else: #標(biāo)志位為1,對應(yīng)打開視頻文件
- cap = cv2.VideoCapture(self.caller.video_path)#打開對應(yīng)路徑的視頻文件
- print("打開視頻文件%s"%self.caller.video_path)
- while(1):
- if(self.caller.flag): #如果視頻結(jié)束標(biāo)志位為1,則退出并清空圖像
- bgImg = np.ones((640,480,3),np.uint8)*240
- self.showViewImg(self.caller.label, bgImg)
- break
- ret, frame = cap.read() #讀取視頻或攝像頭幀
- if(ret==False):#取幀失敗,提示退出循環(huán)
- print("攝像頭打開失!" )
- break
- time.sleep(0.001)
- frame = self.fire_detect(frame)#調(diào)用火焰檢測函數(shù)
-
- frameTest = frame.copy()#原圖備份
- self.showViewImg(self.caller.label, frame)
-
- cap.release()#釋放VideoCapture類對象
- def showViewImg(self,label,img):
- # 提取圖像的尺寸和通道, 用于將opencv下的image轉(zhuǎn)換成Qimage
- #self.label.clear()
- channel = 1
- height = width = 1
- try:
- height, width, channel = img.shape
- except:
- channel = 1
- showImg = None
- if channel != 3:
- showImg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
- else:
- showImg = img.copy()
-
- bytesPerLine = 3 * width
- qImg = QImage(showImg.data, width, height, bytesPerLine,
- QImage.Format_RGB888).rgbSwapped()
- # 將Qimage顯示出來
- label.setPixmap(QPixmap.fromImage(qImg))
-
- def img_detect(self,img):#加載圖片檢測火焰
- print("Image Test")
- frame = self.fire_detect(img)#調(diào)用檢測火焰函數(shù)
- self.showViewImg(self.caller.label, frame)
-
-
- def fire_detect(self,frame): #火焰檢測函數(shù),核心算法
- self.isHaveFire = False #初始化火焰檢測結(jié)果標(biāo)志位False
- redThres = 49 #紅色閾值
- sat = 7 #比例系數(shù)
- blackImg = np.zeros((frame.shape[0],frame.shape[1],1),np.uint8)#創(chuàng)建原圖同大小的黑色圖像
- b,g,r = cv2.split(frame)#通道分離
- for i in range(0,frame.shape[0]): #訪問所有行
- for j in range(0,frame.shape[1]): #訪問所有列
- B = int(b[i,j])#訪問第i行,第j列藍(lán)色像素值
- G = int(g[i,j])#訪問第i行,第j列綠色像素值
- R = int(r[i,j])#訪問第i行,第j列紅色像素值
- maxValue = max(max(B,G),R)#求RBG像素最大值
- minValue = min(min(B,G),R)#求RBG像素最小值
- if (R+G+B) == 0:
- break
- S = (1-3.0*minValue/(R+G+B))#計算S值
- if(R>redThres and R>=G and G>=B and S>((255-R)*sat/redThres)):#火焰像素刪選
- blackImg[i,j] = 255 #滿足火焰像素,黑色圖像對應(yīng)位置變?yōu)榘咨?br />
- else:
- blackImg[i,j] = 0 #不滿足火焰像素,黑色圖像對應(yīng)位置仍為黑色
- blackImg = cv2.medianBlur(blackImg,5)#中值濾波濾除小雜訊
- k1=np.ones((5,5), np.uint8)#指定膨脹核大小5*5
- blackImg = cv2.dilate(blackImg, k1, iterations=1)#膨脹
- #查找火焰部分輪廓
- contours,hierarchy = cv2.findContours(blackImg, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
- index = -1
- maxArea = 0
- for i in range (0,len(contours)):#遍歷輪廓
- (x0, y0, w0, h0) = cv2.boundingRect(contours[i])#獲取輪廓外界矩形
- if(w0>10 and h0>10):#刪選外界矩形寬高均大于10
- #cv2.rectangle(frame,(x0,y0),(x0+w0,y0+h0),(0,255,0),2)
- if(w0*h0>maxArea):#比對輪廓面積
- maxArea = w0*h0 #獲取最大面積
- index = i #獲取最大面積對應(yīng)的輪廓序號
- if index != -1: #輪廓序號變化了說明沒檢測到火焰
- area = cv2.contourArea(contours[index])#獲取火焰輪廓對應(yīng)的面積
- cv2.putText(frame,("FireArea=%0.2f"%(area)), (5,20), font, 0.7, (0,255,0), 2)#圖片上輸出面積
- (x0, y0, w0, h0) = cv2.boundingRect(contours[index])#獲取外界矩形
- cv2.rectangle(frame,(x0,y0),(x0+w0,y0+h0),(0,255,0),2)#繪制外界矩形框出火焰區(qū)域
- self.isHaveFire = True #檢測到火焰標(biāo)志位為True,對應(yīng)會播放聲音
-
- else: #輪廓序號沒變說明沒檢測到火焰
- cv2.putText(frame,("FireArea=0"), (5,20), font, 0.7, (0,255,0), 2)#火焰面積為0
- return frame #返回最終處理后的圖像
-
- if __name__=="__main__":
- pass
復(fù)制代碼
|
-
-
-
火焰識別源碼.rar
2021-4-18 15:24 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
4.06 KB, 下載次數(shù): 60, 下載積分: 黑幣 -5
評分
-
查看全部評分
|