javacv:调取本地摄像头,抓取人脸,保存为图片

MAVEN:

<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv-platform</artifactId>
<version>3.4.2-1.4.2</version>
</dependency>

代码:

import static org.bytedeco.javacpp.opencv_imgproc.COLOR_BGRA2GRAY;
import static org.bytedeco.javacpp.opencv_imgproc.cvtColor;
import static org.bytedeco.javacpp.opencv_imgproc.rectangle;
import static org.bytedeco.javacpp.opencv_imgproc.resize; import javax.swing.JFrame; import org.bytedeco.javacpp.opencv_core.Mat;
import org.bytedeco.javacpp.opencv_core.Rect;
import org.bytedeco.javacpp.opencv_core.RectVector;
import org.bytedeco.javacpp.opencv_core.Scalar;
import org.bytedeco.javacpp.opencv_core.Size;
import org.bytedeco.javacpp.opencv_imgcodecs;
import org.bytedeco.javacpp.opencv_objdetect.CascadeClassifier;
import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber.Exception;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.javacv.OpenCVFrameGrabber; /**
* 调用本地摄像头窗口视频
*
* @author eguid
* @version 2016年6月13日
* @see javavcCameraTest
* @since javacv1.2
*/ public class FaceDetect { public static void main(String[] args) throws Exception, InterruptedException {
// 里面的整数值代表第N个摄像头,电脑内置默认为0,外置的从1开始
OpenCVFrameConverter.ToMat converterToMat = new OpenCVFrameConverter.ToMat();
OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
grabber.start(); // 开始获取摄像头数据
CanvasFrame canvas = new CanvasFrame("摄像头");// 新建一个窗口
canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); canvas.setAlwaysOnTop(true);
Frame videoFrame = null;
Mat videoMat = null;
while (true) {
if (!canvas.isDisplayable()) {// 窗口是否关闭
grabber.stop();// 停止抓取
grabber.close();
System.exit(2);// 退出
}
// 获取摄像头图像并放到窗口上显示, 这里的Frame是一帧视频图像
videoFrame = grabber.grab();
videoMat = converterToMat.convert(videoFrame);
Mat videoMatGray = new Mat();
// 图片灰度处理,更容易识别出人脸
cvtColor(videoMat, videoMatGray, COLOR_BGRA2GRAY);
Mat detectFace = detectFace(videoMat);
videoFrame = converterToMat.convert(detectFace);
canvas.showImage(videoFrame);// 显示带方框的人脸
Thread.sleep(50);// 50毫秒刷新一次图像
}
} /**
* 人脸检测
*
* @param videoMat
* @return
* @throws Exception
*/
public static Mat detectFace(Mat videoMat) throws Exception { System.out.println("Running DetectFace ... ");
// 从配置文件lbpcascade_frontalface.xml中创建一个人脸识别器,该文件位于opencv安装目录中
CascadeClassifier faceDetector = new CascadeClassifier(
"D:\\app\\opencv3.4.2\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");
// 在图片中检测人脸
RectVector faces = new RectVector();
faceDetector.detectMultiScale(videoMat, faces);
System.out.println(String.format("Detected %s faces", faces.size()));
Rect[] rects = faces.get();
if (rects != null && rects.length >= 1) {
for (Rect rect : rects) {
rectangle(videoMat, rect, Scalar.RED);
saveFace(videoMat, rect);
}
}
faceDetector.close();
return videoMat;
} /**
* 抓取人脸保存为图片
*
* @param image
* @param rect
*/
private static void saveFace(Mat image, Rect rect) {
Mat sub = image.rowRange(rect.y(), rect.y() + rect.height()).colRange(rect.x(), rect.x() + rect.width());
Mat mat = new Mat();
Size size = new Size(100, 100);
resize(sub, mat, size);
opencv_imgcodecs.imwrite("d:\\test\\capture\\" + System.currentTimeMillis() + ".png", mat);
}
}
上一篇:ElasticSearch — 集群搭建


下一篇:【opencv】Java实现opencv 调用本地摄像头,实现人脸识别、人形识别、人眼识别