python - 如何在python中的if语句之后运行视频
问题描述
我创建了一个代码来使用 OpenCV 检测视频中的火灾,该技术使用 HSV 颜色空间来检测颜色并标记警报,但问题是当警报开始时视频窗口停止或它不从程序的开始。有人可以帮忙吗?
警报和视频可以在这里下载
import cv2
import numpy as np
import playsound
Alarm_Status = False
def play_sound():
playsound.playsound("Alarm Sound.mp3",True)
# Importing the video
cam = cv2.VideoCapture("Fire.mp4")
while True:
# Reading the camera
ret, frame = cam.read()
# Converting the color to HSV
hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Set the color boundaries
lower = np.array([18, 50, 50], dtype = "uint8")
upper = np.array([35, 255, 255], dtype = "uint8")
# Color detection
mask = cv2.inRange(hsv_frame, lower, upper)
# Create the Output video
output = cv2.bitwise_and(frame, hsv_frame, mask = mask)
# Count the number of red pixels
redPixels = cv2.countNonZero(mask)
if int(redPixels) > 1000 and Alarm_Status == False:
playsound()
Alarm_Status = True
pass
# Showing the video
cv2.imshow("Output", output)
# Stoping the code
if cv2.waitKey(25) == ord("q"):
break
# Destroy the window
cv2.destroyAllWindows()
cam.release()
解决方案
您可以在其他进程中启动警报。创建alarm.py
文件:
import playsound
playsound.playsound("Alarm Sound.mp3",True)
在您的主应用程序中,您可以调用alarm.py
:
import cv2
import numpy as np
import playsound
import subprocess
Alarm_Status = False
# Importing the video
cam = cv2.VideoCapture("Fire.mp4")
while True:
# Reading the camera
ret, frame = cam.read()
# Converting the color to HSV
hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Set the color boundaries
lower = np.array([18, 50, 50], dtype = "uint8")
upper = np.array([35, 255, 255], dtype = "uint8")
# Color detection
mask = cv2.inRange(hsv_frame, lower, upper)
# Create the Output video
output = cv2.bitwise_and(frame, hsv_frame, mask = mask)
# Count the number of red pixels
redPixels = cv2.countNonZero(mask)
if int(redPixels) > 1000 and Alarm_Status == False:
subprocess.Popen(["/usr/bin/python3","./alarm.py"])
Alarm_Status = True
pass
# Showing the video
cv2.imshow("Output", output)
# Stoping the code
if cv2.waitKey(25) == ord("q"):
break
# Destroy the window
cv2.destroyAllWindows()
cam.release()
推荐阅读
- svn - 使用 Sublime Text 作为 SVN 并排比较工具
- java - Firebase RTDB 在 Android 应用中没有响应
- javascript - Highcharts 甘特图或任何图表 x 轴滚动位置位于加载图表的开始/左侧
- wordpress - 在 .htaccess wordpress 中重写
- google-apps-script - 只能在我的个人帐户中运行任何需要权限的谷歌脚本
- kubernetes - 如何使用 kubectl Patch 命令删除 Deployment volumeMounts 中的元素?
- python-3.x - 通过子类(产品)类别->产品->标签从类别类访问manyTomany字段(标签)
- git - 如何在 GitHub 中提交文件以保持其私密性
- javascript - 如何将值从反应本机应用程序传递到节点 js 后端
- python - 我已经使用 pip 安装了 discord.py,但是在尝试将其导入我的程序后,它说找不到?