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.
- 1 Equipment used
- 2 Preparation of the environment
- 3 Installing OpenCV for Python
- 4 Test program: Live display of the Orange Pi camera
- 5 Install v4l2loopback and vidcopy
- 6 Copy stream to /dev/video1 with vidcopy
- 7 Save an image to the keyboard
- 8 Conclusion
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 6 € on Aliexpress). The quality of the image is far from equal to that of the camera v2.1 8MP of the Raspberry Pi, but to begin, the value for money is unbeatable.
|Orange Pi Lite|
|Power 5V – 3A|
Or USB cable, about €2.50
|Armbian on 8Go SD Card classe 10|
|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 it, 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? This is possible without any problems. You have to choose according to your level. If you are starting out, you will 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 reported 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
Accept all questions asked.
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)
Save the script (CTRL + X then Y) and run
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 it does not 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.
# git clone https://github.com/umlaeute/v4l2loopback # cd v4l2loopback/ # make && make install
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 required 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
Place yourself in the vidcopy directory and run the vidcopy.sh script. It takes as a parameter: Here, it remains only to launch the script vidcopy to create a new virtual camera
- -w: width of the image (1600 pixels for a 2MP camera
- -h: height of the image (max. 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 it 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 backwards 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..
All you have to do is connect the python script to 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.
Et voilà ! You can use the image of the Orange Pi camera in your Python projects.
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)
As always with the Orange Pi, there is a much better performance / price ratio than the Raspberry Pi provided that you put your hand to 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.
- Install Home Assistant (HASS) on an Orange Pi running under Armbian
- IoT development based on Orange Pi, Arduino (Firmata), Nodejs, Blynk and Johnny-Five
- Blynk + Node.js + Johnny-Five: drive a Pan-Tilt PTZ SG90 kit on Orange Pi with an Arduino / Firmata
- Start programming with Node Js and Johnny-Five: IoT and robotics based on Arduino, Raspberry Pi, Orange Pi
- Orange Pi: test of the OPI.GPIO package for Node-Red (node-red-contrib-opi-gpio)
- Orange Pi (Armbian): replace the GPIO by an Arduino/Firmata, Node-RED and Johnny-Five