現(xiàn)在,人臉識別可謂是屢見不鮮了,有些公司上班需要人臉識別來打開,地鐵進(jìn)站也運用了人臉識別等等,人臉識別的運用可以說非常的多。下面,分享一篇用Java和opencv來實現(xiàn)人臉識別功能的文章。
背景:最近需要用到人臉識別,但又不花錢使用現(xiàn)有的第三方人臉識別接口,為此使用opencv結(jié)合java進(jìn)行人臉識別(ps:opencv是開源的,使用它來做人臉識別存在一定的誤差,效果一般)。
1.安裝opencv
官網(wǎng)地址:https://opencv.org/ , 由于官網(wǎng)下載速度是真的慢
百度網(wǎng)盤:
鏈接: https://pan.baidu.com/s/1RpsP-I7v8pP2dkqALDw7FQ
提取碼: pq7v
如果是官網(wǎng)下載,就無腦安裝就行了,安裝完畢后。
將圖一的兩個文件復(fù)制到圖二中。
從我網(wǎng)盤下載的,忽略這些。
2.在項目中引入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)入庫依賴
File --> Project Structure,點擊Modules,選擇需要使用opencv.jar的項目。
選擇直接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;
/**
* 人臉比對工具類
* @author Litluecat
* @Title: Opencv 圖片人臉識別、實時攝像頭人臉識別
**/
public class FaceVideo {
private static final Logger log = LoggerFactory.getLogger(FaceVideo.class);
private static final String endImgUrl = "C:\Users\lenovo\Desktop\";
/**
* opencv的人臉識別xml文件路徑
*/
private static final String faceDetectorXML2URL = "D:\Sofeware\opencv\sources\data\haarcascades\haarcascade_frontalface_alt.xml";
/**
* opencv的人眼識別xml文件路徑
*/
private static final String eyeDetectorXML2URL = "D:\Sofeware\opencv\sources\data\haarcascades\haarcascade_eye.xml";
/**
* 直方圖大小,越大精度越高,運行越慢
*/
private static int Matching_Accuracy = 100000;
/**
* 初始化人臉探測器
*/
private static CascadeClassifier faceDetector;
/**
* 初始化人眼探測器
*/
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("開始人臉匹配");
long begin = System.currentTimeMillis();
// 1- 從攝像頭實時人臉識別,識別成功保存圖片到本地
try{
getVideoFromCamera(endImgUrl + "2.jpg");
//僅用于強制拋異常,從而關(guān)閉GUI界面
Thread.sleep(1000);
int err = 1/0;
// 2- 比對本地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("開始強制關(guān)閉");
log.info("人臉匹配結(jié)束,總耗時:{}ms",(System.currentTimeMillis()-begin));
System.exit(0);
}
}
/**
* OpenCV-4.1.1 從攝像頭實時讀取
* @param targetImgUrl 比對身份證圖片
* @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("實時人臉識別", getFace(video, targetImgUrl));
//窗口延遲等待100ms,返回退出按鍵
index = HighGui.waitKey(100);
//當(dāng)退出按鍵為Esc時,退出窗口
if (index == 27) {
break;
}
}
}else{
log.info("攝像頭未開啟");
}
//該窗口銷毀不生效,該方法存在問題
HighGui.destroyAllWindows();
capture.release();
return;
}
/**
* OpenCV-4.1.0 人臉識別
* @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+" 個人臉");
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,"人臉比對線程").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);
// 探測人臉
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未識別出該圖像中的人臉,img={}",img);
}
return faceImg;
}
/**
* 人臉比對
* @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);
// 探測人臉
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image1, faceDetections);
//探測人眼
// MatOfRect eyeDetections = new MatOfRect();
// eyeDetector.detectMultiScale(image1, eyeDetections);
// rect中人臉圖片的范圍
Mat face = null;
for (Rect rect : faceDetections.toArray()) {
//給圖片上畫框框 參數(shù)1是圖片 參數(shù)2是矩形 參數(shù)3是顏色 參數(shù)四是畫出來的線條大小
//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未識別出該圖像中的人臉,img={}",img);
}
return face;
}
}
這邊的人臉識別是另外其線程進(jìn)行比對,代碼如下。
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();
}
}
}
提醒:如果運行異常,請?zhí)砑幽鉶pencv的安裝地址-Djava.library.path=D:Sofewareopencvuildjavax64;
總結(jié):java+opencv做人臉識別的精度不夠,我也是有待學(xué)習(xí),如果大家有更好的方式,能將opencv更好的展現(xiàn)出來,并達(dá)到更精準(zhǔn)的人臉識別,請分享給我,謝謝。
到此這篇關(guān)于java+opencv實現(xiàn)人臉識別的文章就介紹到這了,更多相關(guān)java opencv人臉識別內(nèi)容,請搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,也希望大家以后多多支持!