当前位置:   article > 正文

MediaPipe学习笔记(一)开发环境搭建(win10+vs2019)_mediapipe csdn

mediapipe csdn

前言

因工作需要,再次编译MediaPipe库,上次只是走了下流程,没有任何记录,本次编译时,发现不少问题,所以记录下,供有需要的小伙伴使用

一、安装需要的软件

1.安装msys2

若已安装,请略过本步骤

下载地址:MSYS2

下载最近版本,按照官方说明,安装即可

安装完成后,打开msys2

在打开的MSYS命令行中输入:

pacman -Syu

等待各种包下载升级安装后,继续升级msys2基础数据库,输入如下

pacman -Su

 等待升级完成,然后执行如下命令,安装unzip

pacman -S git patch unzip

2.安装python

笔者之前就已经安装了python3.10,未安装的小伙伴,请自己安装

笔者建议,安装目录,最好不要带汉字和空格,以免后期的各种麻烦

3.安装VS2019

这个网上教程很多,笔者就不解释了

需要安装C++的基本开发环境和WinSDK

笔者安装的WinSDK是10.0.19041.685

4.安装Bazel

下载地址:https://github.com/bazelbuild/bazel/archive/refs/tags/6.5.0.zip

笔者建议:安装Bazel 6.1.1及以后的6系列版本,其他版本不建议安装

下载完成后,解压到指定目录中(注意不要有中文和空格)

5.安装opencv

网上教程比较多,可以直接到OpenCV官网上,直接下载预编译版本

下载地址:https://github.com/opencv/opencv/releases

笔者使用的是,自己之前编译的OpenCV4.8.1

笔者建议:使用与日常开发一样的版本,以减少打包后,库文件过多、过大 

6.安装git

网上教程比较多,笔者不再介绍

二、配置安装环境

笔者开发机上安装了多个开发环境,为避免各开发环境产生冲突,不建议直接修改本机的环境变量

所以笔者只在一个cmd中修改环境变量

1.设置本机环境变量

打开一个cmd(后面就不要关闭了),输入以下命令:

  1. set path={bazel_dir}\bazel-6.5.0-windows-x86_64;{msys2_dir}\msys64\usr\bin;%path%
  2. set BAZEL_SH={msys2_dir}\msys64\usr\bin\bash.exe

需要自己修改下bazel的目录、msys2的安装目录 

2.设置msys2环境变量

打开msys2,输入以下命令:

  1. # Please find the exact paths and version numbers from your local version.
  2. set BAZEL_VS=C:\Program Files \(x86\)\Microsoft Visual Studio\2019\BuildTools
  3. set BAZEL_VC=C:\Program Files \(x86\)\Microsoft Visual Studio\2019\BuildTools\VC
  4. set BAZEL_VC_FULL_VERSION=<Your local VC version>
  5. set BAZEL_WINSDK_FULL_VERSION=<Your local WinSDK version>
  6. set BAZEL_SH=D:\msys64\usr\bin\bash.exe

以上目录需要自己修改下 

  • BAZEL_VS : vs2019的安装目录,笔者的为:D:\Program Files (x86)\Microsoft Visual Studio\2019\Community
  • BAZEL_VC:vs2019安装目录下C++主要目录,笔者的为D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC
  • BAZEL_VC_FULL_VERSION : VS2019的中vc的版本号,该版本号可以在VS2019的安装目录D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Redist\MSVC下找到:

  • BAZEL_WINSDK_FULL_VERSION:WinSDK的版本号,这可以在控制面板-程序-程序和功能中查看,找到Windows Software Development Kit,后面跟的数字就是WinSDK的版本号,如下图所示。

需要注意下,()需要转义下

笔者最终设置为:

  1. set BAZEL_VS=D:\Program Files \(x86\)\Microsoft Visual Studio\2019\Community
  2. set BAZEL_VC=D:\Program Files \(x86\)\Microsoft Visual Studio\2019\Community\VC
  3. set BAZEL_VC_FULL_VERSION=14.29.30133
  4. set BAZEL_WINSDK_FULL_VERSION=10.0.19041.685
  5. set BAZEL_SH=D:\msys64\usr\bin\bash.exe

 需要在MSYS上设置Bazel变量而不是在cmd.exe中,在MSYS中依次输入上述命令

三、下载源码

笔者建议科学上网,以减少下载时间或失败次数

若条件允许git也可以配置下代理,参考命令如下:

  1. #全局代理
  2. git config --global http.proxy http://127.0.0.1:1080
  3. git config --global https.proxy http://127.0.0.1:1080
  4. //查看当前设置
  5. //git config -l
  6. //取消全局代理
  7. //git config --global --unset http.proxy
  8. //git config --global --unset https.proxy

执行如下命令,拉取mediapipe源码

git clone --depth 1 https://github.com/google/mediapipe.git

下载完成后,切换到 mediapipe目录

cd mediapipe

四、编译源码

1.修改配置文件

(1)在mediapipe目录下,找到WORKSPACE文件,使用文本编辑器打开,找到如下位置

  1. new_local_repository(
  2.     name = "windows_opencv",
  3.     build_file = "@//third_party:opencv_windows.BUILD",
  4.     path = "C:\\<path to opencv>\\build",
  5. )

需要将path目录,修改成已安装的OpenCV目录

 

笔者是自己编译的OpenCV,请小伙伴根据实际修改下

(2)在mediapipe/third_party目录下,找到opencv_windows.BUILD文件,使用文本编辑器打开,需要修改如下部分

笔者是自己编译的OpenCV,没有勾选world,需要添加的模块比较多,请小伙伴根据实际修改下

 笔者最终修改结果如下

  1. # Description:
  2. # OpenCV libraries for video/image processing on Windows
  3. licenses(["notice"]) # BSD license
  4. exports_files(["LICENSE"])
  5. OPENCV_VERSION = "481" # 4.8.1
  6. config_setting(
  7. name = "opt_build",
  8. values = {"compilation_mode": "opt"},
  9. )
  10. config_setting(
  11. name = "dbg_build",
  12. values = {"compilation_mode": "dbg"},
  13. )
  14. # The following build rule assumes that the executable "opencv-3.4.10-vc14_vc15.exe"
  15. # is downloaded and the files are extracted to local.
  16. # If you install OpenCV separately, please modify the build rule accordingly.
  17. cc_library(
  18. name = "opencv",
  19. srcs = select({
  20. ":opt_build": [
  21. "x64/vc16/lib/opencv_core" + OPENCV_VERSION + ".lib",
  22. "x64/vc16/bin/opencv_core" + OPENCV_VERSION + ".dll",
  23. "x64/vc16/lib/opencv_calib3d" + OPENCV_VERSION + ".lib",
  24. "x64/vc16/bin/opencv_calib3d" + OPENCV_VERSION + ".dll",
  25. "x64/vc16/lib/opencv_features2d" + OPENCV_VERSION + ".lib",
  26. "x64/vc16/bin/opencv_features2d" + OPENCV_VERSION + ".dll",
  27. "x64/vc16/lib/opencv_highgui" + OPENCV_VERSION + ".lib",
  28. "x64/vc16/bin/opencv_highgui" + OPENCV_VERSION + ".dll",
  29. "x64/vc16/lib/opencv_imgcodecs" + OPENCV_VERSION + ".lib",
  30. "x64/vc16/bin/opencv_imgcodecs" + OPENCV_VERSION + ".dll",
  31. "x64/vc16/lib/opencv_imgproc" + OPENCV_VERSION + ".lib",
  32. "x64/vc16/bin/opencv_imgproc" + OPENCV_VERSION + ".dll",
  33. "x64/vc16/lib/opencv_video" + OPENCV_VERSION + ".lib",
  34. "x64/vc16/bin/opencv_video" + OPENCV_VERSION + ".dll",
  35. "x64/vc16/lib/opencv_videoio" + OPENCV_VERSION + ".lib",
  36. "x64/vc16/bin/opencv_videoio" + OPENCV_VERSION + ".dll",
  37. ],
  38. ":dbg_build": [
  39. "x64/vc16/lib/opencv_core" + OPENCV_VERSION + "d.lib",
  40. "x64/vc16/bin/opencv_core" + OPENCV_VERSION + "d.dll",
  41. "x64/vc16/lib/opencv_calib3d" + OPENCV_VERSION + "d.lib",
  42. "x64/vc16/bin/opencv_calib3d" + OPENCV_VERSION + "d.dll",
  43. "x64/vc16/lib/opencv_features2d" + OPENCV_VERSION + "d.lib",
  44. "x64/vc16/bin/opencv_features2d" + OPENCV_VERSION + "d.dll",
  45. "x64/vc16/lib/opencv_highgui" + OPENCV_VERSION + "d.lib",
  46. "x64/vc16/bin/opencv_highgui" + OPENCV_VERSION + "d.dll",
  47. "x64/vc16/lib/opencv_imgcodecs" + OPENCV_VERSION + "d.lib",
  48. "x64/vc16/bin/opencv_imgcodecs" + OPENCV_VERSION + "d.dll",
  49. "x64/vc16/lib/opencv_imgproc" + OPENCV_VERSION + "d.lib",
  50. "x64/vc16/bin/opencv_imgproc" + OPENCV_VERSION + "d.dll",
  51. "x64/vc16/lib/opencv_video" + OPENCV_VERSION + "d.lib",
  52. "x64/vc16/bin/opencv_video" + OPENCV_VERSION + "d.dll",
  53. "x64/vc16/lib/opencv_videoio" + OPENCV_VERSION + "d.lib",
  54. "x64/vc16/bin/opencv_videoio" + OPENCV_VERSION + "d.dll",
  55. ],
  56. }),
  57. hdrs = glob(["include/opencv2/**/*.h*"]),
  58. includes = ["include/"],
  59. linkstatic = 1,
  60. visibility = ["//visibility:public"],
  61. )

2.编译hello world

以上操作完成后,就可以正常编译了,执行如下命令

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目录

编译成功后,可能会有类似如上的输出,可以执行如下命令,查看运行结果

  1. set GLOG_logtostderr=1
  2. 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等杀毒软件,经常会误报,注意及时放行

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/906030
推荐阅读
相关标签
  

闽ICP备14008679号