Beginner Tutorial: How to Stream Video from Raspberry Pi Camera to Local Computer using Python (P3)

Discover how to stream video from a USB-based camera to your local computer via the local network using Python 3 and Flask with the Picamera2 library. This tutorial builds upon Part 1, where we demonstrated the same process using a Raspberry Pi camera module. Here, we leverage PiCamera2, supported by the Raspberry Pi community, to achieve seamless streaming with your USB-based camera.
Before reading the remainder, be sure to subscribe and support the channel if you have not!
Subscribe:
Support:
Access All Code Related to This Video by Subscribing to the Level 1 Perk on My YouTube Channel

1-) Install the Required Libraries

We need to install flask, opencv, and picamera2 using the apt installer on our raspberry pi. Go into a terminal and run the following commands

sudo apt update; sudo apt install python3-opencv python3-flask python3-picamera2
If you are still having issues with packages later down the line, try using pip to install these packages.

2-) Code and Walkthrough

Now that you have the packages installed you can go ahead and create a python script on your device, name it however you like. Also be sure to have your USB camera plugged in at this point.
The code is as follows:

from flask import Flask, Response 
from picamera2 import Picamera2 import cv2 ### You can donate at https://www.buymeacoffee.com/mmshilleh app = Flask(__name__) camera = Picamera2() camera.configure(camera.create_preview_configuration(main={"format": 'XRGB8888', "size": (640, 480)})) camera.start() def generate_frames(): while True: frame = camera.capture_array() ret, buffer = cv2.imencode('.jpg', frame) frame = buffer.tobytes() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') @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)
This code is a Flask web application that streams video frames from a Raspberry Pi camera module (Picamera2) to a web page. Here's a concise explanation of what the code does:
  • It imports the necessary libraries: Flask for creating the web application, Picamera2 for interacting with the camera module, and cv2 (OpenCV) for image processing.
  • It creates a Flask application instance and initializes the Picamera2 object.
  • It configures the camera settings, specifying the format and size of the preview frames.
  • The generate_frames() function continuously captures frames from the camera, encodes them as JPEG images, and yields them as byte strings with appropriate headers for streaming.
  • The /video_feed route is defined, which calls the generate_frames() function and returns a Response object with the appropriate MIME type for streaming the video frames.
  • Finally, the Flask application is run on the host '0.0.0.0' (accessible from any IP address) and port 5000.
    When this code is run on a Raspberry Pi with a camera module, it starts a web server that streams the live video feed from the camera. The video feed can be accessed by visiting the /video_feed endpoint in a web browser.
    You can access the webstream from your local computer if you go to a browser and type in the following:
    http://<Your Raspberry Pi IP>:5000/video_feed
    You can get your IP address of your Raspberry Pi by entering a terminal on the Raspberry Pi and typing the command ifconfig. You can then find the IP address in the inet section.
    Once this is done you will see a video stream in your chrome browser!

     

    Conclusion

    In this tutorial, we've explored how to stream video from a USB-based camera to your local computer using Python 3, Flask, and the Picamera2 library on a Raspberry Pi. By leveraging the power of these tools, you can easily set up a seamless video streaming solution for your projects.
    If you found this tutorial valuable and want to stay updated with more exciting content, be sure to subscribe to my YouTube channel. Your support means a lot and helps me continue creating helpful tutorials and projects. Additionally, consider supporting the channel by buying me a coffee at https://www.buymeacoffee.com/mmshilleh. Your contributions directly support the creation of more engaging and informative content.
    Don't forget to access all the code related to this video by subscribing to the Level 1 Perk on my YouTube channel. This exclusive perk gives you access to a wealth of resources that will help you take your projects to the next level.
    Thank you for following along, and I look forward to sharing more exciting tutorials with you in the future. Stay tuned and happy coding!

    Create a free account to access full content.

    All access to code and resources on ShillehTek.

    Signup Now

    Already a member? Sign In

    Explore More on Our Blog

    Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

    Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

    Discover how to set up, code, and activate the LED based on detected sound with the Raspberry Pi...

    Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

    Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

    In this tutorial, I’ll guide you through setting up the KY-037 sound sensor with a Raspberry Pi using...

    How to Post to Reddit Using Python

    How to Post to Reddit Using Python

    Post to reddit automatically using a Python script.

    How to Create a Time-Lapse Video with a Raspberry Pi Camera

    How to Create a Time-Lapse Video with a Raspberry Pi Camera

    Learn how to make a timelapse with your Raspberry Pi in Python.

    How to Integrate the MPU6050 with the STM32 Blue Pill

    How to Integrate the MPU6050 with the STM32 Blue Pill

    Learn how to measure acceleration with the STM32 and the MPU6050 in the Arduino IDE.

    Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

    Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

    This comprehensive tutorial will guide you through the process of setting up and programming the STM32 Blue Pill...

    Automate Task Scheduling in AWS with Lambda, Step Functions, and CloudWatch

    Automate Task Scheduling in AWS with Lambda, Step Functions, and CloudWatch

    In this tutorial, I'll show you how to automatically schedule tasks in AWS at regular intervals using AWS...

    Implementing Google reCAPTCHA in a Simple React and Node.js App

    Implementing Google reCAPTCHA in a Simple React and Node.js App

    Learn how to protect your React applications from bots and spam with Google reCAPTCHA integration! This step-by-step tutorial...

    AWS Lambda Tutorial: Using Selenium with Chromedriver in Python

    AWS Lambda Tutorial: Using Selenium with Chromedriver in Python

    In this tutorial, I will guide you through the process of running Selenium with ChromeDriver inside an AWS...

    How to Connect MLX90614 Infrared Thermometer to Raspberry Pi Pico W: MicroPython Tutorial!

    How to Connect MLX90614 Infrared Thermometer to Raspberry Pi Pico W: MicroPython Tutorial!

    Learn how to use the MLX90614 with the Raspberry Pi Pico W and get infrared values in MicroPython.

    Back to blog

    Leave a comment

    Please note, comments need to be approved before they are published.