This repository contains a Python script that performs real-time object detection using YOLOv8 model from Ultralytics. It captures video from the webcam, detects objects in the frames, and annotates them with bounding boxes. Additionally, it plays a sound whenever a person is detected in the video.
-
Install Python: Make sure you have Python 3.x installed on your machine.
-
Create Virtual Environment (optional but recommended):
python3 -m venv venv source venv/bin/activate -
Install Dependencies: Install the required dependencies by running:
pip install ultralytics supervision pygame opencv-python
-
Run the script using the following command:
python main.py
-
The script will access your webcam, detect objects in real-time, and annotate the video feed with bounding boxes.
-
If the script encounters an error, it will attempt to restart itself after a 10-second delay.
-
To exit the script, press the
Esckey.
-
The YOLOv8 model is loaded from the
yolov8l.ptfile. Make sure the file is present in the same directory as the script. -
The script uses the
ultralyticslibrary for object detection and thesupervisionlibrary for annotating the bounding boxes. -
The script uses the
pygamelibrary to play a sound when a person is detected. -
The script keeps track of the time since the last person detection and plays the sound when a person is detected after a certain period.
-
If you want to modify the webcam resolution, you can do so by editing the
--webcam-resolutionargument in themain.pyscript.
-
To use GPU insteed CPU, this could help if your CUDA is 12 +: https://pytorch.org/get-started/locally/, https://discuss.pytorch.org/t/pytorch-for-cuda-12/169447/37?page=2:
pip cache purge pip install torch==2.0.1+cu118 torchvision -f https://download.pytorch.org/whl/torch_stable.html``` -
yolo detect predict model=yolov8l.pt source=0 show=true -
I use this video and repo: https://www.youtube.com/watch?v=QV85eYOb7gk / https://github.com/SkalskiP/yolov8-live