Rosanswers logo

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_detector package 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: https://github.com/wg-perception/people/pull/88

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?

Originally posted by David Lu on ROS Answers with karma: 10932 on 2021-01-11

Post score: 0


1 Answer 1


Rosanswers logo

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 classifier.yaml.in file

 classifier_filename: ${haarcascade_file_path}

and the CMake

configure_file(classifier.yaml.in 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"

Originally posted by sloretz with karma: 3061 on 2021-01-12

This answer was ACCEPTED on the original site

Post score: 2

Original comments

Comment by David Lu on 2021-01-16:
Thanks for this detailed thoughtful answer!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.