Connecting CSI Camera to Raspberry Pi with OpenCV

Click the above“Mechanical and Electronic Engineering Technology” to follow us

My requirements: Raspberry Pi 3B+, operating system Raspbian-stretch, with both Python 2.7.13 and Python 3.5.3 versions.

The CSI (Camera Serial Interface) camera is a common camera interface used in embedded systems or mobile devices. It is typically used for direct connection to processors or image sensors, enabling high-speed image data transmission.
Connecting CSI Camera to Raspberry Pi with OpenCV
CSI interface cameras have the following features:
High-Speed Transmission: CSI interface cameras use differential signal transmission, which can provide high-speed image data transmission rates suitable for real-time image acquisition and processing.
Low Power Consumption: CSI interface cameras can provide low power consumption during transmission, helping to save device power and extend battery life.
Easy Integration: CSI interface cameras can be connected to processors or chips that support the CSI protocol through standardized interfaces, facilitating system integration and development.
High-Quality Images: CSI interface cameras can provide high-quality image capture and transmission capabilities and support different image formats and resolutions.
The following are the basic steps to connect a CSI interface camera to the Raspberry Pi:
  1. Prepare the Hardware: First, ensure you have obtained a CSI interface camera module compatible with the Raspberry Pi. Common models include the official Raspberry Pi camera module and other third-party camera modules.

  2. Turn Off the Raspberry Pi Power: Before operating the Raspberry Pi, make sure to turn it off to avoid any circuit damage or accidents.

  3. Connect the Camera: The CSI interface camera is connected to the Camera interface of the Raspberry Pi. Pay attention to the direction between the camera module and the interface, ensuring correct insertion. Typical CSI interface cameras have a ribbon cable with a metal latch, which needs to be released and carefully pushed in until it reaches the bottom.

  4. Power On the Raspberry Pi: After connecting the camera, power the Raspberry Pi back on and wait for the system to boot up.

  5. Enable Camera Support: Open the Raspberry Pi terminal or log in remotely via SSH, and use the following command to enable camera support:

sudo raspi-config

In the configuration interface, select “Interfacing Options” and then select “Camera”, following the prompts to enable the camera.

Advanced Options select Expand Filesystem to extend the root directory to this SD card, making full use of the SD card’s storage space. If this step is not performed, subsequent commands may hang. Exit the settings interface and restart the Raspberry Pi.

  1. Enter the following command in the command line:This command means to open the modules file with the nano editor:

sudo nano /etc/modules

Add a line at the end of this file:

bcm2835-v4l2

7. Enter the command:

vcgencmd get_camera

Use the following command to take a picture and save it as image.jpg:

raspistill -o image.jpg
Call the camera to take a photo, naming it image.jpg, stored in the /pi/home path, which is the path displayed when the file explorer is opened in the upper left corner of the desktop. If you can see the red light on the camera and there is a photo in the directory, it further indicates that the camera configuration is correct.
8. Install OpenCV running on Python 2 on the Raspberry Pi
Open the command line interface of the Raspberry Pi and execute the following command:
sudo apt-get install libopencv-devsudo apt-get install python-opencv

After installation, enterpython or python2, and press enter

import cv2cv2.__version__

9. Install OpenCV running on Python 3 on the Raspberry Pi

Check pip and python versions

# If the pip version is too low, it may not install, need to upgrade, enter in the terminal pip install -U pip

Install the dynamic link libraries that OpenCV depends on

# Enter in the terminal sudo apt install libqt4-testsudo apt install libqtgui4

Install OpenCV

# Enter in the terminal sudo pip install opencv-python

This step may produce the following error:

THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE. If you have updated the package versions, please update the hashes. Otherwise, examine the package contents carefully; someone may have tampered with them. opencv-contrib-python-headless from https://www.piwheels.org/simple/opencv-contrib-python-headless/opencv_contrib_python_headless-3.4.3.18-cp35-cp35m-linux_armv6l.whl#sha256=ff894c0cc7c98b05b7b260a1dc462e7ad0a4220b042072fc0134a2b7a92bc4a5: Expected sha256 ff894c0cc7c98b05b7b260a1dc462e7ad0a4220b042072fc0134a2b7a92bc4a5 Got 4119d8c56d19ef044c1faca317dd10f2bb3b50cbee77426a22feca9b641c5637

Cause Analysis:

The error translates to a mismatch between the hash value and the package

It is likely that the pip source address is abroad, and the download speed is very slow in China, causing download errors due to network issues, leading to a mismatch of the hash value used.

My solution:

Change pip to a domestic source, and use the command below to install third-party packages

sudo pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple

Test

# Enter in the terminal python3
# Import import cv2 as cvprint(cv.__version__)
Summary: This method installs OpenCV on the Raspberry Pi relatively quickly, with fewer errors, especially when installing on Python 3. Previously, I tried downloading the source code and compiling it myself, which took more than 2 hours but still failed; the operation steps were identical, yet it did not succeed.
Connecting CSI Camera to Raspberry Pi with OpenCV

Want to know more

Quickly scan the code to follow

Leave a Comment