Ask Your Question

Finding OpenCV Paths in ROS Builds

asked 2021-01-11 21:41:22 -0500

David Lu gravatar image

Short version: How do I find the path to haarcascade_frontalface_alt.xml provided by opencv?

Long version: I'm working on getting the face_detectorpackage to work in noetic. In the past, the path to the classifier has been hardcoded, but in noetic, since the build has to work on Ubuntu and Debian, the hardcode isn't cutting it. My failing PR is here:

I feel like there should be some environment variable like $OPENCV_SHARED_DATA_DIR but I can't find a good resource on what that would be. How can I configure my launch/param/source code to find this file on both Ubuntu and Debian?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2021-01-12 10:29:06 -0500

sloretz gravatar image

The Debian package libopencv-dev installs a cmake project config file. It sets CMake variables after find(OpenCV) is called.

$ dpkg -L libopencv-dev | grep cmake

Have a look inside the main one

$ vim /usr/lib/x86_64-linux-gnu/cmake/opencv4/OpenCVConfig.cmake

A CMake variable that looks interesting is OpenCV_INSTALL_PATH.

#    Advanced variables:
#      - OpenCV_SHARED                   : Use OpenCV as shared library
#      - OpenCV_INSTALL_PATH             : OpenCV location
#      - OpenCV_LIB_COMPONENTS           : Present OpenCV modules list
#      - OpenCV_USE_MANGLED_PATHS        : Mangled OpenCV path flag

On Ubuntu Focal with OpenCV4 it resolves to /usr/, so one might think the path to the classifier is


However, Debian Buster with OpenCV 3.2 it's a different path


It looks like just the directory in share is different. That should be solvable with file(GLOB ...)

file(GLOB haarcascade_file_path "${OpenCV_INSTALL_PATH}/share/*/haarcascades/haarcascade_frontalface_alt.xml")

Then you can use CMake configure_file to generate classifier.yaml from a new file

 classifier_filename: ${haarcascade_file_path}

and the CMake

configure_file( classifier.yaml)

The generated file is in CMAKE_CURRENT_BINARY_DIR, so you'll need to install it to the param folder from the build folder

install(FILES "${CMAKE_CURRENT_BINARY_DIR}/classifier.yaml"
edit flag offensive delete link more


Thanks for this detailed thoughtful answer!

David Lu gravatar image David Lu  ( 2021-01-16 14:55:36 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2021-01-11 21:41:22 -0500

Seen: 203 times

Last updated: Jan 12