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 image = new BufferedImage(mat.width(), mat.height(),
BufferedImage.TYPE_3BYTE_BGR);
WritableRaster raster = image.getRaster();
DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer();
byte[] data = dataBuffer.getData();
mat.get(0, 0, data);
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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
FaceRecognitionDemo.zip (12个子文件)
FaceRecognitionDemo
FaceRecognitionDemo.iml 80B
pom.xml 433B
target
classes
com
opencv
FaceRecognitionDemo.class 5KB
VideoGui.class 2KB
generated-sources
annotations
haarcascade_frontalface_alt.xml 898KB
src
test
java
main
resources
java
com
opencv
VideoGui.java 1KB
FaceRecognitionDemo.java 4KB
.idea
misc.xml 513B
compiler.xml 540B
workspace.xml 17KB
inspectionProfiles
Project_Default.xml 384B
pic1236710b.jpg 38KB
共 12 条
- 1
资源评论
倚世独殇
- 粉丝: 78
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功