在 Debian 和 Ubuntu 上安装
安装 Bazelisk。
关注官方 Bazel 文档 安装 Bazelisk
结账 MediaPipe 代码库。
$ cd $HOME $ git clone --depth 1 https://github.com/google/mediapipe.git # Change directory into MediaPipe root directory $ cd mediapipe
安装 OpenCV 和 FFmpeg。
选项 1。使用软件包管理器工具来安装预编译的 OpenCV 库。将通过
libopencv-video-dev
安装 FFmpeg。操作系统 OpenCV Debian 9(拉伸) 2.4 Debian 10 (buster) 3.2 Debian 11(靶心) 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 上(如果安装了 OpenCV 4.5) 此外,还应安装
libopencv-video-dev
和libopencv-contrib-dev
。$ sudo apt-get install -y libopencv-contrib-dev
已配置 MediaPipe 的
opencv_linux.BUILD
和WORKSPACE
适用于 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。遵循 OpenCV 的 文档 从源代码手动构建 OpenCV。
您可能需要将
WORKSPACE
和opencv_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", ], )
仅在使用 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.
运行 Hello World!定义)。
$ 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 尚处于实验阶段。
安装 Bazelisk。
关注官方 Bazel 文档 安装 Bazelisk
结账 MediaPipe 代码库。
$ git clone --depth 1 https://github.com/google/mediapipe.git # Change directory into MediaPipe root directory $ cd mediapipe
安装 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"], )
运行 Hello World!定义)。
$ 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 上安装
准备工作:
安装 Bazelisk。
关注官方 Bazel 文档 安装 Bazelisk
结账 MediaPipe 代码库。
$ git clone --depth 1 https://github.com/google/mediapipe.git $ cd mediapipe
安装 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"], )
确保 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
运行 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 尚处于实验阶段。
安装 MSYS2 并修改
%PATH%
环境 变量。如果 MSYS2 已安装到
C:\msys64
,请将C:\msys64\usr\bin
添加到您的%PATH%
环境变量。安装必要的软件包。
C:\> pacman -S git patch unzip
安装 Python 并允许可执行文件修改
%PATH%
环境 变量。从以下位置下载 Python Windows 可执行文件: https://www.python.org/downloads 并安装。
安装 Visual C++ Build Tools 2019 和 WinSDK
前往 Visual Studio 网站 下载构建工具,并安装 Microsoft Visual C++ 2019 Redistributable 和 Microsoft Build Tools 2019。
从以下位置下载 WinSDK: MicroSoft 官方网站 并安装。
安装 Bazel 或 Bazelisk,并将 Bazel 可执行文件的位置
%PATH%
环境变量。方法 1. 关注 Bazel 官方文档 安装 Bazel 6.1.1 或更高版本。
方法 2. 关注官方 Bazel 文档 安装 Bazelisk
设置 Bazel 变量。详细了解以下内容: “在 Windows 上构建” 。
# 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>
结账 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
安装 OpenCV。
从以下位置下载 Windows 可执行文件: https://opencv.org/releases/ 并安装。MediaPipe 0.10.x 支持 OpenCV 3.4.10。记得修改
WORKSPACE
文件(如果 OpenCV 未安装在C:\opencv
下)。new_local_repository( name = "windows_opencv", build_file = "@//third_party:opencv_windows.BUILD", path = "C:\\<path to opencv>\\build", )
运行 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) 上安装
按照 instruction 至 安装适用于 Linux (Ubuntu) 的 Windows 子系统。
在 Windows 中安装 Windows ADB 并启动 ADB 服务器。
启动 WSL。
安装所需的软件包。
username@DESKTOP-TMVLBJ1:~$ sudo apt-get update && sudo apt-get install -y build-essential git python zip adb openjdk-8-jdk
安装 Bazelisk。
关注官方 Bazel 文档 安装 Bazelisk
结账 MediaPipe 代码库。
username@DESKTOP-TMVLBJ1:~$ git clone --depth 1 https://github.com/google/mediapipe.git username@DESKTOP-TMVLBJ1:~$ cd mediapipe
安装 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"], )
运行 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 的安装与系统的其余部分隔离开来。
在以下位置安装 Docker: 主机系统
使用标记“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
运行 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!
如果您遇到构建错误,请 请参阅问题排查,查找针对多个 常见的构建问题
构建 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