5MP Motion Camera JPEG Image and Movie Catcher
1.1.1.1
A CGI interface to capture and display pictures and movies using OpenCV.
|
This project demonstrates how to receive images from this camera and process them into still images and movies using OpenCV and FFMPEG on Raspbian Buster (recently rebranded as Raspberry Pi OS). The camera sends the following JSON message via HTTP POST:
The battery data lands at the top of the image browsing page.
For more information about this page see the Camera Settings Project. The rest of the data are used to process the value
payload. (It has been elided for brevity. Imagine a very long base64 encoded string in its place.) The name
field tells us which camera sent the data. If you have multiple cameras simply give them distinct names and all will be well. The frames
counter lets us know if we should expect a single JPEG or multiples. Frames are numbered from zero so 0
frames yields one picture. If we get multiples we split them apart and order them by frame number. If we get the reserved value of 255
that tells us we've received an AVI/MJPEG movie. If it passes magic number and FFMPEG validation we convert it to MP4 and save it as a movie. If it does not, we attempt to save as many frames as we can and fallback to saving frame numbered images. See mjpeg_utils::save_avi_frames
for how FFMPEG is used within OpenCV to accomplish all this image processing.
If you haven't already:
It's widely known that the hardest part of using OpenCV is getting it and its dependencies installed. Once you get past that it's a pleasure to use. I built this project on a Raspberry Pi model 4B running the latest (June 2020) Raspbian (now Raspberry Pi OS). I did that because I wanted the project to be accessible to those on a limited budget. The Pi is famously affordable and it has more than enough capability for the tasks at hand. That said, if you have access to a laptop running Ubuntu you might find that a better option. To begin the building, install Clang and LLVM. I copied these directions from https://apt.llvm.org/ so be sure to check there first to see if the directions have changed. BTW: You may be tempted to build with GCC; if you get it to work you're a better build master than me. After that, install OpenCV and it's dependencies. Lastly run cmake
in the usual way in the project root folder.
On Raspbian, as root, do the following;
/usr/lib/cgi-bin/
If you renamed the project in CMakeLists.txt the binary will have a new name as well. Be sure to reflect this name in HTTP_HOST_URL
over in the camera project's platformio.ini.-1. As root, open /etc/apache2/sites-available/000-default.conf
and paste the following in just above the closing </VirtualHost>
tag.
-2. As root, make the directory /var/www/html/motion_camera
and copy the contents of this project's [web_root](web_root) folder into it.
-3. Fix the ownership, group, and permissions.
-4. When you're done /var/www/html/motion_camera
will contain:
-5. You can now test the configuration by pointing your browser to http://your_server:4444/motion_camera
One of the handy things about the design of the CGI interface is that all error messages should be written to the standard error stream; stderr
. The web server writes these into it's error log; /var/log/apache2/error.log
. If this CGI encounters and error it will write a useful message about it to this file. To turn off this output edit debug_output.hpp and comment out DEBUG_OUTPUT
Certainly not. The CGI interface standard is supported by many different HTTP servers. You should have little difficulty adapting this project to work with your chosen web server.
There are also tools you may find useful to you in other projects. For example;
std::map<std::string, std::vector<std::string>>
From this you can create a project that handles HTML GET requests.std::map<std::string, std::vector<std::string>>
- one for variables and another for files. From this you can create a project where forms are used to control a robot/embedded system or your Raspberry Pi. To help you get started there is an everything-but-the-kitchen-sink HTML form. Edit the <form>
tag at the top and change the action
attribute to reflect your server name.If you find a bug please create an issue. If you'd like to contribute please send a pull request.
The following were helpful references in the development of this project.