simple Python3 script realtime object detection

If you've made a patch to quick fix a bug or to add a new feature not yet in the main tree then post it here so others can try it out.
Post Reply
LBDG
Posts: 21
Joined: Mon Jun 23, 2014 10:17 am

simple Python3 script realtime object detection

Post by LBDG »

Hello

I know there are plenty of object detection script out there but I thought I would share this because it is simple to setup and requires no zoneminder changes. It is one python file which can be run with normal permission from the home folder on the zoneminder machine. Most detection scripts I found were either too complicated to setup or had too many options so decided to make my own.

Basically it scans zoneminder monitors of choice every seconds and upon object detection emails and/or stores (to a folder of choice) the detected frame/frames images.

Uses opencv (YOLOv4)

For more details:
https://github.com/lbdc/zm-alarm

*** NOTE: I am not a programmer so code may not meet any standards.
User avatar
burger
Posts: 450
Joined: Mon May 11, 2020 4:32 pm

Re: simple Python3 script realtime object detection

Post by burger »

added to https://wiki.zoneminder.com/ZMES I thought the zm-movie script you previously made was well made, so I'm sure this is good as well. One of the weaknesses of ZMES is that it could become abandonware if no one manages it, so it's good to have other options. Thanks.
fastest way to test streams:
ffmpeg -i rtsp://<user>:<pass>@<ipaddress>:554/path ./output.mp4 (if terminal only)
ffplay rtsp://<user>:<pass>@<ipaddress>:554/path (gui)
find paths on ispydb or in zm hcl

If you are new to security software, read:
https://wiki.zoneminder.com/Dummies_Guide
alabamatoy
Posts: 361
Joined: Sun Jun 05, 2016 2:53 pm

Re: simple Python3 script realtime object detection

Post by alabamatoy »

Thanks so much for creating and sharing this. Looks like a much simpler solution than others I've been trying to understand.

2 questions so far:
LBDG wrote: Sat Nov 25, 2023 1:33 pm Uses opencv (YOLOv4)
1 - Since you say its based on YOLOv4, is there an immediate issue you see with trying to use a later version of YOLO?

2 - You point it at localhost, ie the script is running on the local machine....do you see a problem with running it on a separate machine pointed at port 80 of the ZM host? Im thinking since both would be behind a FW that blocks all port 80 access from outside, the risk would be minimal. Any issues?

I'm looking at YOLOv7 since it appears to be designed to run on a GPU, and I now have a 2000 series Nvidia GPU that's really getting tired of doing nothing.....

Thanks again
LBDG
Posts: 21
Joined: Mon Jun 23, 2014 10:17 am

Re: simple Python3 script realtime object detection

Post by LBDG »

Hello

1 - The reason I stuck to yolo4 is that it seems to be built in OPENCV without relying on external libraries like tensorflow etc...

I was actually pondering looking into more recent versions of yolo versions which would be very easy to implement but did not want to dive into external libraries. If you have a version of yolo in mind I could look into it.

2 - The reason the script has to work on the local machine is for 2 reasons:

a. The "def monitor_state(a_token)" function which I runs locally on the zoneminder machine using the "zmu" executable. This functions scans the status of each cameras to see if they enter alarm or not which triggers the rest. I think I figured the zoneminder API for this.

b. The other is how to get the fid of alarm frames without using mysql but using the API. Currently this is the mysql query: "SELECT EventId, FrameId, TimeStamp, Type, SUM(Score) FROM Frames, Events WHERE Frames.Type='Alarm' AND Events.Id=Frames.EventId AND Frames.TimeStamp >= '" + st + "' AND Frames.Timestamp <= '" + et + "' AND MonitorId=" + m_id + " GROUP BY Timestamp ORDER BY sum(Score) DESC Limit 3;"

Other notes
- Since the Python 3 script operates only on a few images (or as many as defined in the settings) with top ZM Alarm scores, GPU are not necessary. However, using GPU we could update the script to actually scan the whole alarm video for detection.

I have been using this script for quite a while now and it has been running on my ZM machine without any hickups.
Post Reply