Configure objectconfig.yml and test ZMES local

*** NOTE CONFIGS MOVED TO YAML SYNTAX ***

There are some changes to the config file, this is the open public beta so expect things to break and change. I did my best to get it working as stable as I can. In my setup I can leave mlapi, zmes, ZM running all week and not miss any push notifications (I disable zmNinja push and use the new python pushover add-on, it’s blazing fast and has a clickable link to open a browser window and view the event that the notification is about. You can send pushover notifications to a person with no zmNinja and they can watch the event!). That doesn’t mean it will be that stable for your system. I have not tested the import zm zones and only trigger on zm zones functionality but will be testing that within the next couple of days and fixing anything that needs fixing. Everything else has been tested but may need some fixes.

The biggest breaking changes are that !secrets are now formatted as {[secrets]} and ml_sequence:<model>:general ‘pattern’: ‘{{pattern}}’, has been changed to ->

<model>_detection_pattern# so ml_sequence:object:general'object_detection_pattern': '{{object_detection_pattern}}',
# face would be ml_sequence:face:general:
'face_detection_pattern': '{{face_detection_pattern}}',
# See the provided example config files for more info

ZMES and MLAPI use a new way to communicate with each other and it is using symmetrical key encryption! MLAPI can now reply to many ZMES instances!

Enabling mlapi in the zmes config requires setting a key to yes

ml_enable: yes# This is the key to enable using mlapi through ml_routes

In this example, I will be using ‘testuser’ as the username, ‘test123’ as the password and the ZM IP will be 10.0.0.52.

Open objectconfig.ini and secrets.ini in an editor of your choice. I recommend making a copy of the example file and opening it in some sort of editor like Pycharm, VSCode, Sublime, etc. nano is doable but prone to errors, vim can be configured like an IDE, but those that know how to use it probably don’t need most of this tutorial.

In secrets.ini configure the secrets. This is a minimal example for testing only! In depth config breakdown will be coming later in a different article.

ZM_USER: testuser
ZM_PASSWORDL 'test123'
#-- home assistant api url (usually the same as your web GUI) Do NOT add the trailing /api
#HA_SERVER: 'https://ha.yourhost.com:8443'
#-- home assistant 'Long Lived Token' for API access
#HA_TOKEN:
ES_ADMIN_INTERFACE_PASSWORD:ZM_PORTAL: http://10.0.0.52/zm
ZM_API_PORTAL: http://10.0.0.52/zm/api
ES_CERT_FILE: /etc/apache2/ssl/zm-fullchain.pem
ES_KEY_FILE: /etc/apache2/ssl/zm-privkey.pem
ML_USER: mlapi_user
ML_PASSWORD: 'password'
ZM_ML_USER: user to send to mlapi so it can login to ZM API
ZM_ML_PASSWORD: 'password'
mlapi_one_key: the encryption key for the mlapi_one route

Most of the default options can be left for the time being, we are just testing. You may need to tweak a few options here and there.

Here is an example of how to set up debug logging to properly see what’s going on and also add some of the included handy aliases to the shell config.

DEBUG LOGGING

Options->Logging

In ZM->Options->Logs:

  • LOG_DEBUG is on
  • LOG_LEVEL_FILE = debug
  • LOG_LEVEL_SYSLOG = Info

# DB logging is very verbose I recommend off or WARN/FATAL and Syslog Info

  • LOG_LEVEL_DATABASE = None
  • LOG_DEBUG_TARGET = _zmesdetect|_zmeventnotification.

This enables DEBUG logs for both detection and event server scripts. If you have other targets, just separate them with | - example, _zmc|_zmesdetect. If you only want to track detection logs and not ES logs, just do _zmesdetect. You can also enable debug logs for just one monitor’s hooks like so: _zmesdetect_m5|_zmeventnotification. This will enable debug logs only when hooks are run for monitor 5. Just remember this: “detection logs” only deal with detecting objects. The ES logs will tell you whether the detection text was received properly, whether it was written to ZM DB properly and whether it was sent out in a notification.

The above config. will store debug logs in the/var/log/zm directory while Info level logs will be recorded in Syslog. DB logging makes the MySQL DB grow and is very verbose. I recommend using Syslog for Info level logs.

You will likely need to restart ZM after this by issuing sudo systemctl restart zoneminder.service

Adding the es.debug / es.baredebug function and es. aliases

First, I recommend downloading 2 programs for your shell — bat and lsd. Bat here, LSD here. In the ~/git/zmeventnotification folder there is a file named es.debug.objdet and es.baredebug.objdet which are shell functions to help run debugging events to test with. There are some aliases in the TERM_helpers.txt file ->

# First download and install bat, I have my logging aliases setup to use bat for pretty log output! Use es.log.detect to watch the log files for detections and es.log.base to watch the PERL modules logs for active events.
wget https://github.com/sharkdp/bat/releases/download/v0.18.3/bat-musl_0.18.3_amd64.deb
sudo apt install./bat-musl_0.18.3_amd64.deb# Download and install lsd (a pretty ls command)
wget https://github.com/Peltoche/lsd/releases/download/0.20.1/lsd-musl_0.20.1_amd64.deb
----------------------------------------------------------------
# If you want to see the icons in lsd you need to install some hacked powerline type fonts in the terminal/system you use to usually log in to your headless systems (or if you use a system that ZM is installed on, the same system). Google 'nerd fonts' and a github repo will come up, if you clone the whole repo and install them all its like 2-3+ GB! You can just choose a few and install into ~/.local/share/fonts/ and use fontcache to scna and use the new fonts.Example to install 'Hacked' fonts and update font cache list ->cd ~
wget https://github.com/ryanoasis/nerd-fonts/releases/download/v2.1.0/Hack.zip
unzip Hack.zip
mkdir -p ~/.local/share/fonts
# move whatever fonts you grab into the fonts folder
mv ~/Hack*.ttf ~/.local/share/fonts
# rebuild the font cache to use the new fonts
fc-cache -f -v
# Search for the new fonts in the font cache to confirm installation
fc-list | grep "Hack"
OR
fc-list | grep "Nerd"
------------------------------------------------------------sudo apt install ./lsd-musl_0.20.1_amd64.debopen your shell config (.bashrc, /zshrc, etc.) and add these aliases for handy usenano ~/.zshrc# Add ~/.local/bin to your PATH
PATH=/usr/local/cuda-10.2/bin:~/.local/bin"
alias ll='lsd -all'alias es.start.debug='sudo -u www-data /usr/bin/zmeventnotification.pl --debug &'
alias es.start='sudo -u www-data zmdc.pl start zmeventnotification.pl &'
alias es.restart='sudo -u www-data zmdc.pl restart zmeventnotification.pl &'
alias es.stop='sudo -u www-data zmdc.pl stop zmeventnotification.pl &'
alias es.status='sudo -u www-data zmdc.pl status zmeventnotification.pl &'
#alias es.log.base='tail -F /var/log/zm/zmeventnotification.log &'
#alias es.log.detect='tail -F /var/log/zm/zmesdetect_m*.log &'
#alias es.log='tail -F /var/log/zm/zmesdetect_m*.log &'
#alias es.syslog="sudo tail -F /var/log/syslog &"
#IF YOU HAVE DOWNLOADED 'bat - the cat replacement' uncomment these lines instead of the 4 above
alias es.log.base='tail -F /var/log/zm/zmeventnotification.log | bat --style changes,header,rule,snip --paging never -l log --theme DarkNeon &'
alias es.log.detect='tail -F /var/log/zm/zmesdetect_m*.log | bat --style changes,header,rule,snip --paging never -l log --theme TwoDark &'
alias es.log='tail -F /var/log/zm/zmesdetect_m*.log | bat --style changes,header,rule,snip --paging never -l log --theme TwoDark &'
alias es.syslog="sudo tail -F /var/log/syslog | bat --style changes,header,rule,snip --paging never -l log --theme 'Solarized (dark)' &"
# Exit and source the file
source ~/.zshrc
# If ~/.local/bin does not exist create it
mkdir -p ~/.local/bin

Next, install the 2 function files into the ~/.local/bin directory. Move es.baredebug.objdet and es.debug.objdet files in the zmeventnotification directory to ~/.local/bin.

cd ~/git/zmeventnotification
cp es.*.objdet ~/.local/bin
# make the 2 functions executable
chmod +x ~/.local/bin/es.*.objdet

You should now be able to use es.debug.objdet from the command line and the other aliases like es.start.debug and es.log.detect.

For this example, I am not going to go through how to add a monitor. Please have some monitors set up with pre-existing events containing objects in them to test on, or start walking in front of monitors to cause new events and then we can use the handy aliases to test the GPU without even starting the zmes hooks engine.

Let’s say event #15 has a person in it, so we want to run a detection on event # 15.

es.debug.objdet 15

# Logs cherry-picked for some pertinent info

10/20/21 01:43:01.37598 zmesdetect_m1[9154] DBG1 yolo:102->[yolo: loading model data from sequence 'v4 (COCO)']10/20/21 01:43:01.584778 zmesdetect_m1[9154] DBG1 yolo:107->[perf:yolo: 'v4 (COCO)' initialization -> loading 'yolov4.weights' took: 207.51 ms]# This is my new hotfix for what I suspect is some code changes in OpenCV that break the old yolo code. If the OpenCV version is 4.5.2 or above it uses the hotfix, anything under that uses the old code!10/20/21 01:43:01.585941 zmesdetect_m1[9154] DBG1 yolo:120->[yolo: The version of OpenCV that you have requires a slight fix in the code base, applying fix...]10/20/21 01:43:01.587013 zmesdetect_m1[9154] DBG1 yolo:133->[yolo: using GPU for detection, set CUDA/cuDNN backend and target]10/20/21 01:43:01.588184 zmesdetect_m1[9154] DBG1 yolo:206->[yolo: 'v4 (COCO)' (GPU) [input image w*h 800*450 - resized by  model_width/height to w*h: 416*416]]10/20/21 01:43:03.026392 zmesdetect_m1[9154] DBG2 yolo:92->[portalock: released -> 'pyzm_uid33_GPU_lock']10/20/21 01:43:03.46778 zmesdetect_m1[9154] DBG2 yolo:270->[perf:yolo:GPU: 'v4 (COCO)' detection took: 1878.49 ms]

THE GPU WORKS! How about the TPU?

10/20/21 03:11:38.470152 zm_mlapi[24937] DBG2 coral_edgetpu:52 [Waiting for pyzm_uid1000_TPU_lock portalock...]10/20/21 03:11:38.472776 zm_mlapi[24937] DBG2 coral_edgetpu:54 [portalock: got 'pyzm_uid1000_TPU_lock']10/20/21 03:11:38.475012 zm_mlapi[24937] DBG1 coral_edgetpu:206 [coral: 'coral::MobileNETv2-SSD' input image (w*h): 800*450 resized by tpu_model_width/height to 312*312]10/20/21 03:11:38.496878 zm_mlapi[24937] DBG2 coral_edgetpu:71 [portalock: released 'pyzm_uid1000_TPU_lock']10/20/21 03:11:38.499335 zm_mlapi[24937] DBG1 coral_edgetpu:226 [perf:coral: 'coral::MobileNETv2-SSD' detection took:19.09 ms]10/20/21 03:11:38.501664 zm_mlapi[24937] DBG2 coral_edgetpu:245 [coral: the image was resized before processing, scaling bounding boxes in image back up by factors of -> x=2.564 y=1.442]Start configuring the setup, break stuff and report it so I can fix things! MLAPI install will be after DLIB and ALPR. You can skip ahead to that section instead if you would like. There are 2 installs for MLAPI I will do, 1 is installing mlapi onto a system that ZMES and ZM are installed on. The other is installing mlapi onto a system wihtout ZMES and ZM. I fixed a lot of things about mlapi for it to work better with the pyzm logging system when there is no ZM/ZMES. There are also handy mlapi aliases!

--

--