熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java使用OpenCV進行人臉識別

2022-06-13   來源: Java核心技術 

  遇到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版的效果

  java使用opencv例子,java人臉檢測

  相關文件備份

  c

  有運行不了的情況是要重編譯opencv

  題外話(當然據說目前挺火的綠壩用的就是這個)

  從XFImagexml可觀察到綠霸使用了OpenCV的haar分類器進行人臉檢測綠霸附帶的cximagedll CImagedllxcoredll和Xcvdll也來自OpenCV的庫文件都反映出綠霸主要使用了OpenCV來進行圖像方面的處理綠霸 也無視了OpenCV的BSD許可證


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26235.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.