Skip to content
Buy 10+ on select items — save 10% auto-applied
Free US shipping on orders $35+
Order by 3pm ET — ships same-day from the US
Skip to main content

Raspberry Pi Camera: Stream Video with Flask | ShillehTek

January 24, 2024 1924 views

Raspberry Pi Camera: Stream Video with Flask | ShillehTek
Project

Build a Raspberry Pi 4B camera stream using Flask and PiCamera so you can view live video in a browser on your local network with ShillehTek.

20 min Beginner

Video Tutorial (Optional)

Watch first if you want to follow along and see the Raspberry Pi Camera stream running from a Flask app.

Project Overview

Raspberry Pi 4B + Raspberry Pi Camera: In this project you set up a Flask app that streams a live camera feed so you can view the Raspberry Pi Camera video from another device on your local network.

By the end, you will open a browser to a URL like http://<your-pi's-IP-address>:5000/video_feed and see a real-time video stream (useful as a basic local-network security camera).

  • Time: 20 to 45 minutes
  • Skill level: Beginner
  • What you will build: A Flask endpoint that serves a live MJPEG stream from the Pi Camera over your LAN

Parts List

From ShillehTek

External

  • Raspberry Pi (the tutorial example uses a Raspberry Pi 4B)
  • Raspberry Pi Camera module (connected to the Pi)
  • Power supply for the Raspberry Pi
  • A computer or phone on the same local network to view the stream in a web browser
  • Software: Python 3, Flask, PiCamera

Note: You will need the Raspberry Pi's IP address on your network so you can open the video stream URL from another device.

Step-by-Step Guide

Step 1 - Update your Raspberry Pi package lists

Goal: Make sure your Raspberry Pi is up to date before installing dependencies.

What to do: In a terminal on the Raspberry Pi, run:

Code:

sudo apt update

Expected result: The package list update completes without errors.

Step 2 - Enable the Raspberry Pi camera interface

Goal: Enable the camera interface so the Pi can access the connected camera module.

What to do: Open the Raspberry Pi configuration tool:

Code:

sudo raspi-config

Navigate to Interface Options, select Camera, and choose <Yes> to enable it. Restart the Raspberry Pi when prompted so the change takes effect.

Expected result: The camera interface is enabled after reboot.

Step 3 - Install Flask and PiCamera (and pip3 if needed)

Goal: Install the Python libraries needed for the streaming web server.

What to do: Install Flask and PiCamera with pip3:

Code:

pip3 install flask picamera

If you do not have pip3 installed, install it first:

Code:

sudo apt install python3-pip

Expected result: Flask and picamera install successfully via pip3.

Step 4 - Find your Raspberry Pi IP address

Goal: Identify the IP address you will use to open the video stream from another device on your local network.

What to do: Run:

Code:

ifconfig

Note the IP address shown under wlan0 (wireless) or eth0 (wired).

Expected result: You have written down the Raspberry Pi IP address to use in the stream URL.

Step 5 - Create the Flask streaming script

Goal: Create a Python script that starts a web server and streams JPEG frames from the Pi camera as an MJPEG feed.

What to do: Create a Python script (name it whatever you like) and paste in the code below.

Code:

import io
import picamera
from flask import Flask, Response

### You can donate at https://www.buymeacoffee.com/mmshilleh if I saved you time
### Subscribe https://www.youtube.com/@mmshilleh/videos

app = Flask(__name__)

def generate_frames():
    with picamera.PiCamera() as camera:
        camera.resolution = (640, 480)
        camera.framerate = 24
        stream = io.BytesIO()

        for _ in camera.capture_continuous(stream, 'jpeg', use_video_port=True):
            stream.seek(0)
            yield b'--frame\r\nContent-Type: image/jpeg\r\n\r\n' + stream.read() + b'\r\n'
            stream.seek(0)
            stream.truncate()

@app.route('/video_feed')
def video_feed():
    return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, threaded=True)

Expected result: You have a saved Python file that defines a /video_feed route and binds the Flask server to 0.0.0.0:5000.

Step 6 - View the stream from another device on your LAN

Goal: Confirm the stream is reachable from other devices on the same local network.

What to do: Run the script on your Raspberry Pi, then open this URL in a browser on another device:

http://<your-pi's-IP-address>:5000/video_feed

Expected result: The browser displays a live video stream from the Raspberry Pi Camera in real time.

Conclusion

You set up a Raspberry Pi (example: Raspberry Pi 4B) with a Raspberry Pi Camera and a Flask app that streams live video to a /video_feed URL on your local network. This gives you a simple real-time camera feed you can view from any device on the same LAN.

Want the parts and build accessories for projects like this? Grab what you need from ShillehTek.com. If you want help customizing this streaming setup or building an IoT project for your product, check out our IoT consulting services.