您好,登錄后才能下訂單哦!
這篇文章主要介紹了java+opencv如何實(shí)現(xiàn)人臉識(shí)別功能,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
Java的特點(diǎn)有哪些 1.Java語(yǔ)言作為靜態(tài)面向?qū)ο缶幊陶Z(yǔ)言的代表,實(shí)現(xiàn)了面向?qū)ο罄碚?,允許程序員以優(yōu)雅的思維方式進(jìn)行復(fù)雜的編程。 2.Java具有簡(jiǎn)單性、面向?qū)ο?、分布式、安全性、平臺(tái)獨(dú)立與可移植性、動(dòng)態(tài)性等特點(diǎn)。 3.使用Java可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序等。
背景:最近需要用到人臉識(shí)別,但又不花錢使用現(xiàn)有的第三方人臉識(shí)別接口,為此使用opencv結(jié)合java進(jìn)行人臉識(shí)別(ps:opencv是開(kāi)源的,使用它來(lái)做人臉識(shí)別存在一定的誤差,效果一般)。
1.安裝opencv
官網(wǎng)地址:https://opencv.org/ , 由于官網(wǎng)下載速度是真的慢
百度網(wǎng)盤:
鏈接: https://pan.baidu.com/s/1RpsP-I7v8pP2dkqALDw7FQ
提取碼: pq7v
如果是官網(wǎng)下載,就無(wú)腦安裝就行了,安裝完畢后。
將圖一的兩個(gè)文件復(fù)制到圖二中。
從我網(wǎng)盤下載的,忽略這些。
2.在項(xiàng)目中引入pom依賴
<!-- opencv + javacv + ffmpeg--> <dependency> <groupId>org.bytedeco.javacpp-presets</groupId> <artifactId>ffmpeg</artifactId> <version>4.1-1.4.4</version> </dependency> <dependency> <groupId>org.bytedeco</groupId> <artifactId>javacv</artifactId> <version>1.4.4</version> </dependency> <!-- https://mvnrepository.com/artifact/org.bytedeco.javacpp-presets/ffmpeg-platform --> <dependency> <groupId>org.bytedeco.javacpp-presets</groupId> <artifactId>ffmpeg-platform</artifactId> <version>4.1-1.4.4</version> </dependency> <!-- 視頻攝像頭 --> <!-- https://mvnrepository.com/artifact/org.bytedeco/javacv-platform --> <dependency> <groupId>org.bytedeco</groupId> <artifactId>javacv-platform</artifactId> <version>1.4.4</version> </dependency> <!-- https://mvnrepository.com/artifact/org.bytedeco.javacpp-presets/opencv-platform --> <dependency> <groupId>org.bytedeco.javacpp-presets</groupId> <artifactId>opencv-platform</artifactId> <version>4.0.1-1.4.4</version> </dependency>
1.導(dǎo)入庫(kù)依賴
File --> Project Structure,點(diǎn)擊Modules,選擇需要使用opencv.jar的項(xiàng)目。
選擇直接opencv安裝路徑
2.java代碼demo
package org.Litluecat.utils; import org.apache.commons.lang.StringUtils; import org.opencv.core.*; import org.opencv.highgui.HighGui; import org.opencv.highgui.ImageWindow; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; import org.opencv.objdetect.CascadeClassifier; import org.opencv.videoio.VideoCapture; import org.opencv.videoio.VideoWriter; import org.opencv.videoio.Videoio; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Arrays; /** * 人臉比對(duì)工具類 * @author Litluecat * @Title: Opencv 圖片人臉識(shí)別、實(shí)時(shí)攝像頭人臉識(shí)別 **/ public class FaceVideo { private static final Logger log = LoggerFactory.getLogger(FaceVideo.class); private static final String endImgUrl = "C:\\Users\\lenovo\\Desktop\\"; /** * opencv的人臉識(shí)別xml文件路徑 */ private static final String faceDetectorXML2URL = "D:\\Sofeware\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml"; /** * opencv的人眼識(shí)別xml文件路徑 */ private static final String eyeDetectorXML2URL = "D:\\Sofeware\\opencv\\sources\\data\\haarcascades\\haarcascade_eye.xml"; /** * 直方圖大小,越大精度越高,運(yùn)行越慢 */ private static int Matching_Accuracy = 100000; /** * 初始化人臉探測(cè)器 */ private static CascadeClassifier faceDetector; /** * 初始化人眼探測(cè)器 */ private static CascadeClassifier eyeDetector; private static int i=0; static { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); faceDetector = new CascadeClassifier(faceDetectorXML2URL); eyeDetector = new CascadeClassifier(eyeDetectorXML2URL); } public static void main(String[] args) { log.info("開(kāi)始人臉匹配"); long begin = System.currentTimeMillis(); // 1- 從攝像頭實(shí)時(shí)人臉識(shí)別,識(shí)別成功保存圖片到本地 try{ getVideoFromCamera(endImgUrl + "2.jpg"); //僅用于強(qiáng)制拋異常,從而關(guān)閉GUI界面 Thread.sleep(1000); int err = 1/0; // 2- 比對(duì)本地2張圖的人臉相似度 (越接近1越相似) // double compareHist = FaceVideo.compare_image(endImgUrl + "test1.jpg" , endImgUrl + "face.jpg"); // log.info("匹配度:{}",compareHist); // if (compareHist > 0.72) { // log.info("人臉匹配"); // } else { // log.info("人臉不匹配"); // } }catch (Exception e){ log.info("開(kāi)始強(qiáng)制關(guān)閉"); log.info("人臉匹配結(jié)束,總耗時(shí):{}ms",(System.currentTimeMillis()-begin)); System.exit(0); } } /** * OpenCV-4.1.1 從攝像頭實(shí)時(shí)讀取 * @param targetImgUrl 比對(duì)身份證圖片 * @return: void * @date: 2019年8月19日 17:20:13 */ public static void getVideoFromCamera(String targetImgUrl) { //1 如果要從攝像頭獲取視頻 則要在 VideoCapture 的構(gòu)造方法寫 0 VideoCapture capture = new VideoCapture(0); Mat video = new Mat(); int index = 0; if (capture.isOpened()) { while(i<3) { // 匹配成功3次退出 capture.read(video); HighGui.imshow("實(shí)時(shí)人臉識(shí)別", getFace(video, targetImgUrl)); //窗口延遲等待100ms,返回退出按鍵 index = HighGui.waitKey(100); //當(dāng)退出按鍵為Esc時(shí),退出窗口 if (index == 27) { break; } } }else{ log.info("攝像頭未開(kāi)啟"); } //該窗口銷毀不生效,該方法存在問(wèn)題 HighGui.destroyAllWindows(); capture.release(); return; } /** * OpenCV-4.1.0 人臉識(shí)別 * @param image 待處理Mat圖片(視頻中的某一幀) * @param targetImgUrl 匹配身份證照片地址 * @return 處理后的圖片 */ public static Mat getFace(Mat image, String targetImgUrl) { MatOfRect face = new MatOfRect(); faceDetector.detectMultiScale(image, face); Rect[] rects=face.toArray(); log.info("匹配到 "+rects.length+" 個(gè)人臉"); if(rects != null && rects.length >= 1) { i++; if(i==3) { // 獲取匹配成功第3次的照片 Imgcodecs.imwrite(endImgUrl + "face.jpg", image); FaceVideoThread faceVideoThread = new FaceVideoThread(targetImgUrl , endImgUrl + "face.jpg"); new Thread(faceVideoThread,"人臉比對(duì)線程").start(); } } return image; } /** * 人臉截圖 * @param img * @return */ public static String face2Img(String img) { String faceImg = null; Mat image0 = Imgcodecs.imread(img); Mat image1 = new Mat(); // 灰度化 Imgproc.cvtColor(image0, image1, Imgproc.COLOR_BGR2GRAY); // 探測(cè)人臉 MatOfRect faceDetections = new MatOfRect(); faceDetector.detectMultiScale(image1, faceDetections); // rect中人臉圖片的范圍 for (Rect rect : faceDetections.toArray()) { faceImg = img+"_.jpg"; // 進(jìn)行圖片裁剪 imageCut(img, faceImg, rect.x, rect.y, rect.width, rect.height); } if(null == faceImg){ log.info("face2Img未識(shí)別出該圖像中的人臉,img={}",img); } return faceImg; } /** * 人臉比對(duì) * @param img_1 * @param img_2 * @return */ public static double 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(Matching_Accuracy); 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 相關(guān)系數(shù) double res = Imgproc.compareHist(hist_1, hist_2, Imgproc.CV_COMP_CORREL); return res; } /** * 灰度化人臉 * @param img * @return */ public static Mat conv_Mat(String img) { if(StringUtils.isBlank(img)){ return null; } Mat image0 = Imgcodecs.imread(img); Mat image1 = new Mat(); //Mat image2 = new Mat(); // 灰度化 Imgproc.cvtColor(image0, image1, Imgproc.COLOR_BGR2GRAY); //直方均勻 //Imgproc.equalizeHist(image1, image2); // 探測(cè)人臉 MatOfRect faceDetections = new MatOfRect(); faceDetector.detectMultiScale(image1, faceDetections); //探測(cè)人眼 // MatOfRect eyeDetections = new MatOfRect(); // eyeDetector.detectMultiScale(image1, eyeDetections); // rect中人臉圖片的范圍 Mat face = null; for (Rect rect : faceDetections.toArray()) { //給圖片上畫框框 參數(shù)1是圖片 參數(shù)2是矩形 參數(shù)3是顏色 參數(shù)四是畫出來(lái)的線條大小 //Imgproc.rectangle(image0,rect,new Scalar(0,0,255),2); //輸出圖片 //Imgcodecs.imwrite(img+"_.jpg",image0); face = new Mat(image1, rect); } if(null == face){ log.info("conv_Mat未識(shí)別出該圖像中的人臉,img={}",img); } return face; } }
這邊的人臉識(shí)別是另外其線程進(jìn)行比對(duì),代碼如下。
package org.Litluecat.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class FaceVideoThread implements Runnable{ private static final Logger log = LoggerFactory.getLogger(FaceVideoThread.class); private String oneImgUrl = null; private String otherImgUrl = null; public FaceVideoThread(String oneImgUrl, String otherImgUrl){ this.oneImgUrl = oneImgUrl; this.otherImgUrl = otherImgUrl; } @Override public void run() { try { double compareHist = FaceVideo.compare_image(oneImgUrl , otherImgUrl); log.info("匹配度:{}",compareHist); if (compareHist > 0.72) { log.info("人臉匹配"); } else { log.info("人臉不匹配"); } } catch (Exception e) { e.printStackTrace(); } } }
提醒:如果運(yùn)行異常,請(qǐng)?zhí)砑幽鉶pencv的安裝地址-Djava.library.path=D:\Sofeware\opencv\build\java\x64;
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“java+opencv如何實(shí)現(xiàn)人臉識(shí)別功能”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。