Android開發實現人臉識別
之前看到有人在部落格寫用face++做人臉識別app,後來我也照著教程去試了一遍,發現根本行不通,原因在於他呼叫的庫是舊版本,face++已經全面更新了版本.後來我照著face++官網新版本的API文件打了一遍程式碼,發現識別的結果還算差強人意,但要識別多個人臉屬性,需要重複呼叫幾個函式,太麻煩了,這也是小編今天寫這篇文章的初衷,因為小編髮現有一個網站提供的第三方庫不僅能識別人臉,而且呼叫方法簡便,識別多個人臉屬性如性別、年齡、膚色、笑容等只需要幾行程式碼搞定。
使用工具:Android Studio
(1)註冊、登入後,點選“建立應用”
(2)填寫人臉識別模組資訊(應用名稱隨便寫)
填寫完畢後,就會得到它的APPID、API Key和secret key,如下
二、將第三方庫jar包加入Android studio專案裡面去
(1)將Android Studio由Android模式調成Project模式,可以看到libs資料夾,將jar包放入裡面
(2)右擊滑鼠,選擇“Add As Library”,
之後,build.gradle(Module:app)會自動出現以下指令,說明jar包新增成功
三、呼叫jar包裡面的AipOcr函式進行人臉識別
(1)從libs資料夾裡面的新新增 jar包可以看到具體函式,其中我們需要呼叫的函式是AipOcr,呼叫引數有APPID、apiKey、secretKey、圖片的二進位制資料流以及需要查詢的人臉屬性(如性別、年齡、膚色等)
(2)獲取圖片,該app選用兩種途徑:從相簿中選擇和呼叫手機相機拍攝
從相簿中選擇:
getImage=(Button)findViewById(R.id.getImage); getImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent in=new Intent(Intent.ACTION_PICK); in.setType("image/*"); startActivityForResult(in,PHOTO_ALBUM); } });
呼叫相機拍攝:
獲取呼叫許可權
void readRequest(){
if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);
}
if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},2);
}
}
Camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File outputImage=new File(Environment.getExternalStorageDirectory()+File.separator+"face.jpg");
try{
if(outputImage.exists()){
outputImage.delete();
}
outputImage.createNewFile();
}catch (IOException e){
e.printStackTrace();
}
imageUri=Uri.fromFile(outputImage);
ImagePath=outputImage.getAbsolutePath();//拍攝後圖片的儲存路徑
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
startActivityForResult(intent,CAMERA);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//從相簿中選擇的圖片
if(requestCode==PHOTO_ALBUM){
if(data!=null){
Uri uri=data.getData();
Cursor cursor=getContentResolver().query(uri,null,null,null,null);
cursor.moveToNext();
ImagePath=cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA));
cursor.close();
resizePhoto();
myPhoto.setImageBitmap(myBitmapImage);
Log.i("圖片路徑",ImagePath);
}
}
//相機拍攝的圖片
else if(requestCode==CAMERA){
try{
resizePhoto();
myPhoto.setImageBitmap(myBitmapImage);
}catch (Exception e){
e.printStackTrace();
}
}
}
//調整圖片的比例,使其大小小於1M,能夠顯示在手機螢幕上
public void resizePhoto(){
BitmapFactory.Options options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;//返回圖片寬高資訊
BitmapFactory.decodeFile(ImagePath,options);
//讓圖片小於1024
double radio=Math.max(options.outWidth*1.0d/1024f,options.outHeight*1.0d/1024f);
options.inSampleSize=(int)Math.ceil(radio);//向上取整倍數
options.inJustDecodeBounds=false;//顯示圖片
myBitmapImage=BitmapFactory.decodeFile(ImagePath,options);
}
(3)呼叫函式進行人臉識別
detect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
res=null;
detect_tip.setVisibility(View.VISIBLE);
detect_tip.setText("識別中...");
if(myBitmapImage==null){
myBitmapImage=BitmapFactory.decodeResource(getResources(),R.mipmap.face2);
bitmapSmall=Bitmap.createBitmap(myBitmapImage,0,0,myBitmapImage.getWidth(),myBitmapImage.getHeight());
}
else{//由於有些圖片在一些型號手機角度會傾斜,需要進行擺正
int degree=getPicRotate(ImagePath);
Matrix m=new Matrix();
m.setRotate(degree);
bitmapSmall=Bitmap.createBitmap(myBitmapImage,0,0,myBitmapImage.getWidth(),myBitmapImage.getHeight(),m,true);
}
//將圖片由路徑轉為二進位制資料流
ByteArrayOutputStream stream=new ByteArrayOutputStream();
//圖片轉資料流
bitmapSmall.compress(Bitmap.CompressFormat.JPEG,100,stream);
final byte[] arrays=stream.toByteArray();
//網路申請呼叫函式進行人臉識別
new Thread(new Runnable() {
@Override
public void run() {
HashMap<String,String> options=new HashMap<>();
options.put("face_fields","age,gender,race,beauty,expression");//人臉屬性:年齡,性別,膚色,顏值,笑容
AipFace client=new AipFace("10734368","6cvleSFbyRIRHzhijfYrHZFj","SDnCUfrtH0lgrK01HgTe2ZRLNsmCx5xy");
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(6000);
res=client.detect(arrays,options);
try{
Message message = Message.obtain();
message.what = 1;
message.obj = res;
handler.sendMessage(message);
}catch (Exception e){
e.printStackTrace();
Message message = Message.obtain();
message.what = 2;
handler.sendMessage(message);
}
}
}).start();
}
});
}
(4)將返回來的網路資料進行處理
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==1){
JSONObject res=(JSONObject) msg.obj;
face_resultNum=res.optString("result_num");
if(Integer.parseInt(face_resultNum)>=1) {
try {
JSONArray js = new JSONArray(res.optString("result"));
face_age = js.optJSONObject(0).optString("age");
face_gender = js.optJSONObject(0).optString("gender");
if (face_gender.equals("female")) {
face_gender = "女";
} else {
face_gender = "男";
}
face_race = js.optJSONObject(0).optString("race");
if (face_race.equals("yellow")) {
face_race = "黃種人";
} else if (face_race.equals("white")) {
face_race = "白種人";
} else if (face_race.equals("black")) {
face_race = "黑種人";
}else if(face_race.equals("arabs")){
face_race = "阿拉伯人";
}
int express = Integer.parseInt(js.optJSONObject(0).optString("expression"));
if (express == 0) {
face_expression = "無";
} else if (express == 1) {
face_expression = "微笑";
} else {
face_expression = "大笑";
}
face_beauty = js.optJSONObject(0).optString("beauty");
double beauty=Math.ceil(Double.parseDouble(face_beauty)+25);
if(beauty>=100){//對獲得的顏值資料進行一定處理,使得結果更為合理
beauty=99.0;
}
else if(beauty<70){
beauty+=10;
}
else if(beauty>80 && beauty<90){
beauty+=5;
}
else if(beauty>=90 && beauty<95){
beauty+=2;
}
face_beauty=String.valueOf(beauty);
} catch (JSONException e) {
e.printStackTrace();
}
detect_tip.setVisibility(View.GONE);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(faceRecognition.this);
String[] mItems = {"性別:" + face_gender, "年齡:" + face_age, "膚色:" + face_race, "顏值:" + face_beauty, "笑容:" + face_expression};
alertDialog.setTitle("人臉識別報告").setItems(mItems, null).create().show();
}else{
detect_tip.setVisibility(View.VISIBLE);
detect_tip.setText("圖片不夠清晰,請重新選擇");
}
}else{
detect_tip.setVisibility(View.VISIBLE);
detect_tip.setText("圖片不夠清晰,請重新選擇");
}
}
};
由於有些圖片在一些型號手機角度會傾斜(如三星),需要獲取圖片的角度
public int getPicRotate(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
layout佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:background="@color/color2"
android:padding="5sp"
android:gravity="center"
android:text="火眼金睛:一張圖看穿你的歲月"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/face2"
android:layout_gravity="center"
android:layout_weight="1"
android:id="@+id/myPhoto"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/detect_tip"
android:text="識別中..."/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="選圖"
android:background="@drawable/button_decoration"
android:textColor="@color/white"
android:textSize="25sp"
android:layout_weight="1"
android:id="@+id/getImage"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="拍照"
android:background="@drawable/button_decoration"
android:textColor="@color/white"
android:textSize="25sp"
android:layout_weight="1"
android:id="@+id/Camera"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/button_decoration"
android:textColor="@color/white"
android:textSize="25sp"
android:id="@+id/detect"
android:text="識別"/>
</LinearLayout>
</LinearLayout>
四、除錯結果
完整程式碼下載: