Click the card below to follow “Computer Vision Home” for the latest insights on visual/image processing!
Get the latest updates on visual/image processing delivered to you!
Creating products involves not only software but also hardware, which plays a crucial role. Specifically, in computer vision, we need compact edge hardware to deploy our object detection models or algorithms. Edge devices are computing hardware located at the edge of the network, close to the data source and end users. These devices reduce latency and bandwidth usage by processing data locally (at the “edge”), enabling real-time responses in a compact form factor. The Raspberry Pi is one of the most widely used edge devices, and OpenCV is the most popular computer vision library. So, let’s see how to use Raspberry Pi and OpenCV to deploy applications locally at the edge, making it accessible to everyone.
Introduction to Raspberry Pi
The Raspberry Pi is a credit card-sized computer capable of performing various computing tasks typically handled by desktops and servers. It runs on an ARM-based processor and supports multiple operating systems, primarily the Debian-based Linux distribution Raspberry Pi OS (formerly known as Raspbian). The Raspberry Pi features built-in GPIO (General Purpose Input/Output) pins, networking capabilities, and support for peripherals like cameras and sensors, making it an ideal choice for hobbyists, educators, and professionals seeking cost-effective computing solutions.
Before diving into the specifications of the Raspberry Pi, let’s take a look at the current global edge computing market.

The edge computing market is experiencing rapid growth as organizations adopt distributed computing. Global spending on edge computing is surging: IDC estimates that by 2032, spending by enterprises and service providers will reach $20.6 billion, growing to approximately $378 billion by 2040.
The adoption of this technology is driven by the rapid development of AR/VR, 3D, IoT, 5G, and other technologies. Therefore, understanding edge computing is not only beneficial for mastering technical knowledge but also helps you stay in sync with industry trends. Our goal is to learn how to set up the Raspberry Pi and use it to run edge applications!
Now, back to the Raspberry Pi. Since its launch in 2012, the Raspberry Pi has undergone significant changes. Over 60 million units have been sold globally, making it one of the most popular computers of all time. In this article, we will use the latest Raspberry Pi 5, which features a 2.4 GHz Cortex-A76 processor, enhanced GPU (VideoCore VII), and improved PCIe connectivity support. Additionally, it is super affordable, starting at just $50, making computing accessible to everyone.
Setting Up Your Raspberry Pi
It mainly involves two steps:
1. Install and refresh the Raspberry Pi operating system
First, you need to install the rpi-imager. There are two ways to do this:
-
Download the latest version from raspberrypi.com/software and run the installer.
-
Install it from the terminal using your package manager: sudo apt install rpi-imager.
After installing the Imager, click the Raspberry Pi Imager icon or run rpi-imager to launch the application.
Now, we need an SD card to install and run our Raspberry Pi OS. This article uses a Sandisk Ultra 64 GB card.
Now, we must select the device (Raspberry Pi 5), operating system (Raspberry Pi OS 64-bit), and storage device (our SD card). Then, there is another step:
We do not want to customize anything in the operating system. We will click “No” and continue. Finally, we answer “Yes” to the “Are you sure you want to continue?” pop-up window to start writing data to the storage device.
If you see an administrator prompt requesting permission to read and write to the storage medium, please allow the Imager to proceed.
Once the installation is complete, we will see a success message like this:
We can now boot the Raspberry Pi from the storage device!
2. Initial Configuration of Raspberry Pi OS
Now, we boot the Raspberry Pi and follow the instructions:

-
Set country and language: Choose your country, language, and time zone.
-
Change password: Set a new password for the “pi” user to enhance security.
-
Connect to the network: Select and connect to a Wi-Fi network or LAN (if available).
-
Update software: Allow the system to check and install the latest updates.
In the middle, there will be a step asking you to choose between Labwc and Wayfire. Both are lightweight window managers based on Wayland suitable for Raspberry Pi. Please select Labwc as it is the latest version.
Install Miniconda and Set Up Python Environment
First, we go to the Miniconda official download page and select the Miniconda3-latest-Linux-aarch64.sh file. The Raspberry Pi 5 uses an ARM-based processor, specifically the ARMv8 (64-bit) architecture. Aarch64 explicitly refers to the 64-bit ARM architecture, which perfectly matches the CPU of the Raspberry Pi 5.
We will start the process by downloading:
mkdir -p ~/miniconda3wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-aarch64.sh -O ~/miniconda3/miniconda.sh
Then install it:
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
Initialize the conda environment:
~/miniconda3/bin/conda init bash
rm -rf ~/miniconda3/miniconda.sh
After this, open a new terminal window, and you will see that the conda base environment has been added to the terminal.

Our Python environment is now ready, and we can proceed to install OpenCV.
Install VSCode and OpenCV
Install VSCode
The Raspberry Pi OS supports many essential applications, such as VS Code. We just need to select the Raspberry icon in the top left corner of the screen. From the drop-down menu, select “Preferences” > “Recommended Software,” and you will find the package manager.

You can see the VS Code icon here; we must select it, and it will be installed on our Raspberry Pi.
Install OpenCV
To install OpenCV on the Raspberry Pi, we first need to install the library. There are several ways to do this, but the simplest and recommended option is to use the pip package system.
Before installing any software, it is best to update the system. Run the following commands in the terminal:
sudo apt updatesudo apt upgrade
OpenCV requires us to install several dependencies first. Run the following command to install the necessary libraries:
sudo apt install build-essential cmake git libgtk-3-dev libavcodec-dev libavformat-dev libswscale-dev
Now, we will install OpenCV:
pip install opencv-python
Finally, we can run our program.
Run Example OpenCV Python Script on Raspberry Pi
Therefore, we will run this simple Python script that uses OpenCV to read an image:
import cv2import time
# Initialize video capture
capture = cv2.VideoCapture(0)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1024)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 576)
capture.set(cv2.CAP_PROP_FPS, 30) # Requesting 30 FPS from the camera
# Background subtractor
bg_subtractor = cv2.createBackgroundSubtractorMOG2()
# Current mode
mode = "normal"
# FPS calculation
prev_time = time.time()
# Video writer initialization with explicit FPS
save_fps = 15.0 # Adjust this based on your actual processing speed
fourcc = cv2.VideoWriter_fourcc(*"XVID")
out = cv2.VideoWriter("output.avi", fourcc, save_fps, (1024, 576))
while True: ret, frame = capture.read() if not ret: break
frame = cv2.flip(frame, 1) display_frame = frame.copy()
if mode == "threshold": gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) _, display_frame = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) display_frame = cv2.cvtColor(display_frame, cv2.COLOR_GRAY2BGR)
elif mode == "edge": gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray, 100, 200) display_frame = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
elif mode == "bg_sub": fg_mask = bg_subtractor.apply(frame) display_frame = cv2.bitwise_and(frame, frame, mask=fg_mask)
elif mode == "contour": gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) _, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) display_frame = cv2.drawContours(frame.copy(), contours, -1, (0, 255, 0), 2)
# Calculate actual processing FPS curr_time = time.time() processing_fps = 1 / (curr_time - prev_time) prev_time = curr_time
# Display actual processing FPS cv2.putText( display_frame, f"FPS: {int(processing_fps)} Mode: {mode}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2 )
# Write frame to video out.write(display_frame)
# Show video cv2.imshow("Live Video", display_frame)
key = cv2.waitKey(1) & 0xFF if key == ord("t"): mode = "threshold" elif key == ord("e"): mode = "edge" elif key == ord("b"): mode = "bg_sub" elif key == ord("c"): mode = "contour" elif key == ord("n"): mode = "normal" elif key == ord("q"): break
# Clean up
capture.release()
out.release()
cv2.destroyAllWindows()
In this script, we implement four different OpenCV algorithms widely used in image processing and computer vision.
-
Thresholding
-
Edge Detection
-
Background Subtraction
-
Contour Detection
Run the script:
python video.py
Before running the script, ensure that you have connected the USB camera to the Raspberry Pi.
The results you will see:



So far, you have successfully set up the Raspberry Pi, flashed the Raspberry Pi operating system onto the SD card, installed Miniconda, and created a Python environment. You have also installed VS Code and OpenCV, and then tested a Python script using the camera to perform various real-time video processing tasks such as edge detection, background subtraction, and contour tracking.
As edge computing continues to thrive, connecting the physical and digital worlds, understanding how to use devices like the Raspberry Pi will keep you in sync with the latest trends and innovations in the industry.
https://www.raspberrypi.com/documentation/computers/getting-started.html