Setup-Guide
A build and setup guide for various libraries, tools, cloud usage etc.
Tools Required
Download the installer or zip version of cmake. Dlib 19.19 from official website had bug with opencv cv_image which is fixed on github source.
Projects Tested
These are some of the projects I have tested. Most or all of these can be implemented in both pc and android.
Android
Note in order to make latest version of all three working in same project the easiest way for beginner is to setup opencv contrib/opencv, next setup dlib after changing target project name, cmake, ndk paths, finally setup ncnn.
NOTE: Must create C++ android project to make these all work together.
OpenCV Contrib
Build instructions for opencv contrib,
https://medium.com/beesightsoft/build-opencv-opencv-contrib-for-android-on-windows-9894b4fe6386
But easiest way at the moment is download prebuilt version of latest opencv contrib from,
https://pullrequest.opencv.org/buildbot/export/opencv_releases/master-contrib_pack-contrib-android/
Use the following link and template to setup native opencv project for android. Instead of using opencv android from their site just replace it with opencv contrib from the link above.
https://github.com/VlSomers/native-opencv-android-template
Sample OpenCV DNN Setup
https://github.com/ivangrov/Android-Deep-Learning-with-OpenCV
Dlib
Download dlib from github repo and extract to C: drive such that extracted path is C:\dlib\dlib-master\.
Use this dlib powershell script to copy dlib directly into the project. Must change the absolute paths before running the script. Change $CMAKE_BIN_PATH, $NDK, $PROJECT_PATH, $DLIB_PATH.
Execution Method
Paste the script in C:\dlib\ and run powershell as administrator. Paste set-location C:\dlib\dlib-master\ and press enter to go into that folder. Run Set-ExecutionPolicy RemoteSigned and press A.
Next, paste & "C:\dlib\dlib-android-setup.ps1" and enter to start compiling and copying files to your project. Must change paths accordingly in script before execution.
After all dlib files are copied to android project, paste Set-ExecutionPolicy Restricted to powershell and press A.
Further detail on script and execution procedure,
https://stackoverflow.com/questions/60548479/setting-up-dlib-for-android-studio/60550358#60550358
Original script source,
https://github.com/Luca96/dlib-for-android
NCNN
https://github.com/Tencent/ncnn/wiki/how-to-build
Easiest option for android is download prebuilt releases,
https://github.com/Tencent/ncnn/releases
Download vulkan library for GPU support.
Windows
OpenCV Contrib Windows Build From Source For Python Anaconda
Download opencv, opencv_contrib, cmake. Extract and paste both into a folder for path, ex: opencv-compilation. Use cmake-gui browse source to point to opencv folder and click configure. Find OPENCV_EXTRA_MODULES_PATH and point to opencv_contrib modules folder. Point PYTHON3_ variables to correct conda environment path. Untick BUILD_SHARED_LIBS, tick BUILD_opencv_world, click configure and generate. It may be necessary to have numpy installed beforehand. Unchecking unncessary modules will speed up process if python is the only target.
This will generate build folder inside opencv-compilation. Open OpenCV.sln, choose release configuration x64. In solution explorer open CMakeTargets, right click on ALL_BUILD and click build. After compilation done copy cv2.****.pyd file from opencv-compilation\build\lib\python3\Release to Anaconda or Miniconda custom enviroment. For examples, I used miniconda3 and the copy location for pyd file was C:\Users\...\miniconda3\envs\mycustomenv\Lib\site-packages.
Check installation by activating correct environment and running,
import cv2
print(cv2.__version__)
OpenCV Contrib Application
For Visual Studio 2019 Use opencv_worldXXX.lib, Ex: opencv_world430.lib for release configuration and opencv_world430d.lib for debug configuration. If opencv is extracted to C: then it can be fould in C:\opencv\build\x64\vc15\lib.
Depending on CPU cores CUDA compilation can be very slow. OpenCV CUDA build,
https://haroonshakeel.medium.com/build-opencv-4-4-0-with-cuda-gpu-support-on-windows-10-without-tears-aa85d470bcd0.
https://www.pyimagesearch.com/2020/02/03/how-to-use-opencvs-dnn-module-with-nvidia-gpus-cuda-and-cudnn/
OpenCV python CUDA static build,
https://github.com/opencv/opencv/issues/20206.
Dlib
Compile instructions,
http://dlib.net/compile.html
For use cmake to configure and Visual Studio compile ALL_BUILD dlib in release x64 to generate dlib_(...).lib.
In desired project set vc++ include directories, include path to dlib master downloaded from master to extracted folder. Set library directoies to generated dlib.lib. Copy dlib/all/source.cpp to visual studio source files folder where other c++ codes reside.
To enable jpg support set C/C++ > General > Additional Include Directories, to extracted dlib folder something like, C:\dlib-master\dlib\external\libjpeg. Set C/C++ > Preprocessor > Preprocessor definitions, to CRT_SECURE_NO_WARNINGS and DLIB_JPEG_SUPPORT for jpg support.
Example for testing, http://dlib.net/dnn_face_recognition_ex.cpp.html
NCNN
https://github.com/Tencent/ncnn/wiki/how-to-build
Windows 10 LLVM Clang, Ninja, CMake Generate C++ Executable
Tool Links
Ninja Windows Release, https://github.com/ninja-build/ninja/releases
LLVM Download, https://releases.llvm.org/download.html
Process
Make a build folder to store generated files. In cmake set this path. Also set path for source C++ folder. Click configure and select generator. Choose Ninja and tick specify native compilers. Set LLVM bin clang.exe and clang++.exe for C and C++.
There will be error that CMAKE_MAKE_PROGRAM-NOTFOUND. Browse to directory where ninja release was extracted and select ninja.exe. It should also be possible (not tested) to use ninja.exe that comes with android studio setup in android cmake folder.
Now press configure and again configure if all looks correct. Next, click generate. Open cmd navigate to project source folder and run ninja.exe. If not is system path then specify location as, "C:\ninja-win\ninja.exe" on command prompt.
Minimal Example
https://stackoverflow.com/questions/52760801/cmake-building-for-windows-clang-cl-using-ninja-generator
CMakeLists.txt
cmake_minimum_required(VERSION 3.12)
project(hello_world)
add_executable(${PROJECT_NAME} main.cpp)
main.cpp
#include <iostream>
using namespace std;
int main(){
cout << "HELLO" << "\n";
cin.get();
return 0;
}
VSCodium Windows C++ Build and Debugging with g++ and gdb
Instructions for setting up c++ build and debugging on windows. For MinGW gcc, g++, gdb etc. I used Codeblocks MinGW version (may cause problem with std::thread) where in bin folder they are available.
Sample path is, {base_path}\codeblocks\MinGW\bin. Need to have Microsoft C/C++ extensions installed in vscodium.
https://code.visualstudio.com/docs/cpp/config-mingw
To create workspace add vscodium to path and use codium .. Create desired cpp file in the workspace. Next, choose Terminal > Configure Tasks > Shell:g++.exe build active file. This will create a build.json file and can be run from Terminal > Run Build Task... Next, from Run > Add Configurations choose C++ (GDB/LLDB) > g++.exe build and debug active file.
Now, set breakpoints and use Run > Start Debugging. If there is any error with preLaunchTask in launch.json, then matching with task.json label should fix it.
This method with cl.exe works for build, but debugging does not work.
https://code.visualstudio.com/docs/cpp/config-msvc
Windows 10 CMU openpose Setup Visual Studio 2019, CMake, Nvidia GPU
This setup is quite complex.
Prerequisite
First read these carefully,