Last mod: 2025.01.24

Raspberry Pi Zero - PiCamera Stream

Raspberry Pi cameras play a crucial role in IoT solutions by enabling real-time image and video capture for various applications. They are widely used in home automation, surveillance systems, remote monitoring, and live streaming due to their affordability and compatibility with Raspberry Pi boards. Additionally, their small form factor and low power consumption make them ideal for edge computing and real-time video streaming in IoT ecosystems.

Prerequisites

The libraries used to handle Raspberry Pi cameras have changed, leading to some confusion and inconsistencies. As a result, some libraries work, while others may not function as expected. To address this, I provide a detailed description of the hardware I am using for my examples and specify the software versions to ensure clarity and reproducibility.

Hardware

  • Raspberry Pi Zero W
  • Camera ZeroCam OV5647 5MPx - fisheye 160°

Camera ZeroCam OV5647 5MPx

Software

The tutorial describes how to install packages and run applications on a clean system, only with packages existing after recording the image on the SD card.

The example was run on:

  • Raspbian GNU/Linux 12 (bookworm)
  • Python 3.11.2

lsb_release -a & python -V

Library installation

Upgrade the system and install the requires libraries:

sudo apt update & sudo apt -y upgrade
sudo apt install -y python3-picamera2 python3-opencv python3-flask

Example application

The script is written in Python and based on the OpenCV and the Flask framework. Create an stream_app.py file with the contnets:

import sys
sys.path.append('/usr/lib/python3/dist-packages')

from flask import Flask, Response
from picamera2 import Picamera2
import cv2

# Initilization PiCamera2
picam2 = Picamera2()
picam2.configure(picam2.create_video_configuration(main={"size": (640, 480)}))
picam2.start()

# Create Flask app
app = Flask(__name__)

def generate_frames():
    while True:
        # Get frame from camera
        frame = picam2.capture_array()

        # Image encoding to JPEG format
        _, buffer = cv2.imencode('.jpg', frame)
        frame = buffer.tobytes()

        # MJPEG frame streaming
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

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

@app.route('/')
def index():
    return "<img src='/video_stream'>"

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

Run app:

python stream_app.py

We should see a log similar to:

python stream_app.py

Next, we can open web browser with adress http://RASPBERRY_PI_IP:5000/ and we should see streaming visdeo in browser:

http://RASPBERRY_PI_IP:5000/

Sources

https://gitlab.com/dziak.tech/examples/-/tree/main/IoT/RaspberryPiZero_PiCamera

Links

https://www.raspberrypi.com/products/raspberry-pi-zero-w/
https://botland.store/raspberry-pi-cameras/12747-camera-zerocam-ov5647-5mpx-fisheye-160-for-raspberry-pi-zero-5904422319977.html
https://opencv.org/
https://flask.palletsprojects.com/