Skip to content

OpenCV 4.X for NanoPi NEO Air #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 77 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,101 +1,121 @@
## Installing OpenCV 3.4 on FriendlyCore
## Installing OpenCV 4.2.0 on FriendlyCore/FriendlyDesktop
The easiest way to install it is to run FriendlyELEC's script.
Here are the packages and utilities your system will have after you follow the instructions in this tutorial:
* Qt 5.10.0 version of the HighGUI module (Better 2D window interface with zoom, image saving capabilities, etc)
* C++ interface and examples
* C interface and examples
* Python 3.5+ interface and examples
* Python 3.6+ interface and examples

## Currently supported boards
* RK3399
NanoPC T4
* S5P6818
NanoPi Fire3
NanoPi M3
NanoPC T3
NanoPC T3 Plus
* S5P4418
NanoPi Fire2A
NanoPi M2A
NanoPi S2
NanoPC T2
Smart4418

NanoPC M4
NanoPC NEO4
Som-RK3399

***If you are using the s5p4418/s5p6818 development board, please checkout to the "s5pxx18" branch.***

## Installation
***Note: FriendlyCore-20180810 required.
Please download the latest FriendlyCore Image file from the following URL: http://download.friendlyarm.com***

Run the commands below:
***Note: OpenCV has been pre-installed in FriendlyCore/FriendlyDesktop (Version after 201905) and does not require manual installation.
Please download the latest FriendlyCore/FriendlyDesktop Image file from the following URL: http://download.friendlyarm.com***
To make it easy to test python3 examples, you may copy cv-env.sh to the system directory,run the commands below:
```
git clone https://github.com/friendlyarm/install-opencv-on-friendlycore
cd install-opencv-on-friendlycore
./install-opencv.sh
cp examples/cv-env.sh /usr/bin/
```
if you want to use pkg-config, append the following two lines in the "~/.bashrc" file and save it:
```
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
cp examples/py/cv-env.sh /usr/bin/
```


## Test out the OpenCV3.4 and Python3 install
## Test out the OpenCV 4.2.0 and Python3 install
Run the commands below:
```
cd examples/py/
cd ~/install-opencv-on-friendlycore/examples/py/
. cv-env.sh
python ver.py
```
Will display:
```
4.2.0
```
it will activate a virtualenv, if you want to switch projects or otherwise leave your virtualenv, simply run:
```
deactivate
```

## Build some samples included in OpenCV
### c++ sample: facedetect
## Test python sample included in OpenCV 4.2.0: turing

![image](https://github.com/friendlyarm/install-opencv-on-friendlycore/raw/master/examples/images/lena2-300x300.png)
![image](https://github.com/friendlyarm/install-opencv-on-friendlycore/raw/master/examples/images/python-turing.png)

```
cd /usr/local/share/OpenCV/samples/cpp
g++ -ggdb facedetect.cpp -o facedetect `pkg-config --cflags --libs /usr/local/lib/pkgconfig/opencv.pc`
. ~/install-opencv-on-friendlycore/cv-env.sh
. setqt5env
./facedetect --cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml" --nested-cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_eye.xml" --scale=1.3 /usr/local/share/OpenCV/samples/data/data/lena.jpg
cd /usr/local/share/opencv4/samples/python
python turing.py
```
### python sample: turing

![image](https://github.com/friendlyarm/install-opencv-on-friendlycore/raw/master/examples/images/python-turing.png)
## Build Qt5 MIPI-Camera example

![image](https://github.com/friendlyarm/install-opencv-on-friendlycore/raw/rk3399/examples/images/qt5-camera.png)

***Note: To run this demo you will need a camera(ov13850/ov4689/logitech c920 pro) and a display connected.***
Run the commands below:
```
. cv-env.sh
cd ~/install-opencv-on-friendlycore/examples/qt5/mipi-camera-videoprocessor/
qmake-qt5 .
make -j4
```
Run on FriendlyDesktop:
```
su pi
. setqt5env
cd /usr/local/share/OpenCV/samples/python
python turing.py
export DISPLAY=:0.0
./mipi-camera-videoprocessor
```
Run on FriendlyCore:
```
./run-on-friendlycore.sh
```
## Python: Stereo match example

## Face Recognition Using OpenCV and Qt5
***Note: To run this demo you will need a webcam and a display connected.***
This is an implementation of facial detection in C++/Qt5:
![image](https://github.com/friendlyarm/install-opencv-on-friendlycore/raw/rk3399/examples/images/python-stereo-match.png)

Run the commands below:
```
git clone https://github.com/friendlyarm/face-recognizer-gui
cd face-recognizer-gui
mkdir build
cd build
qmake-qt5 ../
make
cd ../
. setqt5env
./opencv
cd ~/install-opencv-on-friendlycore/examples/py/single-camera-python-demo/
./run.sh
```

## Build Qt5 QML example
***Note: To run this demo you will need a webcam and a display connected.***
## Python: Test single camera example
***Note: To run this demo you will need a camera(ov13850/ov4689/logitech c920 pro) and a display connected.***
Run the commands below:
```
cd examples/qt5/CvQml/
qmake-qt5 .
cd ~/install-opencv-on-friendlycore/examples/py/single-camera-python-demo/
./run.sh
```

## Python: Test dual camera example
***Note: To run this demo you will need two cameras(ov13850/ov4689/logitech c920 pro) and a display connected.***
Run the commands below:
```
cd ~/install-opencv-on-friendlycore/examples/py/dual-camera-python-demo/
./run.sh
```

## C++: Test UVC camera example

![image](https://github.com/friendlyarm/install-opencv-on-friendlycore/raw/rk3399/examples/images/cpp-opencamera.png)

***Note: To run this demo you will need a usb camera(logitech c920 pro) and a display connected.***
Build and run the commands below:
```
git clone https://github.com/friendlyarm/rk3399-opencv-uvccamera-cpp-demo.git
cd rk3399-opencv-uvccamera-cpp-demo
mkdir build
cd build
cmake ..
make
su pi
export DISPLAY=:0.0
. setqt5env
./CvQml
./opencamera
```

Binary file added examples/images/cpp-opencamera.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/images/python-stereo-match.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/images/qt5-camera.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
97 changes: 97 additions & 0 deletions examples/py/dual-camera-python-demo/dual-camera.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import numpy as np
import cv2 as cv
import time
import os

#
# Supported cameras
# ----------------------------------------------------------
# MCAM400 (ov4689): https://www.friendlyarm.com/index.php?route=product/product&path=78&product_id=247
# CAM1320 (ov13850): https://www.friendlyarm.com/index.php?route=product/product&path=78&product_id=228
# Logitech C920 pro webcam
#

# selfpath
previewDevs=[]
# mainpath
pictureDevs=[]
# camera type
cameraTypes=[]

# isp1
if os.path.exists("/sys/devices/platform/ff910000.rkisp1/video4linux/v4l-subdev2"):
previewDevs.append("/dev/video1")
pictureDevs.append("/dev/video0")
cameraTypes.append("mipi")

# isp2
if os.path.exists("/sys/devices/platform/ff920000.rkisp1/video4linux/v4l-subdev1") or os.path.exists("/sys/devices/platform/ff920000.rkisp1/video4linux/v4l-subdev5"):
previewDevs.append("/dev/video6")
pictureDevs.append("/dev/video5")
cameraTypes.append("mipi")

# usb camera 1
filename="/sys/class/video4linux/video10/name"
if os.path.isfile(filename):
file = open(filename,mode='r')
filetxt = file.read().lower()
file.close()
if "camera" in filetxt or "uvc" in filetxt or "webcam" in filetxt:
previewDevs.append("/dev/video10")
pictureDevs.append("/dev/video10")
cameraTypes.append("usb")

# usb camera 2
filename="/sys/class/video4linux/video12/name"
if os.path.isfile(filename):
file = open(filename,mode='r')
filetxt = file.read().lower()
file.close()
if "camera" in filetxt or "uvc" in filetxt or "webcam" in filetxt:
previewDevs.append("/dev/video12")
pictureDevs.append("/dev/video12")
cameraTypes.append("usb")

cam_width=800
cam_height=448
def get_camerasrc(index):
if cameraTypes[index] == "mipi":
return 'rkisp device='+previewDevs[index]+' io-mode=4 ! video/x-raw,format=NV12,width='+str(cam_width)+',height='+str(cam_height)+',framerate=30/1 ! videoconvert ! appsink'
if cameraTypes[index] == "usb":
return 'v4l2src device='+previewDevs[index]+' io-mode=4 ! videoconvert ! video/x-raw,format=NV12,width='+str(cam_width)+',height='+str(cam_height)+',framerate=30/1 ! videoconvert ! appsink'

if len(previewDevs) < 2:
print("Please connect two cameras.")
os._exit(1)

cv.namedWindow("left")
cv.namedWindow("right")
cv.moveWindow("left", 40, 80)
cv.moveWindow("right", cam_width+40, 80)

cap_left = cv.VideoCapture(get_camerasrc(0), cv.CAP_GSTREAMER)
cap_right = cv.VideoCapture(get_camerasrc(1), cv.CAP_GSTREAMER)

if not cap_left.isOpened():
print("Cannot capture from camera1. Exiting.")
os._exit(1)

if not cap_right.isOpened():
print("Cannot capture from camera2. Exiting.")
os._exit(1)

last_time = time.time()
while(True):

ret, left_frame = cap_left.read()
cv.imshow('left', left_frame)

ret, right_frame = cap_right.read()
cv.imshow('right', right_frame)

if cv.waitKey(1) & 0xFF == ord('q'):
break

cap_left.release()
cap_right.release()
cv.destroyAllWindows()
11 changes: 11 additions & 0 deletions examples/py/dual-camera-python-demo/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

export ARCHFLAGS="-arch aarch64"
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
export WORKON_HOME=~/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
workon cv
. setqt5env
DISPLAY=:0.0 python3 dual-camera.py
deactivate
75 changes: 75 additions & 0 deletions examples/py/single-camera-python-demo/camera.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import numpy as np
import cv2 as cv
import time
import os

#
# Supported cameras
# ----------------------------------------------------------
# MCAM400 (ov4689): https://www.friendlyarm.com/index.php?route=product/product&path=78&product_id=247
# CAM1320 (ov13850): https://www.friendlyarm.com/index.php?route=product/product&path=78&product_id=228
# Logitech C920 pro webcam
#

# selfpath
previewDevs=[]
# mainpath
pictureDevs=[]
# camera type
cameraTypes=[]

# isp1
if os.path.exists("/sys/devices/platform/ff910000.rkisp1/video4linux/v4l-subdev2"):
previewDevs.append("/dev/video1")
pictureDevs.append("/dev/video0")
cameraTypes.append("mipi")

# isp2
if os.path.exists("/sys/devices/platform/ff920000.rkisp1/video4linux/v4l-subdev1") or os.path.exists("/sys/devices/platform/ff920000.rkisp1/video4linux/v4l-subdev5"):
previewDevs.append("/dev/video6")
pictureDevs.append("/dev/video5")
cameraTypes.append("mipi")

# usb camera
filename="/sys/class/video4linux/video10/name"
if os.path.isfile(filename):
file = open(filename,mode='r')
filetxt = file.read().lower()
file.close()
if "camera" in filetxt or "uvc" in filetxt or "webcam" in filetxt:
previewDevs.append("/dev/video10")
pictureDevs.append("/dev/video10")
cameraTypes.append("usb")

cam_width=800
cam_height=448
def get_camerasrc(index):
if cameraTypes[index] == "mipi":
return 'rkisp device='+previewDevs[index]+' io-mode=4 ! video/x-raw,format=NV12,width='+str(cam_width)+',height='+str(cam_height)+',framerate=30/1 ! videoconvert ! appsink'
if cameraTypes[index] == "usb":
return 'v4l2src device='+previewDevs[index]+' io-mode=4 ! videoconvert ! video/x-raw,format=NV12,width='+str(cam_width)+',height='+str(cam_height)+',framerate=30/1 ! videoconvert ! appsink'

if len(previewDevs) == 0:
print("Please connect a camera.")
os._exit(1)

cap = cv.VideoCapture(get_camerasrc(0), cv.CAP_GSTREAMER)

cv.namedWindow("left")
cv.moveWindow("left", 40, 80)

if not cap.isOpened():
print("Cannot capture from camera. Exiting.")
os._exit(1)
last_time = time.time()

while(True):

ret, frame = cap.read()
cv.imshow('left', frame)

if cv.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
cv.destroyAllWindows()
11 changes: 11 additions & 0 deletions examples/py/single-camera-python-demo/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

export ARCHFLAGS="-arch aarch64"
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
export WORKON_HOME=~/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
workon cv
. setqt5env
DISPLAY=:0.0 python3 camera.py
deactivate
Binary file added examples/py/stereo-match-python-demo/aloeL.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/py/stereo-match-python-demo/aloeR.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading