java

位置:IT落伍者 >> java >> 浏览文章

Java使用OpenCV进行人脸识别


发布日期:2021年11月21日
 
Java使用OpenCV进行人脸识别

遇到opencv使用后列一下

(当然据说目前挺火的 绿坝 用的就是这个)

opencv是个图形函数库内容丰富是Intel资助的开源计算机视觉库

由一系列 C 函数和少量 C++ 类构成实现了图像处理和计算机视觉方面的很多通用算法

OpenCV 对非商业应用和商业应用都是免费(FREE)的

相关网站

下载下来后例子直接运行

有些情况比如提供的例子运行出错需要重新编译

windows下vc重编译时有错误是源程序里有个注释写错了位置改了可以了编译有顺序一般提示d文件找不到顺籐摸瓜的找到源文件编译就可以

有个face检测的程序有意思

可以检测人脸

直接调用人脸检测函数非常简单

人脸检测时年的论文?后来加入了侧脸检测?

#include cvh

#include highguih

#include <stdioh>

#include <stdlibh>

#include <stringh>

#include <asserth>

#include <mathh>

#include <floath>

#include <limitsh>

#include <timeh>

#include <ctypeh>

#ifdef _EiC

#define WIN

#endif

static CvMemStorage* storage = ;

static CvHaarClassifierCascade* cascade = ;

static CvHaarClassifierCascade* nested_cascade = ;

int use_nested_cascade = ;

void detect_and_draw( IplImage* image );

const char* cascade_name =xml;

// //data/haarcascades/haarcascade_frontalface_alt_treexml;

/* ;haarcascade_profilefacexml*/

const char* nested_cascade_name =xml;

// //data/haarcascades/haarcascade_eye_tree_eyeglassesxml;

// //data/haarcascades/;

double scale = ;

int main( int argc char** argv )

{

CvCapture* capture = ;

IplImage *frame *frame_copy = ;

IplImage *image = ;

const char* scale_opt = scale=;

int scale_opt_len = (int)strlen(scale_opt);

const char* cascade_opt = cascade=;

int cascade_opt_len = (int)strlen(cascade_opt);

const char* nested_cascade_opt = nestedcascade;

int nested_cascade_opt_len = (int)strlen(nested_cascade_opt);

int i;

const char* input_name = ;

input_name = argv[];

cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name );

if( !cascade )

{

fprintf( stderr ERROR: Could not load classifier cascade\n );

fprintf( stderr

Usage: facedetect [cascade=\<cascade_path>\]\n

[nestedcascade[=\nested_cascade_path\]]\n

[scale[=<image scale>\n

[filename|camera_index]\n );

return ;

}

if( !input_name || (isdigit(input_name[]) && input_name[] == \) )

capture = cvCaptureFromCAM( !input_name ? : input_name[] );

else if( input_name )

{

storage = cvCreateMemStorage(); image = cvLoadImage( input_name );

if( !image )

capture = cvCaptureFromAVI( input_name );

}

else

image = cvLoadImage( lenajpg );

cvNamedWindow( result );

if( capture )

{

for(;;)

{

if( !cvGrabFrame( capture ))

break;

frame = cvRetrieveFrame( capture );

if( !frame )

break;

if( !frame_copy )

frame_copy = cvCreateImage( cvSize(frame>widthframe>height)

IPL_DEPTH_U frame>nChannels );

if( frame>origin == IPL_ORIGIN_TL )

cvCopy( frame frame_copy );

else

cvFlip( frame frame_copy );

detect_and_draw( frame_copy );

if( cvWaitKey( ) >= )

goto _cleanup_;

}

cvWaitKey();

_cleanup_:

cvReleaseImage( &frame_copy );

cvReleaseCapture( &capture );

}

else

{

if( image )

{

detect_and_draw( image );

cvShowImage( result image );

cvWaitKey();

cvReleaseImage( &image );

}

else if( input_name )

{

/* assume it is a text file containing the

list of the image filenames to be processed one per line */

FILE* f = fopen( input_name rt );

if( f )

{

char buf[+];

while( fgets( buf f ) )

{

int len = (int)strlen(buf) c;

while( len > && isspace(buf[len]) )

len;

buf[len] = \;

printf( file %s\n buf );

image = cvLoadImage( buf );

if( image )

{

detect_and_draw( image );

c = cvWaitKey();

if( c == || c == q || c == Q )

break;

cvReleaseImage( &image );

}

}

fclose(f);

}

}

}

cvDestroyWindow(result);

return ;

}

void detect_and_draw( IplImage* img )

{

static CvScalar colors[] =

{

{{}}

{{}}

{{}}

{{}}

{{}}

{{}}

{{}}

{{}}

};

IplImage *gray *small_img;

int i j;

gray = cvCreateImage( cvSize(img>widthimg>height) );

small_img = cvCreateImage( cvSize( cvRound (img>width/scale)

cvRound (img>height/scale)) );

cvCvtColor( img gray CV_BGRGRAY );

cvResize( gray small_img CV_INTER_LINEAR );

cvEqualizeHist( small_img small_img );

cvClearmemStorage( storage );

if( cascade )

{

double t = (double)cvGetTickCount();

CvSeq* faces = cvHaarDetectObjects( small_img cascade storage

//|CV_HAAR_FIND_BIGGEST_OBJECT

//|CV_HAAR_DO_ROUGH_SEARCH

|CV_HAAR_DO_CANNY_PRUNING

//|CV_HAAR_SCALE_IMAGE

cvSize( ) );

t = (double)cvGetTickCount() t;

printf( detection time = %gms\n t/((double)cvGetTickFrequency()*) );

for( i = ; i < (faces ? faces>total : ); i++ )

{

CvRect* r = (CvRect*)cvGetSeqElem( faces i );

CvMat small_img_roi;

CvSeq* nested_objects;

CvPoint center;

CvScalar color = colors[i%];

int radius;

centerx = cvRound((r>x + r>width*)*scale);

centery = cvRound((r>y + r>height*)*scale);

radius = cvRound((r>width + r>height)**scale);

cvCircle( img center radius color );

if( !nested_cascade )

continue;

cvGetSubRect( small_img &small_img_roi *r );

nested_objects = cvHaarDetectObjects( &small_img_roi nested_cascade storage

//|CV_HAAR_FIND_BIGGEST_OBJECT

//|CV_HAAR_DO_ROUGH_SEARCH

//|CV_HAAR_DO_CANNY_PRUNING

//|CV_HAAR_SCALE_IMAGE

cvSize( ) );

for( j = ; j < (nested_objects ? nested_objects>total : ); j++ )

{

CvRect* nr = (CvRect*)cvGetSeqElem( nested_objects j );

centerx = cvRound((r>x + nr>x + nr>width*)*scale);

centery = cvRound((r>y + nr>y + nr>height*)*scale);

radius = cvRound((nr>width + nr>height)**scale);

cvCircle( img center radius color );

}

}

}

cvShowImage( result img );

cvReleaseImage( &gray );

cvReleaseImage( &small_img );

}

相关数据

haarcascade_frontalface_altxml等

是人脸检测用的人的眼睛鼻子等数据

当然可以改名或使用你自己的

java使用opencv

用JNIOpenCVdll:

FaceDetectionjava

class JNIOpenCV {

static {

SystemloadLibrary(JNIOpenCV);

}

public native int[] detectFace(int minFaceWidth int minFaceHeight String cascade String filename);

}

public class FaceDetection {

private JNIOpenCV myJNIOpenCV;

private FaceDetection myFaceDetection;

public FaceDetection() {

myJNIOpenCV = new JNIOpenCV();

String filename = jpg;

String cascade = haarcascade_frontalface_defaultxml;

int[] detectedFaces = myJNIOpenCVdetectFace( cascade filename);

int numFaces = detectedFaceslength / ;

Systemoutprintln(numFaces = + numFaces);

for (int i = ; i < numFaces; i++) {

Systemoutprintln(Face + i + : + detectedFaces[ * i + ] + + detectedFaces[ * i + ] + + detectedFaces[ * i + ] + + detectedFaces[ * i + ]);

}

}

public static void main(String args[]) {

FaceDetection myFaceDetection = new FaceDetection();

}

}

就可以在java中实现人脸识别了

第一副图是c语言的人脸识别效果

第二幅图是java版的效果

相关文件备份

c

有运行不了的情况是要重编译opencv

题外话(当然据说目前挺火的绿坝用的就是这个)

从XFImagexml可观察到绿霸使用了OpenCV的haar分类器进行人脸检测绿霸附带的cximagedll CImagedllxcoredll和Xcvdll也来自OpenCV的库文件都反映出绿霸主要使用了OpenCV来进行图像方面的处理绿霸 也无视了OpenCV的BSD许可证

               

上一篇:Java的ClassLoader与Package机制

下一篇:JAVA编程解析之classpath的深入理解