微信小程序 人脸识别功能 代码 wx.faceDetect

刚好遇到要写微信小程序人脸识别的功能,就翻了一下微信小程序文档

人脸识别 跳转文档

缺点:1. 照片也可以成功。我试了一下眨眼,但是好像不行,闭眼都能识别到眼睛。

2. ios的微信小程序 有兼容性问题,具体表现在返回的数据不一样,需要自己测试。

然后没有demo,就自己研究了,记录一下。用uniapp写的,所以需要条件编译

HTML(需要用到相机组件)

<view class="">
		<camera v-if="showcamera" device-position="front" resolution="high" style="width: 100%; height: 100vh;">
			<cover-view class="cover_bg">
				<cover-view class="cover_face" :style="{backgroundColor: bgcolor}">
					<cover-view class="cover_text">{
  {tipsText}}</cover-view>
				</co
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
基于uniapp实现的微信小程序可以使用微信提供的wx.faceDetectwx.createCameraContext接口实现人脸识别。具体步骤如下: 1. 在uniapp项目中创建一个新页面,用于实现人脸识别功能。 2. 在该页面中引入微信提供的wx.faceDetectwx.createCameraContext接口。 3. 创建一个canvas元素,用于显示摄像头捕捉到的图像。 4. 调用wx.createCameraContext接口创建一个摄像头对象,并将该对象与canvas元素绑定。 5. 调用wx.faceDetect接口进行人脸检测,并将检测结果绘制到canvas元素上。 6. 根据检测结果,可以实现人脸识别功能,例如人脸比对、人脸识别等。 以下是一个简单的示例代码: ```html <template> <view> <canvas canvas-id="camera" style="width: 100%; height: 100%;"></canvas> </view> </template> <script> export default { onReady() { const ctx = uni.createCameraContext(); const canvas = uni.createSelectorQuery().select('#camera'); canvas.fields({ node: true, size: true, }).exec((res) => { const canvasNode = res[0].node; const canvasWidth = res[0].width; const canvasHeight = res[0].height; ctx.startPreview(); setInterval(() => { ctx.takePhoto({ quality: 'high', success: (res) => { uni.canvasToTempFilePath({ canvasId: 'camera', success: (res) => { wx.faceDetect({ imageBase64: res.tempFilePath, success: (res) => { const faceList = res.faceList; const context = canvasNode.getContext('2d'); context.clearRect(0, 0, canvasWidth, canvasHeight); context.setStrokeStyle('#00ff00'); context.setLineWidth(2); for (let i = 0; i < faceList.length; i++) { const face = faceList[i]; context.strokeRect(face.x, face.y, face.width, face.height); } }, }); }, }); }, }); }, 1000); }); }, }; </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值