dvr-yolov8-detection
is designed for real-time detection of humans, animals, or objects using the YOLOv8 model and OpenCV.
The program supports real-time video streams via RTMP or USB webcams, includes CUDA GPU acceleration for enhanced performance, and provides options for saving detections, triggering alerts and logging events.
The video preview can be run both in a GUI window and headless on a local web server using the included Flask web server setup.
(New) 🐳 A Dockerfile for Dockerized installs is also included.
- Real-time human/animal/object detection and alert system
- Runs on Python + YOLOv8 + OpenCV2
- GUI and (headless) web server versions (
Flask
) - Supports CUDA GPU acceleration, CPU-only mode also supported
- RTMP streams or USB webcams can be used for real-time video sources
- Includes a loopback example and NGINX configuration example for RTMP use (i.e. OBS Studio)
- Detections can be automatically saved as images with a detection log
- Get real-time alerts on detections via Telegram
- Send detection data to any remote SSH/SFTP location
- Separate tool included for offline video file detections for DVR-type faster-than-realtime post-processing (see:
utils/
)
The project uses Python with YOLOv8 and OpenCV2 to perform object detection on either real-time video streams or batches of video files. It processes each video frame by frame, detecting humans by default (other YOLOv8-supported objects can be added as needed). The program can log detections into a separate log file, save detection frames with highlighted objects, and send out audio alerts via pyttsx3
.
Configuration options are available in the config.ini
file.
Note: Using CUDA-enabled OpenCV is recommended for faster operation. CUDA-enabled OpenCV2 needs to be compiled manually and installed separately, as the compiling process is highly dependent on your hardware setup. Refer to the "Troubleshooting" section for guidance and an example build script for OpenCV with CUDA.
Real-time detection also supports additional CUDA features such as CUDA video denoising (note: this feature requires CUDA and is often available only when OpenCV is compiled from source).
- Video source (USB webcam or RTMP stream)
- Confidence threshold for detections
- Enable or disable rescaling of video frames
- CUDA-based video denoising (experimental)
- Log detection details into a separate log file
- Save frames with detected objects as image files
- Model variant selection (e.g., YOLOv8n, YOLOv8s, YOLOv8m)
- ... and other additional customizable options
🐳 For Docker setup, see the DOCKER_SETUP.md for a guide.
- Python 3.6+ (Python 3.10.x recommended)
- Python modules:
- See requirements.txt
- FFmpeg
- Python 3.10.x
- If you wish to use CUDA GPU acceleration, you will need:
- A Nvidia GPU that supports CUDA
- Install CUDA 11.8 or higher to enable GPU-accelerated processing
- Use Miniconda or Mamba for environment management
-
Clone the repository:
git clone https://github.com/FlyingFathead/dvr-yolov8-detection.git cd dvr-yolov8-detection
1.2 (Install Miniconda or Anaconda if not already installed):
-
Download and install Miniconda (recommended):
-
For Linux/macOS:
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh bash Miniconda3-latest-Linux-x86_64.sh
-
For Windows, download the installer from here and follow the installation instructions.
-
-
Set up the environment Conda/Mamba environment:
./setup_mamba_env.sh
This script creates a Conda/Mamba environment with the required dependencies.
-
Run the detection script:
./run_detection.sh
-
Clone the repository:
git clone https://github.com/FlyingFathead/dvr-yolov8-detection.git cd dvr-yolov8-detection
-
Install the required Python packages:
pip install -r requirements.txt
This ensures that all dependencies are managed through the
requirements.txt
file. -
Install OpenCV:
-
For a full version with GUI support (recommended):
pip install opencv-python
-
For CUDA-enabled OpenCV, you need to build it from source. Refer to the OpenCV documentation or check the build script for guidance.
-
-
Install FFmpeg:
-
On Ubuntu:
sudo apt-get install ffmpeg
-
On Windows and macOS, follow the instructions on the FFmpeg download page.
-
This project supports real-time object detection from RTMP streams or USB webcams using YOLOv8. The provided run_detection.sh
script ensures the detection script runs continuously, automatically restarting if it exits.
(New in v0.155): The real-time detection now comes with a mini web server running on Flask that enables you to run the detection framework in your browser by default when headless
and enable_webserver
are set to true
. This will make headless deployment very easy. Note that it listens on 0.0.0.0:5000
by default, which you might want to change (i.e. to 127.0.0.1:5000
) for safety reasons.
-
Set up your RTMP server:
- Use the example NGINX configuration (
example-nginx.conf
) to set up an RTMP loopback server. - Ensure your streaming client (e.g., OBS Studio) is set to stream to
rtmp://127.0.0.1:1935/live
.
- Use the example NGINX configuration (
-
Run the detection script:
./run_detection.sh
-
Run the detection script with the
--use_webcam
option:python3 yolov8_live_rtmp_stream_detection.py --use_webcam true
-
Specify webcam index (if needed):
python3 yolov8_live_rtmp_stream_detection.py --use_webcam true --webcam_index <number>
- Replace
<number>
with the index number of your webcam. - Use the
utils/get_webcams.py
tool to find available webcams and their index numbers.
- Replace
-
Alternatively, configure via
config.ini
:- Set
use_webcam
totrue
. - Set
webcam_index
to your desired webcam index.
- Set
Due to Docker being a popular install option, you can run the program headless and with a Flask
based mini-web server included. Compared to the regular GUI verison, there is likely a small framerate dip and latency in the output, but other than that, the functionality is not too far off from the GUI variant.
In headless Docker installs, make sure that headless
and enable_webserver
are both set to true
.
You can configure the program's parameters by editing the config.ini
file. This allows you to set program parameters such as the input source, input stream address, output directory, confidence threshold, model variant, stream URL, and more.
An example NGINX configuration is provided in example-nginx.conf
. This config sets up an RTMP server that listens on 127.0.0.1:1935
and allows local clients to publish and play streams.
To stream and process the video in real-time, use the ffmpeg_video_stream_loopback.sh
script. Ensure your streaming client (e.g., OBS Studio) is set to stream to rtmp://127.0.0.1:1935/live
.
Use the utils/loopback_test_unit_ffmpeg-python.py
script to set up a loopback for your RTMP stream.
-
Install
ffmpeg-python
:pip install -U ffmpeg-python
-
Run the loopback script:
python3 utils/loopback_test_unit_ffmpeg-python.py
-
Run the detection script:
python3 yolov8_live_rtmp_stream_detection.py
-
Configure your streaming client to stream to:
rtmp://127.0.0.1:1935/live
Note: Using NGINX as a loopback method is highly recommended for stability.
Use utils/batch_humdet_yolo8_opencv2.py
to run YOLOv8 batch detection on directories of video files, suitable for faster offline use.
- If for whatever reason the loopback doesn't seem working, you can create a test stream with i.e.
utils/test_stream_generator.py
. When ran, the script generates synthetic video frames and streams them to your RTMP server using FFmpeg as a subprocess, enabling you to try out if your loopback works. - Run the
test_stream_generator.py
and keep it running in the background, then try to first use VLC to connect to your stream (VLC: Media -> Open Network Stream -> rtmp://127.0.0.1:1935/live/stream
). If this works, the main detection script should work as well.
- Ensure that you have all necessary modules installed with CUDA enabled.
- You may need to compile OpenCV from source to enable CUDA support.
- Refer i.e. to the OpenCV w/ CUDA build script for Ubuntu 22.04LTS or the 24.04LTS build script for some degree of guidance.
- Verify CUDA support by checking if the program detects your GPU on startup.
- See: CUDA Setup Guide
- The program can run in CPU-only mode, though performance may be slower.
- To improve performance:
- Use a smaller model size in the
config.ini
. - Adjust rescale and frame rate options.
- Reduce the resolution and frame rate from the video source.
- Use a smaller model size in the
- Implement more error handling for edge cases
- Refactor code for improved modularity
- Add setup scripts for easier deployment
- Implement threshold settings for alerts (e.g., number of detections within a time frame)
- Add hooks for sending detections to web servers or APIs
- v0.1614
- More reactive log updates to webui
- v0.1613
- Even more load balancing fixes; aggregation parsing improvements
- v0.1612
- Improved thread and queue handling (non-blocking remote sync and other actions, etc)
- Maximum queue sizes can be set separately for saved frames and remote syncs (see
config.ini
)
- v0.1611
- TTS handling changes; test message on startup
- (TODO) Firejail users may still encounter issues due to audio routing inside Firejail instances
- v0.1610
- Remote sync features added, bugfixes
- Firejail & venv switches when remote syncing via SSH/SCP is enabled
- v0.1609
- Remote sync detection logs & frames to a remote SFTP/SSH server with either system
scp
orparamiko
- Can be configured and enabled/disabled in
config.ini
under theremote_sync
options
- Remote sync detection logs & frames to a remote SFTP/SSH server with either system
- v0.1608
- Added persistence to aggregated detections (esp. for web server use)
- Can be enabled or disabled in
config.ini
with the following parameters: enable_persistent_aggregated_detections = true
aggregated_detections_file = ./logs/aggregated_detections.json
- Program version display added to universal
version.py
file
- v0.1607
- New: Get detection alerts via Telegram (optional)
- Use @BotFather on Telegram to create a bot token
- Set your userid(s) (can be multiple users, comma separated) and the bot API token as environment variables:
DVR_YOLOV8_ALLOWED_TELEGRAM_USERS
- allowed users/send alerts to (comma-separated list)
DVR_YOLOV8_TELEGRAM_BOT_TOKEN
- your Telegram bot API token for alerts
- v0.1606
- Performance improvements:
- switched to PyAV-based handling for better RTMP stream reliability with less CPU load
- UI/UX improvements:
- Web server UI/UX enhancements in image carousel browsing
- shows detection area by default, swappable between full frame/area
- clicking on the image now shows its original version
- better error/exception catching overall
- better webUI scaling in various devices etc
- Performance improvements:
- v0.1605 Overall compatibility & bug fixes
- Detection image carousel beta over webUI
- If detection saving is enabled, images can be viewed from webUI
- v0.1604 Frame queue sizes now configurable
- Helps in I/O performance issues when saving detections
config.ini
=>[performance]
=>frame_queue_size
- v0.1603 New configuration for saving
- Choose to save the detection area, the whole frame, or both.
- (see
config.ini
=>save_full_frames
andsave_detection_areas
)
- v0.1602 Queuing on image saving
- Should reduce lag on most systems, even with larger frames
- v0.1601 Active access logging for webUI connections; improved
- Access via webUI is logged by default to
logs/access.log
- See
config.ini
for more options
- Access via webUI is logged by default to
- v0.160 (Oct-13-2024) WebUI access logging added
- Can be enabled/disabled and defined in
config.ini
- Can be enabled/disabled and defined in
- v0.159 (Oct-12-2024) Fixes to the detection saving logic
- v0.158 (Oct-11-2024) Even more webUI updates
- Human detections get aggregated in the webUI within a cooldown period
- (default cooldown period: 30 seconds)
- v0.157 (Oct-11-2024) webUI updates
- Better refreshed data via AJAX
- Minimized lock holding time
web_server.py
=> better FPS webserver_max_fps
value to cap the framerate on the webUI for better performance
- v0.156 (Oct-11-2024) Detection graphs in web UI
- Added
matplotlib
based detection graphs to the web UI - (selectable between 1hr/24hrs/week/month/year)
- Added
- v0.155 (Oct-11-2024) Now comes with a Flask web server!
- The video feed can be monitored real-time using the web interface
- Added a
Flask
mini web server to take care of the streams enable_webserver
andheadless
both set totrue
by default- Server listens at
0.0.0.0:5000
(seeconfig.ini
for more) - This enables quick deployment especially in headless / Docker setups
- v0.154 (Oct-10-2024) 🐳 Dockerized Setup Now Available! 🐳
- Headless mode added for non-GUI/Docker/detection-only modes
- enable in
config.ini
withheadless = true
- or, run the program with
--headless
flag
- enable in
- Added Docker as an install method to ease the setup process
- additional installation guides
- Headless mode added for non-GUI/Docker/detection-only modes
- v0.153
config.ini
& program changes:- Fallback directory (
fallback_save_dir
) - Option to create date-based sub-directories (i.e.
/yolo_detections_path/year/month/day/
)
- v0.152
- Added Conda/Mamba installer script for easier deployment
- v0.151
- Added fallbacks for directories
- v0.1501
- Fallback to non-CUDA modes if CUDA not supported
- v0.15
- Added direct USB webcam support
- Configure via
config.ini
or use--use_webcam
flag - Added
utils/get_webcams.py
to find webcam indices
- Configure via
- Added direct USB webcam support
- v0.1402
- Added detection logging to a file
- v0.1401
- Added
configparser
; now configurable viaconfig.ini
- Added
- v0.140
- Improved configuration option checks
This project is licensed under the GNU General Public License v3.0 (GPLv3).
I created this project to support non-profit and educational endeavors. While the GPLv3 license permits commercial use, I kindly ask that if you plan to use this project for commercial purposes, you consider reaching out to me. Your support and collaboration are greatly appreciated.
For inquiries, suggestions, or collaborations, please contact me at flyingfathead@protonmail.com
or visit FlyingFathead on GitHub.
Contributions are welcome! Feel free to open issues or submit pull requests on GitHub, or contact the author (me) directly at flyingfathead@protonmail.com
.
Developed by FlyingFathead, with digital ghost code contributions from ChaosWhisperer.
Star it if you like it. *;-)