首页 > 其他分享 >face-api基于tensorflow 的人像检测npm 包

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

时间:2023-11-29 11:37:24浏览次数:43  
标签:npm const face api wasm tensorflow tfjs

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()
// not required, but will speed up things drastically (python required)
  • 效果

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

说明

对于基于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()
// not required, but will speed up things drastically (python required)

 

参考资料

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

标签:npm,const,face,api,wasm,tensorflow,tfjs
From: https://blog.51cto.com/rongfengliang/8612806

相关文章

  • Oracle Assets Adjustments API Documentation Supplement (Doc ID 206474.1)
    Appliesto:OracleAssets-Version11.5.10.2andlaterInformationinthisdocumentappliestoanyplatform.PurposeOracleAssetsAdjustmentsAPIYoucanusetheAdjustmentsAPItoadjustassetsdirectlyviaPL/SQLratherthanusingtheOracleAssetsinterf......
  • 通过npm安装nodejs
    npm(NodePackageManager)是Node.js的包管理器,可以让开发人员更方便地安装、上传、管理和共享代码包。而要使用npm,当然就需要先安装Node.js,下面就来讲讲如何通过npm安装nodejs。在Windows上安装nodejs,可以直接下载Node.js的安装包,然后双击运行即可。然而,在macOS......
  • GeoServer API设置WMS服务图层的缓存信息
    importrequestsimportjson#设置缓存时间data=json.dumps({"coverage":{"metadata":{"entry":[{"@key":"cacheAgeMax",&q......
  • ElasticSearch之cat nodes API
    命令样例如下:curl-XGET"https://localhost:9200/_cat/nodes?v=true&pretty"--cacert$ES_HOME/config/certs/http_ca.crt-u"elastic:ohCxPH=QBE+s5=*lo7F9"执行结果输出如下:ipheap.percentram.percentcpuload_1mload_5mload_15mnode.role......
  • 新特性之Date API
    ClockClock类提供了访问当前日期和时间的方法,Clock是时区敏感的,可以用来取代System.currentTimeMillis()来获取当前的微秒数。某个特定的时间点也可以使用Instant类来表示,Instant类也可以用来创建老的java.util.Date对象 publicclassTest{publicstaticvoidmain(Stri......
  • ElasticSearch之cat nodeattrs API
    命令样例如下:curl-XGET"https://localhost:9200/_cat/nodeattrs?v=true&pretty"--cacert$ES_HOME/config/certs/http_ca.crt-u"elastic:ohCxPH=QBE+s5=*lo7F9"执行结果输出如下:nodehostipattrvalu......
  • ElasticSearch之cat master API
    命令样例如下:curl-XGET"https://localhost:9200/_cat/master?v=true&pretty"--cacert$ES_HOME/config/certs/http_ca.crt-u"elastic:ohCxPH=QBE+s5=*lo7F9"执行结果输出如下:idhostipnodeaKgBu7LgS9a6iPYH8n2JPw1......
  • ElasticSearch之cat indices API
    命令样例如下:curl-XGET"https://localhost:9200/_cat/indices?v=true&pretty"--cacert$ES_HOME/config/certs/http_ca.crt-u"elastic:ohCxPH=QBE+s5=*lo7F9"执行结果输出如下:healthstatusindexuuidprirepdocs.coun......
  • npm 与 cnpm
    没有区别npm表示使用外国源cnpm表示使用国内源安装cnmpnpminstall-gcnpm--registry=https://registry.npm.taobao.orgcnpm必安装插件cnpminrm-g用于修改npm源命令nrmlsnrmusetaobaonpmgetregistry获取npm的源查看当前镜像源地址npmgetregis......
  • Apipost也出IDEA插件了?Apipost-Helper!
    IDEA是一款功能强大的集成开发环境(IDE),它可以帮助开发人员更加高效地编写、调试和部署软件应用程序。我们在编写完接口代码后需要进行接口调试等操作,一般需要打开额外的调试工具。今天给大家介绍一款IDEA插件:Apipost-Helper-2.0。代码写完直接编辑器内调试、还支持生成接口文档、接......