kiyasuの日記

ハッピーうれピーよろしく哀愁

M1 MacでMediaPipeを動かす

Installation - mediapipe

基本はこれに沿って行うがいくつか気をつけるところあり

python

pythonコマンドのパスがmacにデフォルトの2.7になってたのでそれを3系統にする。

python version 切り替え mac - kiyasuの日記

% python --version
Python 3.9.7

こうなってれば良いです。

bazel

何も設定を変えずにhello_worldを動かそうとするとコケる。

% bazel run --define MEDIAPIPE_DISABLE_GPU=1 mediapipe/examples/desktop/hello_world:hello_world
2021/09/23 18:19:21 Downloading https://releases.bazel.build/3.7.2/release/bazel-3.7.2-darwin-arm64...
2021/09/23 18:19:22 could not download Bazel: HTTP GET https://releases.bazel.build/3.7.2/release/bazel-3.7.2-darwin-arm64 failed with error 404

これはbazeliskの設定を変えて使用するbazelのバージョンを変えればOK

vi ~/.zshenv

で設定ファイル開いて

export USE_BAZEL_VERSION=4.2.1

を追記して

source ~/.zshenv

でOK。と思いきや改めてhello_world実行するとエラーが出る。

xxxx/mediapipe/mediapipe/mediapipe/framework/BUILD:966:11: Compiling mediapipe/framework/scheduler_queue.cc failed: (Aborted): wrapped_clang failed: error executing command external/local_config_cc/wrapped_clang ‘-D_FORTIFY_SOURCE=1’ -fstack-protector -fcolor-diagnostics -Wall -Wthread-safety -Wself-assign -fno-omit-frame-pointer -O0 -DDEBUG ‘-std=c++11’ ... (remaining 60 argument(s) skipped)
Use --sandbox_debug to see verbose messages from the sandbox
mediapipe/framework/scheduler_queue.cc:212:3: error: expected expression
AUTORELEASEPOOL {
^
mediapipe/framework/scheduler_queue.cc:29:25: note: expanded from macro ‘AUTORELEASEPOOL’
#define AUTORELEASEPOOL @autoreleasepool
^
mediapipe/framework/scheduler_queue.cc:226:5: error: use of undeclared identifier ‘is_idle’
is_idle = IsIdle();
^
mediapipe/framework/scheduler_queue.cc:228:7: error: use of undeclared identifier ‘is_idle’
if (is_idle && idle_callback_) {
^
3 errors generated.
Error in child process ‘/usr/bin/xcrun’. 1
Target //mediapipe/examples/desktop/hello_world:hello_world failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 82.222s, Critical Path: 46.20s
INFO: 738 processes: 187 internal, 551 darwin-sandbox.
FAILED: Build did NOT complete successfully
FAILED: Build did NOT complete successfully

これはmediapipe/framework/scheduler_queue.cc開いて、21行目の

#define AUTORELEASEPOOL @autoreleasepool

#define AUTORELEASEPOOL

に変えればいいです。MediaPipe currently doesn't have the official support of apple silicon devicesだからこうなるらしいです。

2021/10/26追記

10/26時点で該当箇所が28行目〜

#ifdef __APPLE__
#define AUTORELEASEPOOL @autoreleasepool
#else
#define AUTORELEASEPOOL
#endif // __APPLE__

となってたけどこれ逆だよな。同じエラーが出た。ので

#ifdef __APPLE__
#define AUTORELEASEPOOL
#else
#define AUTORELEASEPOOL @autoreleasepool
#endif // __APPLE__


改めて実行

% bazel run --define MEDIAPIPE_DISABLE_GPU=1 \ mediapipe/examples/desktop/hello_world:hello_world
(略)
I20210923 18:56:03.526690 34651456 hello_world.cc:57] Hello World! I20210923 18:56:03.527341 34651456 hello_world.cc:57] Hello World!
I20210923 18:56:03.527349 34651456 hello_world.cc:57] Hello World!
I20210923 18:56:03.527355 34651456 hello_world.cc:57] Hello World!
I20210923 18:56:03.527361 34651456 hello_world.cc:57] Hello World!
I20210923 18:56:03.527367 34651456 hello_world.cc:57] Hello World!
I20210923 18:56:03.527374 34651456 hello_world.cc:57] Hello World!
I20210923 18:56:03.527379 34651456 hello_world.cc:57] Hello World!
I20210923 18:56:03.527385 34651456 hello_world.cc:57] Hello World!
I20210923 18:56:03.527391 34651456 hello_world.cc:57] Hello World!

参考

bazelisk from homebrew looks for macOS arm binary 4.0.0 and 404s · Issue #245 · bazelbuild/bazelisk · GitHub

GitHub - bazelbuild/bazelisk: A user-friendly launcher for Bazel.

HelloWorld on M1 Mac - githubmemory