Zoneminder Darknet yolov2 tiny Hack

Forum for questions and support relating to the 1.30.x releases only.
Post Reply
operat0r
Posts: 13
Joined: Thu Jan 18, 2018 4:03 am

Zoneminder Darknet yolov2 tiny Hack

Post by operat0r » Fri Jan 19, 2018 2:24 pm

UPDATE 8:55 PM 4/18/2018: I gave up on trying to figure out syslog/logrotate and just running tail -F --retry on the normal /var/log/zm/* path...my script would stop working every time the log file rotated. I could never figure out why my logs stopped going to my external mount and I still have not sorted out way to log the system service I setup for the script

UPDATE 01/19/2018: 38% BOAT! Got it working using syslog and just pulling the event ID it bails on the first match person|cat|dog|car

Video:
https://www.flickr.com/photos/freeload101/38899662735

Crazy TODO'S :

* use alarm frames for input only and not entire event? zmtrigger.pl ? API ?
* have it send alerted event ID, alerted frame image (predictions.png) , object(s) detected to email etc
* sort out darknet and make it faster /better
* face detection
* automated face training ?!
* automated car training ?!
* licence plate logging
* match cars/faces
* count trained objects over time create logs/graphs ( IE alert when mail person potentially changes )

... spaghetti code ...
/usr/local/sbin/DARKNET

Code: Select all


#!/bin/bash

# lazy cleanup logs older then 5 days
find /var/log/zm/* -mtime +5 -exec rm '{}' \;

# config

# max number of events over 3000 to trigger smearing
varscorethresh=100

# wait 1 hour for throttling when home
varwait=3600

# default, YOLO only displays objects detected with a confidence of .25 or higher
# set this to .0 to trigger alerts etc
vardarknetthresh=.65
# for debug vardarknetthresh=.1

# START FUNCTIONS ####################################################################################################################################


checksmearing () {
# check for smearing
echo `date` DEBUG: Checking for smearing
varsmearing=`curl --compressed -i -s -k  -X $'GET'     -H $'Host: 25.0.0.151' -H $'Accept-Encoding: gzip, deflate'      $'http://25.0.0.151/zm/index.php?view=events&page=&reload=0&execute=0&action=&subaction=&line=&fid=&filterName=WIPEALLEVENTS&filter%5Bterms%5D%5B0%5D%5Battr%5D=DateTime&filter%5Bterms%5D%5B0%5D%5Bop%5D=%3C&filter%5Bterms%5D%5B0%5D%5Bval%5D=now&sort_field=DateTime&sort_asc=1&limit=&AutoExecuteCmd=' | grep "colTotScore\">[0-9]" | sed -r 's/(.*ore\">)(.*)(<\/td>)/\2/g'  | egrep "([3-9]...)" |wc -l `
        if [[ "${varsmearing}" -gt "${varscorethresh}" ]]
                then
                        echo `date` DEBUG:  Alert Smering triggerd ${varscorethresh} events for scores 3000-9000

                        # restart cam1
                        vartoken=`curl -i -s -k  -X $'POST' --data-binary $'[{\"cmd\":\"Login\",\"action\":0,\"param\":{\"User\":{\"userName\":\"admin\",\"password\":\"\"}}}]' $'http://25.0.0.205/cgi-bin/api.cgi?cmd=Login&token=null'   | grep name |sed -r 's/(.*: \")(.*)(\".*)/\2/g'`
                        curl -v -k  -X $'POST' --data-binary $'[{\"cmd\":\"Reboot\",\"action\":0,\"param\":{}}]' $"http://25.0.0.205/cgi-bin/api.cgi?cmd=Reboot&token=${vartoken}"

                        #restart cam2
                        vartoken=`curl -i -s -k  -X $'POST' --data-binary $'[{\"cmd\":\"Login\",\"action\":0,\"param\":{\"User\":{\"userName\":\"admin\",\"password\":\"\"}}}]' $'http://25.0.0.148/cgi-bin/api.cgi?cmd=Login&token=null'   | grep name |sed -r 's/(.*: \")(.*)(\".*)/\2/g'`
                        curl -v -k  -X $'POST' --data-binary $'[{\"cmd\":\"Reboot\",\"action\":0,\"param\":{}}]' $"http://25.0.0.148/cgi-bin/api.cgi?cmd=Reboot&token=${vartoken}"

                        # restart ZM
                        systemctl stop zoneminder.service;sleep 10;systemctl start zoneminder.service

fi
}


emailalert () {
# is one of our phones on the network ?
# my phones are on segmented wifi and I cant ping all of them so using this wackado expect telnet script ...
# maybe adjust arp timeout 15min ? echo 900 >/proc/sys/net/ipv4/neigh/default/gc_stale_time

cat <<EOF> /tmp/arp
#!/usr/bin/expect
spawn telnet 25.0.0.1
expect "*"
send "echo 900 >/proc/sys/net/ipv4/neigh/default/gc_stale_time\r"
expect "*"
send "arp -an\r"
expect "*"
send "exit\r"
expect eof
EOF


if [[ `expect /tmp/arp | egrep "(0x2.*89:12:2b|0x2.*3a:31:85)"` == "" ]]
        then
                echo `date` DEBUG: Info status set to away not throttling notifications
                echo `date` DEBUG: Info Sending email
                python /usr/local/sbin/GMAIL.py
        else
                echo `date` DEBUG: Info status set to home throttling notifications
                if [[ $(($(date +%s) - startTime))  -lt $varwait ]]
                        then
                                echo `date` DEBUG: Info Not sending email it has not been ${varwait} seconds yet throttling
                        else
                                echo `date` DEBUG: Info Sending email /
                                python /usr/local/sbin/GMAIL.py
                                startTime=$(date +%s)
                fi
fi
}


# MAIN  ####################################################################################################################################


# update : 3/29/2018: not sure what happend .. I lost all logging to /media/data/zm.log ... using zma_m*.log ...I think because at some point in time it changed from local to something else ... so now stuff in rsyslog.conf is broken

# echo '# Save ZoneMinder messages to zm.log' >> /etc/rsyslog.conf
# echo 'local1.*                        /media/data/zm.log' >>  /etc/rsyslog.conf

# does not save zoneminder logs to /var/log/syslog
# sed -r 's/(^)(.*syslog$|.*messages$)/local1.!*;\1\2/g'  /etc/rsyslog.conf > /tmp/1 ; mv /tmp/1 /etc/rsyslog.conf
# /etc/init.d/rsyslog restart;systemctl restart rsyslog.service

# broken .. see above .. tail -f -n 0  /media/data/zm.log | while read LOGLINE
# -f does not work if file is truncated .. need -F --retry tail -f -n 0  /var/log/zm/zma_m*.log | while read LOGLINE
tail -F --retry -n 0  /var/log/zm/zma_m*.log | while read LOGLINE
do
        echo `date` DEBUG: zm.log ${LOGLINE}|egrep "(\bzm[a-z])"
        varevenid=`echo ${LOGLINE}| grep "Closing event"  | sed -r 's/(.*Closing event )(.*)(,.*)/\2/g'`
                if [[ "${varevenid}" != "" ]]
                        then
                                checksmearing
                                vareventpath=`find /usr/share/zoneminder/www/events/ -path "*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*/.${varevenid}*"|sed 's/\..*//g'`
                                cd /home/plex/YOLO/darknet
                                echo `date` DEBUG: Checking varevenid ${varevenid} Path ${vareventpath}
                                for i in `find ${vareventpath} -iname "*capture.jpg"|shuf -n 10`
                                do
                                # look for person|cat|dog|car
                                #vardarknet=`./darknet   detector test cfg/voc.data cfg/tiny-yolo-voc.cfg tiny-yolo-voc.weights  $i -thresh ${vardarknetthresh} 2> /dev/null | egrep -ia "(person|cat|dog|car)"`
                                vardarknet=`./darknet  detect /home/plex/YOLO/darknet/cfg/yolo.cfg /home/plex/YOLO/darknet/yolo.weights $i  -thresh ${vardarknetthresh} 2> /dev/null | egrep -ia "(person|cat|dog|car)"`
                                echo `date` DEBUG: running Darknet on ${varevenid} $i
                                        if [[ "${vardarknet}" != "" ]]
                                                then
                                                        echo `date` DEBUG: Object detected ${varevenid} Path ${vareventpath} backing up image to /media/data/temp/${varevenid}_`date +%m%d%y%I%M%S`.png

                                                        echo `date` DEBUG: ${vardarknet}
                                                        sleep 1
                                                        convert  /home/plex/YOLO/darknet/predictions.png /tmp/tmp.jpg
                                                        cp /tmp/tmp.jpg /media/data/temp/${varevenid}_`date +%m%d%y%I%M%S`.jpg
                                                        cp "$i" /tmp/tmp.jpg
                                                        emailalert
                                                        break
                                        fi
                                done
                fi
done



/etc/systemd/system/DARKNET.service

Code: Select all

[Unit]
Description=DARKNET STARTUP
After=network.target

[Service]
User=root
Group=adm

Type=simple
ExecStart=/bin/bash -c '\
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin ;\
/usr/local/sbin/DARKNET '

[Install]
WantedBy=multi-user.target


Image
Last edited by operat0r on Thu Apr 19, 2018 12:58 am, edited 20 times in total.
Zoneminder Darknet yolov2
viewtopic.php?f=36&t=26906

Paranoid
Posts: 116
Joined: Thu Feb 05, 2009 10:40 pm

Re: Zoneminder Darknet yolov2 tiny Hack

Post by Paranoid » Fri Jan 19, 2018 3:47 pm

operat0r wrote:
Fri Jan 19, 2018 2:24 pm

* I want to run the script on the path after an alarm is triggered
* I am using -120 seconds because I'm too lazy to do it right (IE just looking for modified files instead of actually knowing what JPG's to check)
* I wondered if I can some how turn on degbug logging or something so I can know the alarm frame image names to run darknet on
* I am using tiny yolo with NO GPU as I need 4gig card to save me 9 seconds and 2-3% accuracy .. SO GPU IS NOT WORTH it for just basic "person" detection. (ref google groups post )
Look at zmtrigger.pl. If you enable it then you can telnet to it and it will tell you when events start and stop and what the event number is.
With the monitor number and the event number you can work out where the images will be stored.

Here's an example output

Code: Select all

1|on|1516375720|2927992
1|off|1516375727|2927992
3|on|1516375845|2927993
3|off|1516375848|2927993
2|on|1516375849|2927995
3|on|1516375849|2927994
3|off|1516375852|2927994
2|off|1516375854|2927995
If you turn on analysis images (CREATE_ANALYSIS_IMAGES) then all you need to do is look for images called *-analyse.jpg and corresponding *-capture.jpg is the one that alarmed.

rockedge
Posts: 815
Joined: Fri Apr 04, 2014 1:46 pm
Location: Connecticut,USA
Contact:

Re: Zoneminder Darknet yolov2 tiny Hack

Post by rockedge » Fri Jan 19, 2018 4:23 pm

here is a thread some of us were on using darknet and YOLO . I was using Darknet/YOLO to analyze individual Frames. Also messed around trying out opencv to determine the direction of travel in an motion event in ZM. I am busy at the second but will be back to share some more stuff if your interested.
viewtopic.php?f=36&t=26222

operat0r
Posts: 13
Joined: Thu Jan 18, 2018 4:03 am

Re: Zoneminder Darknet yolov2 tiny Hack

Post by operat0r » Fri Jan 19, 2018 9:59 pm

I first wanted to say thanks for still supporting ZM I used it 10 years ago and it's still great software! I just took me quite a bit longer to get it right this time. Some issues I think I had

* bad wireing .. (I had yellow/green light and I rewired and got solid green on my POE swtich )
* I was setting up zones too soon before I sorted out the basics
* I was trying to make it only alert on real events and not "motion" ( this is REALLY HARD ...RAIN... SNOW...SHADOWS/WIND )



So I am creating a dirty dirty hackjob of a script for darknet. I wondered where the best place to start.

* I want to run the script on the path after an alarm is triggered
* I am using -120 seconds because I'm too lazy to do it right (IE just looking for modified files instead of actually knowing what JPG's to check)
* I wondered if I can some how turn on degbug logging or something so I can know the alarm frame image names to run darknet on
* I am using tiny yolo with NO GPU as I need 4gig card to save me 9 seconds and 2-3% accuracy .. SO GPU IS NOT WORTH it for just basic "person" detection. (ref google groups post )

Hackjob code so far ill fix as a 'plugin'

Code: Select all



# finds jpgs in the events path and runs darknet on them saving the predictions file out to /media/data/temp/_UNSORTED/DERP to look at.
# this is where ill start greping for "people,dogs,cats,cars" 

for i in `find /usr/share/zoneminder/www/events/ -iname "*.jpg" -mmin 2 `  ;do echo $i ;./darknet detector test cfg/voc.data cfg/tiny-yolo-voc.cfg tiny-yolo-voc.weights   $i;cp /home/plex/YOLO/darknet/predictions.png  /media/data/temp/_UNSORTED/DERP/CAM1`date +%m%d%y%I%M%S`.png ;done


Here are my notes:

Code: Select all


# Start with one zone at a time and raise/test sensitive KISS...don't setup like 4 zones and expect to trouble shoot anything. I used my Android to do a lot of tweaking. I also want to sort out "night" mode config so after night mode kicks in a different config is automatically applied.

# use low quality for motion detection and drop frame rate of cam to lower then max of ZM for little to no smearing and I also set "Alarm Frame Count to 2" and "overload ignore frame count to 4" but I think it's more about Alarm Frame Count..I can't be sure..

Explanation of overload ignore frame count
Report this post Quote
Post  by bb99 » Thu May 10, 2012 5:22 pm

For sudden changes to the enviroment, no better tool then Overload Frame Ignore Count. Your fps determines these settings but at 10 fps with Overload Frame Ignore Count set to 4, it only ignors .4 seconds. In other words if a drastic change to the lighting (such as a car with headlights on in darkness) passes within the monitors view it will ignore the number of frames you specify before processing for motion detection.
---
Alarm Frame Count 
This option allows you to specify how many consecutive alarm frames must occur before an alarm event is generated. The usual, and default, value is 1 which implies that any alarm frame will cause or participate in an event. You can enter any value up to 16 here to eliminate bogus events caused perhaps by screen flickers or other transients. Values over 3 or 4 are unlikely to be useful however. Please note that if you have statistics recording enabled then currently statistics are not recorded for the first ‘Alarm Frame Count’-1 frames of an event. So if you set this value to 5 then the first 4 frames will be missing statistics whereas the more usual value of 1 will ensure that all alarm frames have statistics recorded.




https://forums.zoneminder.com/viewtopic.php?f=36&t=26222

# windows software to get basic idea ( install face plugin )
https://www.ispyconnect.com/plugins.aspx

#Videos
https://www.reddit.com/r/DIY/comments/713eka/heres_how_to_build_your_own_smart_security_camera/
https://www.hackster.io/hackerhouse/raspberry-pi-motion-tracking-gun-turret-77fb0b
https://www.youtube.com/watch?v=QfNvhPx5Px8
https://www.youtube.com/watch?v=4eIBisqx9_g&t=11m10s

# eazy getting started
https://pjreddie.com/darknet/yolo/#small

# getting started with GPU
# you need 4gig+ GPU and new nvidia or old GCC4.9
#/GCC4_NEEDED_FOR_DARKNET$ ls
#cpp-4.9_4.9.2-10_amd64.deb  gcc-4.9_4.9.2-10_amd64.deb       libasan1_4.9.2-10_amd64.deb          libcloog-isl-dev_0.18.2-1+b2_amd64.deb  libisl10_0.12.2-2_amd64.deb
#g++-4.9_4.9.2-10_amd64.deb  gcc-4.9-base_4.9.2-10_amd64.deb  libcloog-isl4_0.18.2-1+b2_amd64.deb  libgcc-4.9-dev_4.9.2-10_amd64.deb       libstdc++-4.9-dev_4.9.2-10_amd64.deb
https://github.com/dasGringuen/debian9-install

# GPU memory talk
https://groups.google.com/forum/#!msg/darknet/ZRAEvMmKzFc/iVZgibJiJQAJ


# trying to GPU prep .. 
apt-get remove --purge nvidia-cuda-toolkit libcudnn*
dpkg -i libcudnn7_7.0.5.15-1+cuda9.1_amd64.deb
dpkg -i libcudnn7-dev_7.0.5.15-1+cuda9.1_amd64.deb
dpkg -i cuda-repo-ubuntu1704-9-1-local_9.1.85-1_amd64


sudo dpkg -i cuda-repo-ubuntu1704-9-1-local_9.1.85-1_amd64.deb
sudo apt-key add /var/cuda-repo-9-1-local/7fa2af80.pub
sudo apt-get update
sudo apt-get install cuda


apt install libopencv-dev 
 
# more code/ references
http://machinethink.net/blog/object-detection-with-yolo/
http://ozone.network/apidocs/starter__example_8cpp_source.html
 
# yolov2 traning
https://www.yuthon.com/2016/12/03/Train-YOLOv2-on-my-own-dataset/

# dump reolink using GPU nvidia
ffmpeg -c:v h264_vdpau -rtsp_transport tcp -i "rtsp://admin:@25.0.0.238/h264Preview_01_main" -f segment -segment_time 60 -segment_format mp4  -reset_timestamps 1 -strftime 1 -c copy -map 0 dauphine-%Y%m%d-%H%M%S.mp4

ffmpeg -hwaccel vdpau -i rtsp://admin:@25.0.0.238/h264Preview_01_main -an -f rawvideo -y /dev/null



# complicated zoneminder plugins and bits of code :(
https://gist.github.com/pliablepixels/34b484cca1c23ae9493c5d2e57f0bde8
https://forums.zoneminder.com/viewtopic.php?f=36&t=26222
http://through-the-interface.typepad.com/through_the_interface/2012/09/creating-a-face-recognising-security-cam-with-a-raspberry-pi-part-1.html

# cheap cams 720p max
http://www.zmododirect.com/


#Save as video
By default ZoneMinder saves events as a sequence of images. It is however possible to save an event as a video file. Caution is advised when converting events too video as it is very strenuous on the ZoneMinder machine, however once you have converted an event it can be viewed/downloaded any time without additional stress on the server.
First, make sure you have OPT_FFMPEG under Options -> Images set to yes (checked).
If necessary, set the proper full path for the ffmpeg executable in PATH_FFMPEG (ex.: /usr/bin/ffmpeg)
Open up the default view for an event and Click the video link located in the top left corner
Choose a video export file type and click generate.
Download the video to your machine. If video files have already been generated you will see them listed at the bottom of the page.
When an event gets a video file encoded for it you can choose to automatically include that event with any future exports. For a more detailed explanation on how to select and export events investigate How to export download and view events



Zoneminder Darknet yolov2
viewtopic.php?f=36&t=26906

operat0r
Posts: 13
Joined: Thu Jan 18, 2018 4:03 am

Re: Zoneminder Darknet yolov2 tiny Hack

Post by operat0r » Sun Feb 04, 2018 12:54 pm

Logs kept getting rotated out so I just moved the logs to /media/data/zm.log. This is harder then it looks if you don't know jack @#$% about logrotate/rsyslog...hope this helps

first in last out or whatnot so make sure the first line is the first rule that has to do with local1 .... god I hat rsyslog ...

Code: Select all

cat /etc/rsyslog.conf |grep local1
local1.*                        /media/data/zm.log
local1.!*;auth,authpriv.none            -/var/log/syslog
local1.!*;      mail,news.none          -/var/log/messages





Zoneminder Darknet yolov2
viewtopic.php?f=36&t=26906

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest