前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >opencv使用中出现的问题

opencv使用中出现的问题

作者头像
用户5640963
发布2020-10-26 17:20:24
9040
发布2020-10-26 17:20:24
举报
文章被收录于专栏:卯金刀GG卯金刀GG

最近项目遇到一个问题,springboot2在打包过程中出现的问题;

1、引用本地的jar包,怎么打包到项目;

代码语言:javascript
复制
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类

代码语言:javascript
复制
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;
    }
}

每天提高一点点

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
人脸识别
腾讯云神图·人脸识别(Face Recognition)基于腾讯优图强大的面部分析技术,提供包括人脸检测与分析、比对、搜索、验证、五官定位、活体检测等多种功能,为开发者和企业提供高性能高可用的人脸识别服务。 可应用于在线娱乐、在线身份认证等多种应用场景,充分满足各行业客户的人脸属性识别及用户身份确认等需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档