您好,登錄后才能下訂單哦!
本篇內容主要講解“opencv使用中常見的問題”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“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; } }
到此,相信大家對“opencv使用中常見的問題”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。