ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Finding OpenCV Paths in ROS Builds

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

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: https://github.com/wg-perception/peop...

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
2

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

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
/usr/lib/x86_64-linux-gnu/cmake
/usr/lib/x86_64-linux-gnu/cmake/opencv4
/usr/lib/x86_64-linux-gnu/cmake/opencv4/OpenCVConfig-version.cmake
/usr/lib/x86_64-linux-gnu/cmake/opencv4/OpenCVConfig.cmake
/usr/lib/x86_64-linux-gnu/cmake/opencv4/OpenCVModules-release.cmake
/usr/lib/x86_64-linux-gnu/cmake/opencv4/OpenCVModules.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

${OpenCV_INSTALL_PATH}/share/opencv4/haarcascades/haarcascade_frontalface_alt.xml

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

/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml

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"
        DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/param
)
edit flag offensive delete link more

Comments

Thanks for this detailed thoughtful answer!

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

Question Tools

2 followers

Stats

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

Seen: 892 times

Last updated: Jan 12 '21