Adapt DLL build files

- Added batch file simplifying building the DLL
- Removed unneeded dependencies and includes
- added /FORCE:MULTIPLE linker option to prevent from linker errors due to duplicate symbols
- Adapted README
This commit is contained in:
Christoph Niederberger 2023-02-15 10:07:15 +01:00
parent 67bf74dd6d
commit 90c3e59424
4 changed files with 173 additions and 73 deletions

View File

@ -31,19 +31,11 @@ windows_dll_library(
"//mediapipe/framework/formats:landmark_cc_proto",
"//mediapipe/framework/formats:rect_cc_proto",
"//mediapipe/framework/port:file_helpers",
"//mediapipe/framework/port:opencv_highgui",
"//mediapipe/framework/port:opencv_imgproc",
"//mediapipe/framework/port:opencv_video",
"//mediapipe/framework/port:parse_text_proto",
"//mediapipe/framework/port:status",
"@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/flags:parse",
"//mediapipe/calculators/core:constant_side_packet_calculator",
"//mediapipe/calculators/core:packet_presence_calculator",
"//mediapipe/calculators/core:flow_limiter_calculator",
"//mediapipe/calculators/tflite:tflite_model_calculator",
"//mediapipe/calculators/util:local_file_contents_calculator",
"//mediapipe/graphs/pose_tracking:pose_tracking_cpu_deps",
]
)
@ -51,6 +43,7 @@ windows_dll_library(
# **Implicitly link to face_mesh_lib.dll**
cc_binary(
name = "pose_tracking_cpu",
linkopts = ["/FORCE:MULTIPLE"],
deps = [
"//mediapipe/examples/desktop:demo_run_graph_main",
"//mediapipe/graphs/pose_tracking:pose_tracking_cpu_deps",

View File

@ -1,4 +1,5 @@
## Description
# Description
The pose_tracking_dll module allows for building a Mediapipe-based pose tracking DLL library that can be used with any C++ project. All the dependencies such as tensorflow are built statically into the dll.
Currently, the following features are supported:
@ -6,55 +7,64 @@ Currently, the following features are supported:
- Segmenting the skeleton(s)
- Accessing the 3D coordinates of each node of the skeleton
## Prerequisites
# Prerequisites
Install Mediapipe development environment as follows.
**Note**: This guide assumes the Nimagna development environment. Otherwise, please follow the guidelines on the official Mediapipe website: https://google.github.io/mediapipe/getting_started/install.html#installing-on-windows with the important change that Bazel version 3.7.2 is required and the helpful sidemark that OpenCV version used by default in mediapipe is 3.4.10.
## Remarks
### Install MSYS2
This guide assumes a Nimagna development environment.
- Install MSYS2 from https://www.msys2.org/
- Edit the %PATH% environment variable: If MSYS2 is installed to `C:\msys64`, add `C:\msys64\usr\bin` to your %PATH% environment variable.
Otherwise, please also read the guidelines on the official Mediapipe website: https://google.github.io/mediapipe/getting_started/install.html#installing-on-windows.
### Install necessary packages
## MSYS2
- Run `pacman -S git patch unzip` and confirm installation
- Install MSYS2 from https://www.msys2.org/ (tested with version 20230127)
- The MSYS2 installation path is referred to as `MSYS2DIR` below.
- Install necessary packages:
- `pacman -S git patch unzip` and confirm installation
### Install Python 3.9
## Python 3.11
- Download Python 3.9.9 Windows executable https://www.python.org/downloads/release/python-399/ and install. Note that Python 3.10 does not work.
- Allow the installer to edit the %PATH% environment variable.
- The Python installation path is referred to as `PYTHONDIR` in the following steps. Usually, this is `C:\Users\...\AppData\Local\Programs\Python\Python39` when installing only for the current user.
- Run `pip install numpy` in a new command line.
- Install Python 3.11
- Download Python 3.11.x Windows executable from https://www.python.org/downloads/windows/
- Allow the installer to edit the %PATH% environment variable.
- Note: Newer Python version have not been tested
- The Python installation path is referred to as `PYTHONDIR` below.
- Usually, this is `C:\Users\...\AppData\Local\Programs\Python\Python311` when installing only for the current user.
- Run `pip install numpy` in a new command line.
### Install Visual C++ Build Tools 2019 and WinSDK
## Install Visual C++ Build Tools 2019 and WinSDK
- Download and install VC build tools from https://visualstudio.microsoft.com/visual-cpp-build-tools/ with the following settings:
- Install Visual C++ Build Tools 2019 with WinSDK
- Download and install Visual C++ Build Tools 2019 (16.11 used here) from https://my.visualstudio.com/Downloads?q=visual%20studio%202019%20build&wt.mc_id=o~msft~vscom~older-downloads (if link does not work, use https://visualstudio.microsoft.com/visual-cpp-build-tools/ and search for "older versions" to find the 2019 installer)
- Install with
- "Desktop Development with C++"
- Individual components: select "Windows 10 SDK (10.0.19041.0)"
![image](https://user-images.githubusercontent.com/83065859/148920359-fc5830c2-3eb1-47d4-ba33-8b1ba783b728.png)
- Note: VC Build Tools 2022 or newer WinSDK versions do not compile with the current code base.
- Redistributables and WinSDK 10.0.19041.0 should be installed already. If not, install with Visual Studio or download the [WinSDK from the official Microsoft website](https://developer.microsoft.com/en-us/windows/downloads/sdk-archive/) and install.
## Install Bazel 5.4.0
### Install Bazel 3.7.2
- Download `bazel-5.4.0-windows-x86_64.exe` from https://github.com/bazelbuild/bazel/releases/tag/5.4.0
- Put file into a folder and rename it to `bazel.exe`
- The Bazel installation path is referred to as `BAZEL_PATH` below.
- Download `bazel-3.7.2-windows-x86_64.exe` from https://github.com/bazelbuild/bazel/releases/tag/3.7.2 and rename to `bazel.exe`
- Add the location of the downloaded Bazel executable to the %PATH% environment variable.
- Set additional Bazel environment variables as follows:
- `BAZEL_VS=C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools`
- `BAZEL_VC=C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC`
- `BAZEL_VC_FULL_VERSION=14.29.30133` - or the name of the folder you find in `C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC`
- `BAZEL_WINSDK_FULL_VERSION=10.0.19041.0`
### Checkout Mediapipe
- git clone https://github.com/NimagnaAG/mediapipe
- the repository root folder is referred to as `MEDIAPIPEDIR` in the following steps
### Install OpenCV
## Install OpenCV
- Download OpenCV 3.4.10 from https://sourceforge.net/projects/opencvlibrary/files/3.4.10/opencv-3.4.10-vc14_vc15.exe/download
- Extract OpenCV from into a folder, e.g. `C:\Users\ChristophNiederberge\source\repos\opencv_3.4.10`. This folder is referred to as `OPENCVDIR` in the following steps.
- Edit the `MEDIAPIPEDIR\WORKSPACE` file: Around line 215, is the "windows_opencv" repository. Adapt the path to point to `OPENCVDIR\\build` (using double backslashes):
- Extract OpenCV into a separate folder. This folder is referred to as `OPENCVDIR` in the following steps.
## Checkout Mediapipe
- `git clone https://github.com/NimagnaAG/mediapipe`
- The repository root folder is referred to as `MEDIAPIPEDIR` below.
## Prepare Build
- Edit the `MEDIAPIPEDIR\WORKSPACE` file:
- Around line 215, is the "windows_opencv" repository.
- Adapt the path to point to `OPENCVDIR\\build` (using double backslashes):
```
new_local_repository(
name = "windows_opencv",
@ -62,20 +72,50 @@ Install Mediapipe development environment as follows.
path = "OPENCVDIR\\build",
)
```
- Edit the `MEDIAPIPEDIR\mediapipe\pose_tracking_dll\build.bat` file
- Must change:
- `BAZEL_PATH` -> path to bazel.exe (No default)
- Build configuration
- `MEDIAPIPE_CONFIGURATION` -> Release (Default, `opt`) or Debug (`dbg`)
- Verify:
- `MYSYS_PATH` -> path to `MSYS2DIR\usr\bin` (Default: `C:\msys64\usr\bin`)
- `BAZEL_SH` -> path to bash.exe (Default: `%MYSYS_PATH%\bash.exe`)
- `BAZEL_VS_VERSION` -> Visual Studio version (Default: `2019`)
- `BAZEL_VC_FULL_VERSION` -> Visual Studio Build Tools full version. Depends on the version installed. See `See C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC`
- `BAZEL_WINSDK_FULL_VERSION` -> WinSDK version (Default: `10.0.19041.0`)
- `BAZEL_PYTHON_PATH` -> Path to python.exe (Default: `%LOCALAPPDATA_FORWARDSLASH%/Programs/Python/Python311/python.exe`)
- Optional
- `BAZEL_TMP_BUILD_DIR` -> A temporary build folder
- `TARGET_PATH` -> A target folder to copy the relevant files to, i.e. the mediapipe/version folder of the external repo
#### Installation issue handling
# Build
- Open a Command Prompt
- `cd MEDIAPIPEDIR`
- `mediapipe\pose_tracking_dll\build.bat` ... and take a break!
- The build output can be found in the `MEDIAPIPEDIR\bazel-bin\mediapipe\pose_tracking_dll` folder.
# How to use the DLL
- Go to bazel-bin\mediapipe\pose_tracking_dll
- Link `pose_tracking_cpu.lib` and `pose_tracking_lib.dll.if.lib` statically in your project.
- Make sure `opencv_world3410.dll` and `pose_tracking_lib.dll` are accessible in your executable's DLL search path.
- Include `mediapipe\pose_tracking_dll\pose_tracking.h` header file to access the methods of the library.
# Troubleshooting
## Different OpenCV version
- If you are using a **different OpenCV version**, adapt the `OPENCV_VERSION` variable in the file `mediapipe/external/opencv_<platform>.BUILD` to the one installed in the system (https://github.com/google/mediapipe/issues/1926#issuecomment-825874197).
## How to build
Assuming you're in `MEDIAPIPEDIR`, the root of the repository, run the following commands by replacing `PYTHONDIR` using forward slashes "/" in the path:
## Bazel issues
```
cd mediapipe
bazel build -c opt --define MEDIAPIPE_DISABLE_GPU=1 --action_env PYTHON_BIN_PATH=PYTHONDIR/python.exe pose_tracking_dll:pose_tracking_cpu
```
- If bazel fails to download packages
- run `bazel clean --expunge` and try again.
- If bazel fails with an `fatal error C1083: Cannot open compiler generated file: '': Invalid argument`, your [path is too long](https://stackoverflow.com/questions/34074925/vs-2015-cannot-open-compiler-generated-file-invalid-argument).
- set `BAZEL_TMP_BUILD_DIR` in the batch file to a temporary folder with a short path
- Note: Clean the bazel environment becomes `bazel --output_base=%BAZEL_TMP_BUILD_DIR% clean --expunge`
The results will be stored in the `bazel-bin\mediapipe\pose_tracking_dll` folder.
## Stalled build process
In the case the build stalls, pressing Ctrl+C might not be sufficient to stop the task. In that case, if you try to (resume the) build again,
the following message will be displayed:
@ -91,19 +131,3 @@ taskkill /F /PID 3684
```
After that, you should be able to run the build command again.
### Build debug symbols
- `dbg` can be used in place of `opt` to build the library with debug symbols in Visual Studio pdb format.
### Build issue handling
- If bazel fails to download packages, run `bazel clean --expunge` and try again.
- If bazel fails with an `fatal error C1083: Cannot open compiler generated file: '': Invalid argument`, your [path is too long](https://stackoverflow.com/questions/34074925/vs-2015-cannot-open-compiler-generated-file-invalid-argument). Actually, it is most probably the username...
- Adapt the call to `bazel --output_base=E:\nim_output build -c opt --define MEDIAPIPE_DISABLE_GPU=1 --action_env PYTHON_BIN_PATH=PYTHONDIR/python.exe pose_tracking_dll:pose_tracking_cpu` where `E:\nim_output build` can be replaced with some short path where bazel will store the packages and perform the build.
## How to use
- Go to bazel-bin\mediapipe\pose_tracking_dll
- Link `pose_tracking_cpu.lib` and `pose_tracking_lib.dll.if.lib` statically in your project.
- Make sure `opencv_world3410.dll` and `pose_tracking_lib.dll` are accessible in your working directory.
- Includeptyh `mediapipe\pose_tracking_dll\pose_tracking.h` header file to access the methods of the library.

View File

@ -0,0 +1,83 @@
@echo off
setlocal ENABLEDELAYEDEXPANSION
:: ----------------------------------------------------------
:: Do not change
:: ----------------------------------------------------------
:: needs to be run from repo root
if NOT EXIST mediapipe\pose_tracking_dll\pose_tracking.cpp (
echo Batch file must be run from repository root, i.e. mediapipe\pose_tracking_dll\build.bat
EXIT
)
set "LOCALAPPDATA_FORWARDSLASH=%LOCALAPPDATA:\=/%"
:: ----------------------------------------------------------
:: Adapt the variables below to your local environment
:: ----------------------------------------------------------
:: path to bazel.exe
SET BAZEL_PATH=E:\repos\bazel\5.4.0
:: path to msys
SET MYSYS_PATH=C:\msys64\usr\bin
:: path to msys bash
SET BAZEL_SH=%MYSYS_PATH%\bash.exe
:: Visual Studio C++ Build Tools
SET BAZEL_VS_VERSION=2019
SET BAZEL_VS=C:\Program Files (x86)\Microsoft Visual Studio\%BAZEL_VS_VERSION%\BuildTools
SET BAZEL_VC=%SBAZEL_VS%\VC
:: Visual Studio C++ Build Tools version
:: See C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC
SET BAZEL_VC_FULL_VERSION=14.29.30133
:: Windows SDK version
SET BAZEL_WINSDK_FULL_VERSION=10.0.19041.0
:: full path to python.exe
SET BAZEL_PYTHON_PATH=%LOCALAPPDATA_FORWARDSLASH%/Programs/Python/Python311/python.exe
:: Optional: temporary build path
SET BAZEL_TMP_BUILD_DIR=E:\repos\mp_output
:: Release [opt] or Debug [dbg]
SET MEDIAPIPE_CONFIGURATION=opt
:: Optional: A target path to copy the relevant files after build, i.e. the version folder of the external repo
SET TARGET_PATH=C:\Users\ChristophNiederberge\source\repos\CodeReviews\external\mediapipe\0.8.10.2_x64
:: ----------------------------------------------------------
:: Build posetracking DLL
:: ----------------------------------------------------------
IF NOT [%BAZEL_TMP_BUILD_DIR%]==[] (
ECHO Using temporary build directory: %BAZEL_TMP_BUILD_DIR%
bazel --output_base "%BAZEL_TMP_BUILD_DIR%" build -c %MEDIAPIPE_CONFIGURATION% --define MEDIAPIPE_DISABLE_GPU=1 --action_env PYTHON_BIN_PATH="%BAZEL_PYTHON_PATH%" mediapipe/pose_tracking_dll:pose_tracking_cpu
) ELSE (
bazel build -c %MEDIAPIPE_CONFIGURATION% --define MEDIAPIPE_DISABLE_GPU=1 --action_env PYTHON_BIN_PATH="%BAZEL_PYTHON_PATH%" mediapipe/pose_tracking_dll:pose_tracking_cpu
)
:: ----------------------------------------------------------
:: Copy files to target folder (optional)
:: ----------------------------------------------------------
IF NOT [%TARGET_PATH%]==[] (
IF [%MEDIAPIPE_CONFIGURATION%]==[opt] (
ECHO Copy files to %TARGET_PATH%\x64\Release ...
mkdir %TARGET_PATH%\x64\Release
copy bazel-bin\mediapipe\pose_tracking_dll\pose_tracking_lib.dll %TARGET_PATH%\x64\Release
copy bazel-bin\mediapipe\pose_tracking_dll\pose_tracking_lib.dll.if.lib %TARGET_PATH%\x64\Release
copy bazel-bin\mediapipe\pose_tracking_dll\opencv_world3410.dll %TARGET_PATH%\x64\Release
)
IF [%MEDIAPIPE_CONFIGURATION%]==[dbg] (
ECHO Copy files to %TARGET_PATH%\x64\Debug ...
mkdir %TARGET_PATH%\x64\Debug
copy bazel-bin\mediapipe\pose_tracking_dll\pose_tracking_lib.dll %TARGET_PATH%\x64\Debug
copy bazel-bin\mediapipe\pose_tracking_dll\pose_tracking_lib.dll.if.lib %TARGET_PATH%\x64\Debug
copy bazel-bin\mediapipe\pose_tracking_dll\pose_tracking_lib.pdb %TARGET_PATH%\x64\Debug
copy bazel-bin\mediapipe\pose_tracking_dll\opencv_world3410d.dll %TARGET_PATH%\x64\Debug
)
)

View File

@ -22,13 +22,13 @@
#include "absl/flags/flag.h"
#include "absl/flags/parse.h"
#include "mediapipe/framework/calculator_framework.h"
#include "mediapipe/framework/formats/image_frame.h"
//#include "mediapipe/framework/formats/image_frame.h"
#include "mediapipe/framework/formats/image_frame_opencv.h"
#include "mediapipe/framework/formats/landmark.pb.h"
#include "mediapipe/framework/port/file_helpers.h"
#include "mediapipe/framework/port/opencv_highgui_inc.h"
#include "mediapipe/framework/port/opencv_imgproc_inc.h"
#include "mediapipe/framework/port/opencv_video_inc.h"
//#include "mediapipe/framework/port/opencv_highgui_inc.h"
//#include "mediapipe/framework/port/opencv_imgproc_inc.h"
//#include "mediapipe/framework/port/opencv_video_inc.h"
#include "mediapipe/framework/port/parse_text_proto.h"
#include "mediapipe/framework/port/status.h"