安装依赖
JDK要求
需求java 17
系统要求
centos 8
centos 7需要自行升级glibc到某个版本,哪一个忘记了,风险很大,不要尝试!
基础依赖
# 缺什么就下什么,其中cmake是最重要的
yum -y install epel-release
yum install gcc gcc-c++
yum install cmake
yum install epel-release
yum install python3-devel python3-numpy
yum localinstall --nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm https://download1.rpmfusion.org/nonfree/el/rpmfusion-nonfree-release-7.noarch.rpm
yum install ffmpeg-devel
yum install -y unzip zip
yum install glibc glibc-devel
yum -y install gcc-c++
yum -y install gtk2-devel
yum -y install gimp-devel gimp-devel-tools
yum -y install zlib-devel
yum -y install libtiff-devel libjpeg-devel libpng-devel
yum -y install gstreamer1-plugins-good.x86_64
yum -y install libdc1394.x86_64
yum -y install libraw1394.x86_64
yum -y install libdc1394.x86_64
yum -y install jasper-libs.x86_64
yum -y install swig
yum -y install python39
yum -y install libtool
yum -y install ant
安装nasm
一、下载NASM软件包
到NSAM官网上选择最新的稳定版NASM软件包下载
https://www.nasm.us/pub/nasm/releasebuilds/2.14.02/
二、解压文件
下载nasm.tar.gz文件。
然后解压该文件:
tar -xvf nasm.tar.gz
三、安装
进入你解压文件的位置,终端依次运行:
./configure
make
sudo make install
这样NASM就已经在linux上安装好了,然后在终端使用nasm -version命令可以查看是否安装成功。
安装构建软件所需的工具包
`build-essential`是在Debian或Ubuntu等基于Debian的发行版中使用的软件包,用于安装构建软件所需的基本工具,例如编译器(如gcc),make工具等。而在基于CentOS或RHEL的发行版中,并没有名为`build-essential`的软件包。
在CentOS或RHEL中,您可以分别安装编译器和构建工具。例如,您可以使用以下命令安装常用的编译器和构建工具:
yum -y install gcc
yum -y install make
sudo yum groupinstall "Development Tools"
这将安装`gcc`编译器和`make`构建工具,它们是构建软件所需的基本工具之一。
安装ffmpeg
sudo yum install https://download1.rpmfusion.org/free/el/rpmfusion-free-release-8.noarch.rpm
sudo yum install https://rpmfind.net/linux/epel/7/x86_64/Packages/s/SDL2-2.0.14-2.el7.x86_64.rpm
sudo yum install ffmpeg
sudo yum -y install ffmpeg-devel
检查cmake版本
cmake --version
# 如果低于3.5.1,则按照如下命令执行
# 安装 EPEL 软件仓库,如果你还没有安装的话。你可以使用以下命令来安装 EPEL 软件仓库
sudo yum install epel-release
# 安装 CMake 3.x 版本
sudo yum install cmake3
# 再次验证
cmake --version
# 运行以下命令来设置CMake的符号链接,使之成为默认命令。这将创建一个符号链接,将 cmake3 命令映射到 cmake 命令,这样当你运行 cmake 命令时就会使用新安装的版本。
sudo ln -s /usr/bin/cmake3 /usr/bin/cmake
# 再次验证
cmake --version
# 如果创建符号链接失败,则先执行下列命令,删除原来的链接
sudo rm /usr/bin/cmake
# 再次运行创建符号链接
sudo ln -s /usr/bin/cmake3 /usr/bin/cmake
# 再次验证
cmake --version
准备工作
vi ~/.bashrc
# 将此路径替换为您实际找到的Python 3可执行文件的路径
# 将此路径替换为您实际找到jdk可执行文件的路径
export PYTHON_EXECUTABLE=/usr/bin/python3
export JAVA_HOME=/usr/lib/jvm/jdk-17-oracle-x64
source ~/.bashrc
# 检查是否成功
echo $PYTHON_EXECUTABLE
echo $JAVA_HOME
编译opencv
下载opencv
opencv官网
该教程使用的是opencv 4.8.0
下载对应的opencv_contrib
opencv_contrib的github
选择与你下载的opencv版本一样的
其中包含微信的二维码识别模块
opencv准备工作
在opencv解压目录下新建build文件夹
将opencv_contrib添加进opencv
将opencv_contrib解压,然后直接放在opencv的解压出的文件夹里面
开始编译
进入到opencv的文件夹内
cd build
# 编译命令如下:记得将其中的OPENCV_EXTRA_MODULES_PATH路径修改为自己的路径
cmake -D WITH_OPENGL=ON -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local/opencv -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_CUDA=ON -D WITH_NVCUVID=ON -D WITH_GTK_2_X=ON -D OPENCV_EXTRA_MODULES_PATH=/usr/local/opencv/480/opencv-4.8.0/opencv_contrib-4.8.0/modules -D OPENCV_ENABLE_NONFREE=ON ..
过程很漫长,因为会去github或外网下载依赖包,会卡住,然后下载失败
需要自己去复制下所有会出现卡住的下载链接,然后自己去下载,将其放进自己的服务器并提供下载链接(nginx访问静态资源链接就行)
我遇到的会有下载问题的文件如下:
opencv_contrib-4.8.0/modules/wechat_qrcode/CMakeLists.txt:26 (ocv_download)
https://raw.githubusercontent.com/WeChatCV/opencv_3rdparty/a8b69ccc738421293254aec5ddb38bd523503252/detect.caffemodel
https://raw.githubusercontent.com/WeChatCV/opencv_3rdparty/a8b69ccc738421293254aec5ddb38bd523503252/detect.prototxt
https://raw.githubusercontent.com/WeChatCV/opencv_3rdparty/a8b69ccc738421293254aec5ddb38bd523503252/sr.caffemodel
https://raw.githubusercontent.com/WeChatCV/opencv_3rdparty/a8b69ccc738421293254aec5ddb38bd523503252/sr.prototxt
opencv_contrib-4.8.0/modules/xfeatures2d/cmake/download_boostdesc.cmake:22 (ocv_download)
opencv_contrib-4.8.0/modules/xfeatures2d/CMakeLists.txt:12 (download_boost_descriptors)
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_bi.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_hd.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_064.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_128.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_256.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_lbgm.i
opencv_contrib-4.8.0/modules/xfeatures2d/cmake/download_vgg.cmake:16 (ocv_download)
opencv_contrib-4.8.0/modules/xfeatures2d/CMakeLists.txt:13 (download_vgg_descriptors)
https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_48.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_64.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_80.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_120.i
opencv_contrib-4.8.0/modules/face/CMakeLists.txt:13 (ocv_download)
https://raw.githubusercontent.com/opencv/opencv_3rdparty/8afa57abc8229d611c4937165d20e2a2d9fc5a12/face_landmark_model.dat
下面介绍应该如何将自己的下载链接修改在哪里:
例如:
opencv_contrib-4.8.0/modules/face/CMakeLists.txt:13 (ocv_download)
https://raw.githubusercontent.com/opencv/opencv_3rdparty/8afa57abc8229d611c4937165d20e2a2d9fc5a12/face_landmark_model.dat
具体的报错截图我没有了,但是大家应该都能提取到哪个文件哪一行的链接出错了和链接
上面就可以看到是opencv_contrib-4.8.0/modules/face/CMakeLists.txt文件的第13行中的链接:https://raw.githubusercontent.com/opencv/opencv_3rdparty/8afa57abc8229d611c4937165d20e2a2d9fc5a12/face_landmark_model.dat
中的文件下载不下来
找到这个文件并查看文件详情:
set(the_description "Face recognition etc")
ocv_define_module(face opencv_core
opencv_imgproc
opencv_objdetect
opencv_calib3d # estimateAffinePartial2D() (trainFacemark)
opencv_photo # seamlessClone() (face_swap sample)
WRAP python java objc
)
# NOTE: objdetect module is needed for one of the samples
set(__commit_hash "8afa57abc8229d611c4937165d20e2a2d9fc5a12")
set(__file_hash "7505c44ca4eb54b4ab1e4777cb96ac05")
ocv_download(
FILENAME face_landmark_model.dat
HASH ${__file_hash}
URL
"${OPENCV_FACE_ALIGNMENT_URL}"
"$ENV{OPENCV_FACE_ALIGNMENT_URL}"
"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${__commit_hash}/"
DESTINATION_DIR "${CMAKE_BINARY_DIR}/${OPENCV_TEST_DATA_INSTALL_PATH}/cv/face/"
ID "data"
RELATIVE_URL
STATUS res
)
if(NOT res)
message(WARNING "Face: Can't get model file for face alignment.")
endif()
可以看到他的前缀是https://raw.githubusercontent.com/opencv/opencv_3rdparty,那么我们将文件放在自己服务器上面时,使用nginx访问静态资源需要前缀,除去前缀之后文件的存放地址应该与该文件中的一致
比如你的下载文件的访问路径应该是:
http://[你的服务器的ip地址]/down/opencv/opencv/opencv_3rdparty/${__commit_hash}/
假设你的nginx中down映射的路径是/opt/resource/
那么根据文件原下载链接:https://raw.githubusercontent.com/opencv/opencv_3rdparty/8afa57abc8229d611c4937165d20e2a2d9fc5a12/face_landmark_model.dat,你的下载文件全路径应该放在:
/opt/resource/opencv/opencv/opencv_3rdparty/8afa57abc8229d611c4937165d20e2a2d9fc5a12/face_landmark_model.dat
于是CMakeLists.txt中的内容就变成了:
set(the_description "Face recognition etc")
ocv_define_module(face opencv_core
opencv_imgproc
opencv_objdetect
opencv_calib3d # estimateAffinePartial2D() (trainFacemark)
opencv_photo # seamlessClone() (face_swap sample)
WRAP python java objc
)
# NOTE: objdetect module is needed for one of the samples
set(__commit_hash "8afa57abc8229d611c4937165d20e2a2d9fc5a12")
set(__file_hash "7505c44ca4eb54b4ab1e4777cb96ac05")
ocv_download(
FILENAME face_landmark_model.dat
HASH ${__file_hash}
URL
"${OPENCV_FACE_ALIGNMENT_URL}"
"$ENV{OPENCV_FACE_ALIGNMENT_URL}"
"http://[你的ip地址]/down/opencv/opencv/opencv_3rdparty/${__commit_hash}/"
DESTINATION_DIR "${CMAKE_BINARY_DIR}/${OPENCV_TEST_DATA_INSTALL_PATH}/cv/face/"
ID "data"
RELATIVE_URL
STATUS res
)
if(NOT res)
message(WARNING "Face: Can't get model file for face alignment.")
endif()
将所有下载不下来的文件都按照这种方式进行下载、获取自己的文件下载链接、修改原文件链接之后,应该就会很快编译成功,且过程中的下载缓慢报错应该都会消失,且下载链接替换为你自己的下载链接
看最后的有没有java类型的就行
安装
依旧在build文件夹内
make
make install
这个会很慢,但是多线程下我尝试会报错,你们可自行尝试
最后结果:
在/usr/local/opencv/480/opencv-4.8.0/build/bin文件夹下生成了jar包:
在/usr/local/opencv/480/opencv-4.8.0/build/lib文件夹下生成了各种文件,java所需的是
在/usr/local/opencv/share/java/opencv4下直接是有这两个文件
maven项目中用法:
<dependency>
<groupId>org</groupId>
<artifactId>opencv-4.8.0</artifactId>
<version>4.8.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/opencv-480.jar</systemPath>
</dependency>
windows
要想运行该代码,需要将dll文件放在
C:\Windows\System32\opencv_java480.dll路径下
dll文件:480dll文件
部署到linux
需要将so文件放在/usr/lib文件夹下
加载代码:
使用opencv的任意的功能前,都需要执行该代码
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
识别图中的二维码的最简单示例:
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
WeChatQRCode weChatQRCode = new WeChatQRCode();
Mat mat = Imgcodecs.imread("xxx.jpg");
System.out.println(weChatQRCode.detectAndDecode(mat));
}
480jar包及so和dll文件:jar包及so和dll文件
标签:10,githubusercontent,3rdparty,https,OpenCV,opencv,3.4,yum,install From: https://www.cnblogs.com/wqw553639736/p/18164148