Raspberry PI Camera and Web Streaming

In previous post, we learned basics of Raspberry PI which is a general purpose, low cost, computing platform.

Today, we’ll learn how to install and use Camera module.

The Pi camera board is an HD camera, which captures high resolution images. Its pretty much the same camera in your phone. You can use it to take pictures or record videos.

Camera Module

To start with, we need a camera and here is one video from the official website which shows how to install camera module.

I ordered the camera from amazon (cost approx. 10 euro) and then simply installed it following the steps in video mentioned above and following is the picture of camera installed

Next, we’ll see some simple ways to work with camera using command-line and later with a python program.

Enable Camera Interface

We can enable camera using raspi-config as shown below

and from the Interfaces tab, Enable the Camera interface:

Once you click OK, system will ask for a Reboot and you can Reboot it and login again.

Taking Pictures and Recording Videos

Taking Pictures

Using raspistill utility, which takes still pictures.

raspistill -o test.jpg

following screenshot shows the test.jpg is captured

Now, we can open up the file and you can see that the picture is there:

raspistill command is very versatile command and you can configure it as per your requirement:

raspistill -t 10000 -o test.jpg
raspistill -rot 180 -o test180.jpg
raspistill -hf -vf -o testflip.jpg	
raspistill -w 1920 -h 1080 -o testHD.jpg
raspistill --help

You can find more about options available using –help flag:

Recording Videos

For videos we can use the utility raspivid:

raspivid -o test.h264

and following is the captured video file

So using these two utilities, you can control your raspberry pi camera, directly from command line.

Web Streaming Example

Lets see a web streaming example, where we will see how to stream video to a web-page.

This python code used in this example is originally from the picamera website.

I’ve created a git repository with the python code file rpi_camera_surveillance_system.py . You can edit the file and make changes to it if needed.

Next, we can use VNC Viewer to connect to RPI and simple execute the python script as shown below

python3 rpi_camera_surveillance_system.py

after executing the above shown command, you will see the camera-light turn-on.

We can now open up the web browser on PI and can enter following address to see the live streaming video

http://localhost:8000

That’s all what is needed for our basic web streaming example.

Summary

Raspberry Pi Camera is an inexpensive module and it is easy to install and get started.

You can download the source code from this git repository.

Let me know if you have some comments or questions. Till next time, happy coding.