Skip to content

sandyol55/Ropey-Cam

Repository files navigation

ropey title

Ropey-Cam is a self-contained Python-based surveillance / security / wildlife / camera control system, designed for Raspberry Pi single-board computers with attached camera modules. The system provides real-time, low-latency, MJPEG video streaming to web browsers, while simultaneously monitoring for motion, and recording high-quality H.264/.mp4 video clips when motion is detected, while also supporting remote control of the camera from the browser.

As a long term user of the comprehensive RPi_Cam_Web_Interface with various RPi cameras (V1,V2 and HQ), it was frustrating not being able to use third party cameras e.g IMX462, nor any of the later RPi camera modules with RCWI. Unable to find any suitable libcamera/Picamera2 based alternatives, and as a learning exercise, work started on this basic -very basic- alternative. As a pure Python application it should be relatively easy to access for those keen to modify and extend it for their own use-case.

For those looking for more comprehensive features, the very fully featured raspi-cam-srv is recommended.

"Ropey-Cam" reflects its construction methodology: it is assembled from code snippets and examples from the Picamera2 repository, with minimal Python complexity, and uses a multi-threaded architecture to handle concurrent operations. In it's basic form, when installed on a RPi with the standard desktop OS, the only additional dependency is OpenCV.

Quick Start Example

On a Raspberry Pi running a desktop RPiOS and with camera attached, open a terminal and enter :-

hostname -I

and take a note of the Raspberry Pi's IP address.

Then :-

sudo apt install python3-opencv -y
git clone https://github.com/sandyol55/Ropey-Cam
cd Ropey-Cam
./Ropey-Cam.py

Alternatively

There are only two files required to use Ropey-Cam, the python script Ropey-Cam.py, and its configuration file ropey.ini. So instead of cloning the whole repository, simply open the files, then copy or download them to a Ropey-Cam directory within the /home/users directory on the target local machine. Then proceed to run the file as above. If necessary make file executable with

chmod +x Ropey-Cam.py

It is even possible to download only Ropey-Cam.py, as a ropey.ini file with default values will be built after first run.

Then to access the camera stream and controls :-

From another device on the same network browse to xxx.xxx.x.xxx:8000, where xxx.xxx.x.xxx is the IP address of the Ropey-Cam Raspberry PI.

Or, on the Raspberry Pi computer, open a local browser and request 127.0.0.1:8000 - or even do both - to get simultaneous streams to multiple browsers.

The browser should present a home page as below


Browser screen

Basic operation.

The figures in the top left of the streaming video frame indicate the measure of frame to frame change in the video, against the currently set trigger level. If motion (or noise) in the video is present, in excess of the trigger level, then video recording will be activated as indicated by the red REC stamp in the top right.

To reduce the likelihood of triggering by transient events, or noise, a configurable number of consecutive frames exceeding the change trigger level are required before recording is activated. The default is currently set to 5.

If recording is activated when no apparent motion is present in the video, pressing the Inc_TriggerLevel button repeatedly will increase the frame to frame change threshold at which recording is triggered.

Similarly if motion in the frame does not trigger recording press the Dec_TriggerLevel button repeatedly until recording is triggered.

Once calibrated for the motion/noise environment, test that deliberate motion triggers recording.

The REC stamp will remain for ~ 6 seconds after motion has dropped below the trigger level, while the full video file - containing ~3 seconds pre-trigger, N seconds of motion and ~3 seconds of post-motion video - is saved to a Videos sub-directory that is created on first run of the script. Also saved is a jpeg snapshot of the low-resolution video frame at the trigger moment.

This recording is happening in the background while the live stream continues uninterrupted.

Control buttons

Brief description of the effect of the control buttons, some of which may be familiar to Rpi_Cam_Web_Interface users ;-)

The button text describes the action that will be taken when the button is pressed, while the colour indicates the current state of that control i.e. active/inactive.

Motion_Detect_Off

will disable the active motion detection, but retain the live stream.

will be replaced by Motion_Detect_On after being pressed.

Inc_ and Dec_TriggerLevel

as described above, modify the sensitivity to frame to frame changes.

Manual_Recording_Start

will initiate a recording irrespective of motion triggering.

will be replaced by Manual_Recording_Stop.

DELETE_ALL_FILES

will delete all the timestamped .mp4 video and associated trigger-point .jpg files that are stored in the Videos subdirectory.

Requires to be confirmed with a second press.

EXIT

will end the Ropey-Cam program on the Raspberry Pi,

also requires confirmation with a second press.

RESET

acts as a cancel button for any of the 4 other system buttons,

to allow the first press of those buttons to be ignored and reset.

REBOOT

will reboot the Raspberry Pi.

Best used in an advanced set-up where Ropey-Cam has been set to start on boot up.

SHUTDOWN

will shutdown the Raspberry Pi.

Like EXIT and REBOOT there is a built in delay of ~ 8 seconds, to allow any in-progress recordings to be stored cleanly and for the configuration file to be updated.

All four of the system control buttons change colour to indicate a 'primed' state after the first press.

See the General Notes linked at the bottom of the page for a description of the configuration entry pages and camera control pages which are accessed via the two link blocks below the main control buttons.

Pre-deployment configuration

Typical use is expected to be in a headless remote mode.

Before deploying it will be useful to adjust the configuration to suit the Pi and camera being used by tuning the video resolutions, aspect ratio and framerate. This can now be done from within the browser, with no requirement to make any changes to the script.

The default configuration is currently :-

16:9 output with a 1280x720 main stream resolution and 512x288 for the browser stream, and a framerate of 20 fps.

The sensor default mode is set to 1, which will typically give a full frame (16:9) 2x2 binned mode on HQ and V3 cameras, or a full frame (4:3) 2x2 binned mode on V1 or V2 cameras.

Other recommended steps before deploying are:-

set the Raspberry Pi up as a Samba server

set the Ropey-Cam program to start on boot, with a systemd service.

(Lots of online tutorials for these steps, but links to guides for creating both of these services are included at the bottom of this page).


Performance

As an example the screenshot below is taken from a Pi5, accessing Ropey-Cam running on a Pi3A+ under the Lite version of RPiOS, with both machines connected via Wi-Fi to a home network.

Test screenshot

In the upper left is an ssh session running htop on the remote Pi3A+ during an active recording, indicating useful spare CPU and memory capacity.

In the lower left the Thunar file manager is accessing the Videos subdirectory on the remote Pi3A+ via the network samba share. (Using a separate file manager from the inbuilt pcmanfm allows the thumbnail icon sizes to be increased without affecting the desktop).

And on the right is the browser, showing an image from the camera while it is being pointed at the browsing computer's screen - with a stopwatch in the foreground.

The difference between the stopwatch time in the foreground, and the background - earlier - image of the stopwatch that has been captured, processed, passed through the network and rendered by the browser, is a measure of the system latency. The latency of 170ms is in the middle of the values seen in this particular configuration, which were typically in the 150 to 200 ms range.

Acknowledgements

Much of the code is taken from the examples, discussions and responses to issues as found in the Picamera2 Github repository .

The method of button control was inspired by the code found in this repository .

And signag offered coding suggestions against an early version of the code.

The previous simple mse frame to frame change algorithm has been replaced by a more effective 'Frame Difference' algorithm based on this article

Lite Installation

Samba share

Start on reboot

General Notes

About

Low latency web streaming and motion-triggered surveillance / security / wildlife camera control application for Raspberry Pi that supports some of the basic features of RPI_Cam_Web_Interface.

Topics

Resources

Stars

Watchers

Forks

Contributors

Languages