久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

標題: 火焰識別Python源碼(附帶QT界面)opencv QT5環境 [打印本頁]

作者: 林宏偉    時間: 2021-4-18 15:59
標題: 火焰識別Python源碼(附帶QT界面)opencv QT5環境

火焰識別源碼,親測有用,下載后需要搭建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 (4.56 KB, 下載次數: 207)

51hei.png

火焰識別源碼.rar

4.06 KB, 下載次數: 60, 下載積分: 黑幣 -5


作者: woshishui22    時間: 2021-5-12 13:12
現在好多python代碼,早知道不學C語言了
作者: pppp0    時間: 2021-6-19 15:08

現在好多python代碼,早知道不學C語言了
作者: pppp0    時間: 2021-6-19 15:09

現在好多python代碼,早知道不學C語言了
作者: 小李滋滋滋    時間: 2021-10-22 14:41

現在好多python代碼,早知道不學C語言了

作者: wuwei520    時間: 2022-1-29 16:14
圖像識別方面的吧~~~
作者: leeyeeng    時間: 2022-4-14 17:04
PYTHON 都有哪些庫?
作者: 愛瘋的孩紙    時間: 2022-4-16 08:49
具體怎么使用啊樓主
作者: tuohang2012    時間: 2022-4-20 20:15
建議樓主寫一個程序流程,相比會更完美,同時增強自己的文字功底。
作者: 皮皮蝦米    時間: 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.
您看這該如何解決




歡迎光臨 (http://m.zg4o1577.cn/bbs/) Powered by Discuz! X3.1
主站蜘蛛池模板: 青青草成人在线 | 欧美成人黄色 | h在线视频 | 久久国产精品免费视频 | 久久精品视频免费 | 91久久| 91成人免费 | 午夜免费福利视频 | 国产精品久久久久久久 | 亚洲永久精品视频 | 免费网站观看www在线观 | 黄色国产在线观看 | 亚洲精品1区 | 天天视频国产 | 天天有av| 中文字幕一级片 | 日韩伊人 | 欧美日韩精品久久 | 91蜜桃婷婷狠狠久久综合9色 | 97精品国产97久久久久久免费 | 国产一级免费视频 | 国产精品国产成人国产三级 | 婷婷久久五月 | 一级黄色免费视频 | 成年人网站在线免费观看 | 欧美a级成人淫片免费看 | 亚洲黄色在线观看 | 成人性色生活片 | 97国产视频| 亚洲一区欧美一区 | 91超碰在线观看 | 国产成人免费观看 | 黄色免费一级片 | 四虎永久网址 | 亚洲另类视频 | 超碰在线99 | 午夜精品一区二区三区在线视频 | 97在线免费视频 | 国产日韩精品一区二区 | 免费网站黄 | 黄色免费片 |