Existent-time streaming is a great way to interact with our surroundings. Such as a CCTV camera. But what is the technology behind information technology?

What if we could do exactly that with a Raspberry Pi – by streaming the Pi Camera'due south live feed to any client devices. And even touch on real world applications.

In this tutorial, we're going to embrace this. We're building a alive camera streaming from a Raspberry Pi to your phone (or whatsoever internet-connected device). If you recently got a Raspberry Pi and want to piece of work on a new project, this project may exist a skilful one to showtime on.

Introduction & Superlative Level Overview

By the stop of this tutorial, you'll be able to fix up a wireless stream to view the photographic camera feed from whatever device on your network. This means yous can create your own mini camera stream for any application, think Plant monitor, Smart CCTV camera (with Confront recognition), or even a portable camera.

We're covering the assembly, installing the OS, and Setting up the Python stream. So let'southward go started!

Introduction Raspberry Pi

To get a good understanding, it is always good to understand what hardware we're using. This little piece of Marvel is a Raspberry Pi. Overall, a Raspberry is a reckoner. (A low-cost computer in fact). Like your MacBook or Desktop, it features USB peripherals and other ports such as an Sound Jack, Ethernet, and HDMI ports. Being able to do what your calculator can do – the boards seem to be getting more powerful over time.

The interesting part is the form cistron; considering of its small size, this 'credit-size computer' is useful for different real-world applications and pop in the DIY community.

In this article, we'll be using the Raspberry Pi 4 (4GB) Mode. However, you can apply other models. Go on in mind; performance may vary between models.

What is streaming & How does information technology piece of work?

Flask can be seen as a back-end web server/micro-framework for Python – making it easier to build spider web frameworks using Python.

So how does information technology piece of work?

We're capturing live footage from the Raspberry Pi camera and using a Flask to create the live stream to our customer devices. (which can exist whatsoever device).
Flask is a great span between Python and HTML Web pages and supports Motion JPEG, which works by streaming a sequence of independent JPEG images. Motion JPEG is widely used in Security IP cameras and webcams alike, and this is how we're streaming our live feed.

Further, a device connected to the same network of the Pi would be able to visit the URL see the alive footage of the stream whereby the OpenCV library would be used to admission our video stream.

List of parts

What you'll need to complete this project

  • Raspberry Pi 4
  • Raspberry Pi Camera Module (Mention you lot can utilize the Pi Cam as well)
  • HDMI (Micro HDMI):
  • Micro SD Card
  • Raspberry Pi Power Supply / Power Bank (USB C Charger)
  • Ethernet Cablevision
  • (Keyboard and Mouse)
  • Optional: Pi Enclosure

Pi Cam vs. camera module – You may have heard of the latest Pi Camera Module and if you're wondering almost the Image comparison performance of the two. Hither it is, although the Pi Camera module is more than expensive, it's definitely an improvement in image quality.

For the do good of this project, we're using the cheaper Pi Module.

Building process: 4 Step Process

    1. Connect Raspberry Pi Camera Module
      • The Pi Camera module provides the feature to take pictures, record videos, and stream. While the Pi is powered off, lightly pull on the Photographic camera port latches to expose the photographic camera module port. Insert the camera ribbon cable and button the latch dorsum into position. Typically the correct orientation is with the blue part of the ribbon cable facing towards the USB ports.
      • Note: Handle the camera port clips/latches with care as they're quite frail.
      1. Connect your Pi to the Ethernet cable:

– This is optional: if you plan on using a wired connection for your application

      1. Connect your Pi to the HDMI Output – If you cull to use a Monitor, connect the micro HDMI to the port of the Tv set and the Raspberry Pi (Micro HDMI)

That's the whole installation!

Etching Os:

But before we can plough this Pi on, we'll demand to install an operating system onto the MicroSD. We'll be using the Raspbian OS.

Once the downloaded Image, we'll be flashing the Raspbian OS onto the Micro SD card using Balena Etcher. This procedure tin take up to fifteen minutes (dependent on your SD Card type). Once completed, you lot can safely eject the Micro SD Menu.

Now, Insert your Micro SD card into the Raspberry Pi and connect the Ethernet Cable (if needed). And power the Raspberry Pi 4 by plugging the USB-C cable.

Annotation: This was installed on the Raspbian OS release, August 2020.

Pi OS Setup:

Make sure your Pi has an internet connectedness either via Wireless or Ethernet.

      • Wired Connection – plug an ethernet cable from the Pi to the LAN port of the router.
      • Wireless – Yous can search for your Wi-Fi Access Point and enter the credentials to login wirelessly

Once logged in to the Pi, complete the setup installation dialog boxes and cease.

The next stride is enabling the Camera Port and VNC. The camera port would enable the camera to exist used on the Pi, and VNC would assistance admission the Pi without the demand for a display. In outcome, nosotros are accessing the Pi stream remotely without hooking the Pi upwardly to the monitor again.

Open the last window and run:

sudo raspi-config

Camera port – Select Interface Options, Camera Port, and select Enabled.

VNC Viewer – Select Interface Options, VNC, and select Enabled.

(Enabling SSH is optional to access the pi via your laptop)

Yous can now get out. This may cause your Pi to reboot.

Photographic camera Setup:

In one case you reboot you lot Pi, confirm that your Pi camera works by taking a flick from your camera module by entering this command into your terminal:

raspistill -o Desktop/prototype.jpg

The following command will take a picture and relieve information technology to the Desktop of your Pi.

You can also cheque record a piffling a video output besides:

raspivid -o Desktop/video.h264

Setup of Python Camera Stream

Now that we've confirmed that the camera module is working we can now move into installing the following dependencies and the GitHub Repo.

Upgrading your Pi:

Make sure that all your libraries and packages are up to engagement by running the beneath post-obit commands:

sudo apt-get update

sudo apt-get upgrade

Optional: Make a Virtual Environment:

It is proficient to practise to create a virtual surround for the Python3 library dependencies. The reason is that we want to install Flask in a container within the raspberry pi; if anything goes wrong, you lot don't demand to start again. However, since we're installing a couple of libraries, we'll skip on this.

Installing the Dependencies:

Depending on your Raspberry Pi you may crave installing the post-obit dependencies to create a alive stream.

sudo apt-get update  sudo apt-become upgrade  sudo apt-get install libatlas-base-dev  sudo apt-get install libjasper-dev  sudo apt-go install libqtgui4  sudo apt-get install libqt4-test  sudo apt-go install libhdf5-dev  sudo pip3 install Flask  sudo pip3 install numpy  sudo pip3 install opencv-contrib-python  sudo pip3 install imutils  sudo pip3 install opencv-python

Upon successful installation, we'll now all prepare up to download the camera stream repo to create Pi steam.

Git Clone the Camera Stream Repo:

Open terminal and clone the Camera Stream repo:

cd /home/pi git clone https://github.com/EbenKouao/pi-camera-stream-flask.git

      • Templates Binder – This is where the alphabetize.html webpage would be stored
        • The basic HTML site is what the client would view in the browser and is fully customizable.
        • Notation: is kept constantly updated by the browser with the latest stream of JPEG images
  • Python script:
    • Camera.py – The script accesses openCV and enables the camera module's output, providing the sequence of frames via Move JPEG.
    • master.py – where the Flask stream is created. The main awarding imports a photographic camera class (module).

You can start the Flask Camera Stream via with the following control:

      • sudo python3 /home/pi/pi-photographic camera-stream-flask/main.py

Where /home/pi/pi-photographic camera-stream-flask/main.py is the direct path to your Python script, alternatively, you tin admission the file direct and run the python script.

A picture containing indoor, table, monitor, desk Description automatically generated

Optional: A good thought is to make the camera stream auto-start at bootup of your pi. Yous will at present non need to re-run the script every fourth dimension you want to create the stream. You lot can do this by going editing the /etc/profile to:

      • sudo nano /etc/contour

Go to the terminate of the and add the following (from to a higher place):

      • sudo python3 /abode/pi/pi-camera-stream-flask/master.py

This would cause the following final control to auto-get-go each fourth dimension the Raspberry Pi boots up. This, in event, creates a headless setup, which would be accessed via SSH.
Note: make sure SSH is enabled.

A flat screen tv Description automatically generated

Demo of the flask implementation

You can at present access the live stream of your pi by accessing its URL on port 5000. Visit :5000 in and access this stream on any devices continued to the same Wi-Fi network as you Raspberry Pi.

Note: You tin can detect out your Pi's IP address by entering ifconfig into your terminal command. And look for the Inet Address.

Final Build, Review, and Performance

A picture containing indoor, table, sitting, wooden Description automatically generated

Final Build:

Stream Latency: The Pi stream's latency depends on your network coverage of about 1-ii seconds (in some cases ~500ms). Since we're streaming over our local network, a few factors may affect the operation of your stream such as:

      • Wi-Fi Coverage
      • Wi-Fi Performance
      • Raspberry Pi Build (why Raspberry Pi iv is recommended to make the most out of the operation)

And that's all! Now you've created your ain Raspberry Pi Camera stream. You tin take this one step further and make your ain application from making an indoor alive feed to a Portable camera. The options are down to your imagination.

Next Steps Projects:

If you would like to take this further, here are a few ideas:

      1. Raspberry Pi Smart CCTV Camera
      2. DIY 'GoPro' Portable Camera
      3. Establish Monitor

If interested on the next camera Projection, you lot can build your own Face recognition Smart CCTV photographic camera (a DIY Ring alternative). Head off to smartbuilds.io for a full tutorial into how to build one.

Conclusion

Edifice your own Pi camera stream is a smashing start into the world of live photographic camera stream projects. Using a Raspberry Pi brings down the cost significantly while retaining a expert corporeality of processing ability. This tutorial is to get you started. What live-stream applications will you make next?

References:

    • Pi Camera Stream Flask GitHub Repo – https://github.com/EbenKouao/pi-photographic camera-stream-flask
    • Install the Raspberry Pi OS Image: https://smartbuilds.io/
    • Creating your ain Smart CCTV Photographic camera: https://smartbuilds.io/
    • Video streaming with Flask – More than information on the Theory (External): https://web log.miguelgrinberg.com/post/video-streaming-with-flask