COMPILE.md
To use the library, you can do either of the following:
Set up OpenCV with 4.51+ version.
Download libfacedetection and then run powershell terminal <ins>as administrator</ins>:
cd libfacedetection
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=install -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release -DDEMO=OFF
cmake --build . --config Release
cmake --build . --config Release --target install
Dynamic library(facedetection.dll) is generated. Then, to generate static library(lib), you need to set the parameter BUILD_SHARED_LIBS to OFF with the above commands.
To deploy the facedetection libraries in a Visual Studio C++ console app, in your console application's property pages, <ins>under Release mode</ins> (because the build type is Release), add the path of your libfacedetection\build\install\include\facedetection to VC++ Directories -> Include Directories and the path of your libfacedetection\build\install\lib to VC++ Directories -> Library Directories, and add facedetection.lib to Linker -> Input -> Additional Dependencies.
Add #include "facedetectcnn.h" to your source files. See code example built with Visual Studio.
Set up OpenCV with 4.51+ version.
Same as the step 1 above with Visual Studio. libfacedetection.so is built with BUILD_SHARED_LIBS=ON and libfacedetecion.a is built with the variable set to OFF.
Set the environment variable facedetection_DIR to path\to\libfacedetection\build.
Use find_package(facedetection) in the CMakeLists of your project, or use target_link_libraries( your-program /path/to/libfacedetection/build/install/lib/libfacedetection.so).
find_package(facedetection)
if(facedetection_FOUND)
//your code
endif()
Add #include "facedetectcnn.h" to your source files to use the libraries.
libfacedetection.so is built with BUILD_SHARED_LIBS=ON and libfacedetecion.a is built with the variable set to OFF.facedetection_DIR to path\to\libfacedetection\build.target_link_libraries( your-program /path/to/libfacedetection/build/install/lib/libfacedetection.so)in the CMakelists of your project to use the shared object.#include "facedetectcnn.h" to your source files to use the libraries. See code example built with GNU on Linux/Ubuntu.Install ndk
Download and install to /home/android-ndk from https://developer.android.com/ndk/downloads
Setting environment variables
export ANDROID_NDK=/home/android-ndk
Compile
The host is Linux / Ubuntu
Build
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=install \
-DCMAKE_BUILD_TYPE=MinSizeRel \
-DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \
-DANDROID_ABI="arm64-v8a" \
-DANDROID_PLATFORM=android-18 \
-DUSE_OPENMP=OFF \
-DENABLE_NEON=ON \
-DENABLE_AVX2=OFF \
-DDEMO=OFF
cmake --build . --config MinSizeRel
Install
cmake --build . --config MinSizeRel --target install/strip
The host is Windows
Build
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=%cd%\install -G"Unix Makefiles" -DCMAKE_BUILD_TYPE=MinSizeRel -DCMAKE_TOOLCHAIN_FILE=%ANDROID_NDK%/build/cmake/android.toolchain.cmake -DCMAKE_MAKE_PROGRAM=%ANDROID_NDK%/prebuilt/windows-x86_64/bin/make.exe -DANDROID_ABI=arm64-v8a -DANDROID_ARM_NEON=ON -DANDROID_PLATFORM=android-24 -DUSE_OPENMP=OFF -DENABLE_NEON=ON -DENABLE_AVX2=OFF -DDEMO=OFF
cmake --build . --config MinSizeRel
Install
cmake --build . --config MinSizeRel --target install/strip
msys2 or cygwin
Build
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=install \
-G"Unix Makefiles" \
-DCMAKE_BUILD_TYPE=MinSizeRel \
-DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \
-DCMAKE_MAKE_PROGRAM=${ANDROID_NDK}\prebuilt\windows-x86_64\bin\make.exe \
-DANDROID_ABI=arm64-v8a \
-DANDROID_ARM_NEON=ON \
-DUSE_OPENMP=OFF \
-DENABLE_NEON=ON \
-DENABLE_AVX2=OFF \
-DDEMO=OFF
cmake --build . --config MinSizeRel
Install
cmake --build . --config MinSizeRel --target install/strip
cmake \
-DENABLE_NEON=ON \
-DCMAKE_BUILD_TYPE=RELEASE \
-DCMAKE_TOOLCHAIN_FILE=../aarch64-toolchain.cmake \
..
make
Here is an example of how to use the face detection model in C++:
#include "facedetect.h"
#include <opencv2/opencv.hpp>
#define DETECT_BUFFER_SIZE 0x20000
int main()
{
int * pResults = NULL;
unsigned char * pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE);
Mat image = imread(file_path);
/**
The function that loads the face detection model.
@param result_buffer Buffer memory for storing face detection results, whose size must be 0x20000 * bytes.
@param rgb_image_data Input image, which must be BGR (three channels) instead of RGB image.
@param width The width of the input image.
@param height The height.
@param step The step.
@return An int pointer reflecting the face detection result, see Example for detailed usage.
*/
int * pResults = facedetect_cnn(pBuffer, (unsigned char*)(image.ptr(0)), image.cols, image.rows, (int)image.step);
}
To build the ./example of libfacedetection:
If using Linux/Ubuntu, you can:
Add CMakeLists.txt:
cmake_minimum_required( VERSION 2.8 )
project( example )
find_package( OpenCV REQUIRED )
message(STATUS "OpenCV_LIBS = ${OpenCV_LIBS}")
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( detect-image detect-image.cpp )
add_executable( detect-camera detect-camera.cpp )
target_link_libraries( detect-image ${OpenCV_LIBS} )
target_link_libraries( detect-image /libfacedetection/build/install/lib/libfacedetection.so )
target_link_libraries( detect-image /opencv/build/lib/libopencv_highgui.so )
target_link_libraries( detect-image /opencv/build/lib/libopencv_imgproc.so )
target_link_libraries( detect-image /opencv/build/lib/libopencv_core.so )
target_link_libraries( detect-image /opencv/build/lib/libopencv_imgcodecs.so )
target_link_libraries( detect-camera ${OpenCV_LIBS} )
target_link_libraries( detect-camera /libfacedetection/build/install/lib/libfacedetection.so )
target_link_libraries( detect-camera /opencv/build/lib/libopencv_highgui.so )
target_link_libraries( detect-camera /opencv/build/lib/libopencv_video.so )
target_link_libraries( detect-camera /opencv/build/lib/libopencv_imgproc.so )
target_link_libraries( detect-camera /opencv/build/lib/libopencv_core.so )
target_link_libraries( detect-camera /opencv/build/lib/libopencv_videoio.so )
CMake and make:
cd example
mkdir build
cd build
cmake ..
make
// detect using an image
./detect-image <path to image>
// or detect using camera
./detect-camera <camera-index>
If using Visual Studio 2019, you can:
Generate facedetection.lib as well as facedetection.dll (to avoid errors);
You can either:
OR
libfacedetection\build\install\include\facedetection (as well as your OpenCV include path) to VC++ Directories -> Include Directories and the path of your libfacedetection\build\install\lib (as well as your OpenCV lib path)to VC++ Directories -> Library Directories, and add facedetection.lib and other necessary dependencies to Linker -> Input -> Additional Dependencies.Build the solution and run the powershell terminal in Visual Studio:
cd x64/Release
// detect using an image
./detect-image <path/to/your/image/file>
// or detect using camera
./detect-camera <camera-index>
Sample output of detect-image
Third-party examples