Home | Difficulty level | Advanced | Orange Pi (Armbian): control the camera in Python with OpenCV

Orange Pi (Armbian): control the camera in Python with OpenCV

In the previous tutorial, we saw how to drive (display the live image, record a snapshot and a video clip using the keyboard) on a Raspberry Pi under Raspbian. In this tutorial we will do the same but on an Orange Pi Lite (WiFi version). It is unfortunately not possible to use the Picamera library which was designed to work exclusively for Raspberry Pi, so we will have to use OpenCV which is an extremely powerful Open Source library.

Equipment used

As we have seen in this article, the CSI connector of Orange Pi is different from that of Raspberry Pi. I got the 2MP camera sold by Orange Pi (about 7€). The quality of the image is far from equal to that of the camera v2.1 8MP Raspberry Pi, but to begin, the value for money is unbeatable.

orange pi lite h3 computer board Orange Pi Lite

€12€ (shipping cost, about €3.50 )

orange pi alimentation 5v 3a jack Power 5V – 3A

Or USB cable, about €2.50€

carte sd Armbian on 8Go SD Card classe 10

From €6.90

camera orange pi 2MP csi Orange Pi 2MP camera module

About €7€ (free shipping)

Preparation of the environment

Let’s start by preparing the environment.

Armbian System Update

Unless you’ve just done this, it’s best to always start with a small system update

Installing python 2.7

Once the system is up to date, you can install Python 2.7. Why not Python 3 you say? It is possible without any problem. You have to choose according to your level. If you’re starting out, you’ll find many more examples on Python 2.7.

We take this opportunity to install the package manager for python (pip). Indeed, all deposits may not be declared in the sources of the Armbian distribution. Run the following command to install the pip command:

Installing OpenCV for Python

There are many tutorials on the internet that explain how to install OpenCV (on Raspberry Pi or Orange Pi) in a virtual environment from the source code. It is a solution, but to have tested it is long (a big half-day compilation on my Orange Pi Lite) and especially the use of the virtual environment is tedious, especially if you start. Here, we will follow the shortest path, we will simply install the library with this simple command

Test program: Live display of the Orange Pi camera

Put yourself in the directory of your choice and open a new document, for example testcam_orangepi.py. Paste the code below.

What does this code do?

We call the OpenCV 2 library (import cv2)

An object c is created which captures the video stream sent by the camera (0), that is to say that which is connected to /dev/video0 .

The camera image is displayed until the Escape key is pressed

The video stream (c.read) is read and stored in the variable f ( _, f = c.read () ).

Then the video stream is displayed using the imshow function which takes as parameter:

  • The title of the window in which the video stream will be broadcast
  • The variable that contains the video stream

First test

Save the script (CTRL + X then Y) and run

Oh, it does not work!

Why does not it work?

First, we know that the cv2 library (OpenCV) works. This is already good news, we will not be compelled to compile everything from sources! If we googled the error ( libv4l2: error setting pixformat: Operation not permitted ), we quickly find this article of the designer of the script vidcopy which allows to recopy the video stream produced by the v4l2loopback library.

Install v4l2loopback and vidcopy

We’ll just follow Leonardo Lontra’s tutorial.

Installing v4l2loopback

Installation of vidcopy

Vidcopy is a script that will create a virtual video output ( /dev/video1 ) in which the video stream of the physical camera ( /dev/video0 ) will be copied.

Loading necessary modules

Now, to make the changes permanent, run the following commands. They will add the corresponding rows to the modules in the /etc/modules  configuration file. The modules will be automatically loaded at the next startup.

Copy stream to / dev / video1 with vidcopy

So, you just have to launch the vidcopy script to create a new virtual camera

Put yourself in the vidcopy directory and run the vidcopy.sh script. It takes as parameter:

  • -w: width of the image (1600 pixels for a 2MP camera
  • -h: height of the image (maximum 1200 pixels for a 2MP camera)
  • -r: number of frames per second. For example 20 (maximum 30)
  • -i /dev/video0: Input video stream. Video0 for the camera connected to the CSI connector of the Orange Pi
  • -o /dev/video1: output of the video stream
  • -f: output format. The following formats have been successfully tested: UYVY/YV12/YU12/NV12/NV21 by the author of the script

For the 2MP camera of Orange PI this gives

The script remains open in the terminal. It is stopped with the combination CTRL + C. I do not have enough recoil on the script yet, but asking for an 800 x 600 image, the output switches to 640 x 480.

It only remains to connect the python script on this new video stream. Open the python script and replace 0 by 1 in the VideoCapture function like this c = cv2.VideoCapture (1). Save the script.

There you go ! You can use the image of the Orange Pi camera in your Python projects.

 orange pi lite camera 2MP opencv python live preview
Click to enlarge

Save an image to the keyboard

Last little thing to finish this tutorial, trigger the recording of a picture on the keyboard. Add a variable pics_taken = 0  to the beginning of the code. Then retrieve the code of the key with which you want to record an image. Here it is 1048586 (Return). Add this test and change the destination path of the images. It is done.


As always with the Orange Pi, you have a much better performance/price ratio than the Raspberry Pi if you put your hand on the keyboard ;-). On the performance side, the Orange Pi Lite with its SoC quad-core and its 512MB Ram is perfectly able to support Armbian’s XFCE desktop, Open CV and Python. Remains to be tested on a more complex project, for example facial recognition or motion detection.

  • Greg Miller

    In Leonardo Lontra’s tutorial you linked, he says you need the kernel source. Can you add a step on how to do that? Following the steps you have I get a ‘/lib/modules/…/build: no such file or directory’ error when attempting to build v4l2loopback.

    • Greg Miller

      To answer my own question: I had to do a reboot. The “apt-get upgrade” actually upgraded the kernel, but “uname -r” still returned the old kernel name.

      Attempting to run the scrip above returns the error “Window system doesn’t support OpenGL”. But I was able to use the script below to capture and save an image, so it’s working!

      import cv2
      c = cv2.VideoCapture(1)

      _,f = c.read()

      • Greg Miller

        OpenGL error is resolved by:
        apt-get install libgl1-mesa-dri

        Then another reboot.

  • Pavel Rastopchin

    Hi, I followed the manual but have been stuck on the last stage: When I try to copy the vidoe stream, I get this:

    //Here I get all the same… and then:
    Driver: “v4l2 loopback”
    Card: “Dummy video device (0x0000)”
    Bus: “platform:v4l2loopback-000”
    Version: 1.0
    Capabilities: 85000003
    FMT : CE Desc
    Setting Pixel Format: Device or resource busy

  • techniche

    when I modprobe v4l2loopback, it says the module does not exist; I have followed the instructions given by you to compile and deploy the module; please help;

    • Hello Techniche. Did you get an error message while compiling and installing v4l2loopback (command make && make install)?