Copyright (C) 2022, Axis Communications AB, Lund, Sweden. All Rights Reserved.
This guide explains how to build OpenCV from source and bundle it for use in an ACAP application. The example application runs a background subtraction operation on video from the camera for a very simple motion detection application to demonstrate how to integrate the broad functionality of OpenCV.
building-opencv
├── app
│ ├── example.cpp - The application running OpenCV code
│ ├── imgprovider.cpp - Convenience functions for VDO
│ ├── imgprovider.h - imgprovider headers
│ ├── LICENSE
│ ├── Makefile - The Makefile specifying how the ACAP should be built
│ └── manifest.json - A file specifying execution-related options for the ACAP
├── Dockerfile - Specification of the container used to build the ACAP
├── README.md
└── sources.list - Text file specifying repositories for armhf packages
Note
Depending on the network your local build machine is connected to, you may need to add proxy settings for Docker. See Proxy in build time.
-
Standing in your working directory run the following commands:
On armv7hf architecture
docker build --tag <APP_IMAGE> .
On aarch64 architecture
docker build --tag <APP_IMAGE> --build-arg ARCH=aarch64 .
<APP_IMAGE> is the name to tag the image with, e.g., opencv-app:1.0
Copy the result from the container image to a local directory build:
docker cp $(docker create <APP_IMAGE>):/opt/app ./build
-
You should now have a
build
directory. In it is the.eap
file that is your application. Enable theAllow unsigned apps
toggle and upload the application to your camera. -
Start the application. In the
App log
, a printout from the application should be seen. The same log with continuous scroll can be seen by SSHing to the camera and runningjournalctl -f
. The printout shows whether the application has detected movement in the image or not:opencv_app[0]: starting opencv_app opencv_app[2211]: Running OpenCV example with VDO as video source opencv_app[2211]: Creating VDO image provider and creating stream 1024 x 576 opencv_app[2211]: Dump of vdo stream settings map ===== opencv_app[2211]: chooseStreamResolution: We select stream w/h=1024 x 576 based on VDO channel info. opencv_app[2211]: Start fetching video frames from VDO opencv_app[2211]: Motion detected: YES opencv_app[2211]: Motion detected: YES
The Dockerfile suits as a good overview of the build process;
- First the OpenCV libraries are built with the help of CMake and the ACAP SDK
libraries, especially
libc
andlibstdc++
. - The OpenCV libraries are then copied to the application directory under
lib
. - Finally the ACAP application is built with the build instructions in the Makefile where it links to and bundles the OpenCV libraries to the application.
OpenCV libraries are built with CMake and some special steps are made to get a correct build:
- The ACAP SDK is sourced to get cross compilation variables like
CC
andCXX
which contains the path to the SDK libraries. - To get correct cross compilation settings,
CMAKE_TOOLCHAIN_FILE
is set to the architecture specific file provided by OpenCV. - CMake picks up environment variables like
CC
andCXX
but seems to not work in combination withCMAKE_TOOLCHAIN_FILE
. To get CMake to pick up the cross compiler and SDK library path, these variables are split and set explicitly inCMAKE_{C,CXX}_COMPILER
andCMAKE_{C,CXX}_FLAGS
respectively.
Other noteworthy options are the ones related to NEON
and VFPV3
, which are
optimizations available for the platform that can greatly speed up CPU
operations. This is only necessary for armv7hf, in aarch64 these options are
implicitly present.
The other configuration options are there to make the OpenCV installation quite stripped of functionality not needed for the example application. However, you will likely have to change these options to accommodate your custom application.
The build instructions of the ACAP application are found in the
Makefile. Note the use of the option
-Wl,--no-as-needed,-rpath,'$$ORIGIN/lib'
which will set the runtime share
library search path and is where the bundled libraries will be found when the
application is installed on a device.
The example application source code using OpenCV can be seen in example.cpp. It is a C++ application which uses the OpenCV MOG2 background subtraction and noise filtering to detect changes in the image in order to perform motion detection.
The code is documented to give a clear understanding of what steps are needed to grab frames from the camera and perform operations on them.
The output of the application can be seen through the App log
or by running
journalctl -f
while connected through SSH to the device.