MediapipeとUnity 環境開発

**Docker Windows Container**
⚠️ Hyper-V backend is required (that is, Windows 10 Home is not supported).

##### ①Install Docker Desktop

##### ②Switch to Windows Containers

##### ③Build a Docker image

ここまでは指示通りにやります。

mediapipeが入っているフォルダの場所まで移動してコマンドを実行する。

（）

今回の場合、C:\Users\〇〇(ユーザー名)\unity

```cmd
docker build -t mediapipe_unity:windows . -f docker/windows/x86_64/Dockerfile
```

This process will hang when MSYS2 is being installed.
If this issue occurs, remove C:\ProgramData\Docker\tmp\hcs*\Files\$Recycle.Bin\ manually (hcs* is random name).
cf. https://github.com/docker/for-win/issues/8910

～実行結果～

```cmd
C:\Users\ayari\kosear221026>docker build -t mediapipe_unity:windows . -f docker/windows/x86_64/Dockerfile
Sending build context to Docker daemon    3.5GB
Step 1/56 : ARG NODE_VERSION=16.13.0
Step 2/56 : ARG PYTHON_VERSION=3.9.9
Step 3/56 : ARG ANDROID_SDK_VERSION=30
Step 4/56 : ARG ANDROID_NDK_VERSION=21.4.7075529
Step 5/56 : FROM mcr.microsoft.com/windows/servercore:ltsc2019 as base
 ---> e6b07227af5c
Step 6/56 : SHELL ["cmd", "/C"]
 ---> Using cache
 ---> 4c99bd1c5aca
Step 7/56 : RUN mkdir C:\TEMP
 ---> Using cache
 ---> d85c7ca53523
Step 8/56 : FROM base as msys2
 ---> d85c7ca53523
Step 9/56 : SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
 ---> Using cache
 ---> b8431f8e4963
Step 10/56 : RUN [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;   Invoke-WebRequest -UseBasicParsing -uri "https://github.com/msys2/msys2-installer/releases/download/nightly-x86_64/msys2-base-x86_64-latest.sfx.exe" -OutFile msys2.exe;   .\msys2.exe -y -oC:\;   Remove-Item msys2.exe ;   function msys() { C:\msys64\usr\bin\bash.exe @('-lc') + @Args; }   msys ' ';   msys 'pacman --noconfirm -Syuu';   msys 'pacman --noconfirm -Syuu';   msys 'pacman --needed --noconfirm -S git patch unzip zip p7zip';   msys 'pacman --noconfirm -Scc'
 ---> Using cache
 ---> 8b9199a45baa
Step 11/56 : FROM msys2 as builder
 ---> 8b9199a45baa
Step 12/56 : SHELL ["cmd", "/C"]
 ---> Using cache
 ---> b81489367c20
Step 13/56 : ARG NODE_VERSION
 ---> Using cache
 ---> 6bd106417625
Step 14/56 : RUN setx path "C:\msys64\usr\bin;%PATH%"
 ---> Using cache
 ---> cbef92020ad8
Step 15/56 : ARG CHANNEL_URL=https://aka.ms/vs/16/release/channel
 ---> Using cache
 ---> f041e8500ce5
Step 16/56 : RUN curl -L https://nodejs.org/dist/v%NODE_VERSION%/node-v%NODE_VERSION%-x64.msi -o C:\TEMP\node-install.msi &&     start /wait msiexec.exe /i C:\TEMP\node-install.msi /l*vx "%TEMP%\MSI-node-install.log" /qn ADDLOCAL=ALL &&     del C:\TEMP\node-install.msi &&     curl -L %CHANNEL_URL% -o C:\TEMP\VisualStudio.chman &&     curl -L https://aka.ms/vs/16/release/vs_buildtools.exe -o C:\TEMP\vs_buildtools.exe &&     C:\TEMP\vs_buildtools.exe --quiet --wait --norestart --nocache         --installPath "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools"         --channelUri C:\TEMP\VisualStudio.chman         --installChannelUri C:\TEMP\VisualStudio.chman         --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended &&     curl -L https://aka.ms/vs/16/release/vc_redist.x64.exe -o C:\TEMP\vc_redist.x64.exe &&     start /wait C:\TEMP\vc_redist.x64.exe /install /quiet /norestart &&     del C:\TEMP\vc_redist.x64.exe
 ---> Using cache
 ---> f83ee5167509
Step 17/56 : FROM base as python
 ---> d85c7ca53523
Step 18/56 : ARG PYTHON_VERSION
 ---> Using cache
 ---> 56c9b8ef89d2
Step 19/56 : RUN curl -L https://www.python.org/ftp/python/%PYTHON_VERSION%/python-%PYTHON_VERSION%-amd64.exe -o C:\TEMP\python-installer.exe &&     C:\TEMP\python-installer.exe -Wait /quiet InstallAllUsers=1 TargetDir=C:\Python PrependPath=1 Shortcuts=0 Include_doc=0 Include_test=0 &&     del C:\TEMP\python-installer.exe &&     C:\Python\python.exe -m pip install --upgrade pip &&     C:\Python\python.exe -m pip install numpy
 ---> Using cache
 ---> c0a68fba7896
Step 20/56 : FROM base as opencv
 ---> d85c7ca53523
Step 21/56 : RUN curl -L https://github.com/opencv/opencv/releases/download/3.4.16/opencv-3.4.16-vc14_vc15.exe -o C:\TEMP\opencv-installer.exe &&     start /wait C:\TEMP\opencv-installer.exe -gm2 -y -oC:\ &&     del C:\TEMP\opencv-installer.exe
 ---> Using cache
 ---> b00f1bd806b7
Step 22/56 : FROM builder as android
 ---> f83ee5167509
Step 23/56 : ARG ANDROID_SDK_VERSION
 ---> Using cache
 ---> 744a06524c92
Step 24/56 : ARG ANDROID_NDK_VERSION
 ---> Using cache
 ---> ceed80a3f685
Step 25/56 : ENV OPENJDK_ZIP OpenJDK11U-jdk_x64.zip
 ---> Using cache
 ---> bf147ccbbdfd
Step 26/56 : ENV OPENJDK_SHA256 087d096032efe273d7e754a25c85d8e8cf44738a3e597ad86f55e0971acc3b8e
 ---> Using cache
 ---> 3ddc9d8efd36
Step 27/56 : ENV JAVA_HOME C:\Java\jdk-11.0.13+8
 ---> Using cache
 ---> 63112efb117f
Step 28/56 : RUN curl -L https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.13%2B8/OpenJDK11U-jdk_x64_windows_hotspot_11.0.13_8.zip -o C:\TEMP\%OPENJDK_ZIP% &&     for /F %s in ('sha256sum /c/TEMP/%OPENJDK_ZIP%') do ((test "%s" = "%OPENJDK_SHA256%" || (echo %s 'Checksum Failed' && exit 1))) &&     unzip C:\TEMP\%OPENJDK_ZIP% -d C:\Java &&     del C:\TEMP\%OPENJDK_ZIP%
 ---> Using cache
 ---> 52bfafdb94b9
Step 29/56 : ENV COMMANDLINETOOLS_ZIP commandlinetools.zip
 ---> Using cache
 ---> d84446029186
Step 30/56 : ENV COMMANDLINETOOLS_SHA256 f9e6f91743bcb1cc6905648ca751bc33975b0dd11b50d691c2085d025514278c
 ---> Using cache
 ---> 249e5f0c9651
Step 31/56 : RUN curl -L https://dl.google.com/android/repository/commandlinetools-win-7583922_latest.zip -o C:\TEMP\%COMMANDLINETOOLS_ZIP% &&     for /F %s in ('sha256sum /c/TEMP/%COMMANDLINETOOLS_ZIP%') do ((test "%s" = "%COMMANDLINETOOLS_SHA256%" || (echo 'Checksum Failed' && exit 1))) &&     unzip C:\TEMP\%COMMANDLINETOOLS_ZIP% -d C:\Android &&     del C:\TEMP\%COMMANDLINETOOLS_ZIP%
 ---> Using cache
 ---> 4768da4c84f8
Step 32/56 : RUN powershell -command "for($i=0;$i -lt 30;$i++) { $response += """y`n""" }; $response" | C:\Android\cmdline-tools\bin\sdkmanager --sdk_root=C:\Android --licenses &&     C:\Android\cmdline-tools\bin\sdkmanager --sdk_root=C:\Android --install         "platforms;android-%ANDROID_SDK_VERSION%"         "build-tools;30.0.3"         "ndk;%ANDROID_NDK_VERSION%"
 ---> Using cache
 ---> 67dd8a28b2b1
Step 33/56 : FROM builder
 ---> f83ee5167509
Step 34/56 : ARG ANDROID_NDK_VERSION
 ---> Using cache
 ---> b41bf11641b6
Step 35/56 : ENV ANDROID_HOME C:\Android
 ---> Using cache
 ---> 1707565bbdb7
Step 36/56 : ENV ANDROID_NDK_HOME ${ANDROID_HOME}\ndk\${ANDROID_NDK_VERSION}
 ---> Using cache
 ---> 76348708fd1f
Step 37/56 : ENV PYTHON_INSTALL_PATH=C:\Python
 ---> Using cache
 ---> 0877ee438516
Step 38/56 : ENV PYTHON_BIN_PATH=${PYTHON_INSTALL_PATH}\python.exe
 ---> Using cache
 ---> 2913cc32b996
Step 39/56 : RUN setx path "C:\bin;%PYTHON_INSTALL_PATH%;%PYTHON_INSTALL_PATH%\Scripts;%PATH%" &&     mkdir C:\bin &&     curl -L https://github.com/bazelbuild/bazelisk/releases/download/v1.11.0/bazelisk-windows-amd64.exe -o C:\bin\bazel.exe &&     curl -L https://dist.nuget.org/win-x86-commandline/latest/nuget.exe -o C:\bin\nuget.exe
 ---> Using cache
 ---> ff3b455e03af
Step 40/56 : COPY --from=python C:\Python C:\Python
 ---> Using cache
 ---> 1c44d2a6434d
Step 41/56 : COPY --from=opencv C:\opencv C:\opencv
 ---> Using cache
 ---> 6b227dcf299b
Step 42/56 : COPY --from=android C:\Android C:\Android
 ---> Using cache
 ---> 0756785899f1
Step 43/56 : ENV WS_LONG_PATHS_VERSION 3.4.1
 ---> Using cache
 ---> 98b9314d8f79
Step 44/56 : ENV WS_LONG_PATHS_BASE ndk-wsls-${WS_LONG_PATHS_VERSION}
 ---> Using cache
 ---> d17cf7924343
Step 45/56 : ENV WS_LONG_PATHS_7Z ${WS_LONG_PATHS_BASE}.7z
 ---> Using cache
 ---> 22cf2da9c8c0
Step 46/56 : RUN curl -L https://github.com/simdsoft/wsLongPaths/releases/download/v%WS_LONG_PATHS_VERSION%/%WS_LONG_PATHS_7Z% -o C:\TEMP\%WS_LONG_PATHS_7Z% &&     bash.exe -c "7z x -o/c/TEMP /c/TEMP/${WS_LONG_PATHS_7Z}" &&     C:\TEMP\%WS_LONG_PATHS_BASE%\install.bat %ANDROID_NDK_HOME% %ANDROID_HOME%
 ---> Using cache
 ---> 8d19717be765
Step 47/56 : RUN del C:\TEMP\%WS_LONG_PATHS_7Z% &&     rmdir /s /q C:\TEMP\%WS_LONG_PATHS_BASE%
 ---> Using cache
 ---> 9b4f44a10b75
Step 48/56 : WORKDIR C:\mediapipe
 ---> Using cache
 ---> b3ccb0d5fa1b
Step 49/56 : COPY packages.config .
 ---> Using cache
 ---> cc78eb9e14e6
Step 50/56 : COPY .bazelrc .
 ---> Using cache
 ---> 07d67f30cc6d
Step 51/56 : COPY .bazelversion .
 ---> Using cache
 ---> 7ac3f88efbd4
Step 52/56 : COPY build.py .
 ---> Using cache
 ---> ca98b7d260f4
Step 53/56 : COPY WORKSPACE .
 ---> Using cache
 ---> 6222a570c60b
Step 54/56 : COPY mediapipe_api mediapipe_api
 ---> 0701c79180f8
Step 55/56 : COPY third_party third_party
 ---> 0c5427e08a98
Step 56/56 : CMD ["cmd"]
 ---> Running in f9584fe9bc1b
Removing intermediate container f9584fe9bc1b
 ---> a1f9438acf9f
Successfully built a1f9438acf9f
Successfully tagged mediapipe_unity:windows

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them``
```



##### ④Run a Docker container

*Rem Run with Packages directory mounted to the container*
→「Packages」ディレクトリで実行する必要がある、とありますが、ここでは場所を移動せずに同じディレクトリで作業しました。

以下のようにエラーが出ます。

```cmd
C:\Users\ayari\Documents\unity\kosear221026>cd Packages

C:\Users\ayari\Documents\unity\kosear221026\Packages>docker run --cpus=8 --memory=16g ^
More?     --mount type=bind,src=%CD%\Packages,dst=C:\mediapipe\Packages ^
More?     --mount type=bind,src=%CD%\Assets,dst=C:\mediapipe\Assets ^
More?     -it mediapipe_unity:windows
docker: Error response from daemon: invalid mount config for type "bind": bind source path does not exist: C:\Users\ayari\Documents\unity\kosear221026\Packages\Packages.
See 'docker run --help'.

```


*Rem Specify --cpus and --memory options according to your machine.*
→自分のPCのスペックを調べて、CPUとメモリの値を変更します。

```cmd
docker run --cpus=8 --memory=16g ^
    --mount type=bind,src=%CD%\Packages,dst=C:\mediapipe\Packages ^
    --mount type=bind,src=%CD%\Assets,dst=C:\mediapipe\Assets ^
    -it mediapipe_unity:windows
```

☆実行結果

```cmd
C:\Users\ayari\Documents\unity\kosear221026\Packages>cd ../

C:\Users\ayari\Documents\unity\kosear221026>docker run --cpus=8 --memory=16g ^
More?     --mount type=bind,src=%CD%\Packages,dst=C:\mediapipe\Packages ^
More?     --mount type=bind,src=%CD%\Assets,dst=C:\mediapipe\Assets ^
More?     -it mediapipe_unity:windows
Microsoft Windows [Version 10.0.17763.3165]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\mediapipe>
```



##### ⑤(For Android, Experimental) Apply a patch to bazel.

🔔 You can also make use of Linux containers to build libraries for Android.

Rem Run inside the container

```cmd
git clone https://github.com/bazelbuild/bazel.git C:\bazel
cd C:\bazel
git checkout 5.2.0
git apply ..\mediapipe\third_party\bazel_android_fixes.diff
bazel --output_user_root=C:\_bzl build --java_runtime_version=remotejdk_11 //src:bazel.exe
cp bazel-bin\src\bazel.exe C:\bin
cd C:\mediapipe
```

～実行結果～

```cmd
C:\mediapipe>git clone https://github.com/bazelbuild/bazel.git C:\bazel
Cloning into '/c/bazel'...
remote: Enumerating objects: 643611, done.
remote: Counting objects: 100% (1079/1079), done.
remote: Compressing objects: 100% (523/523), done.
remote: Total 643611 (delta 498), reused 936 (delta 417), pack-reused 642532
Receiving objects: 100% (643611/643611), 853.19 MiB | 2.84 MiB/s, done.
Resolving deltas: 100% (402982/402982), done.
Updating files: 100% (9188/9188), done.
C:\mediapipe>cd C:\bazel
C:\bazel>git checkout 5.2.0
Updating files: 100% (4881/4881), done.
Note: switching to '5.2.0'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 2e8458b781 Release 5.2.0 (2022-06-07)

C:\bazel>git apply ..\mediapipe\third_party\bazel_android_fixes.diff

C:\bazel>bazel --output_user_root=C:\_bzl build --java_runtime_version=remotejdk_11 //src:bazel.exe

C:\bazel>cp bazel-bin\src\bazel.exe C:\bin

C:\bazel>cd C:\mediapipe

C:\mediapipe>
```



##### ⑥Run build command inside the container

（今回はこちらで試しました↓）

```cmd
python build.py build --desktop cpu --opencv cmake -vv
```

Rem or if you'd like to link OpenCV dynamically

```cmd
python build.py build --desktop cpu -vv
```

If the command finishes successfully, the required files will be installed on your host machine.

ものすごく時間がかかります。（20分以上）