这篇文章有讲如何在微信小程序中用tensorflow做人脸识别。
https://zhuanlan.zhihu.com/p/81636351
做人脸识别,先要检测到人脸,获取面部特征,然后去数据库比对。
先看如何做人脸检测,获取轮廓和面部特征。
@tensorflow-models/face-landmarks-detection - npm
这个包可以检测到人脸468个点。查看这个包的源码,发现要下载3个模型文件:
- facemesh:https://tfhub.dev/mediapipe/tfjs-model/facemesh/1/default/1
- iris:https://tfhub.dev/mediapipe/tfjs-model/iris/1/default/2
-
blazeface:https://tfhub.dev/tensorflow/tfjs-model/blazeface/1/default/1
很不幸,大概https://tfhub.dev被国内封锁了或者我们被封锁了,下载不了。
如果model = await tf_face.load();不传参数,会发现预设的模型加载url,是:
- https://www.kaggle.com/models/tensorflow/blazeface/tfJs/default/1/model.json?tfjs-format=file&tfhub-redirect=true
- https://www.kaggle.com/models/mediapipe/facemesh/tfJs/default/1/model.json?tfjs-format=file&tfhub-redirect=true
- https://www.kaggle.com/models/mediapipe/iris/tfJs/default/2/model.json?tfjs-format=file&tfhub-redirect=true
以上资源还可以用。另外,找到中国区的这个网站,也有这些资源:https://hub.tensorflow.google.cn/s?deployment-format=tfjs,分别去下载下来:
- facemesh:https://r2---sn-2x3elnes.gvt1.com/edgedl/tfhub-tfjs-modules/mediapipe/tfjs-model/facemesh/1/default/1.tar.gz
- iris:https://r2---sn-2x3elnes.gvt1.com/edgedl/tfhub-tfjs-modules/mediapipe/tfjs-model/iris/1/default/2.tar.gz
- blazeface:https://r2---sn-2x3elnes.gvt1.com/edgedl/tfhub-tfjs-modules/tensorflow/tfjs-model/blazeface/1/default/1.tar.gz
因为微信小程序不能从本地加载文件,所以必须解压,找个服务器传上去。可以开通“云开发”-“存储”或“云开发”-“静态网站”,把文件传上去。
const tf_face=require('@tensorflow-models/face-landmarks-detection');
let canvas_camera,ctx_camera;
async function main() {
console.log('load....');
console.log(tf_face.SupportedPackages.mediapipeFacemesh);
// Load the MediaPipe Facemesh package.
const model = await tf_face.load(
tf_face.SupportedPackages.mediapipeFacemesh,
{
//detectorModelUrl:'https://tfhub.dev/mediapipe/tfjs-model/facemesh/1/default/1',
detectorModelUrl:'https://???.tcloudbaseapp.com/model/facemesh/model.json',
//irisModelUrl:'https://tfhub.dev/mediapipe/tfjs-model/iris/1/default/2',
irisModelUrl:'https://???.tcloudbaseapp.com/model/iris/model.json',
//modelUrl:'https://tfhub.dev/tensorflow/tfjs-model/blazeface/1/default/1'
modelUrl:'https://???.tcloudbaseapp.com/model/blazeface/model.json'
}
);
console.log('aa');
canvas_camera=wx.createCanvas();
canvas_camera.width=300;
canvas_camera.height=400;
ctx_camera=canvas_camera.getContext('2d');
ctx_camera.drawImage(wx.tmGlobal.imgs['wxh'],
0,0,canvas_camera.width,canvas_camera.height);
// Pass in a video stream (or an image, canvas, or 3D tensor) to obtain an
// array of detected faces from the MediaPipe graph. If passing in a video
// stream, a single prediction per frame will be returned.
const predictions = await model.estimateFaces({
input: canvas_camera//document.querySelector("video")
});
console.log('bb');
pc上,检测一张小图片,用web-gl,大概1.7秒。
再改进一下,不要每次都从云端加载模型文件,保存在本地文件系统,下次从手机本地加载。
const tf_core = require('@tensorflow/tfjs-core');
const tf_plugin = require('./js/libs/@tensorflow/tfjs-plugin/index.js');
const tf_face=require('@tensorflow-models/face-landmarks-detection');
tf_plugin.configPlugin({
//backendName:'wechat-webgl',
fetchFunc: fetchWechat.fetchFunc(),
tf:tf_core,
webgl:tf_webgl,
canvas: tf_canvas
},true);
let fs=wx.getFileSystemManager();
//const c_modelUrl='https://tfhub.dev/mediapipe/tfjs-model/facemesh/1/default/1';
const c_modelUrl='https://???.tcloudbaseapp.com/model/facemesh/model.json';
//const c_irisModelUrl='https://tfhub.dev/mediapipe/tfjs-model/iris/1/default/2';
const c_irisModelUrl='https://???.tcloudbaseapp.com/model/iris/model.json';
//const c_detectorModelUrl:'https://tfhub.dev/tensorflow/tfjs-model/blazeface/1/default/1';
const c_detectorModelUrl='https://???.tcloudbaseapp.com/model/blazeface/model.json';
const fileStorageHandler_blazeface = tf_plugin.fileStorageIO('face_blazeface', fs);
const fileStorageHandler_iris = tf_plugin.fileStorageIO('face_iris', fs);
const fileStorageHandler_facemesh = tf_plugin.fileStorageIO('face_facemesh', fs);
......
let isFaceModelSaved=wx.getStorageSync('isFaceModelSaved');
let model=null;
if (isFaceModelSaved!='yes'){
console.log('load model from server');
model = await tf_face.load(
tf_face.SupportedPackages.mediapipeFacemesh,
{
detectorModelUrl:c_detectorModelUrl,
irisModelUrl:c_irisModelUrl,
modelUrl:c_modelUrl,
shouldLoadIrisModel:true
}
);
console.log(model);
model.pipeline.irisModel.save(fileStorageHandler_iris)
model.pipeline.meshDetector.save(fileStorageHandler_facemesh);
model.pipeline.boundingBoxDetector.blazeFaceModel.save(fileStorageHandler_blazeface);
wx.setStorage({
key:'isFaceModelSaved',
data:'yes'
});
}
else{
console.log('from model from locale');
model = await tf_face.load(
tf_face.SupportedPackages.mediapipeFacemesh,
{
detectorModelUrl:fileStorageHandler_blazeface,
irisModelUrl:fileStorageHandler_iris,
modelUrl:fileStorageHandler_facemesh,
shouldLoadIrisModel:true
}
);
console.log(model);
}
保存下来的文件在零时文件目录下,在pc上,可能是:C:\Users\xxxx\AppData\Local\微信开发者工具\User Data\8bd760e6f7c30cca133c1a584f36db58\WeappSimulator\WeappFileSystem\o6zAJs8Lo1aWsQT2veP8wlUxz6kg\wx283e6dfb50d72a13\usr\tensorflowjs_models
有3*3=9个文件:
- face_blazeface_info.json
- face_blazeface_model_without_weight.json
- face_blazeface_weight_data
- face_facemesh_info.json
- face_facemesh_model_without_weight.json
- face_facemesh_weight_data
- face_iris_info.json
- face_iris_model_without_weight.json
- face_iris_weight_data
如何利用这9个文件,参考这篇资料:微信小游戏用tensorflow.js人体姿势PoseNet控制_小程序集成tensorflow姿态-CSDN博客