opencv使用中出现的问题
时间:2022-07-28
本文章向大家介绍opencv使用中出现的问题,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
最近项目遇到一个问题,springboot2在打包过程中出现的问题;
1、引用本地的jar包,怎么打包到项目;
pom.xml 配置
dependencies标签配置
<!--打包本地jar包-->
<dependency>
<groupId>org.opencv</groupId>
<artifactId>opencv</artifactId>
<version>0.0.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/opencv-440.jar</systemPath>
</dependency>
其中,version为必填,尝试过省略,报错;
plugins标签配置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<!-- 打包本地jar包 -->
<extdirs>${project.basedir}/src/main/resources/lib</extdirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
resources标签 配置
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<includes>
<include>lib/*.jar</include>
</includes>
</resource>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<includes>
<include>lib/*.dll</include>
</includes>
</resource>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<includes>
<include>*.yml</include>
</includes>
</resource>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<includes>
<include>*/*.xml</include>
</includes>
</resource>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<includes>
<include>*/*.json</include>
</includes>
</resource>
</resources>
2、opencv使用人脸识别过程中,需要引入haarcascade_frontalface_alt2.xml文件,如果放在项目的lib文件中,在打包的过程中能够打到包里,但是在动态引用的过程中,由于jar包中文件的引用出现混乱路径的情况,找不到文件。找到一个折中的办法,haarcascade_frontalface_alt2.xml文件放到固定目录下,再引用的时候,只需要读取固定的路径即可。
yml文件配置 path: resourcePath: C:haarcascade_frontalface_alt2.xml # 人脸识别的xml配置文件
NativeConfig.java类
package com.hake.smart.configration;
import com.hake.smart.ymlconfig.YmlConfigUtil;
import lombok.extern.slf4j.Slf4j;
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ClassUtils;
import javax.annotation.Resource;
import java.util.Arrays;
/**
* @Author: Liu Yue
* @Descripition:
* @Date; Create in 2020/8/25 15:59
**/
@Configuration
@Slf4j
public class NativeConfig {
@Resource
private YmlConfigUtil ymlConfigUtil;
@Value("${path.resourcePath}")
private static String resourcePath;
@Value(value = "${path.resourcePath}")
private void setResourcePath(String resourcePath){
this.resourcePath = resourcePath ;
}
@Bean
public NativeConfig initFrontalface(){
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
log.error("路径:{}",resourcePath);
faceDetector = new CascadeClassifier(resourcePath);
return new NativeConfig();
}
/**
* 测试OpenCV是否能运行:需要自行修改图片位置
* @throws Exception 测试是否成功
*/
static CascadeClassifier faceDetector;
/* static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
//String url = "C:/hake/gitremote/hakesmartacceptback/target/classes/lib/haarcascade_frontalface_alt2.xml";
//ClassPathResource("lib/haarcascade_frontalface_alt2.xml");
*//*ClassPathResource resource = new ClassPathResource("lib/haarcascade_frontalface_alt2.xml");
String path = resource.getPath();
path = basepath + path;*//*
URL urlClass = ClassLoader.getSystemResource("lib/haarcascade_frontalface_alt2.xml");
String path = urlClass.getPath();
log.error("路径:{}",path);
path = path.substring(1,path.length());
faceDetector = new CascadeClassifier(path);
}*/
private Mat conv_Mat(String img_1) {
Mat image0 = Imgcodecs.imread(img_1);
Mat image = new Mat();
//灰度转换
Imgproc.cvtColor(image0, image, Imgproc.COLOR_BGR2GRAY);
MatOfRect faceDetections = new MatOfRect();
//探测人脸
faceDetector.detectMultiScale(image, faceDetections);
// rect中是人脸图片的范围
for (Rect rect : faceDetections.toArray()) {
//切割rect人脸
Mat mat = new Mat(image, rect);
return mat;
}
return null;
}
//比较图片相似度
public Boolean compare_image(String img_1, String img_2) {
Mat mat_1 = conv_Mat(img_1);
Mat mat_2 = conv_Mat(img_2);
Mat hist_1 = new Mat();
Mat hist_2 = new Mat();
//颜色范围
MatOfFloat ranges = new MatOfFloat(0f, 256f);
//直方图大小, 越大匹配越精确 (越慢)
MatOfInt histSize = new MatOfInt(1000);
Imgproc.calcHist(Arrays.asList(mat_1), new MatOfInt(0), new Mat(), hist_1, histSize, ranges);
Imgproc.calcHist(Arrays.asList(mat_2), new MatOfInt(0), new Mat(), hist_2, histSize, ranges);
// CORREL 相关系数
double res = Imgproc.compareHist(hist_1, hist_2, Imgproc.CV_COMP_CORREL);
log.info("OPENCV人脸相似度:{}",res);
int lifePhotostandard = ymlConfigUtil.getLifePhotostandard();
log.info("相识度标准:{}",lifePhotostandard);
boolean bl = res * 100 > lifePhotostandard ? true:false;
return bl;
}
}
每天提高一点点
- 分享一个 HTTPS A+ 的 nginx 配置
- Vijos P1785 同学排序【模拟】
- Vijos P1784 数字统计【模拟】
- 网络安全黑暗森林法则:2015 ISC 深度回顾
- Codeforces 626G Raffles(贪心+线段树)
- window.opener.location 安全风险讨论
- Vijos P1497 立体图【模拟】
- Vijos P1127 级数求和【模拟】
- 新型漏洞:利用浏览器Cookie绕过HTTPS并窃取私人信息
- Vijos P1113 不高兴的津津【模拟】
- Linux下MySQL的彻底卸载和安装配置字符集
- Codeforces 626F Group Projects(滚动数组+差分dp)
- Vijos P1103 校门外的树【线段树,模拟】
- BZOJ 1061: [Noi2008]志愿者招募【单纯形裸题】
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- OpenCV与图像处理(四)
- 常用功能加载宏——单元格数据连接
- Nest.js 从零到壹系列(八):使用 Redis 实现登录挤出功能
- 创建常用功能加载宏
- 3分钟短文 | Linux 使用curl发起post请求的4个常用方式
- 类模块——接口
- OpenCV与图像处理(三)
- 常用功能加载宏——单元格数字转变
- OpenCV与图像处理(二)
- ECCV2020 | SOD100K:超低参数量的高效显著性目标检测算法,广义OctConv和动态权重衰减
- Rust FFI 编程 - Rust导出共享库02
- 常用功能加载宏——单元格数字格式
- 【翻译】200行代码讲透RUST FUTURES (2)
- 【Rust日报】2020-07-23 Rust 的 CI 将使用 GitHub Actions
- 【每周一库】- Tonic 基于Rust的gRPC实现