找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4193|回复: 9
收起左侧

火焰识别Python源码(附带QT界面)opencv QT5环境

  [复制链接]
ID:610753 发表于 2021-4-18 15:59 | 显示全部楼层 |阅读模式

火焰识别源码,亲测有用,下载后需要搭建opencv  QT5的环境。不懂得话可以私聊我


  1. # -*- coding: cp936 -*-
  2. import sys
  3. import threading #线程模块
  4. import cv2
  5. import numpy as np
  6. import time
  7. import os
  8. import sys
  9. from PyQt5 import Qt
  10. from PyQt5 import QtCore
  11. from PyQt5.QtGui import QImage, QPixmap,QFont,QIcon
  12. from PyQt5.QtWidgets import (QApplication,QDialog, QFileDialog, QGridLayout,
  13.                 QLabel, QPushButton,QHBoxLayout,QFrame,QWidget,QLineEdit)



  14. font = cv2.FONT_HERSHEY_SIMPLEX #设置字体

  15. class Work(threading.Thread):
  16.     def __init__(self, caller):
  17.         threading.Thread.__init__(self)
  18.         self.caller = caller #父类调用者
  19.         self.isHaveFire = False #全局变量是否检测到火焰
  20.         
  21.     def run(self): #线程启动后自动调用此函数
  22.         cap = cv2.VideoCapture()#初始化VideoCapture类对象
  23.         if(self.caller.video_flag == 1):#标志位为1,对应打开摄像头
  24.             cap = cv2.VideoCapture(0)  #打开摄像头
  25.             print("打开摄像头")
  26.         else: #标志位为1,对应打开视频文件
  27.             cap = cv2.VideoCapture(self.caller.video_path)#打开对应路径的视频文件
  28.             print("打开视频文件%s"%self.caller.video_path)
  29.         while(1):
  30.             if(self.caller.flag): #如果视频结束标志位为1,则退出并清空图像
  31.                 bgImg = np.ones((640,480,3),np.uint8)*240
  32.                 self.showViewImg(self.caller.label, bgImg)
  33.                 break
  34.             ret, frame = cap.read() #读取视频或摄像头帧
  35.             if(ret==False):#取帧失败,提示退出循环
  36.                 print("摄像头打开失败!" )
  37.                 break
  38.             time.sleep(0.001)
  39.             frame = self.fire_detect(frame)#调用火焰检测函数
  40.             
  41.             frameTest = frame.copy()#原图备份
  42.             self.showViewImg(self.caller.label, frame)
  43.             
  44.         cap.release()#释放VideoCapture类对象

  45.     def showViewImg(self,label,img):
  46.         # 提取图像的尺寸和通道, 用于将opencv下的image转换成Qimage
  47.         #self.label.clear()
  48.         channel = 1
  49.         height = width = 1
  50.         try:
  51.             height, width, channel = img.shape
  52.         except:
  53.             channel = 1
  54.         showImg = None
  55.         if channel != 3:
  56.             showImg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
  57.         else:
  58.             showImg = img.copy()
  59.         
  60.         bytesPerLine = 3 * width
  61.         qImg = QImage(showImg.data, width, height, bytesPerLine,
  62.                            QImage.Format_RGB888).rgbSwapped()

  63.         # 将Qimage显示出来
  64.         label.setPixmap(QPixmap.fromImage(qImg))
  65.         
  66.     def img_detect(self,img):#加载图片检测火焰
  67.         print("Image Test")
  68.         frame = self.fire_detect(img)#调用检测火焰函数
  69.         self.showViewImg(self.caller.label, frame)
  70.         
  71.          
  72.     def fire_detect(self,frame): #火焰检测函数,核心算法
  73.         self.isHaveFire = False #初始化火焰检测结果标志位False
  74.         redThres = 49 #红色阈值
  75.         sat = 7 #比例系数
  76.         blackImg = np.zeros((frame.shape[0],frame.shape[1],1),np.uint8)#创建原图同大小的黑色图像
  77.         b,g,r = cv2.split(frame)#通道分离
  78.         for i in range(0,frame.shape[0]): #访问所有行
  79.             for j in range(0,frame.shape[1]): #访问所有列
  80.                 B = int(b[i,j])#访问第i行,第j列蓝色像素值
  81.                 G = int(g[i,j])#访问第i行,第j列绿色像素值
  82.                 R = int(r[i,j])#访问第i行,第j列红色像素值
  83.                 maxValue = max(max(B,G),R)#求RBG像素最大值
  84.                 minValue = min(min(B,G),R)#求RBG像素最小值
  85.                 if (R+G+B) == 0:
  86.                     break
  87.                 S = (1-3.0*minValue/(R+G+B))#计算S值
  88.                 if(R>redThres and R>=G and G>=B and S>((255-R)*sat/redThres)):#火焰像素删选
  89.                    blackImg[i,j] = 255 #满足火焰像素,黑色图像对应位置变为白色
  90.                 else:
  91.                    blackImg[i,j] = 0 #不满足火焰像素,黑色图像对应位置仍为黑色
  92.         blackImg = cv2.medianBlur(blackImg,5)#中值滤波滤除小杂讯
  93.         k1=np.ones((5,5), np.uint8)#指定膨胀核大小5*5
  94.         blackImg = cv2.dilate(blackImg, k1, iterations=1)#膨胀
  95.         #查找火焰部分轮廓
  96.         contours,hierarchy = cv2.findContours(blackImg, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
  97.         index = -1
  98.         maxArea = 0
  99.         for i in range (0,len(contours)):#遍历轮廓
  100.             (x0, y0, w0, h0) = cv2.boundingRect(contours[i])#获取轮廓外界矩形
  101.             if(w0>10 and h0>10):#删选外界矩形宽高均大于10
  102.                 #cv2.rectangle(frame,(x0,y0),(x0+w0,y0+h0),(0,255,0),2)
  103.                 if(w0*h0>maxArea):#比对轮廓面积
  104.                     maxArea = w0*h0 #获取最大面积
  105.                     index = i #获取最大面积对应的轮廓序号
  106.         if index != -1: #轮廓序号变化了说明没检测到火焰
  107.             area = cv2.contourArea(contours[index])#获取火焰轮廓对应的面积
  108.             cv2.putText(frame,("FireArea=%0.2f"%(area)), (5,20), font, 0.7, (0,255,0), 2)#图片上输出面积
  109.             (x0, y0, w0, h0) = cv2.boundingRect(contours[index])#获取外界矩形
  110.             cv2.rectangle(frame,(x0,y0),(x0+w0,y0+h0),(0,255,0),2)#绘制外界矩形框出火焰区域
  111.             self.isHaveFire = True #检测到火焰标志位为True,对应会播放声音
  112.             
  113.         else: #轮廓序号没变说明没检测到火焰
  114.             cv2.putText(frame,("FireArea=0"), (5,20), font, 0.7, (0,255,0), 2)#火焰面积为0
  115.         return frame #返回最终处理后的图像
  116.         

  117. if __name__=="__main__":
  118.     pass
复制代码

51hei.png

火焰识别源码.rar

4.06 KB, 下载次数: 59, 下载积分: 黑币 -5

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:242753 发表于 2021-5-12 13:12 | 显示全部楼层
现在好多python代码,早知道不学C语言了
回复

使用道具 举报

ID:940457 发表于 2021-6-19 15:08 | 显示全部楼层

现在好多python代码,早知道不学C语言了
回复

使用道具 举报

ID:940457 发表于 2021-6-19 15:09 | 显示全部楼层

现在好多python代码,早知道不学C语言了
回复

使用道具 举报

ID:974144 发表于 2021-10-22 14:41 | 显示全部楼层

现在好多python代码,早知道不学C语言了
回复

使用道具 举报

ID:661945 发表于 2022-1-29 16:14 | 显示全部楼层
图像识别方面的吧~~~
回复

使用道具 举报

ID:171525 发表于 2022-4-14 17:04 | 显示全部楼层
PYTHON 都有哪些库?
回复

使用道具 举报

ID:512969 发表于 2022-4-16 08:49 | 显示全部楼层
具体怎么使用啊楼主
回复

使用道具 举报

ID:58930 发表于 2022-4-20 20:15 | 显示全部楼层
建议楼主写一个程序流程,相比会更完美,同时增强自己的文字功底。
回复

使用道具 举报

ID:786205 发表于 2022-5-10 10:01 | 显示全部楼层
您好 我在Ubuntu系统里运行您的代码 报错了,错误如下:
QObject::moveToThread: Current thread (0x107c340) is not the object's thread (0x1745370).
Cannot move to target thread (0x107c340)

qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/wyh/.local/lib/python3.8/site-packages/cv2/qt/plugins" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: xcb, eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl.
您看这该如何解决
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表