当前位置:首页 > 代码 > 正文

opencv人眼识别代码(opencv识别眼睛)

admin 发布:2022-12-19 19:24 164


今天给各位分享opencv人眼识别代码的知识,其中也会对opencv识别眼睛进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

加载人像检测模型的代码是

很多人都认为人脸识别是一项非常难以实现的工作,看到名字就害怕,然后心怀忐忑到网上一搜,看到网上N页的教程立马就放弃了。这些人里包括曾经的我自己。其实如果如果你不是非要深究其中的原理,只是要实现这一工作的话,人脸识别也没那么难。今天我们就来看看如何在40行代码以内简单地实现人脸识别。

一点区分

对于大部分人来说,区分人脸检测和人脸识别完全不是问题。但是网上有很多教程有无无意地把人脸检测说成是人脸识别,误导群众,造成一些人认为二者是相同的。其实,人脸检测解决的问题是确定一张图上有木有人脸,而人脸识别解决的问题是这个脸是谁的。可以说人脸检测是是人识别的前期工作。今天我们要做的是人脸识别。

所用工具

Anaconda 2——Python 2

Dlib

scikit-image

Dlib

对于今天要用到的主要工具,还是有必要多说几句的。Dlib是基于现代C++的一个跨平台通用的框架,作者非常勤奋,一直在保持更新。Dlib内容涵盖机器学习、图像处理、数值算法、数据压缩等等,涉猎甚广。更重要的是,Dlib的文档非常完善,例子非常丰富。就像很多库一样,Dlib也提供了Python的接口,安装非常简单,用pip只需要一句即可:

pip install dlib

上面需要用到的scikit-image同样只是需要这么一句:

pip install scikit-image

注:如果用pip install dlib安装失败的话,那安装起来就比较麻烦了。错误提示很详细,按照错误提示一步步走就行了。

人脸识别

之所以用Dlib来实现人脸识别,是因为它已经替我们做好了绝大部分的工作,我们只需要去调用就行了。Dlib里面有人脸检测器,有训练好的人脸关键点检测器,也有训练好的人脸识别模型。今天我们主要目的是实现,而不是深究原理。感兴趣的同学可以到官网查看源码以及实现的参考文献。今天的例子既然代码不超过40行,其实是没啥难度的。有难度的东西都在源码和论文里。

首先先通过文件树看一下今天需要用到的东西:

准备了六个候选人的图片放在candidate-faces文件夹中,然后需要识别的人脸图片test.jpg。我们的工作就是要检测到test.jpg中的人脸,然后判断她到底是候选人中的谁。另外的girl-face-rec.py是我们的python脚本。shape_predi

怎样使用OpenCV进行人脸识别

1.环境搭建:

整个项目的结构图:

2.编写DetectFaceDemo.java,代码如下:

package com.njupt.zhb.test;

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;

//

// Detects faces in an image, draws boxes around them, and writes the results

// to "faceDetection.png".

//

public class DetectFaceDemo {

public void run() {

System.out.println("\nRunning DetectFaceDemo");

System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath());

// Create a face detector from the cascade file in the resources

// directory.

//CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());

//Mat image = Highgui.imread(getClass().getResource("lena.png").getPath());

//注意:源程序的路径会多打印一个‘/’,因此总是出现如下错误

/*

* Detected 0 faces Writing faceDetection.png libpng warning: Image

* width is zero in IHDR libpng warning: Image height is zero in IHDR

* libpng error: Invalid IHDR data

*/

//因此,我们将第一个字符去掉

String xmlfilePath=getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1);

CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath);

Mat image = Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1));

// Detect faces in the image.

// MatOfRect is a special container class for Rect.

MatOfRect faceDetections = new MatOfRect();

faceDetector.detectMultiScale(image, faceDetections);

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

// Draw a bounding box around each face.

for (Rect rect : faceDetections.toArray()) {

Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));

}

// Save the visualized detection.

String filename = "faceDetection.png";

System.out.println(String.format("Writing %s", filename));

Highgui.imwrite(filename, image);

}

}

3.编写测试类:

package com.njupt.zhb.test;

public class TestMain {

public static void main(String[] args) {

System.out.println("Hello, OpenCV");

// Load the native library.

System.loadLibrary("opencv_java246");

new DetectFaceDemo().run();

}

}

//运行结果:

//Hello, OpenCV

//

//Running DetectFaceDemo

///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml

//Detected 8 faces

//Writing faceDetection.png

opencv人脸识别c++

#include opencv2\core\core.hpp#include opencv2\imgproc\imgproc.hpp#include opencv2\highgui\highgui.hpp#include opencv2\video\background_segm.hpp#include opencv2/objdetect/objdetect.hpp#include iostream using namespace cv;void detectAndDraw( Mat img, CascadeClassifier cascade, CascadeClassifier nestedCascade, double scale, bool tryflip );//Mat imageresize(Mat image, Size size); /*int main(){ //VideoCapture cap(0); //打开默认摄像头 VideoCapture cap("F:/nihao.mp4"); if(!cap.isOpened()) { return -1; } Mat frame; Mat edges; CascadeClassifier cascade, nestedCascade; bool stop = false; //训练好的文件名称,放置在可执行文件同目录下 cascade.load("haarcascade_frontalface_alt.xml"); nestedCascade.load("haarcascade_eye_tree_eyeglasses.xml"); while(!stop) { capframe; detectAndDraw( frame, cascade, nestedCascade,2,0 ); if(waitKey(30) =0) stop = true; } return 0; } */int main(){ Mat image=imread("F:/quanjiafu.jpg"); CascadeClassifier cascade,nestedcascade; cascade.load("F:/Opencv2.4.9/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml"); nestedcascade.load("F:/Opencv2.4.9/opencv/sources/data/haarcascades/haarcascade_eye_tree_eyeglasses.xml"); detectAndDraw(image,cascade,nestedcascade,2,0); waitKey(0); return 0;} void detectAndDraw( Mat img, CascadeClassifier cascade, CascadeClassifier nestedCascade, double scale, bool tryflip ) { int i = 0; double t = 0; //建立用于存放人脸的向量容器 vectorRect faces, faces2; //定义一些颜色,用来标示不同的人脸 const static Scalar colors[] = { CV_RGB(0,0,255), CV_RGB(0,128,255), CV_RGB(0,255,255), CV_RGB(0,255,0), CV_RGB(255,128,0), CV_RGB(255,255,0), CV_RGB(255,0,0), CV_RGB(255,0,255)} ; //建立缩小的图片,加快检测速度 //nt cvRound (double value) 对一个double型的数进行四舍五入,并返回一个整型数! Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 ); //转成灰度图像,Harr特征基于灰度图 cvtColor( img, gray, CV_BGR2GRAY ); //改变图像大小,使用双线性差值 resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR ); //变换后的图像进行直方图均值化处理 equalizeHist( smallImg, smallImg ); //程序开始和结束插入此函数获取时间,经过计算求得算法执行时间 t = (double)cvGetTickCount(); //检测人脸 //detectMultiScale函数中smallImg表示的是要检测的输入图像为smallImg,faces表示检测到的人脸目标序列,1.1表示 //每次图像尺寸减小的比例为1.1,2表示每一个目标至少要被检测到3次才算是真的目标(因为周围的像素和不同的窗口大 //小都可以检测到人脸),CV_HAAR_SCALE_IMAGE表示不是缩放分类器来检测,而是缩放图像,Size(30, 30)为目标的 //最小最大尺寸 cascade.detectMultiScale( smallImg, faces, 1.1, 2, 0 //|CV_HAAR_FIND_BIGGEST_OBJECT //|CV_HAAR_DO_ROUGH_SEARCH |CV_HAAR_SCALE_IMAGE , Size(30, 30)); //如果使能,翻转图像继续检测 if( tryflip ) { flip(smallImg, smallImg, 1); cascade.detectMultiScale( smallImg, faces2, 1.1, 2, 0 //|CV_HAAR_FIND_BIGGEST_OBJECT //|CV_HAAR_DO_ROUGH_SEARCH |CV_HAAR_SCALE_IMAGE , Size(30, 30) ); for( vectorRect::const_iterator r = faces2.begin(); r != faces2.end(); r++ ) { faces.push_back(Rect(smallImg.cols - r-x - r-width, r-y, r-width, r-height)); } } t = (double)cvGetTickCount() - t; // qDebug( "detection time = %g ms\n", t/((double)cvGetTickFrequency()*1000.) ); for( vectorRect::const_iterator r = faces.begin(); r != faces.end(); r++, i++ ) { Mat smallImgROI; vectorRect nestedObjects; Point center; 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 = cvRound((r-x + r-width*0.5)*scale); center.y = cvRound((r-y + r-height*0.5)*scale); //Size s=Size(cvRound((r-width + r-height)*0.25*scale)*2,cvRound((r-width + r-height)*0.25*scale)*2); //Mat image=imread("F:/yaoming1.jpg"); //Mat nimage=imageresize(image,s); //Mat imageROI=img(Rect(center.x-s.width/2,center.y-s.height/2,nimage.cols,nimage.rows)); //addWeighted(imageROI,0.1,nimage,3,0.,imageROI); radius = cvRound((r-width + r-height)*0.25*scale); circle( img, center, radius, color,2, 8, 0 ); } else rectangle( img, cvPoint(cvRound(r-x*scale), cvRound(r-y*scale)), cvPoint(cvRound((r-x + r-width-1)*scale), cvRound((r-y + r-height-1)*scale)), color, 3, 8, 0); if( nestedCascade.empty() ) continue; smallImgROI = smallImg(*r); //同样方法检测人眼 nestedCascade.detectMultiScale( smallImgROI, nestedObjects, 1.1, 2, 0 //|CV_HAAR_FIND_BIGGEST_OBJECT //|CV_HAAR_DO_ROUGH_SEARCH //|CV_HAAR_DO_CANNY_PRUNING |CV_HAAR_SCALE_IMAGE , Size(30, 30) ); for( vectorRect::const_iterator nr = nestedObjects.begin(); nr != nestedObjects.end(); nr++ ) { center.x = cvRound((r-x + nr-x + nr-width*0.5)*scale); center.y = cvRound((r-y + nr-y + nr-height*0.5)*scale); radius = cvRound((nr-width + nr-height)*0.25*scale); circle( img, center, radius, color, 3, 8, 0 ); } } cv::imshow( "result", img );}//Mat imageresize(Mat image,Size size){// Mat nimage=Mat(size,CV_32S);// resize(image,nimage,size);// return nimage;

OPENCV人物识别

opencv用adaboost检测人脸,是基于haar特征的。要检测人眼可以自己训练自己的分类器,检测别的同样道理可以自己训练。

opencv中人脸检测代码如何转换成人眼检测

把人脸检测代码中所有的haarcascade_frontalface_alt2(有的用的是haarcascade_frontalface_alt)替换成haarcascade_eye就可以了,它们分别是OpenCV提供的不同分类器,前者是人脸,后者是人眼,希望可以帮到你

求人眼定位的源代码,要求是C语言的。

人眼定位在opencv里都有实现,ASM啊,Haar特征什么的.你可以下载最新版的opencv,配环境可以搜索一下VS2010或是VS2008的例子,很容易,直接读了model,Haar抓眼睛很快的,ASM对一般正脸来说抓器官位置也很准

而且你要做工程的话,opencv还是不错的图像处理库

关于opencv人眼识别代码和opencv识别眼睛的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/20217.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载