OpenCV on Yocto Image - cu-ecen-aeld/yocto-assignments-base GitHub Wiki

Description

This page helps you to install OpenCV and compile an application on Yocto.

Links to implementation

  1. Core-image-aesd.bb changes
  2. Sample recipe file for application

Installation of OpenCV on Yocto

Add the packages necessary for OpenCV into the core-image-aesd.bb file. The opencv, libopencv-core and libopencv-imgproc are sufficient for running most OpenCV based applications.

IMAGE_INSTALL:append = " opencv libopencv-core libopencv-imgproc"

Recipe modifications for loading an OpenCV application

The OpenCV application will vary based on your final project. An example implementation for the project at MotionSense can be found at opencv_application_code with recipe at recipe. This application is designed to accept video feed from Gstreamer and run a motion detection algorithm by finding the difference between video frames. The application also pushes a messsage using MQTT to a Mosquitto Broker.

The steps below outline the changes to build your custom Yocto OpenCV application.

  1. Yocto requires package config within the recipe to be able to successfully compile the OpenCV application. Add this using the inherit, DEPENDS and RDEPENDS_${PN} variables. Include any other RDEPENDS libraries as you seem fit based on your application.
inherit pkgconfig
DEPENDS = "pkgconfig opencv"
RDEPENDS_${PN} = "libopencv-core libopencv-imgproc libopencv-highgui libopencv-videoio libopencv-imgcodecs libopencv-gapi"
  1. Add the Target LDFLAGS required for the compilation of your program into the recipe.
TARGET_LDFLAGS += "-pthread -lrt -L/usr/lib -lopencv_core -lopencv_flann -lopencv_video -lrt -lstdc++fs -lstdc++ -lopencv_gapi"
  1. Modify do_compile section of the recipe to use pkg config to add the required libraries for compilation.
do_compile () {
	${CXX} ${CXXFLAGS} ${LDFLAGS} ${S}/aesd_opencv_file.cpp `pkg-config --cflags opencv4` `pkg-config --libs opencv4` -o aesd_opencv_file
}

Testing the end application

The installation of OpenCV on the target for the built Yocto image was tested using print(cv2.__version__).

233870288-eb8307bc-2754-4694-aefc-8f37e15f7970

The aesd_opencv_file was tested to run on Raspberry Pi 3B+ on the built Yocto Image.

WhatsApp Image 2023-05-03 at 11 13 40 AM