This guide explains how to build an object detection system using the YOLOv8 model to detect cigarettes from a webcam feed, with an additional feature of triggering an alert sound when a cigarette is detected.
To get started, you need the following libraries and tools:
You can install the required libraries using:
pip install opencv-python ultralytics pygame
Here's the full code for the object detection system:
import cv2 from ultralytics import YOLO import pygame import threading import time # YOLOv8 model loading model = YOLO('trained_model.pt') # Opening the webcam cap = cv2.VideoCapture(0) # Alert sound file alert_sound = 'alert_sound.mp3' # Initializing pygame for sound playback pygame.mixer.init() # Function to play alert sound asynchronously def play_alert_sound(): while True: pygame.mixer.music.load(alert_sound) pygame.mixer.music.play() print("Alert sound played!") time.sleep(1) # Starting the sound playback in a new thread alert_thread = threading.Thread(target=play_alert_sound, daemon=True) alert_thread.start() # Main loop for video processing and object detection while True: ret, frame = cap.read() if not ret: break # Detect objects using YOLOv8 results = model(frame) cigarette_detected = False for result in results[0].boxes: class_id = int(result.cls.item()) if model.names[class_id] == 'cigarette': cigarette_detected = True break if cigarette_detected: print("Cigarette detected!") pygame.mixer.music.set_volume(1.0) else: print("No cigarette detected!") pygame.mixer.music.set_volume(0.0) annotated_frame = results[0].plot() cv2.imshow('Webcam Feed', annotated_frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
YOLOv8 Model: The YOLOv8 model is pre-trained on various objects, including a cigarette. The trained_model.pt
file is the custom-trained YOLOv8 model that detects the "cigarette" class.
Webcam Feed: OpenCV is used to capture frames from the webcam. The frames are passed to the YOLOv8 model for object detection.
Alert Sound: When a cigarette is detected, the program triggers an alert sound using Pygame. A separate thread is used to play the sound asynchronously, preventing it from blocking the video processing.
Real-Time Detection: As the program processes each frame, it checks whether a cigarette is detected. If detected, an alert sound is played, and if no cigarette is found, the sound is muted.
Exit Condition: Press the 'q' key to exit the video feed.
The system will show the webcam feed with real-time object detection annotations, and the alert sound will play whenever a cigarette is detected. Here’s an example of what the output might look like:
https://www.youtube.com/watch?v=9ECnFEY9Sg0
This project demonstrates how to integrate object detection with real-time video processing and alert systems. By using YOLOv8 for object detection and Pygame for sound management, we can create an intelligent system that reacts to specific objects detected in the video feed.
This is an example of how you can structure and document your work using Markdown. You can include images, like the webcam feed screenshot shown above, and even add links to relevant resources or repositories.
Let me know if you'd like more details or need help with any section!