java opencv人脸识别_OpenCV Java实现人脸识别和裁剪功能

本文介绍了如何使用Java和OpenCV在Windows环境下进行人脸识别和图像裁剪。首先,详细阐述了OpenCV的安装配置过程,接着展示了如何在Eclipse中设置OpenCV,并引入haarcascade_frontalface_alt.xml文件。通过一个Spring Boot应用,提供了包含人脸检测、矩形标记和图像裁剪功能的REST API。代码示例展示了如何读取上传的图片,检测人脸并进行裁剪,最后返回处理后的图像。
摘要由CSDN通过智能技术生成

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

安装及配置

1.首先安装opencv,

这里我下载的是windows版的3.4.5

31afc538c54cb75bf14bac4e550ea466.png

然后安装即可……

2.eclipse配置opencv

window->preferences->java->user libraries

51a7a78368e663b4453cad7ad4b2b0f6.png

new输入你的libraries名

ddd333517bc18913d741935bf97019a4.png

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

42de818651e8aab476b475b505728182.png

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

04a190a412b761d7754dfb1bb07b1560.png

1a4b42ce703c1798d59547efef7feeab.png

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

人脸识别

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

17a33722681cccb295049ac36180a453.png

2fefe9d04ac749b9b787d2b9bcdc9338.png

然后引入即可。

引入haarcascade_frontalface_alt.xml这个xml文件:

36e8a5f197cd7fed75fabaeece76fe45.png

我的pom文件如下:

org.springframework.boot

spring-boot-starter-web

org.bytedeco.javacpp-presets

ffmpeg

3.1.2-1.2

org.bytedeco

javacv

1.4.1

org.bytedeco.javacpp-presets

ffmpeg-platform

3.4.2-1.4.1

commons-io

commons-io

2.4

org.bytedeco

javacv-platform

1.4.1

org.bytedeco.javacpp-presets

opencv-platform

3.4.1-1.4.1

junit

junit

4.12

test

org.springframework.boot

spring-boot-starter-test

test

修改我的端口号:

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();

}

}

}

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

bf2e7a7a5040011c10030845424d6b50.png

b17eb6da7eb3da26e764e38b8c9c0595.png

67e680aadf30bb0019eb8c6d07a5cce6.png

e2054bb6e59b83ebd72565100cf24fe7.png

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值