OpenCV c++ cannot find <opencv2/face.hpp>

Viewed 550

I am trying to use opencv2/face.hpp. I am calling it in my code with this...

#include <opencv2/face.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp>
#include <opencv2/video.hpp>

All the hpp files except for face.hpp work perfectly. I compile with this on my macOS Catalina:

clang++ -std=c++11 source.cpp `pkg-config --cflags --libs opencv` -o test

and I get this error:

RPPG.cpp:10:10: fatal error: 'opencv2/face.hpp' file not found
#include <opencv2/face.hpp>

The weird thing is that I have face.hpp. When I run:

ls /usr/local/Cellar/opencv/4.5.0_4/include/opencv4//opencv2/

I see face.hpp and face/ along side all the other hpp files I am including. I installed via brew and I think this is where clang is pulling the library. Why isn't it finding face?

2 Answers

I see there are a few upvotes so posting my solution. I just had to edit my pkg-config paths.

Because I had the opencv version I wanted installed here (from brew):

/usr/local/Cellar/opencv/4.5.0_4/include/opencv4//opencv2/

I used this command:

$ export PKG_CONFIG_PATH="/usr/local/Cellar/opencv/4.5.0_5/lib/pkgconfig/

This added opencv4 to my list of libraries usable by pkg-config. When I run:

pkg-config --libs opencv

I get the same thing, BUT I now have opencv4, so when I run:

pkg-config --libs opencv4

I get what I want. face.hpp is included in this version.

It seems that you are using macOS. I would recommend you to use CMakeLists.txt like this:

cmake_minimum_required(VERSION 3.17)
project(YOUR_PROJECT_NAME)
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )

set(CMAKE_CXX_STANDARD 20)

add_executable(YOUR_PROJECT_NAME YOUR_FILES)
#for example add_executable(Folder source.cpp)
target_link_libraries(YOUR_PROJECT_NAME ${OpenCV_LIBS} )

Be sure, that you have already installed openCV with brew (brew install opencv). To run your program after that your should:

  • Create a folder in the downloaded folder project. E.g: mkdir build
  • Execute cmake command in the build folder: cd build && cmake ..
  • Execute make command to get a bin.
  • Execute ./{NAME} to run.

Clang is default compiler in macOS. You should modify your CMakeLists.txt if you want to use gcc for example.

Related