face-api基于tensorflow 的人像检测npm 包

face-api基于tensorflow 的人像检测npm 包,原始项目为justadudewhohacks/face-api.js 但是因为缺少维护, 社区有人自己fork 了
一个新的vladmandic/face-api,可以更好的支持tensorflow 新版本,当然很不错还可以支持基于wasm 的backend (@tensorflow/tfjs-backend-wasm)

参考使用

  • demo.js
 
// import nodejs bindings to native tensorflow,
// not required, but will speed up things drastically (python required)
require('@tensorflow/tfjs-node');
 
// implements nodejs wrappers for HTMLCanvasElement, HTMLImageElement, ImageData
const canvas  = require('canvas');
 
const faceapi = require('@vladmandic/face-api/dist/face-api.node.js');
 
// patch nodejs environment, we need to provide an implementation of
// HTMLCanvasElement and HTMLImageElement
const { Canvas, Image, ImageData } = canvas
faceapi.env.monkeyPatch({ Canvas, Image, ImageData })
 
 
async function init(){
   // 需要自己加载模型,在https://github.com/vladmandic/face-api/tree/master/model 目录中,原始项目缺少模型,需要自己找
    await faceapi.nets.ssdMobilenetv1.loadFromDisk('models')
    const img = await canvas.loadImage('test.png')
    const detection = await faceapi.detectSingleFace(img)
    console.log(detection)
}
 
init()
  • 效果

说明

对于基于nodejs 开发ai的同学,使用@tensorflow/tfjs-node 还是很方便的,目前face-api 已经集成了不少模型,可以直接使用,同时也支持webassembly的玩法,参考wasm 集成

// import nodejs bindings to native tensorflow,
// not required, but will speed up things drastically (python required)
// require('@tensorflow/tfjs-node');
 
// implements nodejs wrappers for HTMLCanvasElement, HTMLImageElement, ImageData
const canvas  = require('canvas');
 
// const faceapi = require('@vladmandic/face-api/dist/face-api.node.js');
 
const tf = require('@tensorflow/tfjs-node');
const wasm = require('@tensorflow/tfjs-backend-wasm');
const faceapi = require('@vladmandic/face-api/dist/face-api.node-wasm.js'); // use this when using face-api in dev mode
// 加载wasm 文件
wasm.setWasmPaths({
    'tfjs-backend-wasm.wasm': './node_modules/@tensorflow/tfjs-backend-wasm/dist/tfjs-backend-wasm.wasm',
    'tfjs-backend-wasm-simd.wasm': './node_modules/@tensorflow/tfjs-backend-wasm/dist/tfjs-backend-wasm-simd.wasm',
    'tfjs-backend-wasm-threaded-simd.wasm': './node_modules/@tensorflow/tfjs-backend-wasm/dist/tfjs-backend-wasm-threaded-simd.wasm'
});
 
// patch nodejs environment, we need to provide an implementation of
// HTMLCanvasElement and HTMLImageElement
const { Canvas, Image, ImageData } = canvas
faceapi.env.monkeyPatch({ Canvas, Image, ImageData })
 
 
async function init(){
    await tf.setBackend('wasm');
    await tf.ready();
    await faceapi.nets.ssdMobilenetv1.loadFromDisk('models')
    const img = await canvas.loadImage('face.png')
    const detection = await faceapi.detectSingleFace(img)
    console.log(detection)
}
 
init()
 
 

参考资料

https://github.com/justadudewhohacks/face-api.js
https://github.com/vladmandic/face-api

posted on 2023-11-06 10:26  荣锋亮  阅读(198)  评论(0编辑  收藏  举报

导航