安裝

在 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 程式庫。系統會透過 libopencv-video-dev 安裝 FFmpeg。

    作業系統 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! 中的 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! 中的 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. 執行 Hello World! 中的 C++ 範例

    $ 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++ Build Tools 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. 執行 Hello World! 中的 C++ 範例

    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!
    

如果遇到建構錯誤,請參閱疑難排解,瞭解幾種常見建構問題的解決方法。

在 Windows Subsystem for Linux (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 程式庫。FFmpeg 會透過 libopencv-video-dev 安裝。

    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. 執行 Hello World! 中的 C++ 範例

    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