Java实现人脸检测,java线程教程

  • @Auther: DarkKing

  • @Date: 2019/10/2 11:06

  • @Description:

*/

public class DetectFace {

//定义程序的基础路径

private String basePath =System.getProperty(“user.dir”);

//人眼识别分类器路径

private String eyeConfigPath=basePath+"\src\com\facedetect\config\haarcascade_eye_tree_eyeglasses.xml";

//人脸识别分类器路径

private String faceConfigPath=basePath+"\src\com\facedetect\config\haarcascade_frontalface_alt2.xml";

static{

// 载入opencv的库

String opencvpath = System.getProperty(“user.dir”) + “\libs\x64\”;

String opencvDllName = opencvpath + Core.NATIVE_LIBRARY_NAME + “.dll”;

System.load(opencvDllName);

}

/**

  • opencv实现人脸识别

  • @param imagePath

  • @param outFile

  • @throws Exception

*/

public void detectFace(String imagePath, String outFile) throws Exception

{

System.out.println("Running DetectFace …,config path is "+faceConfigPath);

String basePath =System.getProperty(“user.dir”);

String path= basePath+ “\src\com\facedetect\tmp\”;

// 从配置文件lbpcascade_frontalface.xml中创建一个人脸识别器,该文件位于opencv安装目录中,为了方便从安装方便放到了程序路径里

CascadeClassifier faceDetector = new CascadeClassifier(faceConfigPath);

//创建图片处理对象

Mat image = Imgcodecs.imread(imagePath);

// 在图片中检测人脸

MatOfRect faceDetections = new MatOfRect();

//多条件结果检测

faceDetector.detectMultiScale(image, faceDetections);

System.out.println(String.format(“Detected %s faces”, faceDetections.toArray().length));

//检测结果集

Rect[] rects = faceDetections.toArray();

// 在每一个识别出来的人脸周围画出一个方框

for (int i = 0; i < rects.length; i++) {

Rect rect = rects[i];

Imgproc.rectangle(image, new Point(rect.x-2, rect.y-2),

new Point(rect.x + rect.width, rect.y + rect.height),

new Scalar(0, 255, 0));

Mat copy = new Mat(image,rect);

Mat temp = new Mat();

copy.copyTo(temp);

//输出图片

Imgcodecs.imwrite(path+i+".png", temp);

}

Imgcodecs.imwrite(outFile, image

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值