安装

在 Debian 和 Ubuntu 上安装

  1. 安装 Bazelisk。

    按照官方 Bazel 文档安装 Bazelisk。

  2. 检出 MediaPipe 代码库。

    $ cd $HOME
    $ git clone --depth 1 https://github.com/google/mediapipe.git
    
    # Change directory into MediaPipe root directory
    $ cd mediapipe
    
  3. 安装 OpenCV 和 FFmpeg。

    选项 1。使用软件包管理器工具安装预编译的 OpenCV 库。FFmpeg 将通过 libopencv-video-dev 安装。

    操作系统 OpenCV
    Debian 9 (stretch) 2.4
    Debian 10 (buster) 3.2
    Debian 11 (bullseye) 4.5
    Ubuntu 16.04 LTS 2.4
    Ubuntu 18.04 LTS 3.2
    Ubuntu 20.04 LTS 4.2
    Ubuntu 20.04 LTS 4.2
    Ubuntu 21.04 4.5
    $ sudo apt-get install -y \
        libopencv-core-dev \
        libopencv-highgui-dev \
        libopencv-calib3d-dev \
        libopencv-features2d-dev \
        libopencv-imgproc-dev \
        libopencv-video-dev
    

    注意:在 Debian 11/Ubuntu 21.04 上,使用 libopencv-video-dev 安装 OpenCV 4.5 时,还应安装 libopencv-contrib-dev

    $ sudo apt-get install -y libopencv-contrib-dev
    

    MediaPipe 的 opencv_linux.BUILDWORKSPACE 已针对 OpenCV 2/3 进行配置,应该可以在任何架构上正常运行:

    # WORKSPACE
    new_local_repository(
      name = "linux_opencv",
      build_file = "@//third_party:opencv_linux.BUILD",
      path = "/usr",
    )
    
    # opencv_linux.BUILD for OpenCV 2/3 installed from Debian package
    cc_library(
      name = "opencv",
      linkopts = [
        "-l:libopencv_core.so",
        "-l:libopencv_calib3d.so",
        "-l:libopencv_features2d.so",
        "-l:libopencv_highgui.so",
        "-l:libopencv_imgcodecs.so",
        "-l:libopencv_imgproc.so",
        "-l:libopencv_video.so",
        "-l:libopencv_videoio.so",
      ],
    )
    

    对于 OpenCV 4,您需要根据当前架构修改 opencv_linux.BUILD

    # WORKSPACE
    new_local_repository(
      name = "linux_opencv",
      build_file = "@//third_party:opencv_linux.BUILD",
      path = "/usr",
    )
    
    # opencv_linux.BUILD for OpenCV 4 installed from Debian package
    cc_library(
      name = "opencv",
      hdrs = glob([
        # Uncomment according to your multiarch value (gcc -print-multiarch):
        #  "include/aarch64-linux-gnu/opencv4/opencv2/cvconfig.h",
        #  "include/arm-linux-gnueabihf/opencv4/opencv2/cvconfig.h",
        #  "include/x86_64-linux-gnu/opencv4/opencv2/cvconfig.h",
        "include/opencv4/opencv2/**/*.h*",
      ]),
      includes = [
        # Uncomment according to your multiarch value (gcc -print-multiarch):
        #  "include/aarch64-linux-gnu/opencv4/",
        #  "include/arm-linux-gnueabihf/opencv4/",
        #  "include/x86_64-linux-gnu/opencv4/",
        "include/opencv4/",
      ],
      linkopts = [
        "-l:libopencv_core.so",
        "-l:libopencv_calib3d.so",
        "-l:libopencv_features2d.so",
        "-l:libopencv_highgui.so",
        "-l:libopencv_imgcodecs.so",
        "-l:libopencv_imgproc.so",
        "-l:libopencv_video.so",
        "-l:libopencv_videoio.so",
      ],
    )
    

    方法 2.运行 setup_opencv.sh 以自动从源代码构建 OpenCV 并修改 MediaPipe 的 OpenCV 配置。此选项会自动执行选项 3 中定义的所有步骤。

    选项 3。按照 OpenCV 的文档手动从源代码构建 OpenCV。

    您可能需要修改 WORKSPACEopencv_linux.BUILD,以将 MediaPipe 指向您自己的 OpenCV 库。假设 OpenCV 将安装到默认推荐的 /usr/local/

    OpenCV 2/3 设置:

    # WORKSPACE
    new_local_repository(
      name = "linux_opencv",
      build_file = "@//third_party:opencv_linux.BUILD",
      path = "/usr/local",
    )
    
    # opencv_linux.BUILD for OpenCV 2/3 installed to /usr/local
    cc_library(
      name = "opencv",
      linkopts = [
        "-L/usr/local/lib",
        "-l:libopencv_core.so",
        "-l:libopencv_calib3d.so",
        "-l:libopencv_features2d.so",
        "-l:libopencv_highgui.so",
        "-l:libopencv_imgcodecs.so",
        "-l:libopencv_imgproc.so",
        "-l:libopencv_video.so",
        "-l:libopencv_videoio.so",
      ],
    )
    

    OpenCV 4 设置:

    # WORKSPACE
    new_local_repository(
      name = "linux_opencv",
      build_file = "@//third_party:opencv_linux.BUILD",
      path = "/usr/local",
    )
    
    # opencv_linux.BUILD for OpenCV 4 installed to /usr/local
    cc_library(
      name = "opencv",
      hdrs = glob([
        "include/opencv4/opencv2/**/*.h*",
      ]),
      includes = [
        "include/opencv4/",
      ],
      linkopts = [
        "-L/usr/local/lib",
        "-l:libopencv_core.so",
        "-l:libopencv_calib3d.so",
        "-l:libopencv_features2d.so",
        "-l:libopencv_highgui.so",
        "-l:libopencv_imgcodecs.so",
        "-l:libopencv_imgproc.so",
        "-l:libopencv_video.so",
        "-l:libopencv_videoio.so",
      ],
    )
    

    当前的 FFmpeg 设置在 ffmpeg_linux.BUILD 中定义,适用于任何架构:

    # WORKSPACE
    new_local_repository(
      name = "linux_ffmpeg",
      build_file = "@//third_party:ffmpeg_linux.BUILD",
      path = "/usr"
    )
    
    # ffmpeg_linux.BUILD for FFmpeg installed from Debian package
    cc_library(
      name = "libffmpeg",
      linkopts = [
        "-l:libavcodec.so",
        "-l:libavformat.so",
        "-l:libavutil.so",
      ],
    )
    
  4. 仅适用于使用 GPU 加速的 Linux(而非 OS X)运行桌面示例。

    # Requires a GPU with EGL driver support.
    # Can use mesa GPU libraries for desktop, (or Nvidia/AMD equivalent).
    sudo apt-get install mesa-common-dev libegl1-mesa-dev libgles2-mesa-dev
    
    # To compile with GPU support, replace
    --define MEDIAPIPE_DISABLE_GPU=1
    # with
    --copt -DMESA_EGL_NO_X11_HEADERS --copt -DEGL_NO_X11
    # when building GPU examples.
    
  5. 运行 Hello World! in C++ 示例

    $ export GLOG_logtostderr=1
    
    # if you are running on Linux desktop with CPU only
    $ bazel run --define MEDIAPIPE_DISABLE_GPU=1 \
        mediapipe/examples/desktop/hello_world:hello_world
    
    # If you are running on Linux desktop with GPU support enabled (via mesa drivers)
    $ bazel run --copt -DMESA_EGL_NO_X11_HEADERS --copt -DEGL_NO_X11 \
        mediapipe/examples/desktop/hello_world:hello_world
    
    # Should print:
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    

如果您遇到构建错误,请参阅问题排查,了解一些常见构建问题的解决方案。

在 CentOS 上安装

免责声明:在 CentOS 上运行 MediaPipe 目前处于实验阶段。

  1. 安装 Bazelisk。

    按照官方 Bazel 文档安装 Bazelisk。

  2. 结账 MediaPipe 代码库。

    $ git clone --depth 1 https://github.com/google/mediapipe.git
    
    # Change directory into MediaPipe root directory
    $ cd mediapipe
    
  3. 安装 OpenCV。

    方法 1. 使用软件包管理器工具安装预编译版本。

    $ sudo yum install opencv-devel
    

    方法 2. 从源代码构建 OpenCV。

    new_local_repository(
        name = "linux_opencv",
        build_file = "@//third_party:opencv_linux.BUILD",
        path = "/usr/local",
    )
    
    new_local_repository(
        name = "linux_ffmpeg",
        build_file = "@//third_party:ffmpeg_linux.BUILD",
        path = "/usr/local",
    )
    
    cc_library(
        name = "opencv",
        srcs = glob(
            [
                "lib/libopencv_core.so",
                "lib/libopencv_highgui.so",
                "lib/libopencv_imgcodecs.so",
                "lib/libopencv_imgproc.so",
                "lib/libopencv_video.so",
                "lib/libopencv_videoio.so",
            ],
        ),
        hdrs = glob([
            # For OpenCV 3.x
            "include/opencv2/**/*.h*",
            # For OpenCV 4.x
            # "include/opencv4/opencv2/**/*.h*",
        ]),
        includes = [
            # For OpenCV 3.x
            "include/",
            # For OpenCV 4.x
            # "include/opencv4/",
        ],
        linkstatic = 1,
        visibility = ["//visibility:public"],
    )
    
    cc_library(
        name = "libffmpeg",
        srcs = glob(
            [
                "lib/libav*.so",
            ],
        ),
        hdrs = glob(["include/libav*/*.h"]),
        includes = ["include"],
        linkopts = [
            "-lavcodec",
            "-lavformat",
            "-lavutil",
        ],
        linkstatic = 1,
        visibility = ["//visibility:public"],
    )
    
  4. 运行 Hello World! in C++ 示例

    $ export GLOG_logtostderr=1
    # Need bazel flag 'MEDIAPIPE_DISABLE_GPU=1' if you are running on Linux desktop with CPU only
    $ bazel run --define MEDIAPIPE_DISABLE_GPU=1 \
        mediapipe/examples/desktop/hello_world:hello_world
    
    # Should print:
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    

如果您遇到构建错误,请参阅问题排查,了解一些常见构建问题的解决方案。

在 macOS 上安装

  1. 准备工作:

    • 安装 Homebrew
    • 通过 xcode-select --install 安装 Xcode 及其命令行工具。
  2. 安装 Bazelisk。

    按照官方 Bazel 文档安装 Bazelisk。

  3. 检出 MediaPipe 代码库。

    $ git clone --depth 1 https://github.com/google/mediapipe.git
    
    $ cd mediapipe
    
  4. 安装 OpenCV 和 FFmpeg。

    方法 1. 使用 HomeBrew 软件包管理器工具安装预编译的 OpenCV 3 库。FFmpeg 将通过 OpenCV 安装。

    $ brew install opencv@3
    
    # There is a known issue caused by the glog dependency. Uninstall glog.
    $ brew uninstall --ignore-dependencies glog
    

    方法 2. 使用 MacPorts 软件包管理器工具安装 OpenCV 库。

    $ port install opencv
    
    new_local_repository(
        name = "macos_opencv",
        build_file = "@//third_party:opencv_macos.BUILD",
        path = "/opt",
    )
    
    new_local_repository(
        name = "macos_ffmpeg",
        build_file = "@//third_party:ffmpeg_macos.BUILD",
        path = "/opt",
    )
    
    cc_library(
        name = "opencv",
        srcs = glob(
            [
                "local/lib/libopencv_core.dylib",
                "local/lib/libopencv_highgui.dylib",
                "local/lib/libopencv_imgcodecs.dylib",
                "local/lib/libopencv_imgproc.dylib",
                "local/lib/libopencv_video.dylib",
                "local/lib/libopencv_videoio.dylib",
            ],
        ),
        hdrs = glob(["local/include/opencv2/**/*.h*"]),
        includes = ["local/include/"],
        linkstatic = 1,
        visibility = ["//visibility:public"],
    )
    
    cc_library(
        name = "libffmpeg",
        srcs = glob(
            [
                "local/lib/libav*.dylib",
            ],
        ),
        hdrs = glob(["local/include/libav*/*.h"]),
        includes = ["local/include/"],
        linkopts = [
            "-lavcodec",
            "-lavformat",
            "-lavutil",
        ],
        linkstatic = 1,
        visibility = ["//visibility:public"],
    )
    
  5. 确保已安装 Python 3 和 Python“six”库。

    $ brew install python
    $ sudo ln -s -f /usr/local/bin/python3.7 /usr/local/bin/python
    $ python --version
    Python 3.7.4
    $ pip3 install --user six
    
  6. 运行 C++ 版“Hello World!”示例

    $ export GLOG_logtostderr=1
    # Need bazel flag 'MEDIAPIPE_DISABLE_GPU=1' as desktop GPU is currently not supported
    $ bazel run --define MEDIAPIPE_DISABLE_GPU=1 \
        mediapipe/examples/desktop/hello_world:hello_world
    
    # Should print:
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    

如果您遇到构建错误,请参阅问题排查,了解一些常见构建问题的解决方案。

在 Windows 上安装

免责声明:在 Windows 上运行 MediaPipe 尚处于实验阶段。

  1. 安装 MSYS2 并修改 %PATH% 环境变量。

    如果 MSYS2 已安装到 C:\msys64 下,请将 C:\msys64\usr\bin 添加到 %PATH% 环境变量中。

  2. 安装必要的软件包。

    C:\> pacman -S git patch unzip
    
  3. 安装 Python 并允许可执行文件修改 %PATH% 环境变量。

    https://www.python.org/downloads 下载 Python Windows 可执行文件,然后进行安装。

  4. 安装 Visual C++ 生成工具 2019 和 WinSDK

    前往 VisualStudio 网站,下载构建工具,然后安装 Microsoft Visual C++ 2019 Redistributable 和 Microsoft Build Tools 2019。

    Microsoft 官方网站下载 WinSDK 并进行安装。

  5. 安装 Bazel 或 Bazelisk,并将 Bazel 可执行文件的位置添加到 %PATH% 环境变量中。

    方法 1. 按照 Bazel 官方文档中的说明安装 Bazel 6.5.0 或更高版本。

    方法 2. 按照官方 Bazel 文档安装 Bazelisk。

  6. 设置 Bazel 变量。如需详细了解“在 Windows 上构建”,请参阅 Bazel 官方文档。

    # Please find the exact paths and version numbers from your local version.
    C:\> set BAZEL_VS=C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools
    C:\> set BAZEL_VC=C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC
    C:\> set BAZEL_VC_FULL_VERSION=<Your local VC version>
    C:\> set BAZEL_WINSDK_FULL_VERSION=<Your local WinSDK version>
    
  7. 检出 MediaPipe 代码库。

    C:\Users\Username\mediapipe_repo> git clone --depth 1 https://github.com/google/mediapipe.git
    
    # Change directory into MediaPipe root directory
    C:\Users\Username\mediapipe_repo> cd mediapipe
    
  8. 安装 OpenCV。

    https://opencv.org/releases/ 下载 Windows 可执行文件并进行安装。MediaPipe 0.10.x 支持 OpenCV 3.4.10。如果 OpenCV 未安装在 C:\opencv 中,请记得修改 WORKSPACE 文件。

    new_local_repository(
        name = "windows_opencv",
        build_file = "@//third_party:opencv_windows.BUILD",
        path = "C:\\<path to opencv>\\build",
    )
    
  9. 运行 C++ 版“Hello World!”示例

    C:\Users\Username\mediapipe_repo>bazel build -c opt --define MEDIAPIPE_DISABLE_GPU=1 --action_env PYTHON_BIN_PATH="C://python_36//python.exe" mediapipe/examples/desktop/hello_world
    
    C:\Users\Username\mediapipe_repo>set GLOG_logtostderr=1
    
    C:\Users\Username\mediapipe_repo>bazel-bin\mediapipe\examples\desktop\hello_world\hello_world.exe
    
    # should print:
    # I20200514 20:43:12.277598  1200 hello_world.cc:56] Hello World!
    # I20200514 20:43:12.278597  1200 hello_world.cc:56] Hello World!
    # I20200514 20:43:12.279618  1200 hello_world.cc:56] Hello World!
    # I20200514 20:43:12.279618  1200 hello_world.cc:56] Hello World!
    # I20200514 20:43:12.279618  1200 hello_world.cc:56] Hello World!
    # I20200514 20:43:12.279618  1200 hello_world.cc:56] Hello World!
    # I20200514 20:43:12.279618  1200 hello_world.cc:56] Hello World!
    # I20200514 20:43:12.279618  1200 hello_world.cc:56] Hello World!
    # I20200514 20:43:12.279618  1200 hello_world.cc:56] Hello World!
    # I20200514 20:43:12.280613  1200 hello_world.cc:56] Hello World!
    

如果您遇到构建错误,请参阅问题排查,了解一些常见构建问题的解决方案。

在适用于 Linux 的 Windows 子系统 (WSL) 上安装

  1. 按照说明安装适用于 Linux (Ubuntu) 的 Windows 子系统。

  2. 安装 Windows ADB 并在 Windows 中启动 ADB 服务器。

  3. 启动 WSL。

  4. 安装所需的软件包。

    username@DESKTOP-TMVLBJ1:~$ sudo apt-get update && sudo apt-get install -y build-essential git python zip adb openjdk-8-jdk
    
  5. 安装 Bazelisk。

    按照官方 Bazel 文档安装 Bazelisk。

  6. 检出 MediaPipe 代码库。

    username@DESKTOP-TMVLBJ1:~$ git clone --depth 1 https://github.com/google/mediapipe.git
    
    username@DESKTOP-TMVLBJ1:~$ cd mediapipe
    
  7. 安装 OpenCV 和 FFmpeg。

    方法 1. 使用软件包管理器工具安装预编译的 OpenCV 库。将通过 libopencv-video-dev 安装 FFmpeg。

    username@DESKTOP-TMVLBJ1:~/mediapipe$ sudo apt-get install libopencv-core-dev libopencv-highgui-dev \
                           libopencv-calib3d-dev libopencv-features2d-dev \
                           libopencv-imgproc-dev libopencv-video-dev
    

    方法 2. 运行 setup_opencv.sh 以自动基于源代码构建 OpenCV,并修改 MediaPipe 的 OpenCV 配置。

    方法 3. 请按照 OpenCV 的文档从源代码手动构建 OpenCV。

    new_local_repository(
        name = "linux_opencv",
        build_file = "@//third_party:opencv_linux.BUILD",
        path = "/usr/local",
    )
    
    cc_library(
        name = "opencv",
        srcs = glob(
            [
                "lib/libopencv_core.so",
                "lib/libopencv_highgui.so",
                "lib/libopencv_imgcodecs.so",
                "lib/libopencv_imgproc.so",
                "lib/libopencv_video.so",
                "lib/libopencv_videoio.so",
            ],
        ),
        hdrs = glob(["include/opencv4/**/*.h*"]),
        includes = ["include/opencv4/"],
        linkstatic = 1,
        visibility = ["//visibility:public"],
    )
    
  8. 运行 C++ 版“Hello World!”示例

    username@DESKTOP-TMVLBJ1:~/mediapipe$ export GLOG_logtostderr=1
    
    # Need bazel flag 'MEDIAPIPE_DISABLE_GPU=1' as desktop GPU is currently not supported
    username@DESKTOP-TMVLBJ1:~/mediapipe$ bazel run --define MEDIAPIPE_DISABLE_GPU=1 \
        mediapipe/examples/desktop/hello_world:hello_world
    
    # Should print:
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    

如果您遇到构建错误,请参阅问题排查,了解一些常见构建问题的解决方案。

使用 Docker 进行安装

这将使用一个 Docker 映像,以便将 MediaPipe 的安装与系统的其余部分隔离开来。

  1. 在宿主系统上安装 Docker

  2. 构建标记为“mediapipe”的 Docker 映像。

    $ git clone --depth 1 https://github.com/google/mediapipe.git
    $ cd mediapipe
    $ docker build --tag=mediapipe .
    
    # Should print:
    # Sending build context to Docker daemon  147.8MB
    # Step 1/9 : FROM ubuntu:latest
    # latest: Pulling from library/ubuntu
    # 6abc03819f3e: Pull complete
    # 05731e63f211: Pull complete
    # ........
    # See http://bazel.build/docs/getting-started.html to start a new project!
    # Removing intermediate container 82901b5e79fa
    # ---> f5d5f402071b
    # Step 9/9 : COPY . /edge/mediapipe/
    # ---> a95c212089c5
    # Successfully built a95c212089c5
    # Successfully tagged mediapipe:latest
    
  3. 运行 C++ 版“Hello World!”示例

    $ docker run -it --name mediapipe mediapipe:latest
    
    root@bca08b91ff63:/mediapipe# GLOG_logtostderr=1 bazel run --define MEDIAPIPE_DISABLE_GPU=1 mediapipe/examples/desktop/hello_world
    
    # Should print:
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    

如果您遇到构建错误,请参阅问题排查,了解一些常见构建问题的解决方案。

  1. 构建 MediaPipe Android 示例。

    $ docker run -it --name mediapipe mediapipe:latest
    
    root@bca08b91ff63:/mediapipe# bash ./setup_android_sdk_and_ndk.sh
    
    # Should print:
    # Android NDK is now installed. Consider setting $ANDROID_NDK_HOME environment variable to be /root/Android/Sdk/ndk-bundle/android-ndk-r19c
    # Set android_ndk_repository and android_sdk_repository in WORKSPACE
    # Done
    
    root@bca08b91ff63:/mediapipe# bazel build -c opt --config=android_arm64 mediapipe/examples/android/src/java/com/google/mediapipe/apps/objectdetectiongpu:objectdetectiongpu
    
    # Should print:
    # Target //mediapipe/examples/android/src/java/com/google/mediapipe/apps/objectdetectiongpu:objectdetectiongpu up-to-date:
    # bazel-bin/mediapipe/examples/android/src/java/com/google/mediapipe/apps/objectdetectiongpu/objectdetectiongpu_deploy.jar
    # bazel-bin/mediapipe/examples/android/src/java/com/google/mediapipe/apps/objectdetectiongpu/objectdetectiongpu_unsigned.apk
    # bazel-bin/mediapipe/examples/android/src/java/com/google/mediapipe/apps/objectdetectiongpu/objectdetectiongpu.apk
    # INFO: Elapsed time: 144.462s, Critical Path: 79.47s
    # INFO: 1958 processes: 1 local, 1863 processwrapper-sandbox, 94 worker.
    # INFO: Build completed successfully, 2028 total actions