赞
踩
因工作需要,再次编译MediaPipe库,上次只是走了下流程,没有任何记录,本次编译时,发现不少问题,所以记录下,供有需要的小伙伴使用
若已安装,请略过本步骤
下载地址:MSYS2
下载最近版本,按照官方说明,安装即可
安装完成后,打开msys2
在打开的MSYS命令行中输入:
pacman -Syu
等待各种包下载升级安装后,继续升级msys2基础数据库,输入如下
pacman -Su
等待升级完成,然后执行如下命令,安装unzip
pacman -S git patch unzip
笔者之前就已经安装了python3.10,未安装的小伙伴,请自己安装
笔者建议,安装目录,最好不要带汉字和空格,以免后期的各种麻烦
这个网上教程很多,笔者就不解释了
需要安装C++的基本开发环境和WinSDK
笔者安装的WinSDK是10.0.19041.685
下载地址:https://github.com/bazelbuild/bazel/archive/refs/tags/6.5.0.zip
笔者建议:安装Bazel 6.1.1及以后的6系列版本,其他版本不建议安装
下载完成后,解压到指定目录中(注意不要有中文和空格)
网上教程比较多,可以直接到OpenCV官网上,直接下载预编译版本
下载地址:https://github.com/opencv/opencv/releases
笔者使用的是,自己之前编译的OpenCV4.8.1
笔者建议:使用与日常开发一样的版本,以减少打包后,库文件过多、过大
网上教程比较多,笔者不再介绍
笔者开发机上安装了多个开发环境,为避免各开发环境产生冲突,不建议直接修改本机的环境变量
所以笔者只在一个cmd中修改环境变量
打开一个cmd(后面就不要关闭了),输入以下命令:
- set path={bazel_dir}\bazel-6.5.0-windows-x86_64;{msys2_dir}\msys64\usr\bin;%path%
-
- set BAZEL_SH={msys2_dir}\msys64\usr\bin\bash.exe
需要自己修改下bazel的目录、msys2的安装目录
打开msys2,输入以下命令:
- # Please find the exact paths and version numbers from your local version.
- set BAZEL_VS=C:\Program Files \(x86\)\Microsoft Visual Studio\2019\BuildTools
- set BAZEL_VC=C:\Program Files \(x86\)\Microsoft Visual Studio\2019\BuildTools\VC
- set BAZEL_VC_FULL_VERSION=<Your local VC version>
- set BAZEL_WINSDK_FULL_VERSION=<Your local WinSDK version>
- set BAZEL_SH=D:\msys64\usr\bin\bash.exe
以上目录需要自己修改下
需要注意下,()需要转义下
笔者最终设置为:
- set BAZEL_VS=D:\Program Files \(x86\)\Microsoft Visual Studio\2019\Community
- set BAZEL_VC=D:\Program Files \(x86\)\Microsoft Visual Studio\2019\Community\VC
- set BAZEL_VC_FULL_VERSION=14.29.30133
- set BAZEL_WINSDK_FULL_VERSION=10.0.19041.685
- set BAZEL_SH=D:\msys64\usr\bin\bash.exe
需要在MSYS上设置Bazel变量而不是在cmd.exe中,在MSYS中依次输入上述命令
笔者建议科学上网,以减少下载时间或失败次数
若条件允许git也可以配置下代理,参考命令如下:
- #全局代理
- git config --global http.proxy http://127.0.0.1:1080
- git config --global https.proxy http://127.0.0.1:1080
-
- //查看当前设置
- //git config -l
- //取消全局代理
- //git config --global --unset http.proxy
- //git config --global --unset https.proxy
执行如下命令,拉取mediapipe源码
git clone --depth 1 https://github.com/google/mediapipe.git
下载完成后,切换到 mediapipe目录
cd mediapipe
(1)在mediapipe目录下,找到WORKSPACE文件,使用文本编辑器打开,找到如下位置
- new_local_repository(
- name = "windows_opencv",
- build_file = "@//third_party:opencv_windows.BUILD",
- path = "C:\\<path to opencv>\\build",
- )
需要将path目录,修改成已安装的OpenCV目录
笔者是自己编译的OpenCV,请小伙伴根据实际修改下
(2)在mediapipe/third_party目录下,找到opencv_windows.BUILD文件,使用文本编辑器打开,需要修改如下部分
笔者是自己编译的OpenCV,没有勾选world,需要添加的模块比较多,请小伙伴根据实际修改下
笔者最终修改结果如下
- # Description:
- # OpenCV libraries for video/image processing on Windows
-
- licenses(["notice"]) # BSD license
-
- exports_files(["LICENSE"])
-
- OPENCV_VERSION = "481" # 4.8.1
-
- config_setting(
- name = "opt_build",
- values = {"compilation_mode": "opt"},
- )
-
- config_setting(
- name = "dbg_build",
- values = {"compilation_mode": "dbg"},
- )
-
- # The following build rule assumes that the executable "opencv-3.4.10-vc14_vc15.exe"
- # is downloaded and the files are extracted to local.
- # If you install OpenCV separately, please modify the build rule accordingly.
- cc_library(
- name = "opencv",
- srcs = select({
- ":opt_build": [
- "x64/vc16/lib/opencv_core" + OPENCV_VERSION + ".lib",
- "x64/vc16/bin/opencv_core" + OPENCV_VERSION + ".dll",
- "x64/vc16/lib/opencv_calib3d" + OPENCV_VERSION + ".lib",
- "x64/vc16/bin/opencv_calib3d" + OPENCV_VERSION + ".dll",
- "x64/vc16/lib/opencv_features2d" + OPENCV_VERSION + ".lib",
- "x64/vc16/bin/opencv_features2d" + OPENCV_VERSION + ".dll",
- "x64/vc16/lib/opencv_highgui" + OPENCV_VERSION + ".lib",
- "x64/vc16/bin/opencv_highgui" + OPENCV_VERSION + ".dll",
- "x64/vc16/lib/opencv_imgcodecs" + OPENCV_VERSION + ".lib",
- "x64/vc16/bin/opencv_imgcodecs" + OPENCV_VERSION + ".dll",
- "x64/vc16/lib/opencv_imgproc" + OPENCV_VERSION + ".lib",
- "x64/vc16/bin/opencv_imgproc" + OPENCV_VERSION + ".dll",
- "x64/vc16/lib/opencv_video" + OPENCV_VERSION + ".lib",
- "x64/vc16/bin/opencv_video" + OPENCV_VERSION + ".dll",
- "x64/vc16/lib/opencv_videoio" + OPENCV_VERSION + ".lib",
- "x64/vc16/bin/opencv_videoio" + OPENCV_VERSION + ".dll",
- ],
- ":dbg_build": [
- "x64/vc16/lib/opencv_core" + OPENCV_VERSION + "d.lib",
- "x64/vc16/bin/opencv_core" + OPENCV_VERSION + "d.dll",
- "x64/vc16/lib/opencv_calib3d" + OPENCV_VERSION + "d.lib",
- "x64/vc16/bin/opencv_calib3d" + OPENCV_VERSION + "d.dll",
- "x64/vc16/lib/opencv_features2d" + OPENCV_VERSION + "d.lib",
- "x64/vc16/bin/opencv_features2d" + OPENCV_VERSION + "d.dll",
- "x64/vc16/lib/opencv_highgui" + OPENCV_VERSION + "d.lib",
- "x64/vc16/bin/opencv_highgui" + OPENCV_VERSION + "d.dll",
- "x64/vc16/lib/opencv_imgcodecs" + OPENCV_VERSION + "d.lib",
- "x64/vc16/bin/opencv_imgcodecs" + OPENCV_VERSION + "d.dll",
- "x64/vc16/lib/opencv_imgproc" + OPENCV_VERSION + "d.lib",
- "x64/vc16/bin/opencv_imgproc" + OPENCV_VERSION + "d.dll",
- "x64/vc16/lib/opencv_video" + OPENCV_VERSION + "d.lib",
- "x64/vc16/bin/opencv_video" + OPENCV_VERSION + "d.dll",
- "x64/vc16/lib/opencv_videoio" + OPENCV_VERSION + "d.lib",
- "x64/vc16/bin/opencv_videoio" + OPENCV_VERSION + "d.dll",
- ],
- }),
- hdrs = glob(["include/opencv2/**/*.h*"]),
- includes = ["include/"],
- linkstatic = 1,
- visibility = ["//visibility:public"],
- )
以上操作完成后,就可以正常编译了,执行如下命令
bazel build -c opt --define MEDIAPIPE_DISABLE_GPU=1 --action_env PYTHON_BIN_PATH="D://Program//Python310//python.exe" mediapipe/examples/desktop/hello_world
其中PYTHON_BIN_PATH参数,需要小伙伴修改成自己的python目录
编译成功后,可能会有类似如上的输出,可以执行如下命令,查看运行结果
- set GLOG_logtostderr=1
-
- bazel-bin\mediapipe\examples\desktop\hello_world\hello_world.exe
输出结果如下:
1.笔者建议先看下官方文档,本文档只支持当前时间编译
注:实际测试,发现版本v0.10.11可以正常编译hello world,但无法编译其他示例,建议在bug修改前,先下载编译v0.10.10版本
2.win版本,当前不支持gpu加速,若想支持GPU加速,需要编译linux版本
3.在编译或运行时,360等杀毒软件,经常会误报,注意及时放行
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。