How to stream live video from your Raspberry Pi camera

0

The official Raspberry Pi camera comes in several forms. From original v1.3 to the latest version Raspberry Pi HQ Camera, all use the same software. Using the raspistill and raspivid commands, we can take still images and videos directly from the terminal.

More advanced uses included manual control of the white balance, color, saturation, and brightness of an image and video. We can also create a simple video streaming service with just one line of code.

In this tutorial, we’ll learn how to set up the camera, take some test photos, and then create a test flow to make sure everything works before we embark on a video art creation project using video art. ‘live video image effects that we can save on our desktop computer with very little effort.

Configure a Raspberry Pi camera

If you already know how to set up a Pi camera module, you can go ahead. These steps will work for all Raspberry Pi camera modules (including third party ones).

With the Raspberry Pi turned off.

1. Open the camera port by gently lifting the plastic latch upward.

(Image credit: Tom’s Hardware)

2. Insert the ribbon connector with the blue tab facing the USB / Ethernet ports.

(Image credit: Tom’s Hardware)

3. Close the lock on the connector and pull it very gently to make sure it is in place.

4. Turn on your Raspberry Pi then go to Preferences >> Raspberry Pi Configuration.

(Image credit: Tom’s Hardware)

5. Click the Activate button for the camera found in the Interfaces tab.

(Image credit: Tom’s Hardware)

6. Click on OK and to restart pi.

7. Open a Terminal and type the following command to take a quick photo to test the camera.

$ raspistill -o test.jpg

After five seconds, an image will be taken and saved as test.jpg. Using the file manager, verify that the image is correct before continuing.

Test a flow

To start a flow, we need to open a terminal and enter a fairly long command. Make sure your Raspberry Pi is connected to the network. For best performance use an Ethernet cable, Wi-Fi will work, but you may see dropouts.

1. Obtain the hostname of your Raspberry Pi. Open a terminal and type this command for your hostname. Write down the host name. You may need to add “.local” at the end, depending on your network.

$ hostname

2. Run the streaming command. The one-line command to run a live video stream from the camera is quite long, so let’s step through the command before we run it.

-o is our output, in this case set to none.

-t is the length of the video clip, using zero will set it to infinite.

-w and -h are the width and height of the video, in this case 800 x 600.

-fps are the frames per second for the video stream, a lower value should minimize loss.

| cvlc is a pipe that takes the output of the raspivid command, our video stream and streams the video using an h264 codec through the real-time streaming protocol (rtsp) over our network.

Run this command in a terminal on your Raspberry Pi.

raspivid -o - -t 0 -w 800 -h 600 -fps 12  | cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8080/}' :demux=h264

3. On your Windows / Mac / Linux computer install VLC then open VLC.

4. Go to Media >> Open network feed, or press CTRL + N.

(Image credit: Tom’s Hardware)

5. Enter the rtsp address and port of your Raspberry Pi. The rtsp address is the hostname of your Raspberry Pi, prefixed with rtsp: //, and the port has been set to 8080. Click Play to start the stream. Here is our example rtsp address.

rtsp://raspberrypi:8080/

(Image credit: Tom’s Hardware)

It will take a few seconds for VLC to catch up with the flow, please be patient. Soon you will see a video streaming from your Raspberry Pi, with a delay of about 5-10 seconds.

6. To record a flow, click on Play >> Save then specify a file name. Recording ends when the stop button is pressed.

(Image credit: Tom’s Hardware)

Be arty: broadcast with filters

The last part of this project is where we get creative. We are going to create a script that will first create an array (a list) of all the possible image effects. Next, we create a variable to store the length of the array, before randomly picking a number in the array that will control the effect used when uploading our stream. We are going to write the code on the Raspberry Pi using a GUI text editor.

1. Launch Geany (from the menu) and create a new file called random_stream.sh and remember to save often.

2. Enter the first line of code, which will tell the code where to find the Bash interpreter.

#!/bin/bash

3. Create an array to store all the possible image effects in this project. There are 20 effects in total, and each has its own place in the table, allowing our code to choose a specific effect based on a random number.

array[0]="none"
array[1]="negative"
array[2]="solarise"
array[3]="sketch"
array[4]="denoise"
array[5]="emboss"
array[6]="oilpant"
array[7]="hatch"
array[8]="gpen"
array[9]="pastel"
array[10]="watercolour"
array[11]="film"
array[12]="blur"
array[13]="saturation"
array[14]="colourswap"
array[15]="washedout"
array[16]="posterise"
array[17]="colourpoint"
array[18]="colourbalance"
array[19]="cartoon"

4. Create a variable called size to store the number of effects in the array. This variable will store the output of a command (via {}) that checks the length of the array.

size=${#array[@]}

5. Create another variable, index, which will store a random number between zero and the length of the array, in this case 20.

index=$(($RANDOM % $size))

6. Print the chosen filter on the terminal, then wait a second.

echo ${array[$index]}
sleep 1

7. Use raspivid to create an infinite flow at 800 x 600 resolution at 15 fps. The -ifx switch will be filled with an effect chosen at random from the table. To broadcast live video, this time we use a standard tcp stream. It works a bit faster than rstp but your stream might have some artifacts. The Pi listens (-l) for connections from any local IP address.

raspivid -t 0 -w 800 -h 600 -ifx ${array[$index]} -fps 15 -l -o tcp://0.0.0.0:5000

8. Save the code in the / home / pi / directory and exit the editor.

9. Open a terminal and use this command to make the code executable.

$ chmod +x random_stream.sh

ten. Run the code.

$ ./random_stream.sh

11. On your Windows / Mac / Linux computer open VLC.

12. Go to Media >> Open network feed, or press CTRL + N.

(Image credit: Tom’s Hardware)

13. Enter the tcp address and the port of your Raspberry Pi. The tcp address is the hostname of the Raspberry Pi, prefixed with tcp / h264: //, and the port set to 5000. Click Play to start the stream. Here is our example of a tcp address.

tcp/h264://raspberrypi.local:5000

(Image credit: Tom’s Hardware)

14. To record a flow, click on Play >> Save then specify a file name. Recording ends when the stop button is pressed.

(Image credit: Tom’s Hardware)

15. To change the effect, press CTRL + C in the Raspberry Pi terminal, then press the UP key and enter to run the command again, hopefully with a different filter.

Complete list of codes

#!/bin/bash
array[0]="none"
array[1]="negative"
array[2]="solarise"
array[3]="sketch"
array[4]="denoise"
array[5]="emboss"
array[6]="oilpant"
array[7]="hatch"
array[8]="gpen"
array[9]="pastel"
array[10]="watercolour"
array[11]="film"
array[12]="blur"
array[13]="saturation"
array[14]="colourswap"
array[15]="washedout"
array[16]="posterise"
array[17]="colourpoint"
array[18]="colourbalance"
array[19]="cartoon"
size=${#array[@]}
index=$(($RANDOM % $size))
echo ${array[$index]}
sleep 1
raspivid -t 0 -w 800 -h 600 -ifx ${array[$index]} -fps 15 -l -o tcp://0.0.0.0:5000


Source link

Leave A Reply

Your email address will not be published.