import cv2
import time
import numpy as np
# 打开摄像头
cap = cv2.VideoCapture(0) # 参数0表示自己电脑的摄像头,1则一般表示电脑外接的摄像头
# 设置开始时间
start_time = time.time()
# 设置帧率
frame_rate = 2 # 每秒两帧
# 循环读取摄像头数据
while True:
# 检查是否需要读取下一帧
elapsed_time = time.time() - start_time
if elapsed_time < 1 / frame_rate:
continue
# 读取一帧视频数据
ret, frame = cap.read()
if not ret:
break
# 进行火焰检测
img = frame # 直接将收集到的帧率当作图片使用
redThre = 115 # 指的是115~135红色分量阈值
sThre = 65 # 指的是55~65饱和度阈值
B = img[:, :, 0]
G = img[:, :, 1]
R = img[:, :, 2]
B1 = img[:, :, 0] / 255
G1 = img[:, :, 1] / 255
R1 = img[:, :, 2] / 255
minValue = np.array(
np.where(R1 <= G1, np.where(G1 <= B1, R1, np.where(R1 <= B1, R1, B1)), np.where(G1 <= B1, G1, B1)))
sumValue = R1 + G1 + B1
# HSI中S分量计算公式
S = np.array(np.where(sumValue != 0, (1 - 3.0 * minValue / sumValue), 0))
Sdet = (255 - R) / 20
STh
树莓派外接摄像头火焰检测模块
于 2023-07-27 21:36:43 首次发布