OpenCV4Android development facial recognition
Recently, we used opencv for face recognition on android.
Now the code is briefly displayed. If the environment is set up, you can write it before it is set up.
It is easier to directly call the java api. You do not need to download opencv and decompress it.
Paste the program now
First Layout
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
Android: id = "@ + id/image"
Android: layout_width = "fill_parent"
Android: layout_height = "400dp"/>
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: orientation = "horizontal">
Android: id = "@ + id/text"
Android: layout_width = "100dp"
Android: layout_height = "wrap_content"/>
Android: layout_marginLeft = "10dp"
Android: id = "@ + id/button"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "detection"/>
Android: layout_marginLeft = "10dp"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: id = "@ + id/tu"
Android: text = "getting images"/>
Next, the main function:
Public class MainActivity extends Activity implements OnClickListener {
Final private int PICTURE_CHOOSE = 1;
Private ImageView imageView;
Private Bitmap bitmap;
Private Button button, tu;
Private TextView textView;
Private CascadeClassifier cascadeClassifier;
Private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback (this ){
@ Override
Public void onManagerConnected (int status ){
Switch (status ){
Case LoaderCallbackInterface. SUCCESS :{
}
Break;
Default :{
Super. onManagerConnected (status );
}
Break;
}
}
};
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
FindView ();
Button. setOnClickListener (this );
Button. setVisibility (View. INVISIBLE );
Tu. setOnClickListener (this );
ImageView. setImageBitmap (bitmap );
}
Private void findView (){
ImageView = (ImageView) findViewById (R. id. image );
Button = (Button) findViewById (R. id. button );
TextView = (TextView) findViewById (R. id. text );
Tu = (Button) findViewById (R. id. tu );
}
@ Override
Public void onClick (View v ){
Switch (v. getId ()){
Case R. id. button:
Try {
InputStream is = this. getResources (). openRawResource (R. raw. lbpcascade_frontalface );
File cascadeDir = this. getDir ("cascade", Context. MODE_PRIVATE );
File cascadeFile = new File (cascadeDir, "lbpcascade_frontalface.xml ");
FileOutputStream OS = new FileOutputStream (cascadeFile );
Byte [] buffer = new byte [4096];
Int bytesRead;
While (bytesRead = is. read (buffer ))! =-1 ){
OS. write (buffer, 0, bytesRead );
}
Is. close ();
OS. close ();
CascadeClassifier = new CascadeClassifier (cascadeFile. getAbsolutePath ());
If (cascadeClassifier. empty ()){
CascadeClassifier = null;
} Else
CascadeFile. delete ();
CascadeDir. delete ();
} Catch (IOException e ){
E. printStackTrace ();
}
Mat testmat = new Mat ();
System. out. println ("cccccccccxxxxxxxxxxxxccccccc" + bitmap );
Utils. bitmapToMat (bitmap, testmat );
System. out. println ("xxxxxxxxxxxxxxxxxxxxxx" + testmat );
MatOfRect facedetect = new MatOfRect ();
CascadeClassifier. detectMultiScale (testmat, facedetect );
System. out. println ("zzzzzzzzzzzzzzzz" + facedetect. toArray (). length );
Int facenum = 0;
For (Rect rect: facedetect. toArray ()){
Core. rectangle (testmat, new Point (rect. x, rect. y), new Point (
Rect. x + rect. width, rect. y + rect. height), new Scalar (
255, 0, 0 ));
++ Facenum;
}
Utils. matToBitmap (testmat, bitmap );
ImageView. setImageBitmap (bitmap );
TextView. setText ("Facecount:" + facenum );
Break;
Case R. id. tu:
System. out. println ("'sssssssssssssssssssssssssssssssssssssssssssss ");
Intent photoPickerIntent = new Intent (Intent. ACTION_PICK );
PhotoPickerIntent. setType ("image /*");
StartActivityForResult (photoPickerIntent, PICTURE_CHOOSE );
Break;
Default:
Break;
}
}
@ Override
Protected void onActivityResult (int requestCode, int resultCode,
Intent intent ){
// TODO Auto-generated method stub
Super. onActivityResult (requestCode, resultCode, intent );
System. out. println ("cccccccccccccccccccccccccccccccccccccc ");
If (requestCode = PICTURE_CHOOSE ){
If (intent! = Null ){
Cursor cursor = getContentResolver (). query (intent. getData (),
Null, null );
Cursor. moveToFirst ();
Int idx = cursor. getColumnIndex (ImageColumns. DATA );
String filesrc = cursor. getString (idx );
Options options = new Options ();
Options. inJustDecodeBounds = true;
Bitmap = BitmapFactory. decodeFile (filesrc, options );
Options. inSampleSize = Math. max (1, (int) Math. ceil (Math. max (
(Double) options. outWidth/1024f,
(Double) options. outHeight/1024f )));
Options. inJustDecodeBounds = false;
Bitmap = BitmapFactory. decodeFile (filesrc, options );
TextView. setText ("click detection ");
System. out. println ("ccccccccccccccc" + bitmap );
ImageView. setImageBitmap (bitmap );
Button. setVisibility (View. VISIBLE );
} Else {
Log. d ("ccccccccccc", "idButSelPic Photopicker canceled ");
}
}
}
@ Override
Protected void onResume (){
// TODO Auto-generated method stub
Super. onResume ();
OpenCVLoader. initAsync (OpenCVLoader. OPENCV_VERSION_2_4_9, this,
MLoaderCallback );
}
}
Programs are available. The general process is to go to the image in the album for face recognition: attach
Next we should process face recognition in real-time videos.