欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

OpenCV Java实现人脸识别和裁剪功能

程序员文章站 2024-02-24 08:29:11
本文实例为大家分享了opencv java实现人脸识别和裁剪的具体代码,供大家参考,具体内容如下 安装及配置 1.首先安装opencv, 这里我下载的是windows...

本文实例为大家分享了opencv java实现人脸识别和裁剪的具体代码,供大家参考,具体内容如下

安装及配置

1.首先安装opencv,

这里我下载的是windows版的3.4.5

OpenCV Java实现人脸识别和裁剪功能

然后安装即可……

2.eclipse配置opencv

window->preferences->java->user libraries

OpenCV Java实现人脸识别和裁剪功能

new输入你的libraries名

OpenCV Java实现人脸识别和裁剪功能

这里我的安装目录是d:\opencv,所以是:

OpenCV Java实现人脸识别和裁剪功能

然后引入dll,我是64位机子,所以是:

OpenCV Java实现人脸识别和裁剪功能

OpenCV Java实现人脸识别和裁剪功能

ok,下面创建java项目做java与opencv的人脸识别。

人脸识别

创建项目后首先右击选择properties

OpenCV Java实现人脸识别和裁剪功能

OpenCV Java实现人脸识别和裁剪功能

然后引入即可。

引入haarcascade_frontalface_alt.xml这个xml文件:

OpenCV Java实现人脸识别和裁剪功能

我的pom文件如下:

 <dependencies>
 <dependency>
 <groupid>org.springframework.boot</groupid>
 <artifactid>spring-boot-starter-web</artifactid>
 </dependency>
 
 
 <dependency>
 <groupid>org.bytedeco.javacpp-presets</groupid>
 <artifactid>ffmpeg</artifactid>
 <version>3.1.2-1.2</version>
 </dependency>
 <dependency>
 <groupid>org.bytedeco</groupid>
 <artifactid>javacv</artifactid>
 <version>1.4.1</version>
 </dependency>
 <!-- https://mvnrepository.com/artifact/org.bytedeco.javacpp-presets/ffmpeg-platform -->
 <dependency>
 <groupid>org.bytedeco.javacpp-presets</groupid>
 <artifactid>ffmpeg-platform</artifactid>
 <version>3.4.2-1.4.1</version>
 </dependency>
 <dependency>
 <groupid>commons-io</groupid>
 <artifactid>commons-io</artifactid>
 <version>2.4</version>
 </dependency>
 
 
 <!-- 视频摄像头 -->
 <!-- https://mvnrepository.com/artifact/org.bytedeco/javacv-platform -->
 <dependency>
 <groupid>org.bytedeco</groupid>
 <artifactid>javacv-platform</artifactid>
 <version>1.4.1</version>
 </dependency>
 <!-- https://mvnrepository.com/artifact/org.bytedeco.javacpp-presets/opencv-platform -->
 <dependency>
 <groupid>org.bytedeco.javacpp-presets</groupid>
 <artifactid>opencv-platform</artifactid>
 <version>3.4.1-1.4.1</version>
 </dependency>
 
 <dependency>
 <groupid>junit</groupid>
 <artifactid>junit</artifactid>
 <version>4.12</version>
 <scope>test</scope>
 </dependency>
 
 <dependency>
 <groupid>org.springframework.boot</groupid>
 <artifactid>spring-boot-starter-test</artifactid>
 <scope>test</scope>
 </dependency>
 </dependencies>

修改我的端口号:

server.port=8889

最后代码如下:

import java.io.bufferedinputstream;
import java.io.bytearrayoutputstream;
import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.ioexception;
 
import javax.servlet.http.httpservletresponse;
 
import org.apache.commons.io.fileutils;
import org.opencv.core.core;
import org.opencv.core.mat;
import org.opencv.core.matofrect;
import org.opencv.core.point;
import org.opencv.core.rect;
import org.opencv.core.scalar;
import org.opencv.core.size;
import org.opencv.imgcodecs.imgcodecs;
import org.opencv.imgproc.imgproc;
import org.opencv.objdetect.cascadeclassifier;
import org.springframework.beans.factory.annotation.value;
import org.springframework.core.io.resource;
import org.springframework.web.bind.annotation.postmapping;
import org.springframework.web.bind.annotation.restcontroller;
import org.springframework.web.multipart.multipartfile;
 
/* 
* @author zzf 
* @date 2019年1月17日 下午12:04:45 
*/
@restcontroller
public class opencvcontroller {
 
 @value("classpath:haarcascade_frontalface_alt.xml")
 private resource xml;
 
 @postmapping("/face")
 public void facedetector(httpservletresponse response, multipartfile file) throws ioexception {
 // d:\workspace-sts-3.9.2.release\opencv\src\main\resources
 // string opencvpath = system.getproperty("user.dir") +
 // "\\src\\main\\resources\\";
 // string opencvdllname = opencvpath + core.native_library_name + ".dll";
 // system.load(opencvdllname);
 system.loadlibrary(core.native_library_name);
 system.out.println("人脸检测开始……");
 
 // 创建临时文件,因为boot打包后无法读取文件内的内容
 file targetxmlfile = new file("src/" + xml.getfilename() + "");
 fileutils.copyinputstreamtofile(xml.getinputstream(), targetxmlfile);
 cascadeclassifier facedetector = new cascadeclassifier(targetxmlfile.tostring());
 if (facedetector.empty()) {
 system.out.println("请引入文件……");
 return;
 }
 // 创建图片tempfile
 file tempfile = new file("src/" + file.getoriginalfilename() + "");
 fileutils.copyinputstreamtofile(file.getinputstream(), tempfile);
 
 // 读取创建的图片tempfile
 mat image = imgcodecs.imread(tempfile.tostring());
 matofrect facedetections = new matofrect();
 // 进行人脸检测
 facedetector.detectmultiscale(image, facedetections);
 system.out.println(string.format("检测到人脸: %s", facedetections.toarray().length));
 integer i = 1;
 // 制图将图填充到image中
 for (rect rect : facedetections.toarray()) {
 imgproc.rectangle(image, new point(rect.x, rect.y), new point(rect.x + rect.width, rect.y + rect.height),
  new scalar(0, 255, 0), 3);
 imagecut(tempfile.tostring(), i+".jpg", rect.x, rect.y, rect.width, rect.height);// 进行图片裁剪
 i++;
 }
 // 下面部分是返回给页面
 string filename = file.getoriginalfilename();
 imgcodecs.imwrite(filename, image);
 file imgfile = new file(filename);
 if (imgfile.exists()) {
 response.getoutputstream().write(tobytearray(imgfile));
 response.getoutputstream().close();
 }
 
 // 删除临时文件
 if (targetxmlfile.exists() && targetxmlfile.isfile()) {
 if (targetxmlfile.delete()) {
 system.out.println("删除临时文件" + targetxmlfile + "成功!");
 }
 }
 if (imgfile.exists() && imgfile.isfile()) {
 if (imgfile.delete()) {
 system.out.println("删除临时文件" + imgfile + "成功!");
 }
 }
 if (tempfile.exists() && tempfile.isfile()) {
 if (tempfile.delete()) {
 system.out.println("删除临时文件" + tempfile + "成功!");
 }
 }
 }
 
 public static void imagecut(string imagepath, string outfile, int posx, int posy, int width, int height) {
 // 原始图像
 mat image = imgcodecs.imread(imagepath);
 // 截取的区域:参数,坐标x,坐标y,截图宽度,截图长度
 rect rect = new rect(posx, posy, width, height);
 // 两句效果一样
 mat sub = image.submat(rect); // mat sub = new mat(image,rect);
 mat mat = new mat();
 size size = new size(width, height);
 imgproc.resize(sub, mat, size);// 将人脸进行截图并保存
 imgcodecs.imwrite(outfile, mat);
 system.out.println(string.format("图片裁切成功,裁切后图片文件为: %s", outfile));
 
 }
 
 public static byte[] tobytearray(file file) throws ioexception {
 file f = file;
 if (!f.exists()) {
 throw new filenotfoundexception("file not exists");
 }
 bytearrayoutputstream bos = new bytearrayoutputstream((int) f.length());
 bufferedinputstream in = null;
 try {
 in = new bufferedinputstream(new fileinputstream(f));
 int buf_size = 1024;
 byte[] buffer = new byte[buf_size];
 int len = 0;
 while (-1 != (len = in.read(buffer, 0, buf_size))) {
 bos.write(buffer, 0, len);
 }
 return bos.tobytearray();
 } catch (ioexception e) {
 e.printstacktrace();
 throw e;
 } finally {
 try {
 in.close();
 } catch (ioexception e) {
 e.printstacktrace();
 }
 bos.close();
 }
 }
 
}

下面来一张我男神们的合照

OpenCV Java实现人脸识别和裁剪功能

OpenCV Java实现人脸识别和裁剪功能

OpenCV Java实现人脸识别和裁剪功能

OpenCV Java实现人脸识别和裁剪功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。