Skip to main content

How to Set Up Video Streaming with Raspberry Pi Camera

Pi_Camera

How To Perform Video Streaming Using Raspberry Pi? - Appuals.com

Nowadays people are paying their attention to work with Single Board Computers (SBCs) for their projects rather than using large computers or bunch of micro-controllers inside a limited space. There are several reasons behind this. These SBCs are highly integrated circuit boards with many kinds of microprocessors, memory chips, GPIO (Input/output) features with different communication protocols and etc. within a small single printed circuit board. Considerably high computational power with essential external interfacing I/O features and low power consumption makes these kinds of circuits are ideal for robotics, IoT, Machine learning and many other portable applications. Here we are going to work on a considerably advanced project using a Raspberry Pi SBC which I was implemented as a sub part of one of my university projects.

Therefore, in this blog post I am discussing about setting up a Raspberry Pi computer and use it for a useful and interesting project related to basic robotics and many embedded systems.

Setting Up Raspberry Pi Step1: Here we are going to flash Raspbian operating System to a Raspberry Pi SBC. I used a Raspberry Pi 3 Model B platform and a 16GB microSD card for the process. For our application any available version of RPi will be okay and at least 8GB microSD card is required. There are two steps to install Raspbian in to Raspberry pi.

  1. 1.  You need to download latest Raspbian image file to your computer from official website. (The latest version is Raspbian Buster July 2019 is for this date) I recommend to download ‘Raspbian Buster with desktop and recommended software’ option from the following link since there is a valuable software pack in it.  After download the image file, write it to your microSD using ‘Win32DiskImager’ software.
    

Link: https://www.raspberrypi.org/downloads/raspbian/

  1.  2. Online Installing process with NOOBS which is an easy operating system installer.
    

Link: https://www.raspberrypi.org/downloads/noobs/

Step2: After successfully installing Raspbian into your microSD card plug it into your RPi and connect a Display through HDMI port, a USB mouse, a Keyboard and finally a 5V 2.5A micro USB power supply. It will boot up after several seconds with an initialization process.

After a successful bootup to the Raspbian desktop, you have to do several configuration setups to enable camera interface.

Open up a terminal window and enter following command to enable camera interface.

$ sudo raspi-config

This will open a new configuration interface with several options. You need to go the ‘Interfacing options’. There you will see several interfaces like SPI, VNC, Camera, SSH, I2C, Serial and Remote GPIO. Enable Camera interface and SSH(Secure Shell interface) from the above list. Here we enabled SSH of RPi in order to remotely access to our Pi from a computer. We will cover enabling camera step in the following topic.

Interfacing the Pi Camera

Now we are going to work with Raspberry Pi camera. In different models of RPi computers there is a dedicated socket for interfacing a camera. You need to connect the Pi camera to the Camera interface of your Raspberry Pi through the ribbon cable connector carefully as the following picture.

Now make sure you enable the Camera interface on your pi following below steps.

Open a terminal and type

                                           $sudo raspi-config

It will open the raspberry pi configuration window with several kinds of settings. You need to navigate through the menu using arrow keys to the Interfacing Options and select it by hitting enter.

Then, enable the camera interface as you see in the following screen shot.

Finally, finish configuration by saving the settings. It will ask to reboot your pi while quitting. Hit "yes".

After successfully interfacing the camera to the RPi you can play with several python scripts which are related to Pi camera including taking photo stills, Videos, sequence of stills, and some other interesting activities which can be important for your projects. I am not going to go through these codes. You can find these basic scripts in the link below..

https://github.com/ChandulaNethmal/Pi_Camera/tree/master/Pi_cam

Now we need to connect our raspberry pi to a local are network (i.e. through Wifi or Ethernet) selecting available connections. Not only the Pi, the computer which you are going to use to watch the live video also must be connected to the same local area network.(i.e. to the same wifi hotspot).

Live Streaming with Pi Camera Here we are focusing on transmitting a video stream capturing from the pi camera to a computer or mobile phone via a local network in real-time.

First of all, let’s take a look at the python Script. Following is the first part of the code which is creating the web interface to the live stream receiver side.

PAGE="""\

<title>Raspberry Pi - Live Stream</title>

Raspberry Pi - Live Stream

Video Streaming Raspberry Pi Camera | Random Nerd Tutorials

"""

Here we are taking the stream in to a web page according to the above HTML formatting. Title of the page is “Raspberry Pi – Live Stream” as you can see in the fourth row. We are taking the source of stills sequence from the code which is defined as “stream.mpeg”, which is having a width of 640 pixels and height of 480 pixels in the center of the window.

class StreamingOutput(object):

def __init__(self):

    self.frame = None

    self.buffer = io.BytesIO()

    self.condition = Condition()



def write(self, buf):

    if buf.startswith(b'\xff\xd8'):

        self.buffer.truncate()

        with self.condition:

            self.frame = self.buffer.getvalue()

            self.condition.notify_all()

        self.buffer.seek(0)

    return self.buffer.write(buf)

Let’s define a python class called “StreamingOutput” containing two functions for initializing and writing the buffered frames.

Other than the above class, we are defining several other classes as StreamingHandler and StreamingServer. In the streaming handler we are maintaining the live stream with HTTP requests and image data sending. Streaming Server will keep the server function in the network and maintain the threading.

with picamera.PiCamera(resolution='640x480', framerate=24) as camera:

output = StreamingOutput()

#Uncomment the next line to change your Pi's Camera rotation (in degrees)

#camera.rotation = 90

camera.start_recording(output, format='mjpeg')

try:

    address = ('', 8000)

    server = StreamingServer(address, StreamingHandler)

    server.serve_forever()

finally:


    camera.stop_recording()

Finally, we have the main code which is continuously running. Here we are defining the resolution and frame rate of each frame at the beginning as “640x480” and “24fps”. You can change these two parameters according to your needs, but there are some limitations in the setup. Processing power of both RPi and Camera cause limitation of FPS and resolution. Even if the quality will increase with the resolution, it causes higher delays between frames depending on the LAN(WiFi) connectivity strength. Then we are defining the streaming server address as “IPV4_address_of _ur_pi,port”. Here we define our port as “8000” and the IPV4 address will depend on your local network and it will automatically assign.

You can download the full Python Script from this link.

https://github.com/ChandulaNethmal/Pi_Camera/tree/master/Live_streaming

Now we are going to run the code on the Raspberry pi node through the Raspberry pi Terminal.

You will see something like following screenshot after that.

Congrtulations!.............You have successfully deployed the live streaming server.

Now we need to access to that server through another computer/mobile phone using a web browser. Open your web browser from a laptop or desktop computer or mobile phone while keep running the camera stream of our previous setup. In the web browser enter the following address in the URL typing bar according to your” Rpi_ipv4_address:Port” as the following example of mine.

192.168.1.4:8080

It will bring the web page we created in the very first of the python code and the live video frames from your PI Camera as follows.

Now we have successfully deployed our project with the pi camera. You can use this setup in various projects such as a surveillance system with RPi, teleoperating robots and so on. Also you can change basic setting of the video in the python scripts and adjust the stream according to your requirements.

 

Video linkL


 

 

Comments

popular posts

AX2358 Homemade 5.1 Remote Kit with 16x2 LCD

AX2358 Homemade 5.1 Remote Kit with 16x2 LCD                                               AX2358 is I used for this 5.1 remote kit project. It is Built-in 2-channel to 6-channel converter and 6-channel volume controller. It has 4 stereo inputs and one 6 channel input. Any stereo input terminals of AX2358 are selected, it will be directly converted to 6 channels and then output through volume adjustment but when the signal from the 6-channel input terminal is selected, it directly enters the volume adjustment and then outputs it without any processing. It has 6-channel individual volume control in this IC (0 to -79dB, 1dB/step). This IC working by I2C communication. The data are transmitted to the AX2358 via the SDA and SCL. So I need a microcontroller. I choose the microcontroller is Atmega328 for this. This microcontroller programmed with Arduino UNO.      ...

DIY Tutorial Resetting Toner Chip on Ricoh SP C250DN Printer in Simple Steps

  DIY Tutorial Resetting Toner Chip on Ricoh SP C250DN Printer in Simple Steps The RICOH SP 250DN printer toners have a chip that keeps track of the number of pages that have been printed. This is anoying because it will render a refill useless. In order to reuse this kind of toner, there are two things you need to do: refill the toner with ink (if needed) reset the toner chip (or replace it) There is plenty of information explaining how to refill the toner but little information on how to erase the toner chip. This document deals with the second point: how to dump the chip and reset it. It took me a while to get everything setup and to have my toner chip reset so I would like to share this process in order to help other to do the same with their printer toner cartridges. I will step through the process of understanding the problem, analysing the chip circuit, dumping the chip contents and writing back a pattern so the printer will be able to initialize the chip and set t...

DIY Metal Detector using Arduino A Simple Guide to Building Your Own at Home

  DIY Metal Detector using Arduino A Simple Guide to Building Your Own at Home How does a metal detector work? As we can see there are three things that are using to complete the whole project. Electronic circuit, Arduino, and a copper coil. here actually we are making a proximity sensor that detects the metal with a  metal detector  using Arduino . Here are the steps to build a DIY metal detector using an Arduino: Materials required: Arduino UNO Copper coil Diode 10nf capacitor Buzzer Led 220 ohm &  330 Ohm Resistor Jumper wires and a breadboard USB cable for uploading the code Circuit Diagram Arduino metal detector:-  Arduino UNO 10 nf Capacitor A2 Pin Terminal 1 GND Terminal 2 Arduino UNO Buzzer D6 Pin Positive GND  Negative Arduino LED 220 Ohm Resistor D5 Pin   Terminal 1   Anode Pin Terminal 2 GND Cathode Pin   Arduino UNO Copper coil Diode 330-ohm res   Terminal 1 Terminal 1 Terminal 1 A1 Pin     Terminal 2 A2 Pin ...

How to Calculate Inverter Battery Backup Time

  How to Calculate Inverter Battery Backup Time? Backup Time (in hours) = Battery Capacity (in Ah) X Input voltage (V) / Total Load (in Watts) Whenever you plan to buy an inverter battery, there is a desire to know some important information in your mind. Like how much will be the backup of the inverter battery, how many hours of backup will be available for how many ah batteries, etc. In this informative information, here you will know the backup time calculation formula.   Taking your inputs, you can calculate your total load, like: 3 tube led lights = 40 x 3 = 120 Watts 2 fans = 75 x 2 = 150 Watts 1 Bulb router = 1×20 Watts = 20 Watts So, the total load in your case is 120 + 150 + 20 = 290 Watts. Now, let us apply all these values in the above-said battery backup time formula.  Backup Time (in hours) = Battery Capacity (in Ah) X Input voltage (V) / Total Load (in Watts) Backup Time (in hours) = 150 x 12 / 290 = 6.2 So, your inverter battery will last around 6.2 hours ....

Wireless Joystick controlled Robot Car using Arduino, 433Mhz RF and L298N Motor Driver

    Wireless Joystick controlled Robot Car using Arduino – In this tutorial, you will learn how to control a Robot Car wirelessly using Arduino, L298N Motor driver, and  433 Mhz RF transmitter and Receiver . The robot control system can be activated and de-activated using the Built-in Joystick push Button. Arduino Uno Arduino Nano Mega 2560: Robot Car chassis kit: L298N motor driver: 2-Axis Analog Joystick 433 MHz Transmitter and Receiver Modules: DISCLAIMER: Please Note: these are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way! wireless joystick Interfacing: Watch this tutorial for the robot parts assembling and connections. All the connections are exactly the same as explained in my previous tutorial. The only modification that I did is the addition of the 433 Mhz RF receiver module…the VCC pin of the receiver module is connected with the 5 volts…the ground pin of the ...