Raspberry Pi video at 30fps using Camera module

We have been playing quite a bit with the new Raspberry Pi, a previous article covered video recording using a USB Webcam and a neat little piece of software called Motion. If you haven’t seen that yet, have a look at http://sjj.azurewebsites.net/?p=701.

I’ve now managed to get my hands on the Raspberry Pi Camera Module. It’s a really nice piece of hardware that costs around £25 and they claim you should be able to get 30fps at HD resolutions. You also get a NoIR (No InfraRed) version for recording in low light conditions.

We’ll attempt to get some motion detection done using this module and hopefully attain some high resolution high fps video.

A couple of things. As usual, I’ll assume you have some basic understanding of Linux terminal commands. Also, if you want to control the Rpi using SSH from windows, which is what I’ll be doing, use PuTTY (see http://www.chiark.greenend.org.uk/~sgtatham/putty/).

Right, let’s get started. First things first-make sure your Rpi is up-to-date by:

sudo apt-get update
sudo apt-get upgrade

Next, you need to plug in the camera module via the CSI connector.

blog
Figure 1: Connected Rpi camera module. Note this is the NoIR model.

We now need to make sure the camera is actually enabled. Type:

sudo raspi-config

A menu will open up. Go to 5-Enable Camera and make sure Enable is selected. Your Rpi will reboot once this is done.

To test if your camera is working, type the following in the terminal:

raspivid -g

You should see the camera window open up and display some output for around 5 seconds. Notice the brilliant quality, frame rate and resolution.

Now, we can make use of motion to achieve what we’re trying to do. One of the issues we hit was that motion on it’s own is unable to read data off the CSI connector, hence cannot use the camera. One workaround is to use motion-mmal (an alternative that uses mmal to read data off the camera). However, we face the same restriction with this-the highest frame rates we could get at 720p (1280 x 720) was around 3-4fps.

Instead, we use a combination of OpenCV and mmal. OpenCV (see http://opencv.org/) is a library that allows users to manipulate image/video data and output useful information from it. It can do complicated tasks such as face recognition and is used in a variety of industries. OpenCV is able to read data from the camera at around 15fps. This is fairly adequate quality for recorded video. What we do is use OpenCV for the motion detection, and once motion is detected, we will use the mmal libraries (essentially like the raspivid command above) to record video. This means that we will be able to get high frame video without compromising on quality and resolution.

I make use of a github project written by sodnpoo (http://www.sodnpoo.com/) which makes things quite easy. The project is located at: https://github.com/sodnpoo/rpi-mmal-opencv-modetect.

1. Install the necessary prerequisites.

sudo apt-get install cmake libopencv-dev

2. Create a directory to place the files in and go to the directory.

mkdir -p /home/pi/src/raspberrypi
cd /home/pi/src/raspberrypi

3. Build the prerequisite libraries.

make -C /opt/vc/src/hello_pi/libs/vgfont

4. Download the files and unzip.

wget https://github.com/sodnpoo/rpi-mmal-opencv-modetect/archive/master.zip
unzip master.zip

5. Create and go to the build folder.

mkdir build
cd build

6. Build the project.

cmake ../rpi-mmal-opencv-modetect-master
make

Done! You’ve successfully set up everything that’s required. Now to test it, run:

./mmal_opencv_modect > video.h264 2> video.log

You can alter video.h264 for any other name that you want. If you want it stored in any other directory make sure you give the absolute path. 2> video.log writes a log of the video being recorded. Use Cntrl+C to stop recording.

Note: This script should be run as is above only if you are in the build directory. If not, the absolute path must be specified.

Tip: Since video files can get quite chunky quite quickly, why don’t you write the files to your network share. If you still haven’t set up your network share, look at the RPi+USB Camera article (http://sjj.azurewebsites.net/?p=701). Then simply specify to write the file to that location.

Blog-SB-6
Figure 2: My build directory showing the video file I just recorded, with the log.

Links: These are just some of the references I used.

Article by Umang Rajdev

http://www.raspberrypi.org/forums/viewtopic.php?f=43&t=44982
http://www.raspberrypi.org/forums/viewtopic.php?f=43&t=76414
http://www.sodnpoo.com/posts.xml/raspberrypi_camera_with_opencv_motion_detection_and_recording.xml

Leave a Reply

Your email address will not be published. Required fields are marked *