Android TensorFlow Lite 人臉辨識
在上一篇,我們介紹過使用TensorFlow Lite來辨識物品,此篇我們來探討如何達成人臉辨識。
由於無法取得TensorFlow Lite的人臉模型,因此我們必須自已訓練模型來分析圖片。
Step 1: 安裝TensorFlow
我們在Ubuntu機器上先安裝TensorFlow
1 | pip install --upgrade "tensorflow==1.7.*" |
Step 2: 下載TensorFlow Script
1 | git clone https://github.com/googlecodelabs/tensorflow-for-poets-2 |
Step 3: 下載訓練圖片
我們在網路上擷取一些圖片,來當作訓練資料。
每種圖片至少要20張。
將下載好的圖片放置/tensorflow-for-poets-2/tf_files中,並使用一個父類別的資料夾,如
/tensorflow-for-poets-2/tf_files/people/cheryl
/tensorflow-for-poets-2/tf_files/people/fivebear
/tensorflow-for-poets-2/tf_files/people/sonia
在父類別資料夾中的子資料夾名稱,之後會成為我們訓練出來的Label名稱
Step 4: 設定Config
繼續在終端機輸入
IMAGE_SIZE : 圖像解析度,若設的太高,則分析起來會花比較長的時間。
可設定的值為 128,160,192, or 224px
ARCHITECTURE : 設定比對值,可設定為 1.0, 0.75, 0.50, or 0.25。
1 | IMAGE_SIZE=224 |
Step 5: 訓練資料
在終端機輸入
1 | python -m scripts.retrain |
程式會執行Python script : scripts.retrain,根據我們的訓練圖資會產生訓練資訊-retrained_graph.pb以及Label - peopleLabels.txt
Step 6: 轉譯為Mobile訓練資料
首先我們必須先安裝bazel, freeze_graph, toco等工具。
安裝bazel : 可參考官網的安裝方式
下載TensorFlow Source code
1 | git clone https://github.com/tensorflow/tensorflow.git |
編譯 reeze_graph, toco
1 | cd tensorflow/ |
回到tensorflow-for-poets-2資料夾後輸入
1 | toco |
執行完後會產生Mobile所使用的訓練資料檔people.tflite,再將people.tflite和peopleLabels.txt 放到Project的/src/main/assets中。
並更改前篇文章程式中的MODEL_NAME和LABEL_NAME即可。
1 | MODEL_NAME = "people"; |
執行結果
Android TensorFlow Lite 人臉辨識
https://nickcarter9.github.io/2019/04/11/2019/2019_04_11-tensorflow_lite_face_recognition/