Android人臉識別開發入門--基於虹軟免費SDK實現

轉自:http://blog.csdn.net/feishixin/article/details/77718680

引言

人工智能時代快速來臨,其中人臉識別是當前比較熱門的技術,在國內也越來越多的運用,例如刷臉打卡,刷臉APP,身份識別,人臉門禁等。當前的人臉識別技術分爲WEBAPI和SDK調用兩種方式,WEBAPI需要實時聯網,SDK調用可以離線使用。

Android作爲一個比較廣泛的平臺,如何實現人臉識別功能呢。

本文章將以一個示例的形式介紹一下我在這方面的經驗。

本次使用的虹軟提供的人臉識別的SDK,此SDK也可根據不同應用場景設計,針對性強。包括人臉檢測、人臉跟蹤、人臉識別,即使在離線環境下也可正常運行。
虹軟公司是一傢俱有硅谷背景的圖像處理公司,除了人臉技術以外,還有多項圖像及視頻處理技術。他們的雙攝像頭處理算法和人臉美化算法囊括了包括OPPO VIVO,SUMAMNG一系列手機廠商。

項目的目標

我們需要實現一個人臉識別功能。簡單來說,就是機的後置攝像頭,識別攝像頭中實時拍到的人臉信息,如果人庫註冊過,則顯示識別後的人臉信息,如登記的名字;如果不在,提示未註冊。
這個功能具有多個應用場景,比如,火車站或者打卡和門禁系統中。

人臉識別的過程

人臉識別包括兩個必備的過程,人臉註冊和實時識別。
人臉註冊是指把人臉的特徵信息註冊到人臉信息庫中。人臉註冊的來源可以有很多種,比如

國家身份證庫

企業自建人臉識別庫

互聯網大數據庫

人臉特徵提取是一個不可逆的過程,你無法從人臉特徵信息還原一個人的臉部照片。

在線庫在使用時,需要傳遞照片信息,或者提取圖像特徵值,

離線的SDK相對安全,但是,在線的SDK通常提供更多的接入和調用方式,這個要結合實際情況來選擇。

人臉註冊和識別的過程可以用下面的圖來表示。

image

準備工作

在開發之前需要到虹軟的官網
http://www.arcsoft.com.cn/ai/arcface.html
下載用到的android庫,下載的壓縮包中有3個壓縮包,如下圖:
2.png
其中,第一個Face Detection,人臉檢測。

人臉檢測是人臉技術的基礎,使用虹軟人臉引擎,能夠幫助您檢測並且定位到影像(圖片或者視頻)中的人臉。

第二個Face Recognition,

人臉識別。引擎可獨立運行在終端設備或者獨立服務器中,應用端可獨立完成算法運行,能保證用戶數據的私密性,自主運營與保護用戶敏感信息。
第三個Face Tracking,

人臉跟蹤。精確定位並追蹤面部區域位置,隨着人物臉部位置的變化能夠快速定位人臉位置,並且適用於不同表情、性別、年齡、姿態、光照等條件。

這三包的結構基本相同,我們需要把它們解壓。
- doc 此目錄中存放GUIDE文檔,是說明文檔,裏面介紹了公開發布的一些API,並提供了示例代碼。
- libs 開發中需要用到的庫,需要把它們目錄結構不變的全部引用到你的項目項目中。
- sampleCode 示例代碼

注意:開發中還需要APP_Id和SDK_Key的激活碼,這些激活碼可以在賬號管理–》您的申請記錄,對應的下載應用中找到相應的激活碼。

版本與環境要求

根據SDK的說明,我們使用的版本爲android arm32,版本爲5.0.我們使用的IDE爲android studio,你也可以使用eclipse,不過依然建議你使用android studio,因爲它現在已經是事實上的標準。

一步一步實現人臉識別功能

本文將以這三個庫爲基礎,從人臉註冊開始,到人臉識別結束。全程演示人臉識別的流程。如果你不想從頭開始,你可以到https://github.com/asdfqwrasdf/ArcFaceDemo 下載完整的示例程序

新建項目

打開android studio,建立項目,API兼容性選擇4.0。

導入依賴包

虹軟人臉SDK的包是so包,你可以在下載的壓縮包中把這些文件找到並導入。
導入後的工程文件夾如下所示。

image.png

定義並實現人臉庫的相關功能

如前面所述,我們希望定義自己 的人臉庫,人臉庫在程序中使用List存儲,在系統中保存爲txt文件。

通過查詢引擎,可以知道人臉信息是保存在AFR_FSDKFace類中的。這的主要結構爲

  public static final int FEATURE_SIZE = 22020;
  byte[] mFeatureData;
  • 1
  • 2

如果要進行人臉註冊,我們需要定義另外一個類來把人臉信息和姓名關聯起來。

class FaceRegist {
        String mName;
        List<AFR_FSDKFace> mFaceList;

        public FaceRegist(String name) {
            mName = name;
            mFaceList = new ArrayList<>();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

包含特徵信息的長度和內容的byte數組。
我們把這些功能定義在類FaceDB中。FaceDB需要包含引擎定義,初始化,把人臉信息保存在版本庫和從版本庫中讀出人臉信息這些功能

初始化引擎

爲了程序結構性考慮,我們將人臉識別相關的代碼獨立出來一個類FaceDB,並定義必要的變量

public static String appid = "bCx99etK9Ns4Saou1EbFdC18xHdY9817EKw****";
public static String ft_key = "CopwZarSihp1VBu5AyGxfuLQdRMPyoGV2C2opc****";
public static String fd_key = "CopwZarSihp1VBu5AyGxfuLXnpccQbWAjd86S8****";
public static String fr_key = "CopwZarSihp1VBu5AyGxfuLexDsi8yyELdgsj4****";


String mDBPath;
List<FaceRegist> mRegister;
AFR_FSDKEngine mFREngine;
AFR_FSDKVersion mFRVersion;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

定義有參數的構造函數來初始化引擎

public FaceDB(String path) {
        mDBPath = path;
        mRegister = new ArrayList<>();
        mFRVersion = new AFR_FSDKVersion();
        mUpgrade = false;
        mFREngine = new AFR_FSDKEngine();
        AFR_FSDKError error = mFREngine.AFR_FSDK_InitialEngine(FaceDB.appid, FaceDB.fr_key);
        if (error.getCode() != AFR_FSDKError.MOK) {
            Log.e(TAG, "AFR_FSDK_InitialEngine fail! error code :" + error.getCode());
        } else {
            mFREngine.AFR_FSDK_GetVersion(mFRVersion);
            Log.d(TAG, "AFR_FSDK_GetVersion=" + mFRVersion.toString());
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

定義析構函數釋放引擎佔用的系統資源

public void destroy() {
        if (mFREngine != null) {
            mFREngine.AFR_FSDK_UninitialEngine();
        }

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

實現人臉增加和讀取功能

通常人臉庫會存放在數據庫中,本次我們使用List來進行簡單的模擬,並將其保存在文本文件中,需要時從文本中讀取,保存時寫入到文件中。

我們使用addFace方法將待註冊的人臉信息添加到人臉庫中

public  void addFace(String name, AFR_FSDKFace face) {
        try {
            //check if already registered.
            boolean add = true;
            for (FaceRegist frface : mRegister) {
                if (frface.mName.equals(name)) {
                    frface.mFaceList.add(face);
                    add = false;
                    break;
                }
            }
            if (add) { // not registered.
                FaceRegist frface = new FaceRegist(name);
                frface.mFaceList.add(face);
                mRegister.add(frface);
            }

            if (!new File(mDBPath + "/face.txt").exists()) {
                if (!saveInfo()) {
                    Log.e(TAG, "save fail!");
                }
            }

            //save name
            FileOutputStream fs = new FileOutputStream(mDBPath + "/face.txt", true);
            ExtOutputStream bos = new ExtOutputStream(fs);
            bos.writeString(name);
            bos.close();
            fs.close();

            //save feature
            fs = new FileOutputStream(mDBPath + "/" + name + ".data", true);
            bos = new ExtOutputStream(fs);
            bos.writeBytes(face.getFeatureData());
            bos.close();
            fs.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

使用loadFaces從文件中讀取人臉

public boolean loadFaces(){
        if (loadInfo()) {
            try {
                for (FaceRegist face : mRegister) {
                    Log.d(TAG, "load name:" + face.mName + "'s face feature data.");
                    FileInputStream fs = new FileInputStream(mDBPath + "/" + face.mName + ".data");
                    ExtInputStream bos = new ExtInputStream(fs);
                    AFR_FSDKFace afr = null;
                    do {
                        if (afr != null) {
                            if (mUpgrade) {
                                //upgrade data.
                            }
                            face.mFaceList.add(afr);
                        }
                        afr = new AFR_FSDKFace();
                    } while (bos.readBytes(afr.getFeatureData()));
                    bos.close();
                    fs.close();
                }
                return true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            if (!saveInfo()) {
                Log.e(TAG, "save fail!");
            }
        }
        return false;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

實現業務邏輯

實現人臉註冊功能

人臉識別的前提條件就是人臉信息要先註冊到人臉庫中,註冊人臉庫

第一步當然是獲取待註冊的照片,我們可以可以使用攝像頭,也可以使用照片。我們使用AlertDialog彈出選擇框

new AlertDialog.Builder(this)
                        .setTitle("請選擇註冊方式")
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .setItems(new String[]{"打開圖片", "拍攝照片"}, this)
                        .show();
  • 1
  • 2
  • 3
  • 4
  • 5

在對應的事件處理函數中進行處理

switch (which){
    case 1://攝像頭
        Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
        ContentValues values = new ContentValues(1);
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
        mPath = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        getImageByCamera.putExtra(MediaStore.EXTRA_OUTPUT, mPath);
        startActivityForResult(getImageByCamera, REQUEST_CODE_IMAGE_CAMERA);
        break;
    case 0://圖片
        Intent getImageByalbum = new Intent(Intent.ACTION_GET_CONTENT);
        getImageByalbum.addCategory(Intent.CATEGORY_OPENABLE);
        getImageByalbum.setType("image/jpeg");
        startActivityForResult(getImageByalbum, REQUEST_CODE_IMAGE_OP);
        break;
    default:;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

獲取一張照片後,後續我們就需要實現人臉檢測功能。

    if (requestCode == REQUEST_CODE_IMAGE_OP && resultCode == RESULT_OK) {
            mPath = data.getData();
            String file = getPath(mPath);
            //TODO: add image coversion
        }
  • 1
  • 2
  • 3
  • 4
  • 5

在上面的代碼中,我們獲取到了我們需要的圖像數據bmp,把圖片取出來
我們在Application類用函數 decodeImage中實現這段代碼

public static Bitmap decodeImage(String path) {
        Bitmap res;
        try {
            ExifInterface exif = new ExifInterface(path);
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

            BitmapFactory.Options op = new BitmapFactory.Options();
            op.inSampleSize = 1;
            op.inJustDecodeBounds = false;
            //op.inMutable = true;
            res = BitmapFactory.decodeFile(path, op);
            //rotate and scale.
            Matrix matrix = new Matrix();

            if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
                matrix.postRotate(90);
            } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
                matrix.postRotate(180);
            } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
                matrix.postRotate(270);
            }

            Bitmap temp = Bitmap.createBitmap(res, 0, 0, res.getWidth(), res.getHeight(), matrix, true);
            Log.d("com.arcsoft", "check target Image:" + temp.getWidth() + "X" + temp.getHeight());

            if (!temp.equals(res)) {
                res.recycle();
            }
            return temp;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

調用AFD_FSDK_StillImageFaceDetection返回檢測到的人臉信息

人臉註冊 ,首先要先檢測出來人臉,對於靜態圖片,虹軟人臉SDK中對應的是FD,提供了一個方法名稱,叫AFD_FSDK_StillImageFaceDetection 。
我們來看一下參數列表

類型 名稱 說明
byte[] data 輸入的圖像數據
int width 圖像寬度
int height 圖像高度
int format 圖像格式
List list 檢測到的人臉會放到到該列表裏。

注意AFD_FSDKFace對象引擎內部重複使用,如需保存,請clone一份AFD_FSDKFace對象或另外保存

AFD_FSDKFace是人臉識別的結果,定義如下

public class AFD_FSDKFace {
    Rect mRect;
    int mDegree;
    }
  • 1
  • 2
  • 3
  • 4

mRect定義一個了一個矩形框Rect

在此之前我們需要注意虹軟人臉SDK使用的圖像格式是NV21的格式,所以我們需要將獲取到的圖像轉化爲對應的格式。在Android_extend.jar中提供了對應的轉換函數

    byte[] data = new byte[mBitmap.getWidth() * mBitmap.getHeight() * 3 / 2];
                ImageConverter convert = new ImageConverter();
                convert.initial(mBitmap.getWidth(), mBitmap.getHeight(), ImageConverter.CP_PAF_NV21);
                if (convert.convert(mBitmap, data)) {
                    Log.d(TAG, "convert ok!");
                }
                convert.destroy();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

現在我們就可以調用AFD_FSDK_StillImageFaceDetection方法了

err  = engine.AFD_FSDK_StillImageFaceDetection(data, mBitmap.getWidth(), mBitmap.getHeight(), AFD_FSDKEngine.CP_PAF_NV21, result);
  • 1
  • 2

繪出人臉框

在List中保存了檢測到的人臉的位置信息和深度信息。
我們可以將檢測到的人臉位置信息在圖片上用一個矩形框繪製出來表示檢測到的人臉信息。

Canvas canvas = mSurfaceHolder.lockCanvas();
   if (canvas != null) {
      Paint mPaint = new Paint();
      boolean fit_horizontal = canvas.getWidth() / (float)src.width() < canvas.getHeight() / (float)src.height() ? true : false;
      float scale = 1.0f;
      if (fit_horizontal) {
         scale = canvas.getWidth() / (float)src.width();
         dst.left = 0;
         dst.top = (canvas.getHeight() - (int)(src.height() * scale)) / 2;
         dst.right = dst.left + canvas.getWidth();
         dst.bottom = dst.top + (int)(src.height() * scale);
      } else {
         scale = canvas.getHeight() / (float)src.height();
         dst.left = (canvas.getWidth() - (int)(src.width() * scale)) / 2;
         dst.top = 0;
         dst.right = dst.left + (int)(src.width() * scale);
         dst.bottom = dst.top + canvas.getHeight();
      }
      canvas.drawBitmap(mBitmap, src, dst, mPaint);
      canvas.save();
      canvas.scale((float) dst.width() / (float) src.width(), (float) dst.height() / (float) src.height());
      canvas.translate(dst.left / scale, dst.top / scale);
      for (AFD_FSDKFace face : result) {
         mPaint.setColor(Color.RED);
         mPaint.setStrokeWidth(10.0f);
         mPaint.setStyle(Paint.Style.STROKE);
         canvas.drawRect(face.getRect(), mPaint);
      }
      canvas.restore();
      mSurfaceHolder.unlockCanvasAndPost(canvas);
      break;
   }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

將人臉註冊到人臉庫

檢測到了人臉,我們可以輸入相應的描述信息,加入到人臉庫中。

爲了提高識別的準確性,我們可以對一個人多次註冊人臉信息。

public  void addFace(String name, AFR_FSDKFace face) {
   try {
      //check if already registered.
      boolean add = true;
      for (FaceRegist frface : mRegister) {
         if (frface.mName.equals(name)) {
            frface.mFaceList.add(face);
            add = false;
            break;
         }
      }
      if (add) { // not registered.
         FaceRegist frface = new FaceRegist(name);
         frface.mFaceList.add(face);
         mRegister.add(frface);
      }

      if (!new File(mDBPath + "/face.txt").exists()) {
         if (!saveInfo()) {
            Log.e(TAG, "save fail!");
         }
      }
      //save name
      FileOutputStream fs = new FileOutputStream(mDBPath + "/face.txt", true);
      ExtOutputStream bos = new ExtOutputStream(fs);
      bos.writeString(name);
      bos.close();
      fs.close();
      //save feature
      fs = new FileOutputStream(mDBPath + "/" + name + ".data", true);
      bos = new ExtOutputStream(fs);
      bos.writeBytes(face.getFeatureData());
      bos.close();
      fs.close();
   } catch (FileNotFoundException e) {
      e.printStackTrace();
   } catch (IOException e) {
      e.printStackTrace();
   }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

最後,別忘記了銷燬人臉檢測引擎哦

err = engine.AFD_FSDK_UninitialFaceEngine(); 
Log.d("com.arcsoft", "AFD_FSDK_UninitialFaceEngine =" + err.getCode()); 
  • 1
  • 2
  • 3

實現人臉識別

上面的代碼準備完畢後,就可以開始我們的人臉識別的功能了。我們使用一個第三方的擴展庫,ExtGLSurfaceView的擴展 庫CameraGLSurfaceView,用ImageView和TextView顯示檢測到的人臉和相應的描述信息。

首先是定義layout。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

    <com.guo.android_extend.widget.CameraSurfaceView
        android:id="@+id/surfaceView"
        android:layout_width="1dp"
        android:layout_height="1dp"/>

    <com.guo.android_extend.widget.CameraGLSurfaceView
        android:id="@+id/glsurfaceView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"/>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView"
        android:layout_alignRight="@+id/imageView"
        android:layout_below="@+id/imageView"
        android:layout_marginTop="10dp"
        android:text="@string/app_name"
        android:textAlignment="center"/>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView"
        android:layout_alignRight="@+id/imageView"
        android:layout_below="@+id/textView"
        android:layout_marginTop="10dp"
        android:text="@string/app_name"
        android:textAlignment="center"/>
</RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

因爲引擎需要的圖像格式是NV21的,所以需要將攝像頭中的圖像格式預設置爲NV21

public Camera setupCamera() {
   // TODO Auto-generated method stub
   mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
   try {
      Camera.Parameters parameters = mCamera.getParameters();
      parameters.setPreviewSize(mWidth, mHeight);
      parameters.setPreviewFormat(ImageFormat.NV21);

      for( Camera.Size size : parameters.getSupportedPreviewSizes()) {
         Log.d(TAG, "SIZE:" + size.width + "x" + size.height);
      }
      for( Integer format : parameters.getSupportedPreviewFormats()) {
         Log.d(TAG, "FORMAT:" + format);
      }

      List<int[]> fps = parameters.getSupportedPreviewFpsRange();
      for(int[] count : fps) {
         Log.d(TAG, "T:");
         for (int data : count) {
            Log.d(TAG, "V=" + data);
         }
      }
      mCamera.setParameters(parameters);
   } catch (Exception e) {
      e.printStackTrace();
   }
   if (mCamera != null) {
      mWidth = mCamera.getParameters().getPreviewSize().width;
      mHeight = mCamera.getParameters().getPreviewSize().height;
   }
   return mCamera;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

從攝像頭識別人臉,需要使用FT庫,FT庫在人臉跟蹤算法上對人臉檢測部分進行了優化,是專門爲視頻處理而優化的庫。

初始化人臉檢測引擎(FT)

和FD一樣,我們需要初始化人臉識別FT引擎。

Log.d(TAG, "AFT_FSDK_InitialFaceEngine =" + err.getCode());
err = engine.AFT_FSDK_GetVersion(version);
Log.d(TAG, "AFT_FSDK_GetVersion:" + version.toString() + "," + err.getCode());
  • 1
  • 2
  • 3
  • 4

在攝像頭的預覽事件處理函數中,先調用FT的人臉識函數函數,然後再調用FR中的人臉信息特徵提取數函數。

AFT_FSDKError err = engine.AFT_FSDK_FaceFeatureDetect(data, width, height, AFT_FSDKEngine.CP_PAF_NV21, result);

AFR_FSDKError error = engine.AFR_FSDK_ExtractFRFeature(mImageNV21, mWidth, mHeight, AFR_FSDKEngine.CP_PAF_NV21,mAFT_FSDKFace.getRect(), mAFT_FSDKFace.getDegree(), result);
  • 1
  • 2
  • 3
  • 4

這裏面的result中保存了人臉特徵信息。我們可以將其保存下來或下來並與系統中的其它信息進行對比。

AFR_FSDKMatching score = new AFR_FSDKMatching();
float max = 0.0f;
String name = null;
for (FaceDB.FaceRegist fr : mResgist) {
   for (AFR_FSDKFace face : fr.mFaceList) {
      error = engine.AFR_FSDK_FacePairMatching(result, face, score);
      Log.d(TAG,  "Score:" + score.getScore() + ", AFR_FSDK_FacePairMatching=" + error.getCode());
      if (max < score.getScore()) {
         max = score.getScore();
         name = fr.mName;
      }
   }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

當score的特徵信息大於0.6時,我們就可以認爲匹配到了人臉。顯示人臉匹配信息。

上面的循環中,可以看到,是遍歷了真個庫進行尋找。我們的目的是爲了演示,實際情況下,我們可以在找到一個匹配值比較高的人臉後,就跳出循環。

運行結果

我們來看一下運行的結果。
效果還不錯吧。鍾漢良帥哥一枚。

zhl.png

本文檔中所有的代碼都可以在https://github.com/asdfqwrasdf/ArcFaceDemo 下載。如果你需要尋找更多的人臉識別的demo,也可以到虹軟的論壇中去尋找。
http://www.arcsoft.com.cn/bbs/forum.php?mod=forumdisplay&fid=45&page=1

附錄:會遇到的問題及解決方案

如果你使用的是github中的示例,你可能會遇到下面的問題。

Plugin with id ‘com.android.application’ not found

直接從github上下載的源代碼會有這個問題。

解決方案:打開 [項目文件夾]\app\build.gradle 文件

在文件末尾添加

buildscript {

    repositories {

        mavenCentral()

    }

    dependencies {

        classpath 'com.android.tools.build:gradle:1.0.0'

    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

Failed to find build tools revision 25.0.0.2

這個主要是build 的版本和gradle中指定的版本不一致,按照提示下載或者修改版本指定就可以了。

android {
    compileSdkVersion 24
    buildToolsVersion "25.0.2"
    }
  • 1
  • 2
  • 3
  • 4

install_failed_no_maching_abis

下載的代碼在gradle編譯完成後,直接默認運行會出現這個錯誤。原因是由於使用了native libraries 。該native libraries 不支持當前的cpu的體系結構。
首先請檢查是否導入了必要的so文件。一共需要導入四個.so文件。
另外,請確認使用是的真機調試。因爲調用了攝像頭,請使用真機調試。

後記

人臉識別是當前的熱點技術,使用範圍廣,用戶體驗良好,對硬件的依賴低,不需要昂貴的傳感器芯片。一個高清的攝像頭就可以完成。以前的成本是人臉識別的SDK比較貴,但現在虹軟的SDK免費之後,集成的成本就大大降低了。

發佈了2 篇原創文章 · 獲贊 34 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章