見出し画像

MediaPipe C++: How to build for macOS and compile the example

Goal

Use MediaPipe in C++ on a macOS environment, similar to how it’s used in Python.

Environment

H/W: Mac mini M2 Pro 32GB + 2TB

Points that differ from the documentation

- Python is installed via conda.
- Although the documentation mentions Python3, the Python version installed via conda is used.

Important Notes


```bash
brew install bazelisk
```
- If you install bazelisk on an environment that already has bazel, Homebrew might provide additional setup instructions.
- Be sure to follow these instructions if prompted.
- In some cases, no instructions may appear, so confirm whether any are given.

Installation via Homebrew

If already installed, this step can be skipped.
```bash
brew install bazelisk
brew install opencv
brew install protobuf
brew install pkg-config
```

My Environment


```txt
$ sw_vers
ProductName: macOS
ProductVersion: 15.0.1
BuildVersion: 24A348

$ which python
/opt/anaconda3/envs/ML/bin/python

$ python --version
Python 3.12.4

$ brew list --versions | grep -y bazel
bazel 7.3.2
bazelisk 1.22.1

$ brew list --versions | grep -y opencv
opencv 4.10.0_12

$ brew list --versions | grep -y protobuf
protobuf 28.2 28.3

$ brew list --versions | grep -y pkg-config
pkg-config 0.29.2_3
```
Execute the following shell script if the above environment is correctly set up:
```bash
export PYTHON_BIN_PATH=$(which python)
export HERMETIC_PYTHON_VERSION=$(python -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')
export OPENCV_INCLUDE_PATH=$(brew --prefix opencv)/include/opencv4
export OPENCV_LIB_PATH=$(brew --prefix opencv)/lib

git clone https://github.com/google/mediapipe.git
cd mediapipe

bazel build -c opt --define MEDIAPIPE_DISABLE_GPU=1 \
--action_env PYTHON_BIN_PATH=$PYTHON_BIN_PATH \
--action_env OPENCV_INCLUDE_PATH=$OPENCV_INCLUDE_PATH \
--action_env OPENCV_LIB_PATH=$OPENCV_LIB_PATH \
mediapipe/examples/desktop/hello_world:hello_world

bazel-bin/mediapipe/examples/desktop/hello_world/hello_world
```

If the following output appears, the build, compilation, and execution of the example code were successful:
```txt
$ bazel-bin/mediapipe/examples/desktop/hello_world/hello_world
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1729944071.306982 6327274 hello_world.cc:58] Hello World!
I0000 00:00:1729944071.307023 6327274 hello_world.cc:58] Hello World!
I0000 00:00:1729944071.307025 6327274 hello_world.cc:58] Hello World!
I0000 00:00:1729944071.307026 6327274 hello_world.cc:58] Hello World!
I0000 00:00:1729944071.307027 6327274 hello_world.cc:58] Hello World!
I0000 00:00:1729944071.307028 6327274 hello_world.cc:58] Hello World!
I0000 00:00:1729944071.307028 6327274 hello_world.cc:58] Hello World!
I0000 00:00:1729944071.307029 6327274 hello_world.cc:58] Hello World!
I0000 00:00:1729944071.307030 6327274 hello_world.cc:58] Hello World!
I0000 00:00:1729944071.307031 6327274 hello_world.cc:58] Hello World!
```
Enjoy!


いいなと思ったら応援しよう!