How to: Turn a cameras RTSP stream into motion detection

UPDATE: This method is now implemented in to the IpCamera binding and is easy to use, just follow the bindings documentation for more information.

I have had good results doing this, however as none of my cameras need this I am not interested in testing this for months on end hence this thread will hopefully collect the results from users that are keen to do this. Wanted to document it and give a thread for people to post what works for them and also why you don’t want to do this. You can easily do this via a script (plenty of scripts using this method are on GitHub for ideas) as Openhab allows a simple REST call to move any control inside of Openhab from your script very easily…

Do I recommend this?
Short answer is no I don’t. Slightly longer answer, I recommend you buy a better camera that has motion ability available by an API or at least Onvif as that makes a camera that costs slightly more be far better “value”. See the IpCamera binding and what cameras have better support in that binding if you have not purchased a camera yet. If you already own a camera and you most likely have Openhab sitting at 2% CPU usage then why not try it.

How much CPU?
How much CPU this uses depends on the FPS of the stream and how many pixels the camera has. It is a good idea to not bother trying unless your stream is 1080p maximum and you reduce the framerate to 10fps. Trying to do this on a 4k camera with 30 FPS is going to need a serious CPU but with 1080p and 10fps I found a cpu load of 25% average on my Odriod C2 ARM processor based system. My newer Odroid N2+ can do 1080p and 25fps with only 8% extra load on the system. An additional power draw of 1-2 watts is nothing to worry about.

The Alarm is lagging behind real time
This is a clue you have not read the above question and answer as big lags occur when your CPU can not keep up to real time. When working correctly it is around 1-2 seconds behind realtime due to processing a RTSP stream. FFmpeg will tell you when you are at or above speed=1.0x and if your CPU can not keep up the longer you have the stream running, the longer the lag gets.

Can I exclude areas that have movement?
Only watching a section of the cameras view for motion can be done with these crop filter examples. This allows it to ignore a tree that moves in the wind or people that walk past on a footpath. You may need to do this to remove a clock that ticks over on top of the video footage. I got it working fine without cropping out the clock but I did not try to optimize for day and night conditions.
https://ffmpeg.org/ffmpeg-filters.html#Examples-52

What command to use?

You can test the ffmpeg detection with this command via putty, ctrl+c closes the stream, then if you press your UP arrow the command is re-called so you can edit it with any tweaks you want to test without having to retype it each time. Also the right hand button of your mouse will PASTE the text into the terminal of putty.

ffmpeg -rtsp_transport tcp -i rtsp://user:pass@192.168.0.6:554/streamurl -vf select='gte(scene\,0.005)',metadata=print -an -f null -

Adjust the 0.005 value up and down to set how sensitive it is and this is just a good starting point to have a play. You may want to try noise reduction filters first if your camera has poor picture quality, the options are huge with ffmpeg. The results will need to be parsed by filtering how many detections occur in a set time frame and how close together. So when set right you will see more lines getting reported when you have movement.

3 Likes

Awesome!

Please use the ipcamera binding readme for all information on how to setup and use this feature as it has been merged into openHAB 2.5.9 and newer.

2 Likes

Will test later tonight :+1:

Look forward to hearing how this build works as I changed the method completely as it meant half the cpu usage and this means the control behaves differently. Both cameras I have tried this on have worked.

@matt1 so on setting 1 I get false motion alerts (not many though) but anything higher nothing.

Don’t know how this works but is there any chance we can have some form of multiplier (Maybe a string ie 0.25x, 0.5x, 1x, etc) added for the slider value? Save you having to keep rebuilding whilst people test

It does detect motion thought at 1, and the cpu usage is great now - 4K 15fps VBR HIGH, 10% for ffmpeg (and it’s also doing mjpeg for 4 other cams)

New build is up and it now prints the ffmpeg command that is running into the debug logs so you can check what value is set and then do testing via the manual method which is outlined in the first post of this thread. I really do not want to spend any more of my time testing this as all my cameras have far better “smart” alarms built in and I have another 3 new features to work on. So any testing you or anyone else does is great, give feedback like you have done already and I’ll make some changes based on your testing. Try starting at a value of 15 this time with todays build…

Some feedback of the following would be great and anything else you notice.

  1. Day Vs Night does it need a different setting or does the 1 work for both?
  2. Does this run without stopping for hours, days, weeks, months? There is no check if it has stopped with no auto restart and that may be something to add if it proves to be a useful feature.

The other thing to do would be to allow a user to specify crop options as it is possible some of your false positives could be removed by cropping out trees that move.

Seems to be working ok, have set at 35, it’s nighttime atm and so far all good
Will check my logs against my app notifications tomorrow night to see if everything was tickety boo

@matt1 Update
Motion detection still running this morning so didn’t hang or anything
35 worked well at night but didn’t pick up motion in daylight
25 is picking up motion in daylight

Will test further at 25 for false alarms and also later when it switches to night mode

Seems to be working nigh on perfect though and cpu usage is still low for fffmpeg (this won’t affect java process at all will it as seem to have something else maxing java to 94% usage :grimacing:)

Great news, looks like it can stay in the binding then after it gets a few areas polished up to allow cropping.

No idea, best way is to disable things and if the CPU drops you know the cause. None of the new code runs until the control is moved away from the 0 position.

Hi @matt1,

Question about the ip camera binding.
I would like to crop the area where ffmpeg detects motion using these options:

-filter:v crop=500:300:150:200

Where do I add it in the Thing config using Paper UI?
I tried adding this filter to the Motion Options field, but it didn’t seem to work.

Any ideas what I’m doing wrong?

Thanks,
Randy

Nothing, but there was a bug stopping it from working when I tried it here. Made a change and works fine now. Are you using openHAB V3?

Using 2.5.10

You will need to move to V3 for it to work.

Ok - thanks @matt1

Just a heads up for anyone using this feature that I have made some improvements to it and need some people to test the changes. If you used X as your detection control, you need to halve it to have the same result, ie Use 12 instead of 25. This means you can change the detection over a greater range and more settings should work well.

See here for the precompiled jar and the reasons for the change.

[IPCamera] No FFMPEG Motion Detection · Issue #10683 · openhab/openhab-addons (github.com)

1 Like

I’m a newbie and I have a Hikvision DS-KD8003 doorbell. Between the doorbell and the street I have 5 meters of garden where I want to detect motion. The ipcamera binding does not currently support its API but works ok with rtsp stream.

I want to record 90s mp4 videos for each motion detection. I have installed a pir sensor but as it produces a lot of false positives I would like to try RTSP motion detection in this area crop=iw*(20/100):ih*(31/100):iw*(37/100):ih

ffmpeg can record that area using ffmpeg -i 'rtsp://user:pw@192.168.127.10:554/Streaming/Channels/101' -acodec aac -vcodec h264 -vf 'select=gt(scene\,0.004),crop=iw*(20/100):ih*(31/100):iw*(37/100):ih' test-raw2.mp4

Can you pls guide me on how to do it using the ipcamera binding ?

I use OH 3.2.0.M5

example in this post

Working doorbell: Hikvision DS-HD1 - Tutorials & Examples / Solutions - openHAB Community

1 Like