参考

bazeliskの導入

とりあえず~/apps/binに入れておく。PATHも通しておいたほうがよい。

1
2
3
4
5
% mkdir -p ~/apps/bin
% curl -L -o ~/apps/bin/bazelisk https://github.com/bazelbuild/bazelisk/releases/download/v1.11.0/bazelisk-linux-arm64
% (cd ~/apps/bin; ln -sf bazelisk bazel)
% chmod +x ~/apps/bin/bazelisk
% PATH="${PATH}:${HOME}/apps/bin"

ビルドに必要なものの用意

1
2
3
4
% sudo apt install build-essential python3-dev cmake protobuf-compiler python3-numpy
% git clone https://github.com/google/mediapipe
% cd mediapipe
% git checkout v0.8.9

不要な機能を除外し、同様に不要なライブラリのリンクをしないようにする。 また、carotene_o4tを無効化する。

1
2
3
4
5
% sed -i -e "/\"imgcodecs\"/d;/\"calib3d\"/d;/\"features2d\"/d;/\"highgui\"/d;/\"video\"/d;/\"videoio\"/d" third_party/BUILD
% sed -i -e "/-ljpeg/d;/-lpng/d;/-ltiff/d;/-lImath/d;/-lIlmImf/d;/-lHalf/d;/-lIex/d;/-lIlmThread/d;/-lrt/d;/-ldc1394/d;/-lavcodec/d;/-lavformat/d;/-lavutil/d;/-lswscale/d;/-lavresample/d" third_party/BUILD
% vi third_party/BUILD
cmake_external()のcache_entries{}"ENABLE_NEON": "OFF", と "WITH_TENGINE": "OFF", を追記

編集したthird_party/BUILDのdiffは下記の通り。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
% git diff third_party/BUILD
diff --git a/third_party/BUILD b/third_party/BUILD
index e2044cf..0100547 100644
--- a/third_party/BUILD
+++ b/third_party/BUILD
@@ -76,12 +76,6 @@ load("@rules_foreign_cc//tools/build_defs:cmake.bzl", "cmake_external")
 # linker, so if library A depends on library B, library B must come _after_.
 # Hence core is at the bottom.
 OPENCV_MODULES = [
-    "calib3d",
-    "features2d",
-    "highgui",
-    "video",
-    "videoio",
-    "imgcodecs",
     "imgproc",
     "core",
 ]
@@ -113,6 +107,8 @@ cmake_external(
         "WITH_PNG": "ON",
         "WITH_TIFF": "ON",
         "WITH_WEBP": "OFF",
+       "ENABLE_NEON": "OFF",
+       "WITH_TENGINE": "OFF",
         # Optimization flags
         "CV_ENABLE_INTRINSICS": "ON",
         "WITH_EIGEN": "ON",
@@ -149,25 +145,10 @@ cmake_external(
         # the build, and so depends on what is installed on the local system.
         # After building, the linkopts for the current setup can be extracted
         # from lib/pkgconfig/opencv.pc in bazel-out
-        "-ljpeg",
-        "-lpng",
         "-lz",
-        "-ltiff",
-        "-lImath",
-        "-lIlmImf",
-        "-lIex",
-        "-lHalf",
-        "-lIlmThread",
-        "-ldc1394",
-        "-lavcodec",
-        "-lavformat",
-        "-lavutil",
-        "-lswscale",
-        "-lavresample",
         "-ldl",
         "-lm",
         "-lpthread",
-        "-lrt",
     ],
     shared_libraries = select({
         "@bazel_tools//src/conditions:darwin": ["libopencv_%s.%s.dylib" % (module, OPENCV_SO_VERSION) for module in OPENCV_MODULES],

編集が完了したら、早速ビルドを開始する。

1
2
3
% PATH="${PATH}:${HOME}/apps/bin"
% python3 setup.py gen_protos
% time python3 setup.py bdist_wheel

/usr/bin/pythonが存在しないと下記のようなエラーが発生する。 どうやらどこかのスクリプトのShebangで#!/usr/bin/env pythonが用いられている模様。 とりあえずsudo ln -s /usr/bin/python3.9 /usr/bin/pythonしておけば通った。

1
2
3
4
5
6
7
8
9
10
ERROR: /home/gloria/.cache/bazel/_bazel_gloria/c703987e42a2b396bbfb309c04bccc16/external/org_tensorflow/tensorflow/core/util/BUILD:379:24: Action external/org_tensorflow/tensorflow/core/util/version_info.cc failed: (Exit 127): bash failed: error executing command /bin/bash -c 'bazel-out/aarch64-opt-ST-e0f78fafe98f/bin/external/org_tensorflow/tensorflow/tools/git/gen_git_source --generate "$@" --git_tag_override=${GIT_TAG_OVERRIDE:-}' '' ... (remaining 4 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
/usr/bin/env: 'python': No such file or directory
Target //mediapipe/modules/face_detection:face_detection_short_range_cpu failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 4961.073s, Critical Path: 3555.34s
INFO: 2026 processes: 265 internal, 1761 linux-sandbox.
FAILED: Build did NOT complete successfully
python3 setup.py bdist_wheel  2.09s user 3.12s system 0% cpu 1:22:43.05 total

Raspberry Pi 4(modelB 8GB)では、合計3時間程度でビルドできた。 メモリは常に2GB以上必要となるようなので、最低でも4GBモデルが必要となりそう。(2023/04/24追記: 4GBでは足りなかった)

1
python3 setup.py bdist_wheel  10.31s user 4.24s system 0% cpu 1:14:15.02 total

メモリが不足する場合は、下記のようにswapfileなどを用意して回避する手法もあるが、 少なくともUSB-SSDを用いるなどの対策を行わないとSDカードがあっという間に壊れる可能性が高いので注意。 (そもそもビルドのようなストレージに負荷が掛かる作業は、SDカード上では行わない方がよい。)

1
2
3
4
5
6
% sudo dd if=/dev/zero of=/swapfile bs=1M count=4096
% sudo mkswap /swapfile
% sudo swapon /swapfile
% swapon
NAME      TYPE SIZE USED PRIO
/swapfile file   4G 1.5M   -2

インストール

ビルドしたmediapipeライブラリと、mediapipeの利用に必要なライブラリ群をインストールしておく。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
% sudo apt install cython3 python3-opencv
% pip3 install pillow
% pip3 install dist/mediapipe-dev-cp39-cp39-linux_aarch64.whl
Processing ./dist/mediapipe-dev-cp39-cp39-linux_aarch64.whl
Collecting opencv-contrib-python
  Downloading opencv_contrib_python-4.5.4.60-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (44.7 MB)
     |████████████████████████████████| 44.7 MB 87 kB/s
Collecting absl-py
  Downloading absl_py-1.0.0-py3-none-any.whl (126 kB)
     |████████████████████████████████| 126 kB 7.8 MB/s
Collecting matplotlib
  Downloading matplotlib-3.5.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (11.6 MB)
     |████████████████████████████████| 11.6 MB 9.0 MB/s
Collecting protobuf>=3.11.4
  Downloading protobuf-3.19.1-cp39-cp39-manylinux2014_aarch64.whl (913 kB)
     |████████████████████████████████| 913 kB 8.9 MB/s
Requirement already satisfied: numpy in /usr/lib/python3/dist-packages (from mediapipe==dev) (1.19.5)
Requirement already satisfied: attrs>=19.1.0 in /usr/lib/python3/dist-packages (from mediapipe==dev) (20.3.0)
Requirement already satisfied: six in /usr/lib/python3/dist-packages (from absl-py->mediapipe==dev) (1.16.0)
Collecting packaging>=20.0
  Downloading packaging-21.3-py3-none-any.whl (40 kB)
     |████████████████████████████████| 40 kB 4.0 MB/s
Requirement already satisfied: pillow>=6.2.0 in /home/gloria/.local/lib/python3.9/site-packages (from matplotlib->mediapipe==dev) (8.4.0)
Collecting pyparsing>=2.2.1
  Downloading pyparsing-3.0.6-py3-none-any.whl (97 kB)
     |████████████████████████████████| 97 kB 3.6 MB/s
Collecting kiwisolver>=1.0.1
  Downloading kiwisolver-1.3.2-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.4 MB)
     |████████████████████████████████| 1.4 MB 8.2 MB/s
Collecting fonttools>=4.22.0
  Downloading fonttools-4.28.4-py3-none-any.whl (885 kB)
     |████████████████████████████████| 885 kB 7.9 MB/s
Collecting cycler>=0.10
  Downloading cycler-0.11.0-py3-none-any.whl (6.4 kB)
Collecting python-dateutil>=2.7
  Downloading python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
     |████████████████████████████████| 247 kB 6.0 MB/s
Installing collected packages: pyparsing, python-dateutil, packaging, kiwisolver, fonttools, cycler, protobuf, opencv-contrib-python, matplotlib, absl-py, mediapipe
  WARNING: The scripts fonttools, pyftmerge, pyftsubset and ttx are installed in '/home/gloria/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed absl-py-1.0.0 cycler-0.11.0 fonttools-4.28.4 kiwisolver-1.3.2 matplotlib-3.5.1 mediapipe-dev opencv-contrib-python-4.5.4.60 packaging-21.3 protobuf-3.19.1 pyparsing-3.0.6 python-dateutil-2.8.2