Penginstalan

Menginstal di Debian dan Ubuntu

  1. Instal Bazelisk.

    Ikuti dokumentasi Bazel resmi untuk menginstal Bazelisk.

  2. Lihat repositori MediaPipe.

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

    Opsi 1. Gunakan alat pengelola paket untuk menginstal library OpenCV yang telah dikompilasi sebelumnya. FFmpeg akan diinstal melalui libopencv-video-dev.

    OS 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
    

    Catatan. Di Debian 11/Ubuntu 21.04 saat OpenCV 4.5 diinstal dengan libopencv-video-dev, libopencv-contrib-dev juga harus diinstal.

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

    opencv_linux.BUILD dan WORKSPACE MediaPipe sudah dikonfigurasi untuk OpenCV 2/3 dan akan berfungsi dengan benar di arsitektur apa pun:

    # 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",
      ],
    )
    

    Untuk OpenCV 4, Anda perlu mengubah opencv_linux.BUILD dengan mempertimbangkan arsitektur saat ini:

    # 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",
      ],
    )
    

    Opsi 2. Jalankan setup_opencv.sh untuk mem-build OpenCV secara otomatis dari sumber dan mengubah konfigurasi OpenCV MediaPipe. Opsi ini akan otomatis melakukan semua langkah yang ditentukan dalam Opsi 3.

    Opsi 3. Ikuti dokumentasi OpenCV untuk mem-build OpenCV secara manual dari kode sumber.

    Anda mungkin perlu mengubah WORKSPACE dan opencv_linux.BUILD untuk mengarahkan MediaPipe ke library OpenCV Anda sendiri. Asumsikan OpenCV akan diinstal ke /usr/local/ yang direkomendasikan secara default.

    Penyiapan 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",
      ],
    )
    

    Penyiapan 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",
      ],
    )
    

    Penyiapan FFmpeg saat ini ditentukan di ffmpeg_linux.BUILD dan akan berfungsi untuk arsitektur apa pun:

    # 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. Untuk menjalankan contoh desktop hanya di Linux (bukan di OS X) dengan akselerasi GPU.

    # 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. Jalankan contoh Hello World! dalam 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!
    

Jika Anda mengalami error build, baca Pemecahan masalah untuk menemukan solusi dari beberapa masalah build umum.

Menginstal di CentOS

Pernyataan penyangkalan: Menjalankan MediaPipe di CentOS bersifat eksperimental.

  1. Instal Bazelisk.

    Ikuti dokumentasi Bazel resmi untuk menginstal Bazelisk.

  2. Lihat repositori MediaPipe.

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

    Opsi 1. Gunakan alat pengelola paket untuk menginstal versi yang telah dikompilasi sebelumnya.

    $ sudo yum install opencv-devel
    

    Opsi 2. Build OpenCV dari kode sumber.

    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. Jalankan contoh Hello World! dalam 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!
    

Jika Anda mengalami error build, baca Pemecahan masalah untuk menemukan solusi dari beberapa masalah build umum.

Menginstal di macOS

  1. Persiapan:

    • Instal Homebrew.
    • Instal Xcode dan Alat Command Line-nya oleh xcode-select --install.
  2. Instal Bazelisk.

    Ikuti dokumentasi Bazel resmi untuk menginstal Bazelisk.

  3. Lihat repositori MediaPipe.

    $ git clone --depth 1 https://github.com/google/mediapipe.git
    
    $ cd mediapipe
    
  4. Instal OpenCV dan FFmpeg.

    Opsi 1. Gunakan alat pengelola paket HomeBrew untuk menginstal library OpenCV 3 yang telah dikompilasi sebelumnya. FFmpeg akan diinstal melalui OpenCV.

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

    Opsi 2. Gunakan alat pengelola paket MacPorts untuk menginstal library 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. Pastikan Python 3 dan library "six" Python telah diinstal.

    $ 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. Jalankan contoh Hello World! dalam 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!
    

Jika Anda mengalami error build, baca Pemecahan masalah untuk menemukan solusi dari beberapa masalah build umum.

Menginstal di Windows

Pernyataan penyangkalan: Menjalankan MediaPipe di Windows bersifat eksperimental.

  1. Instal MSYS2 dan edit variabel lingkungan %PATH%.

    Jika MSYS2 diinstal ke C:\msys64, tambahkan C:\msys64\usr\bin ke variabel lingkungan %PATH% Anda.

  2. Instal paket yang diperlukan.

    C:\> pacman -S git patch unzip
    
  3. Instal Python dan izinkan file yang dapat dieksekusi untuk mengedit variabel lingkungan %PATH%.

    Download file yang dapat dieksekusi Python Windows dari https://www.python.org/downloads dan instal.

  4. Menginstal Visual C++ Build Tools dan WinSDK

    Buka situs VisualStudio, download alat build, dan instal Microsoft Visual C++ 2022 Redistributable dan Microsoft Build Tools 2022.

    Download WinSDK dari situs resmi Microsoft dan instal.

  5. Instal Bazel atau Bazelisk dan tambahkan lokasi file yang dapat dieksekusi Bazel ke variabel lingkungan %PATH%.

    Opsi 1. Ikuti dokumentasi Bazel resmi untuk menginstal Bazel 6.5.0 atau yang lebih tinggi.

    Opsi 2. Ikuti dokumentasi Bazel resmi untuk menginstal Bazelisk.

  6. Menetapkan variabel Bazel. Pelajari detail selengkapnya tentang "Build on Windows" dalam dokumentasi resmi Bazel.

    # Please find the exact paths and version numbers from your local version.
    C:\> set BAZEL_VS=C:\Program Files (x86)\Microsoft Visual Studio\<version>\BuildTools
    C:\> set BAZEL_VC=C:\Program Files (x86)\Microsoft Visual Studio\<version>\BuildTools\VC
    C:\> set BAZEL_VC_FULL_VERSION=<Your local VC version>
    C:\> set BAZEL_WINSDK_FULL_VERSION=<Your local WinSDK version>
    
  7. Lihat repositori 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. Instal OpenCV.

    Download file yang dapat dieksekusi Windows dari https://opencv.org/releases/ dan instal. MediaPipe 0.10.x mendukung OpenCV 3.4.10. Jangan lupa untuk mengedit file WORKSPACE jika OpenCV tidak diinstal di C:\opencv.

    new_local_repository(
        name = "windows_opencv",
        build_file = "@//third_party:opencv_windows.BUILD",
        path = "C:\\<path to opencv>\\build",
    )
    
  9. Jalankan contoh Hello World! dalam 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!
    

Jika Anda mengalami error build, baca Pemecahan masalah untuk menemukan solusi dari beberapa masalah build umum.

Menginstal di Windows Subsystem for Linux (WSL)

  1. Ikuti petunjuk untuk menginstal Windows Subsystem for Linux (Ubuntu).

  2. Instal ADB Windows dan mulai server ADB di Windows.

  3. Luncurkan WSL.

  4. Instal paket yang diperlukan.

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

    Ikuti dokumentasi Bazel resmi untuk menginstal Bazelisk.

  6. Lihat repositori MediaPipe.

    username@DESKTOP-TMVLBJ1:~$ git clone --depth 1 https://github.com/google/mediapipe.git
    
    username@DESKTOP-TMVLBJ1:~$ cd mediapipe
    
  7. Instal OpenCV dan FFmpeg.

    Opsi 1. Gunakan alat pengelola paket untuk menginstal library OpenCV yang telah dikompilasi sebelumnya. FFmpeg akan diinstal melalui 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
    

    Opsi 2. Jalankan setup_opencv.sh untuk mem-build OpenCV secara otomatis dari sumber dan mengubah konfigurasi OpenCV MediaPipe.

    Opsi 3. Ikuti dokumentasi OpenCV untuk mem-build OpenCV secara manual dari kode sumber.

    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. Jalankan contoh Hello World! dalam 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!
    

Jika Anda mengalami error build, baca Pemecahan masalah untuk menemukan solusi dari beberapa masalah build umum.

Menginstal menggunakan Docker

Tindakan ini akan menggunakan image Docker yang akan mengisolasi penginstalan mediapipe dari sistem lainnya.

  1. Instal Docker di sistem host Anda.

  2. Build image docker dengan tag "mediapipe".

    $ 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. Jalankan contoh Hello World! dalam C++.

    $ 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!
    

Jika Anda mengalami error build, baca Pemecahan masalah untuk menemukan solusi dari beberapa masalah build umum.

  1. Build contoh Android MediaPipe.

    $ 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