Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 使用 Face++ SDK 實現人臉識別功能詳解

編輯:關於 Android 程式設計

點開案例可以看到,眾多我們熟知的軟件都在使用這家公司提供的 SDK。

然後在開發者中心的「開發工具與 SDK 下載」中下載所需 SDK。

接著在「我的應用」中創建應用,它會給我們兩個密鑰(Key 與 Secret),要保存好,程序中會用到。

筆者這裡實現的是面部捕捉,並識別性別與年齡,先看效果圖:

下面來看實現。

1. 常量類 Constant,用來存放密鑰

public class Constant {
    // 設置之前獲取的兩個常量
    public static final String Key = "你的Key";
    public static final String Secret = "你的Secret";
}

2. 網絡檢測工具類 InternetDetect(因為要發送網絡請求,屬耗時操作,需在新線程中執行)

public class InternetDetect {
    public interface CallBack {
        void success(JSONObject jsonObject);
        void error(FaceppParseException exception);
    }

    public static void dectect(final Bitmap bitmap, final CallBack callBack) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    HttpRequests httpRequests = new HttpRequests(Constant.Key, Constant.Secret, true, true);
                    // 挖取整個視圖
                    Bitmap bitmapsmall = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight());
                    // API 要求傳入字節數組,故用字節數組輸出流
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmapsmall.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    byte[] arrays = stream.toByteArray();
                    // 構建請求參數並發送
                    PostParameters parameters = new PostParameters();
                    parameters.setImg(arrays);
                    JSONObject jsonObject = httpRequests.detectionDetect(parameters);
                    if (callBack != null) {
                        callBack.success(jsonObject);
                    }
                } catch (FaceppParseException e) {
                    e.printStackTrace();
                    if (callBack != null) {
                        callBack.error(e);
                    }
                }
            }
        }).start();
    }
}

3. MainActivity

核心邏輯:點擊按鈕從系統相冊選圖,通過 ContentResolver 拿到圖片路徑並壓縮(因接口要求圖片不超過 3M),再調用 InternetDetect.dectect 上傳識別;識別成功後解析返回的 JSONObject,把人臉框、性別、年齡等信息繪製到原圖上並刷新顯示。

private void resizePhoto() {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(ImagePath, options);
    // 計算縮放倍數,使寬高盡可能小於 1024
    double ratio = Math.max(options.outWidth * 1.0d / 1024f, options.outHeight * 1.0d / 1024f);
    options.inSampleSize = (int) Math.ceil(ratio);
    options.inJustDecodeBounds = false;
    myBitmapImage = BitmapFactory.decodeFile(ImagePath, options);
}

在回調中拿到 JSON 後,解析 face 數組,逐張人臉讀取 position(位置)、gender(性別)、age(年齡),並用 Canvas 在原圖上繪製人臉框與信息氣泡,最後刷新 ImageView。

布局文件比較簡單,這裡不再贅述。

這裡標出幾個實作中遇到的坑,供大家避開:

1. 寫按鍵響應時,要判斷用戶是否「未選圖直接點擊 detect」,否則取圖時會有空指針異常導致崩潰。
2. 上傳圖片要保證小於 3M,因此注意壓縮。
3. 氣泡處理有技巧,把它當作 TextView 更方便。
4. 繪製氣泡時也要注意縮放,否則人臉很小時氣泡會佔據整張圖,影響用戶體驗。

希望對大家有所幫助,歡迎轉載但請標明出處。如有不足之處,歡迎留言,我會盡快回復並改正!

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved