您的位置:首页 > 运维架构

OpenCV——面部识别FaceDetector的简单使用

2016-11-29 10:46 295 查看
下面是简单的demo,从我的项目中抽取出来,这个方法可以简单检测出人脸,返回的rect可以进行下一步操作

参数文件lbpcascade_frontalface.xml是可以在文件夹内 ../OpenCV\opencv\sources\data\lbpcascades获得,OpenCV提供了多个不同来别的参数文件,可以逐一尝试玩耍

从我实验的角度来看,我选取的这个比较容易监测到正脸,但是也出现了非人脸的情况,需要进一步改进,有空的话也可以自己训练,但是图片样本要好好找

以上。

import java.util.List;

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.highgui.Highgui;
import org.opencv.objdetect.CascadeClassifier;

public class TestFace {
public static String filePath="D:\\Logs\\OCR\\TestUse\\test6.png";
public static String outputPath="D:\\Logs\\OCR\\TestUse\\FaceDetect10.png";

public static void main(String[] args){
TestFace testFace=new TestFace();
testFace.faceDetect();
}

public List<Rect> faceDetect(){

Mat image = Highgui.imread(filePath);
String facePath="D:\\Test\\lbpcascade_frontalface.xml";
CascadeClassifier faceDetector = new CascadeClassifier(facePath);
MatOfRect faceDetections = new MatOfRect();

faceDetector.detectMultiScale(image, faceDetections);
for (int i=0;i<faceDetections.toArray().length;i++){
Rect rect=faceDetections.toArray()[i];
Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x
+ rect.width, rect.y + rect.height), new Scalar(255, 0, 0));
}

//Piant the Rect of face
Mat srcImg = Highgui.imread(filePath);
int fullW = srcImg.width();
int fullH = srcImg.height();

int FareaX=(int)(fullW*0.75);
int FareaY=fullH/2;
int FareaWid=fullW/4;
int FareaHeight=fullH/4;

int one_thirdH = fullH/3;

Core.rectangle(image, new Point(FareaX, FareaY), new Point(FareaX
+FareaWid, FareaY + FareaHeight), new Scalar(0, 255, 0));
Highgui.imwrite(outputPath, image);

return faceDetections.toList();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: