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

sudo apt-get update
sudo apt-get upgrade

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.

sudo apt-get install python-dev

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:

sudo apt-get install python-pip

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

sudo apt-get install python-opencv

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.

import cv2
c = cv2.VideoCapture(0)

while(1):
  _,f = c.read()
  cv2.imshow('Camera Orange Pi',f)
  k = cv2.waitKey(5)
  if k==1048603:
      #Esc key to stop, or 27 depending your keyboard
      #Touche ESC appuyee. le code peut dependre du clavier. Normalement 27
      break
  elif k==-1:
      continue
  #uncomment to know the code of of the key pressed
  #Decommenter pour connaitre le code de la touche pressee 
  #else:
      #print k 

cv2.destroyAllWindows()

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 .

c = cv2.VideoCapture(0)

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
cv2.imshow('Camera Orange Pi',f)

First test

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

python testcam_orangepi.py

Oh, it does not work!

libv4l2: error setting pixformat: Operation not permitted
HIGHGUI ERROR: libv4l unable to ioctl S_FMT
libv4l2: error setting pixformat: Operation not permitted
libv4l1: error setting pixformat: Operation not permitted
libv4l2: error setting pixformat: Operation not permitted
libv4l1: error setting pixformat: Operation not permitted
HIGHGUI ERROR: libv4l unable to ioctl VIDIOCSPICT

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /usr/local/opencv-2.4.10/modules/highgui/src/window.cpp, line 261
Traceback (most recent call last):
  File "testcv.py", line 7, in <module>
    cv2.imshow('e2',f)
cv2.error: /usr/local/opencv-2.4.10/modules/highgui/src/window.cpp:261: error: (-215) size.width>0 && size.height>0 in function imshow

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

git clone https://github.com/umlaeute/v4l2loopback
cd v4l2loopback/
make && make install

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.

apt-get install libv4l-dev
git clone https://github.com/lhelontra/vidcopy
cd vidcopy/
gcc vidcopy.c -o vidcopy

Loading necessary modules

modprobe gc2035
modprobe vfe_v4l2
sleep 5
modprobe v4l2loopback

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.

echo "v4l2loopback" | sudo tee -a /etc/modules
echo "gc2035" | sudo tee -a /etc/modules
echo "vfe_v4l2" | sudo tee -a /etc/modules

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

./vidcopy -w 800 -h 600 -r 30 -i /dev/video0 -o /dev/video1 -f UYVY

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.

Required width: 800
Required height: 600
Required FPS: 30
input device: /dev/video0
output device: /dev/video1
Driver Caps:
  Driver: "sunxi-vfe"
  Card: "sunxi-vfe"
  Bus: "sunxi_vfe sunxi_vfe.0"
  Version: 1.0
  Capabilities: 05000001
  FMT : CE Desc
--------------------
  422P:    planar YUV 422
  YU12:    planar YUV 420
  YV12:    planar YVU 420
  NV16:    planar YUV 422 UV combined
  NV12:    planar YUV 420 UV combined
  NV61:    planar YUV 422 VU combined
  NV21:    planar YUV 420 VU combined
  HM12:    MB YUV420
  YUYV:    YUV422 YUYV
  YVYU:    YUV422 YVYU
  UYVY:    YUV422 UYVY
  VYUY:    YUV422 VYUY
  BA81:    RAW Bayer BGGR 8bit
  GBRG:    RAW Bayer GBRG 8bit
  GRBG:    RAW Bayer GRBG 8bit
  GRBG:    RAW Bayer RGGB 8bit
  BG10:    RAW Bayer BGGR 10bit
  GB10:    RAW Bayer GBRG 10bit
  BA10:    RAW Bayer GRBG 10bit
  BA10:    RAW Bayer RGGB 10bit
  BG12:    RAW Bayer BGGR 12bit
  GB12:    RAW Bayer GBRG 12bit
  BA12:    RAW Bayer GRBG 12bit
  BA12:    RAW Bayer RGGB 12bit
Selected Camera Mode:
  Width: 640
  Height: 480
  PixFmt: UYVY
capturing..

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.

elif k==1048586:
      pics_taken += 1
      cv2.imwrite('/home/pi/Desktop/img_' + str(pics_taken) + ".jpg", f)

Conclusion

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.

Subscribe to the weekly newsletter

No spam and no other use will be made of your email. You can unsubscribe anytime.

  • 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()
      cv2.imwrite(‘test.png’,f)

      • 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

    • Bullfrogerwytsch

      i have the same issue, has anyone figured it out?

  • 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)?

  • Samson Tam

    after I actived the vidcopy, the video1 is success created. but I am trying to run the c++ program with opencv. it showed the below error.

    libv4l2: error getting pixformat: Invalid argument
    libv4l2: error getting pixformat: Invalid argument
    HIGHGUI ERROR: V4L: device /dev/video1: Unable to query number of channels

    please help. if there is any information needed, please lead me know too.

    • Samson Tam

      when I actives vidcopy, i have follow error messgae.

      [ 380.526657] [VFE_ERR]input index(1) > dev->dev_qty(1)-1 invalid!
      [ 380.574094] [CSI][GC2035]enable oe!
      [ 380.574436] [CSI][GC2035]V4L2_IDENT_SENSOR=2035[CSI][GC2035]disalbe oe!

      is it the reason why i failed opening video1?

DIY Projects