酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

写一个yolo火灾监测系统

# YOLO火灾监测系统实现 下面是一个基于YOLOv8的火灾监测系统实现,使用Python和OpenCV: ```python import cv2 from ultralytics import YOLO import time class FireDetectionSystem: def __init__(self, model_path='yolov8n-fire.pt', alert_threshold=0.7): """ 初始化火灾检测系统 :param model_path: YOLOv8模型路径 :param alert_threshold: 火灾检测置信度阈值 """ self.model = YOLO(model_path) # 加载预训练模型 self.alert_threshold = alert_threshold self.alarm_status = False self.last_alert_time = 0 self.alert_cooldown = 10 # 警报冷却时间(秒) def process_frame(self, frame): """ 处理视频帧并检测火灾 :param frame: 输入视频帧 :return: 带检测结果的帧 """ results = self.model(frame, verbose=False) # 禁用详细输出 for result in results: boxes = result.boxes for box in boxes: if box.conf[0] > self.alert_threshold and box.cls[0] == 0: # 假设类别0是火灾 self.trigger_alert() x1, y1, x2, y2 = map(int, box.xyxy[0]) cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 2) cv2.putText(frame, f'Fire {box.conf[0]:.2f}', (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2) return frame def trigger_alert(self): """触发警报""" current_time = time.time() if current_time - self.last_alert_time > self.alert_cooldown: print("ALERT: Fire detected!") self.alarm_status = True self.last_alert_time = current_time def run_live_detection(self, source=0): """ 实时运行火灾检测 :param source: 视频源(0为默认摄像头) """ cap = cv2.VideoCapture(source) while cap.isOpened(): ret, frame = cap.read() if not ret: break processed_frame = self.process_frame(frame) cv2.imshow('Fire Detection', processed_frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() if __name__ == "__main__": detector = FireDetectionSystem() detector.run_live_detection() ``` ## 系统说明 1. 这个系统使用YOLOv8模型进行火灾检测,你需要先训练或下载一个专门检测火灾的YOLOv8模型(如yolov8n-fire.pt) 2. 主要功能包括: - 实时视频流处理 - 火灾检测和边界框标注 - 警报触发机制(带冷却时间) 3. 扩展建议: - 添加声音警报 - 集成短信/邮件通知 - 记录检测日志 - 支持RTSP流输入 ## 模型训练 要训练自定义火灾检测模型,可以使用以下步骤: 1. 收集火灾/烟雾图像数据集 2. 标注图像中的火灾区域 3. 使用YOLOv8训练命令: ```bash yolo task=detect mode=train model=yolov8n.pt data=fire_dataset.yaml epochs=100 imgsz=640 ``` ## 三条额外需要注意的逻辑点 1. 确保使用的YOLO模型是专门针对火灾检测训练的,通用物体检测模型可能效果不佳 2. 警报冷却时间机制防止短时间内重复触发警报,应根据实际应用场景调整 3. 系统性能取决于硬件配置,高分辨率视频流可能需要GPU加速 [2025-06-22 01:40:04 | AI写代码神器 | 908点数解答]

相关提问