aert667
Posts: 6
Joined: Wed Sep 28, 2022 7:23 pm

Motion detection and provision of compressed RTSP camera stream

Fri Jan 27, 2023 8:15 pm

Dear all,

I'm have the following hardware in use:
  • Raspberry Pi 4 Modell B 8GB RAM
  • Raspberry-Pi-OS 64 bit "Bullseye" with latest updates
  • Raspberry Pi Camera Module 3
  • Webcam 1080p with MJPEG output
With this hardware constraints I try to fullfill the following requirements:
  1. The software shall provide an output (e.g. on the cli or starts a scripts), when a motion in the live camera video is detected
  2. The system shall provide the camera video via RTSP to other clients in the local network (e.g. to VLC). The provided stream shall be compressed (e.g. h264) to save bandwith
  3. The CPU load shall be as low as possible.
I tried now different possible solution, but none of them could fullfill all requirements. Enclose my tested solutions.

Solution 1
I' used the included motion detection of libcamera-vid post processing with the following code in a script for the Camera Module 3:

Code: Select all

libcamera-vid -t 0 -n --inline --lores-width 128 --lores-height 96 --post-process-file motion_detect.json
I use the post-processing motion detection in verbose mode, so there will be an output on the CLI, when a motion is detected. This I can detect with a python script and react. So requirement 1 and 3 are fullfilled (CPU load is low), but unfortunately no RTSP stream is provided so requirement 2 is not fullfilled.

Solution 2
I'm using the following code to provide a compressed RTSP stream via libcamera-vid and the Camera Module 3:

Code: Select all

libcamera-vid -n -t 0 --inline --width 1920 --height 1080 --framerate 15 -o - | cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554/stream}' :demux=h264
This works in the local network with low CPU load so requirements 2 and 3 are fullfilled. Unfortunately I did not find a way to also use the post-processing motion detection filter. This means requirement 1 is not fullfilled.

Solution 3
I additionally installed the linux software motion. For me it did not work directly together witch libcamera-vid, so I was using again the possibility of libcamera-vid to provide the compressed RTSP stream via the Camera Module 3 (requirement 2 fullfilled). This RTSP stream is then also provided to the motion software and here the motion is detected and a script can be excecuted (requirement 1 fullfilled). Unfortunately the CPU load is now very hight. I assume this is the case because motion can't do the motion detection directly on the h.264 stream and has to somehow transfers it in another format (this is only an assumption). Also deactivation of webstream in motion did not help.

Solution 4
I used v4l2 to get the stream from the Webcam 1080p into motion. This worked perfectly and a motion could be detected and a script was executed (requirement 1 fullfilled). The CPU load was also low (requirement 3 fullfilled). Unfortunately motion does not provide an compressed RTSP stream, but a not good compressed MJPEG stream to the network (requirement 2 is not fullfilled).

Summary
In the end nothing could fullfill all of my requirements. Do you have maybe an idea? Maybe you know a way how to provide the RTSP stream via libcamera-vid AND use the post-processing filter (combination of solution 2 and 1)?

bidrohini
Posts: 201
Joined: Thu Oct 20, 2022 1:52 pm

Re: Motion detection and provision of compressed RTSP camera stream

Sat Jan 28, 2023 10:13 am

It sounds like you have tried a few different solutions and have encountered some limitations with each one. Here are a few ideas that might help:

One option to combine Solution 1 and Solution 2 would be to use the motion detection output from Solution 1 to trigger a script that starts the RTSP stream from Solution 2. This way, the stream is only sent when motion is detected, which could help reduce the overall CPU load.

Another option would be to use a different software for motion detection that is able to work directly with H.264 streams, rather than using libcamera-vid's post-processing filter. This would eliminate the need to convert the stream format, which could help reduce the CPU load.

If you are open to using a different hardware, you could consider using a USB webcam with built-in motion detection capabilities. These webcams can be configured to trigger an event or start streaming when motion is detected, which would allow you to achieve all your requirements with less processing power.

If you are open to using a different operating system, you could use something like MotionEyeOS which is a linux distro that is built specifically for use with webcams and motion detection. It has a simple web interface and supports RTSP out of the box.

Overall, it sounds like you may need to make some trade-offs in terms of which requirements are most important to you and what solutions are feasible with your current hardware and software. With the information you provided, it is difficult to point out a single solution that will meet all your requirements.

aert667
Posts: 6
Joined: Wed Sep 28, 2022 7:23 pm

Re: Motion detection and provision of compressed RTSP camera stream

Sat Jan 28, 2023 8:38 pm

Thank you very much for your feedback. Enclose my feedback:
One option to combine Solution 1 and Solution 2 would be to use the motion detection output from Solution 1 to trigger a script that starts the RTSP stream from Solution 2. This way, the stream is only sent when motion is detected, which could help reduce the overall CPU load.
With this feedback I updated my requirement 2 a little bit (see end of the post), because I want that the stream is always available not only if a motion is detected.
Another option would be to use a different software for motion detection that is able to work directly with H.264 streams, rather than using libcamera-vid's post-processing filter. This would eliminate the need to convert the stream format, which could help reduce the CPU load.
This is an opiton, but I did not find a sloution until now. I tried with VLC, but this did not work. Do you have here an idea, which program could be used?
If you are open to using a different hardware, you could consider using a USB webcam with built-in motion detection capabilities. These webcams can be configured to trigger an event or start streaming when motion is detected, which would allow you to achieve all your requirements with less processing power.
As mentioned in the answer to your first quote - The strean RTSP stream shal lbe provided all the time. It would be a solution, if the camera can provide the RTSP stream and trigger an event if motion is detected. With this feedback I also udated my constraints, because I want that the camera is physically connected to the Raspberry Pi. The reason is that I want to put the raspberry pi, together with a monitor and the camera in a custom made box.
If you are open to using a different operating system, you could use something like MotionEyeOS which is a linux distro that is built specifically for use with webcams and motion detection. It has a simple web interface and supports RTSP out of the box.
An alternative OS is not possible as I need also some other functionality.
Overall, it sounds like you may need to make some trade-offs in terms of which requirements are most important to you and what solutions are feasible with your current hardware and software. With the information you provided, it is difficult to point out a single solution that will meet all your requirements.
With the information above I update my constraints and requirements:

Hard contraints for the system:
  1. Raspberry Pi 4 Modell B 8GB RAM
  2. Raspberry-Pi-OS 64 bit "Bullseye" with latest updates
  3. Camera shall be physically connected to the Raspberry Pi 4, because in the end it shall be placed in a custom houisng together with the Raspberry Pi
Functional requirements to the system:
  1. The software shall provide an output (e.g. on the cli or starts a scripts), when a motion in the live camera video is detected
  2. The system shall provide the camera video via RTSP to other clients in the local network (e.g. to VLC) all the time (e.g. independent if a motion is detected or not). The provided stream shall be compressed (e.g. h264) to save bandwith
  3. The CPU load shall be as low as possible.

Return to “Camera board”