import java.util.List;
import java.util.Vector;
import org.opencv.core.Core;
import org.opencv.core.CvType;
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.opencv.videoio.VideoCapture;
import org.opencv.videoio.Videoio;
class SimpleSample {
final static int CV_HAAR_DO_CANNY_PRUNING = 1;
final static int CV_HAAR_SCALE_IMAGE = 2;
final static int CV_HAAR_FIND_BIGGEST_OBJECT = 4;
final static int CV_HAAR_DO_ROUGH_SEARCH = 8;
static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }
public void FaceDetection(String fileinname) {
double scale = 1.3;
CascadeClassifier faceDetector = new CascadeClassifier("E://workspace//facetest//bin//haarcascade_frontalface_alt2.xml");
CascadeClassifier eyeDetector = new CascadeClassifier("E://workspace//facetest//bin//haarcascade_eye.xml");
Mat image = Imgcodecs.imread(fileinname);
// Detect faces in the image.
// MatOfRect is a special container class for Rect.
//MatOfRect faceDetections = new MatOfRect();
//faceDetector.detectMultiScale(image, faceDetections);
if( !image.empty() )//读取图片数据不能为空
{
int i = 0;
double t = 0;
MatOfRect faceDetections = new MatOfRect();
Scalar colors[] = { new Scalar(0,0,255),new Scalar(0,128,255),new Scalar(0,255,255),
new Scalar(0,255,0),new Scalar(255,128,0),new Scalar(255,255,0),
new Scalar(255,0,0),new Scalar(255,0,255)} ;
// Imgproc.
Mat gray = new Mat();
Mat smallImg = new Mat((int)(image.rows()/scale), (int)(image.cols()/scale), CvType.CV_8UC1 );
Imgproc.cvtColor( image, gray, Imgproc.COLOR_BGR2GRAY );//因为用的是类haar特征,所以都是基于灰度图像的,这里要转换成灰度图像
Imgproc.resize( gray, smallImg, smallImg.size(), 0, 0, Imgproc.INTER_LINEAR );//将尺寸缩小到1/scale,用线性插值
Imgproc.equalizeHist( smallImg, smallImg );//直方图均衡
t = (double)Core.getTickCount();//用来计算算法执行时间
faceDetector.detectMultiScale( smallImg, faceDetections,1.1, 2, 0|CV_HAAR_SCALE_IMAGE,new Size(100, 100),new Size(200, 200) );
t = (double)Core.getTickCount() - t;//相减为算法执行的时间
//System.out.println("detection time = "+t/((double)Core.getTickCount()*1000.)+" ms\n");
List<Rect> rects = faceDetections.toList();
for(int m=0;m<rects.size();m++){
Rect rect = rects.get(m);
Mat smallImgROI = new Mat();
MatOfRect nestedObjects = new MatOfRect();
Point center = new Point();
Scalar color = colors[i%8];
int radius;
center.x = (int)((rect.x + rect.width*0.5)*scale);
center.y = (int)((rect.y + rect.height*0.5)*scale);
radius = (int)((rect.width + rect.height)*0.25*scale);
Imgproc.circle( image, center, radius, color, 3, 8, 0 );
if(eyeDetector.empty() )
continue;
smallImgROI = new Mat(smallImg,rect);
//和上面的函数功能一样
eyeDetector.detectMultiScale( smallImgROI, nestedObjects,1.1, 2, 0|CV_HAAR_SCALE_IMAGE,new Size(30, 30),new Size(100, 100) );
List<Rect> rectEyes = nestedObjects.toList();
for(int n=0;n<rectEyes.size();n++){
Rect rectEye = rectEyes.get(n);
center.x = (int)((rect.x + rectEye.x + rectEye.width*0.5)*scale);
center.y = (int)((rect.y + rectEye.y + rectEye.height*0.5)*scale);
radius = (int)((rectEye.width + rectEye.height)*0.25*scale);
Imgproc.circle( image, center, radius, color, 3, 8, 0 );//将眼睛也画出来,和对应人脸的图形是一样的
}
}
String filename = fileinname.toUpperCase().replace("JPG", "PNG");
System.out.println(String.format("Writing %s", filename));
Imgcodecs.imwrite(filename, image);
}
}
public void FaceDetection(Mat imageSrc,String fileinname) {
double scale = 1.3;
CascadeClassifier faceDetector = new CascadeClassifier("E://workspace//facetest//bin//haarcascade_frontalface_alt2.xml");
CascadeClassifier eyeDetector = new CascadeClassifier("E://workspace//facetest//bin//haarcascade_eye.xml");
Mat image = imageSrc;
// Detect faces in the image.
// MatOfRect is a special container class for Rect.
//MatOfRect faceDetections = new MatOfRect();
//faceDetector.detectMultiScale(image, faceDetections);
if( !image.empty() )//读取图片数据不能为空
{
int i = 0;
double t = 0;
MatOfRect faceDetections = new MatOfRect();
Scalar colors[] = { new Scalar(0,0,255),new Scalar(0,128,255),new Scalar(0,255,255),
new Scalar(0,255,0),new Scalar(255,128,0),new Scalar(255,255,0),
new Scalar(255,0,0),new Scalar(255,0,255)} ;
Mat gray = new Mat();
Mat smallImg = new Mat((int)(image.rows()/scale), (int)(image.cols()/scale), CvType.CV_8UC1 );
Imgproc.cvtColor( image, gray, Imgproc.COLOR_BGR2GRAY );//因为用的是类haar特征,所以都是基于灰度图像的,这里要转换成灰度图像
Imgproc.resize( gray, smallImg, smallImg.size(), 0, 0, Imgproc.INTER_LINEAR );//将尺寸缩小到1/scale,用线性插值
Imgproc.equalizeHist( smallImg, smallImg );//直方图均衡
t = (double)Core.getTickCount();//用来计算算法执行时间
faceDetector.detectMultiScale( smallImg, faceDetections,1.1, 2, 0|CV_HAAR_SCALE_IMAGE,new Size(100, 100),new Size(200, 200) );
t = (double)Core.getTickCount() - t;//相减为算法执行的时间
//System.out.println("detection time = "+t/((double)Core.getTickCount()*1000.)+" ms\n");
List<Rect> rects = faceDetections.toList();
for(int m=0;m<rects.size();m++){
Rect rect = rects.get(m);
Mat smallImgROI = new Mat();
MatOfRect nestedObjects = new MatOfRect();
Point center = new Point();
Scalar color = colors[i%8];
int radius;
center.x = (int)((rect.x + rect.width*0.5)*scale);
center.y = (int)((rect.y + rect.height*0.5)*scale);
radius = (int)((rect.width + rect.height)*0.25*scale);
Imgproc.circle( image, center, radius, color, 3, 8, 0 );
if(eyeDetector.empty() )
continue;
smallImgROI = new Mat(smallImg,rect);
//和上面的函数功能一样
eyeDetector.detectMultiScale( smallImgROI, nestedObjects,1.1, 2, 0|CV_HAAR_SCALE_IMAGE,new Size(30, 30),new Size(100, 100) );
List<Rect> rectEyes = nestedObjects.toList();
for(int n=0;n<rectEyes.size();n++){
Rect rectEye = rectEyes.get(n);
center.x = (int)((rect.x + rectEye.x + rectEye.width*0.5)*scale);
center.y = (int)((rect.y + rectEye.y + rectEye.height*0.5)*scale);
radius = (int)((rectEye.width + rectEye.height)*0.25*scale);
Imgproc.circle( image, center, radius, color, 3, 8, 0 );//将眼睛也画出来,和对应人脸的图形是一样的
}
}
String filename = fileinname.toUpperCase().replace("JPG", "PNG");
System.out.println(String.format("Writing %s", filename));
Imgcodecs.imwrite(filename, image);
}
}
public void run(String fileinname) {
System.out.println("\nRunning DetectFaceDemo");
// Create a face detector from the cascade file in the resources
// directory.
CascadeClassifier faceDetector = new CascadeClassifier("E://workspace//facetest//bin//lbpcascade_frontalface.xml");
//getClass().getResource("
没有合适的资源?快使用搜索试试~ 我知道了~
基于开源OPENCV人脸识别(java版)
共54个文件
xml:42个
dll:4个
java:2个
1星 需积分: 46 153 下载量 55 浏览量
2016-08-01
17:32:37
上传
评论 6
收藏 19.05MB RAR 举报
温馨提示
基于开源OPENCV3.1做人脸识别,该程序是一个简单的demo版本,可以实现如下功能:1.识别人脸和人眼;2.调用摄像头录制avi;3.可以对图片进行高斯处理。
资源推荐
资源详情
资源评论
收起资源包目录
facetest.rar (54个子文件)
facetest
bin
lbpcascade_frontalcatface.xml 119KB
libfacedetect.dll 781KB
lbpcascade_silverware.xml 46KB
haarcascade_righteye_2splits.xml 192KB
haarcascade_frontalface_default.xml 908KB
haarcascade_frontalface_alt2.xml 528KB
haarcascade_upperbody.xml 767KB
haarcascade_lefteye_2splits.xml 191KB
haarcascade_frontalface_alt.xml 661KB
SimpleSampleCap.class 10KB
lbpcascade_frontalface.xml 51KB
SimpleSample.class 10KB
haarcascade_frontalcatface_extended.xml 353KB
haarcascade_licence_plate_rus_16stages.xml 47KB
haarcascade_smile.xml 184KB
haarcascade_lowerbody.xml 386KB
haarcascade_profileface.xml 809KB
haarcascade_frontalcatface.xml 370KB
haarcascade_eye.xml 333KB
lbpcascade_profileface.xml 46KB
haarcascade_frontalface_alt_tree.xml 2.56MB
haarcascade_russian_plate_number.xml 74KB
haarcascade_fullbody.xml 466KB
haarcascade_eye_tree_eyeglasses.xml 588KB
opencv_java310.dll 29.96MB
.settings
org.eclipse.jdt.core.prefs 598B
src
SimpleSample.java 11KB
lbpcascade_frontalcatface.xml 119KB
libfacedetect.dll 781KB
lbpcascade_silverware.xml 46KB
haarcascade_righteye_2splits.xml 192KB
haarcascade_frontalface_default.xml 908KB
haarcascade_frontalface_alt2.xml 528KB
haarcascade_upperbody.xml 767KB
haarcascade_lefteye_2splits.xml 191KB
haarcascade_frontalface_alt.xml 661KB
lbpcascade_frontalface.xml 51KB
haarcascade_frontalcatface_extended.xml 353KB
haarcascade_licence_plate_rus_16stages.xml 47KB
haarcascade_smile.xml 184KB
haarcascade_lowerbody.xml 386KB
haarcascade_profileface.xml 809KB
haarcascade_frontalcatface.xml 370KB
haarcascade_eye.xml 333KB
SimpleSampleCap.java 11KB
lbpcascade_profileface.xml 46KB
haarcascade_frontalface_alt_tree.xml 2.56MB
haarcascade_russian_plate_number.xml 74KB
haarcascade_fullbody.xml 466KB
haarcascade_eye_tree_eyeglasses.xml 588KB
opencv_java310.dll 29.96MB
.project 384B
.classpath 529B
libs
armeabi
opencv-310.jar 306KB
共 54 条
- 1
资源评论
- wx:fulltilt82018-07-22没有用,少了配置文件
- 「已注销」2018-08-16少文件,无法运行
yanjiqiang
- 粉丝: 2
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功