MediaPipe 怎么编译 Coral Edge TPU 示例并部署到 Raspberry Pi 或 Coral Dev Board【免费下载链接】mediapipeCross-platform, customizable ML solutions for live and streaming media.项目地址: https://gitcode.com/GitHub_Trending/med/mediapipeMediaPipe 仓库在mediapipe/examples/coral下提供了两个跑在 Edge TPU 上的示例face_detection_tpu人脸检测和object_detection_tpu目标检测。如果你的目标板是 Raspberry PiARM32或 Coral Dev BoardARM64而编译机是 x86就需要交叉编译仓库自带了预配置好的 Docker 编译环境和 Makefile编译产物拷到板子上装好 OpenCV 运行库后即可运行。本文基于 Coral 示例文档、Makefile 和 demo_run_graph_main.cc 给出完整路径。两个示例与它们的配置文件编译目标都在 mediapipe/examples/coral/BUILD 中定义编译目标图形配置文件使用的模型mediapipe/examples/coral:face_detection_tpugraphs/face_detection_desktop_live.pbtxtmodels/face-detector-quantized_edgetpu.tflitemediapipe/examples/coral:object_detection_tpugraphs/object_detection_desktop_live.pbtxtmodels/object-detector-quantized_edgetpu.tflite标签文件 models/object_detection_labelmap.txt注意图形配置里的模型路径是相对仓库根目录写的如mediapipe/examples/coral/models/...tflite所以部署时必须保证目标板上的目录结构与仓库一致——这也是文档建议直接拷贝整个mediapipe文件夹的原因。主路径用 Docker 交叉编译在 x86 编译机上进入 MediaPipe 仓库根目录按目标平台选择PLATFORM# ARM32Raspberry Pi make -C mediapipe/examples/coral PLATFORMarmhf docker # ARM64Coral Dev Board make -C mediapipe/examples/coral PLATFORMarm64 docker这条命令会用 Dockerfile.armhf 或 Dockerfile.arm64 构建镜像并进入一个已装好交叉工具链crossbuild-essential-armhf/arm64、对应架构的libusb-1.0-0-dev、libopencv-*-dev和 bazel 的 shell。进入后执行对应的 bazel 命令--cpu必须与平台匹配# ARM32Raspberry Pi bazel build \ --crosstool_topcrosstool//:toolchains \ --compilergcc \ --cpuarmv7a \ --define darwinn_portable1 \ --define MEDIAPIPE_DISABLE_GPU1 \ --define MEDIAPIPE_EDGE_TPUusb \ --linkopt-l:libusb-1.0.so \ mediapipe/examples/coral:face_detection_tpu build # ARM64Coral Dev Board bazel build \ --crosstool_topcrosstool//:toolchains \ --compilergcc \ --cpuaarch64 \ --define darwinn_portable1 \ --define MEDIAPIPE_DISABLE_GPU1 \ --define MEDIAPIPE_EDGE_TPUusb \ --linkopt-l:libusb-1.0.so \ mediapipe/examples/coral:face_detection_tpu buildDocker 环境预定义了${BAZEL_CPU}armhf 镜像为armv7aarm64 镜像为aarch64上面两条命令也可以合并写成一条--cpu${BAZEL_CPU}即可目标换成mediapipe/examples/coral:object_detection_tpu就是目标检测示例。更省事的是直接用 Makefile 的build目标它已包含上述 bazel 参数目标由BAZEL_TARGET指定默认就是face_detection_tpu在编译机上一行完成make -C mediapipe/examples/coral \ BAZEL_TARGETmediapipe/examples/coral:face_detection_tpu \ build或把编译过程放进 Docker 一条命令跑完无需先进 shellmake -C mediapipe/examples/coral \ PLATFORMarmhf \ DOCKER_COMMANDmake -C mediapipe/examples/coral BAZEL_TARGETmediapipe/examples/coral:face_detection_tpu build \ docker一点差异需要知道文档中手写的 bazel 命令使用--define MEDIAPIPE_EDGE_TPUusb而 Makefile 的build目标内部写的是MEDIAPIPE_EDGE_TPUall同时启用 USB 和 PCIe 支持。两条路径产物都能运行 USB 版示例按你实际使用的命令保留即可。构建完成后二进制会被自动复制到仓库根目录下的out/platform目录平台取值见文档的汇总表架构PLATFORM输出目录目标板ARM32PLATFORMarmhfout/armv7aRaspberry PiARM64PLATFORMarm64out/aarch64Coral Dev Board同样的方式可以交叉编译仓库里其他目标文档给出的例子是mediapipe/examples/desktop/hand_tracking:hand_tracking_cpu。在 x86 机器上直接编译可选路径如果你的目标是在本机Linux x86运行示例也可以不做交叉编译。前提是安装libusb-1.0-0-dev然后bazel build \ --compilation_modeopt \ --define darwinn_portable1 \ --define MEDIAPIPE_DISABLE_GPU1 \ --define MEDIAPIPE_EDGE_TPUusb \ --linkopt-l:libusb-1.0.so \ mediapipe/examples/coral:face_detection_tpu buildMEDIAPIPE_EDGE_TPU的取值按 Coral 设备类型选择usb对应 Linux/macOS 上的 USB 设备pci对应 Linux 上的 PCIe 设备all同时启用两者。部署到目标板编译成功后把输出二进制连同模型、图形配置一起拷到目标板。文档建议直接拷贝整个mediapipe文件夹以省去挑选依赖文件的麻烦下面的userhost是目标板上的登录用户名和地址需要按实际环境替换scp -r mediapipe userhost:.在目标板上安装 OpenCV 运行时库需要 root 权限修改的是目标板系统而非编译机sudo apt-get install -y \ libopencv-core-dev \ libopencv-highgui-dev \ libopencv-calib3d-dev \ libopencv-features2d-dev \ libopencv-imgproc-dev \ libopencv-video-dev如果目标板要接 Coral USB 加速棒还要装libusb运行时库sudo apt-get install -y \ libusb-1.0-0运行与结果验证把 USB 摄像头和 Coral 设备接到目标板进入拷贝下来的目录运行# Face Detection GLOG_logtostderr1 ./face_detection_tpu --calculator_graph_config_file \ mediapipe/examples/coral/graphs/face_detection_desktop_live.pbtxt # Object Detection GLOG_logtostderr1 ./object_detection_tpu --calculator_graph_config_file \ mediapipe/examples/coral/graphs/object_detection_desktop_live.pbtxt运行行为的细节来自 demo_run_graph_main.cc不带--input_video_path时默认打开摄像头设备 0设置 640x480、30 FPS结果在名为 MediaPipe 的窗口里实时显示检测框以红色r: 255, g: 0, b: 0、线宽 4 绘制在视频上按任意键退出。可选传--input_video_path 视频文件改为处理本地视频视频播完自动结束。可选传--output_video_path 输出.mp4把结果保存为 .mp4avc1 编码而不是开窗显示。程序以GLOG_logtostderr1运行时日志输出到 stderr正常跑完会打印Success!失败时打印Failed to run the graph: 原因并以非零状态退出可以用这条判断启动/运行是否成功。限制与边界文档给出的 Coral 编译路径都带--define MEDIAPIPE_DISABLE_GPU1即这些构建不启用 GPU。图形配置中的模型、标签路径是相对仓库根目录的运行二进制时当前工作目录必须包含完整的mediapipe/目录结构否则找不到模型文件。usb变体需要系统里有libusb运行时pci变体只适用于 Linux。目标板上是否识别 Coral 设备不在本文档范围内文档只说明依赖库的安装方式。【免费下载链接】mediapipeCross-platform, customizable ML solutions for live and streaming media.项目地址: https://gitcode.com/GitHub_Trending/med/mediapipe创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
