Java使用OpenCV实现人脸识别
通过OpenCV实现人脸识别,包括图片,视频,摄像头中人脸识别。
首先看一下效果(在网上随便找的一张图片):
下面开始说一下如何实现的:
第一步: 需要安装OpenCV
下载链接:https://opencv.org/ 或 https://download.csdn.net/download/u012804490/10813245
第二步:创建一个Java项目
第三步:将OpenCV的jar包引入项目(截图演示)
然后 点OK。
第四步:代码实现
package com.opencv;
import org.opencv.core.*;
import org.opencv.core.Point;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.videoio.VideoCapture;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import static org.opencv.imgproc.Imgproc.CV_BILATERAL;
public class FaceRecognitionDemo {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// filePath可以是图片路径,也可以是视频路径
String filePath ="pic1236710b.jpg";
playVideo(filePath);
// 调取摄像头,进行人脸识别
// recording();
}
public static void playVideo(String fileName){
VideoCapture capture = new VideoCapture(fileName);
System.out.println(capture.get(CV_BILATERAL));
if (!capture.isOpened()) {
System.out.println("未能打开文件");
return;
}
showFace(capture);
}
public static void recording(){
VideoCapture capture = new VideoCapture(0);
System.out.println(capture.get(CV_BILATERAL));
if (!capture.isOpened()) {
System.out.println("未能打开文件");
return;
}
showFace(capture);
}
public static void showFace(VideoCapture capture){
//获得视屏流帧的宽度
int frame_width = (int) capture.get(3);
//获得视屏流帧的高度
int frame_height = (int) capture.get(4);
//将视频显示类进行实例化
VideoGui videoGui = new VideoGui();
videoGui.createWin("视频播放",new Dimension(frame_width,frame_height));
Mat mat = new Mat();
String file = "haarcascade_frontalface_alt.xml";
CascadeClassifier classifier = new CascadeClassifier(file);
MatOfRect faceDetections = new MatOfRect();
while (capture.read(mat)){
classifier.detectMultiScale(mat, faceDetections);
System.out.println(String.format("Detected %s faces",
faceDetections.toArray().length));
for (Rect rect : faceDetections.toArray()) {
Imgproc.rectangle(mat, new Point(rect.x, rect.y),
new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 0, 255)
);
}
BufferedImage bufferedImage = matToBufferedImage(mat);
//显示视频
videoGui.imgShow(bufferedImage);
}
// 关闭视频文件
capture.release();
try {
Thread.sleep(5000);
videoGui.imgClose();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static BufferedImage matToBufferedImage(Mat mat){
try {
MatOfByte matOfByte = new MatOfByte();
Imgcodecs.imencode(".jpg", mat, matOfByte);
byte[] byteArray = matOfByte.toArray();
InputStream in = new ByteArrayInputStream(byteArray);
BufferedImage bufImage = ImageIO.read(in);
return bufImage;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
package com.opencv;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class VideoGui extends JComponent {
private BufferedImage image;
private JDialog ui = new JDialog();
@Override
public void paintComponent(Graphics g){
Graphics2D g2d = (Graphics2D) g;
if(image == null){
g2d.setPaint(Color.BLACK);
g2d.fillRect(0,0,this.getWidth(),this.getHeight());
}else{
g2d.drawImage(image,0,0,this.getWidth(),this.getHeight(),null);
}
}
public void createWin(String title,Dimension size){
ui.setTitle(title);
ui.getContentPane().setLayout(new BorderLayout());
ui.getContentPane().add(this,BorderLayout.CENTER);
ui.setSize(new Dimension(size));
ui.setVisible(true);
}
public void imgShow(BufferedImage image){
this.image = image;
this.repaint();
}
public void imgClose(){
ui.dispose();
}
}
第五步:运行:
可能抛异常:Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java400 in java.library.path
解决办法: 需要配置一下 VM options:
-Djava.library.path=C:\OpenCV\opencv\build\java\x64;C:\OpenCV\opencv\build\x64\vc14\bin
再次运行,成功!
项目下载链接: https://download.csdn.net/download/u012804490/10813371
如果对您有帮助就打个赏吧(一元以内即可)