博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于opencv的人脸检测
阅读量:6382 次
发布时间:2019-06-23

本文共 5700 字,大约阅读时间需要 19 分钟。

hot3.png

由于项目需要了解下图像识别,看到opencv有丰富的类库,就到官网看了下例子,因为本身项目是基于java的,所以尝试改成java版本熟悉下opencv。这里只是说人脸检测,还没能识别是谁的脸。

一、环境准备

1.下载opencv,如下由于网络原因下到了opencv-3.4.1,下载完成后执行安装,下面的opencv就是解压后的目录

09459bfc44fa5dcb7631c38ed3b13da7c7f.jpg

2.配置java.library.path

8778b0be66718752fa9d072064a948af007.jpg

3.配置jar依赖

~\opencv\build\java这个目录下有个jar包opencv-341.jar添加到工程依赖,用maven的话可以手动添加jar包依赖,也可以从中央仓库下载。阿里云仓库找到如下版本了。也可以用。System.loadLibrary(Core.NATIVE_LIBRARY_NAME);改成System.loadLibrary("opencv_java341");

org.openpnp
opencv
3.2.0-1

二、opencv官网示例代码

https://docs.opencv.org/4.0.1/d4/d26/samples_2cpp_2facedetect_8cpp-example.html,这个是C++版本,翻译下来:

public class NesttyMain  {    public static void main(String[] args){        System.out.println(System.getProperty("java.library.path"));        System.loadLibrary("opencv_java341");        CascadeClassifier cascade= new CascadeClassifier();        CascadeClassifier nestedCascade = new CascadeClassifier();//下面的训练好的文件改成自己的目录                cascade.load("D:\\3rd_app\\opencv3.4.1\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_alt.xml");        nestedCascade.load("D:\\3rd_app\\opencv3.4.1\\opencv\\build\\etc\\haarcascades\\haarcascade_eye_tree_eyeglasses.xml");        Mat image = Imgcodecs                .imread("C:\\Users\\Administrator\\Desktop\\3women.jpg");        detectAndDraw(image,cascade,nestedCascade,1,false);    }    static void  detectAndDraw( Mat img, CascadeClassifier cascade,                        CascadeClassifier nestedCascade,                        double scale, boolean tryflip ) {        double t = 0;        MatOfRect faces = new MatOfRect();        MatOfRect faces2 = new MatOfRect();        Scalar colors[] =                {                        new Scalar(255, 0, 0),                        new Scalar(255, 128, 0),                        new Scalar(255, 255, 0),                        new Scalar(0, 255, 0),                        new Scalar(0, 128, 255),                        new Scalar(0, 255, 255),                        new Scalar(0, 0, 255),                        new Scalar(255, 0, 255)                };        Mat gray = new Mat();        Mat smallImg = new Mat();        Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY);        double fx = 1 / scale;        Imgproc.resize(gray, smallImg, new Size(), fx, fx, 5);        Imgproc.equalizeHist(smallImg, smallImg);        t = (double) Core.getTickCount();        cascade.detectMultiScale(smallImg, faces,                1.1, 2, 0                        //|CASCADE_FIND_BIGGEST_OBJECT                        //|CASCADE_DO_ROUGH_SEARCH                        | CASCADE_SCALE_IMAGE,                new Size(),                new Size(150, 150));        if (tryflip) {            Core.flip(smallImg, smallImg, 1);            cascade.detectMultiScale(smallImg, faces2,                    1.1, 2, 0                            //|CASCADE_FIND_BIGGEST_OBJECT                            //|CASCADE_DO_ROUGH_SEARCH                            | CASCADE_SCALE_IMAGE,                    new Size(0, 0),                    new Size(300, 300));            for (Rect rect : faces2.toArray()) {                Imgproc.rectangle(img, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));            }        }        t = (double) Core.getTickCount() - t;        System.out.println(String.format("detection time = %g ms\n", t * 1000 / Core.getTickFrequency()));        System.out.println(String.format("detection faces = %s \n", faces.toArray().length));        int i = 0;        for (Rect rect : faces.toArray()) {            Rect r = rect;            Mat smallImgROI;            MatOfRect nestedObjects = new MatOfRect();            Point center = new Point();            Scalar color = colors[i % 8];            int radius;            double aspect_ratio = (double) r.width / r.height;            if (0.75 < aspect_ratio && aspect_ratio < 1.3) {                center.x = Math.round((r.x + r.width * 0.5) * scale);                center.y = Math.round((r.y + r.height * 0.5) * scale);                radius = (int) Math.round((r.width + r.height) * 0.25 * scale);                Imgproc.circle(img, center, radius, color, 3, 8, 0);            } else                Imgproc.rectangle(img, new Point(Math.round(r.x * scale), Math.round(r.y * scale)),                        new Point(Math.round((r.x + r.width - 1) * scale), Math.round((r.y + r.height - 1) * scale)),                        color, 3, 8, 0);            if (nestedCascade.empty())                continue;            smallImgROI = smallImg.submat(r);            nestedCascade.detectMultiScale(smallImgROI, nestedObjects,                    1.1, 2, 0                            //|CASCADE_FIND_BIGGEST_OBJECT                            //|CASCADE_DO_ROUGH_SEARCH                            //|CASCADE_DO_CANNY_PRUNING                            | CASCADE_SCALE_IMAGE,                    new Size(),                    new Size(40, 40));            System.out.println(String.format("detection eyes = %s \n", nestedObjects.toArray().length));            for (Rect rectNested : nestedObjects.toArray()) {                {                    Rect nr = rectNested;                    center.x = Math.round((r.x + nr.x + nr.width * 0.5) * scale);                    center.y = Math.round((r.y + nr.y + nr.height * 0.5) * scale);                    radius = (int) Math.round((nr.width + nr.height) * 0.25 * scale);                    Imgproc.circle(img, center, radius, color, 3, 8, 0);                }                i++;            }            Imgcodecs.imwrite("ouput.png", img);        }        img.dump();    }}

最终效果:

a19c83b26b616b0a8c77ae417170f310ab2.jpg8755a096e38c970e9cc3b3ac048be9a9e66.jpg

转载于:https://my.oschina.net/u/1271447/blog/2993533

你可能感兴趣的文章
Android消息推送之GCM方式(一)
查看>>
js无缝滚动,不平滑(求高人指点)
查看>>
js 调用父窗口的方法
查看>>
抓包工具tshark使用备忘
查看>>
CF 329B(Biridian Forest-贪心-非二分)
查看>>
attack source code
查看>>
dp之多重背包hdu1114
查看>>
Android音频介绍
查看>>
PYTHON小CASE
查看>>
Java Web----Java Web的数据库操作(二)
查看>>
hdu4336 Card Collector 状态压缩dp
查看>>
JAVA安卓和C# 3DES加密解密的兼容性问题(2013年8月修改版)
查看>>
简单的分页存储过程,Json格式日期转换为一般日期
查看>>
航空货运:运价类别Rate Class
查看>>
Windows 7下在DebugView中显示调试信息
查看>>
linux group
查看>>
应用市场中包名(package name)的唯一性
查看>>
mount --bind 的妙用
查看>>
SOAP协议
查看>>
C# WinForm 技巧八:界面开发之“WeifenLuo.WinFormsUI.Docking+OutLookBar” 使用
查看>>