当前位置: 首页 > article >正文

【OpenCV】使用Python和OpenCV实现火焰检测

1、 项目源码和结构(转) 

https://github.com/mushfiq1998/fire-detection-python-opencv

2、 运行环境

# 安装playsound:用于播放报警声音

pip install playsound

# 安装opencv-python:cv2用于图像和视频处理,特别是用于检测火灾

pip install opencv-python

3、 fireDetection.py

import cv2         # Library for openCV
import threading   # Library for threading -- which allows code to run in backend
import playsound   # Library for alarm sound
import smtplib     # Library for email sending

# To access xml file which includes positive and negative images of fire. 
# (Trained images) File is also provided with the code.
fire_cascade = cv2.CascadeClassifier('fire_detection_cascade_model.xml')

# To start camera this command is used "0" for laptop inbuilt camera 
# and "1" for USB attahed camera
# vid = cv2.VideoCapture(0) 

vid = cv2.VideoCapture("videos\\fire2.mp4")
runOnce = False # created boolean

# defined function to play alarm post fire detection using threading
def play_alarm_sound_function(): 
    # to play alarm # mp3 audio file is also provided with the code.
    playsound.playsound('fire_alarm.mp3',True) 
    print("Fire alarm end") # to print in consol

# Defined function to send mail post fire detection using threading
def send_mail_function(): 
    
    recipientmail = "add recipients mail" # recipients mail
    recipientmail = recipientmail.lower() # To lower case mail
    
    try:
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.ehlo()
        server.starttls()
        # Senders mail ID and password
        server.login("add senders mail", 'add senders password') 
        # recipients mail with mail message
        server.sendmail('add recipients mail', recipientmail, "Warning fire accident has been reported") 
         # to print in consol to whome mail is sent
        print("Alert mail sent sucesfully to {}".format(recipientmail))
        server.close() ## To close server
        
    except Exception as e:
        print(e) # To print error if any
		
while(True):
    Alarm_Status = False
    # Value in ret is True # To read video frame
    ret, frame = vid.read() 
    # To convert frame into gray color
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
    # to provide frame resolution
    fire = fire_cascade.detectMultiScale(frame, 1.2, 5) 

    ## to highlight fire with square 
    for (x,y,w,h) in fire:
        cv2.rectangle(frame,(x-20,y-20),(x+w+20,y+h+20),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = frame[y:y+h, x:x+w]

        print("Fire alarm initiated")
        # To call alarm thread
        threading.Thread(target=play_alarm_sound_function).start()  

        if runOnce == False:
            print("Mail send initiated")
            # To call alarm thread
            threading.Thread(target=send_mail_function).start() 
            runOnce = True
        if runOnce == True:
            print("Mail is already sent once")
            runOnce = True

    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
  • 加载训练模型:代码加载预训练的机器学习模型fire_detection_cascade_model.xml(XML 文件),该模型可以检测图像中的火灾。
  • 设置视频源:设置视频输入源,可以是笔记本电脑内置摄像头,也可以是外接USB 摄像头。该代码当前配置为从名为“fire2.mp4”的文件中读取视频。
  • 播放报警声音:定义播放报警声音的函数play_alarm_sound_function(),该函数在后台运行(线程)并播放名为“fire_alarm.mp3”的警报声音文件。
  • 发送电子邮件:send_mail_function()定义了另一个函数来发送电子邮件。它使用 Gmail 的 SMTP 服务器向指定收件人发送有关火灾检测的警告电子邮件。代码中需要提供发件人的电子邮件和密码。

4、结果 


http://www.kler.cn/a/465288.html

相关文章:

  • 基于Java的超级玛丽游戏的设计与实现【源码+文档+部署讲解】
  • UML之泛化、特化和继承
  • web漏洞之文件包含漏洞
  • mysql连接时报错1130-Host ‘hostname‘ is not allowed to connect to this MySQL server
  • Java实现自动化生成SQL COALESCE表达式
  • Kafka3.x KRaft 模式 (没有zookeeper) 常用命令
  • Spring Boot 中 TypeExcludeFilter 的作用及使用示例
  • 数据挖掘——聚类
  • vue3基础,小白从入门到精通
  • 三维算法基础知识
  • Unity Shader:从基础使用到动画实现全解析
  • 二层交换机和三层交换机
  • Vue3+Vue-router(history+路由前缀)+Vite 下本地刷新找不到页面问题
  • 钉钉h5微应用引用钉钉文件地址
  • 解决MYSQL Table has no partition for value from column_list的问题
  • jenkins修改端口以及开机自启
  • Kafka和Jenkins实现EMR上PySpark和EC2上Airflow的CI/CD
  • tcpdump的常见方法
  • Matlab中文注释乱码
  • 力扣编程从0-1
  • Elasticsearch JavaRestClient版
  • SQL 中索引分析,查询表索引
  • 滑雪护目镜欧盟CE认证EN 174测试标准
  • 在正则表达式中,\1 是用来引用第一个捕获组的内容的。捕获组是用括号 () 包裹的部分
  • Linux下卸载与安装JDK
  • 流体神经网络简介