Add-on ZM Advanced Person Detector - v. 0.05a

Forum for questions and support relating to the 1.34.x releases only.
Post Reply
fontiano
Posts: 69
Joined: Wed Dec 25, 2019 10:29 am

Add-on ZM Advanced Person Detector - v. 0.05a

Post by fontiano »

Premise: I am not a developer. I did not translate notes from Italian.

This is a mini guide to install "Zoneminder Advanced Person Detector" on Debian 10.

Introduction:

I wrote a set of scripts to analyze zoneminder events and notify via telegram the presence of people in the recorded videos / images.

The script is activated at the end of an event so, in order not to have too much delay on notifications, I recommend setting the maximum length of a recording to 2 minutes and the minimum based on the computing power of the system (I set it to 1 minute but if you have a powerful system you can set it even lower)

Each time an event ends, the verification of the videos starts and in this phase many resources are used based on the number of frames to be analyzed. To minimize false positives I decided to use 2 different yolov5 ultralytics models: yolov5l and yolov5x. If people are identified after the first scan with yolov5l, a second detection is activated with the yolov5x model. If after the second scan there are at least 2 frames with people inside, a notification is sent via telegram of the first and last image detected with relative description of the event (time, camera etc.). All detection will be saved on "/archivio/" directory.

The script was created to be used with the "modetect" function. Theoretically it could be used to detect objects, animals etc. but since I don't need it, I haven't made the necessary changes.

Zoneminder must be configured to record video or jpegs. If none of the options are enabled, the scripts will not work.

I want to share my work because I am sure it might be of interest to someone and I hope it will be improved, tidied up and maybe integrated as an optional feature on the official version of Zoneminder.


Prerequisites:

Zoneminder 1.34.x (https://wiki.zoneminder.com/Debian_10_B ... om_ZM_Repo)
Event Notification Server >= 5.11 (https://zmeventnotification.readthedocs.io/en/latest/)
GPU Nvidia with CUDA support with 4 Gb VRAM or more (I'm using 1650 with 4GB vram)
CUDA Drivers >= 10.2
yolo v5 by ultralytics (https://github.com/ultralytics/yolov5)
tsp (ts Debian version)
telegram.sh (https://github.com/fabianonline/telegram.sh)

Installation guide:

It is necessary to have ZoneMinder and ZmEventNotification Server already installed and running.

Create directory /archivio/ and add permission to www-data user

Create directory /scripts/zoneminder2/ and extract here all the scripts:

/scripts/zoneminder2/ae_standard_ultra_fast_5.sh - this script is for testing / tuning
Usage:
sudo -u www-data bash /scripts/zoneminder2/ae_standard_ultra_fast_5.sh $idevent $idmonitor $monitorname $cause

/scripts/zoneminder2/downloadframes_nocopy.sh - this script is used to see how many and which frames are analyzed based on the parameters set (testing and tuning)
Usage:
sudo -u www-data bash /scripts/zoneminder2/ae_standard_ultra_fast_5.sh $idevent
Output on /dev/shm/$IDEVENT

/scripts/zoneminder2/ae_standard_ultra_giorno_fast5.sh - this is the script for the day

/scripts/zoneminder2/ae_standard_ultra_notte_fast5.sh - this is the script for the night

/script/zoneminder2/coda-rilevamenti.sh - this script is to see if there are any detections in the queue

/scripts/zoneminder2/listaframes.py - this is the script that generates the list of frames to be extracted from mp4 based on the variables set. It is based on time only and ignores the scoring of alarmed frames. (passthrough or 264 encode)

/scripts/zoneminder2/listaframes_id.py - this is the script that generates the list of frames to be analyzed based on the variables set. It is based on time only and ignores the scoring of alarmed frames. (jpeg record)

/scripts/zoneminder2/secrets.sh - here you can set user and password of ZoneMinder

/scripts/zoneminder2/variabili.sh - here you can tune variables for detection and telegram stuff (bot ID, channel ID, user ID)

/scripts/zoneminder2/variabili.py - this is used to extract info from json (I hope someone will replace it with the official wrapper)

Install yolov5: https://github.com/ultralytics/yolov5

git clone on /scripts/ so you will have /scripts/yolov5/ and install requirements as described on the site.
IMPORTANT: download weights on /scripts/yolov5/weights/ -> https://drive.google.com/drive/folders/ ... A6ueKRpN2J

Install telegram.sh: https://github.com/fabianonline/telegram.sh

git clone on /scripts/zoneminder2/ so you will have /scripts/zoneminder2/telegram-sh/ . You need first to create a Telegram Bot (https://riptutorial.com/telegram-bot/ex ... -botfather) and then you have to set variables on "variables.sh"

Modify zm_event_end.sh:

/var/lib/zmeventnotification/bin/zm_event_end.sh

## Start

#!/bin/bash

# Just a dummy script for event end. Do what you want here
# When invoked by zmeventnotification.pl it will be passed:
# $1 = eventId that triggered an alarm
# $2 = monitor ID of monitor that triggered an alarm
# $3 = monitor Name of monitor that triggered an alarm
# $4 = cause of alarm

# If people run it as is, without modifying it, lets make sure we
# return the cause back so its sent in the notification
echo "$(date): POST EVENT FOR EID:${1} FOR MONITOR ${2} NAME ${3} CAUSE ${4}" > /tmp/post_log.txt


# maximum number of scripts that can be run simultaneously
tsp -S 3

# day script from 06:00 AM to 00.30 AM
currenttime=$(date +%H:%M)
if [[ "$currenttime" < "00:30" ]] || [[ "$currenttime" > "06:00" ]] && [[ $3 != "InternoFabbrica" ]]; then
tsp -n bash /scripts/zoneminder2/ae_standard_ultra_giorno_fast5.sh ${1} ${2} ${3} """${4}"""
exit 0
fi

# night script from 00.30 AM to 06:00 AM
if [[ "$currenttime" > "00:30" ]] && [[ "$currenttime" < "06:00" ]]; then
tsp -n bash /scripts/zoneminder2/ae_standard_ultra_notte_fast5.sh ${1} ${2} ${3} """${4}"""
exit 0
fi


exit 0

## End

If everything was done correctly the script should run correctly.

Final notes:
Each time a detection is started with the yolov5 models, about 1GB of video ram is used. Currently the script does not allow more than 3 detections simultaneously to avoid going out of memory (4gb VRAM). you can check video memory status with this command: watch -n 1 nvidia-smi
If you have more than 4 GB of VRAM you can modify the scripts to allow more detections at time.

Feel free to modify my project and share improvements.


TODO:
function to delete old logs (/tmp/"$idevent".log) and old archived images (/archivio/) - I put a script on cron
speed up notification without losing accuracy
parametrize classes to enable detecions of other objects

rewrite all scripts in a decent manner
Attachments
zoneminder2.7z
(4.02 KiB) Downloaded 96 times
Production: zm 1.34.26 - Debian 11 | Test: zm 1.36.33 - Debian 12
Post Reply