Code With Python
39.2K subscribers
886 photos
27 videos
22 files
769 links
This channel delivers clear, practical content for developers, covering Python, Django, Data Structures, Algorithms, and DSA – perfect for learning, coding, and mastering key programming skills.
Admin: @HusseinSheikho || @Hussein_Sheikho
Download Telegram
#YOLOv8 #ComputerVision #FireDetection #Python #AI #Safety

Lesson: Real-Time Fire Detection in an Industrial Facility with YOLOv8 and Alarm System

This tutorial guides you through building a computer vision project from scratch. We will use the YOLOv8 model to detect fire in a video feed from an industrial setting and trigger an alarm sound upon detection.

---

#Step 1: Project Setup and Dependencies

First, we need to install the necessary Python libraries. We'll use ultralytics for the YOLOv8 model, opencv-python for video processing, and playsound to trigger our alarm. Open your terminal or command prompt and run the following command:

pip install ultralytics opencv-python playsound

After installation, create a Python file (e.g., fire_detector.py) and import these libraries.

import cv2
from ultralytics import YOLO
from playsound import playsound
import threading

# Hashtags: #Setup #Python #OpenCV #YOLOv8


---

#Step 2: Load the Model and Prepare the Alarm System

We will load a pre-trained YOLOv8 model. For a real-world application, you must train a custom model on a dataset of fire and smoke images. For this example, we will write the code assuming you have a custom model named fire_model.pt that knows how to detect 'fire'.

You also need an alarm sound file (e.g., alarm.wav) in the same directory as your script.

# Load your custom-trained YOLOv8 model
# IMPORTANT: The standard YOLOv8 models do not detect 'fire'.
# You must train your own model on a fire dataset.
model = YOLO('fire_model.pt') # Replace with your custom model path

# Path to your alarm sound file
ALARM_SOUND_PATH = "alarm.wav"

# A flag to ensure the alarm plays only once per detection event
alarm_on = False

def play_alarm():
"""Plays the alarm sound in a separate thread."""
global alarm_on
print("ALARM: Fire Detected!")
playsound(ALARM_SOUND_PATH)
alarm_on = False # Reset alarm flag after sound finishes

# Hashtags: #AIModel #AlarmSystem #SafetyFirst


---

#Step 3: Main Loop for Video Processing and Detection

This is the core of our application. We will open a video file, read it frame by frame, and pass each frame to our YOLOv8 model for inference. If the model detects 'fire' with a certain confidence, we will draw a bounding box around it and trigger the alarm.

Create a video file named industrial_video.mp4 or use your own video source.
1