uni小程序-人脸采集、人脸检测功能

适用于人脸采集,人脸检测等功能。

逻辑就是先调起摄像头,获取到实时帧数据之后,使用wx.faceDetect进行人脸检测,检测到正面的人脸就可以拍照上传给后端了

直接上代码

<template>
	<view class="content">
			<view class="camera-box">
				<view class="camera-bg-box"><camera class="camera" device-position="front" flash="off" resolution="low"></camera></view>
				<view v-show="tipsText" class="camera-tip">{{ tipsText }}</view>
			</view>
	</view>
</template>
<script>
export default {
	data() {
		return {
			tipsText: '',
		};
	},

	onShow() {
        let that = this
		// 初始化相机引擎
		that.initData();
	},

	methods: {
		// 初始化相机引擎
		initData() {
			let that = this;
			// #ifdef MP-WEIXIN
			// 1、初始化人脸识别
			wx.initFaceDetect();
			// 2、创建 camera 上下文 CameraContext 对象
			that.cameraEngine = wx.createCameraContext();
			// 3、获取 Camera 实时帧数据
			const listener = that.cameraEngine.onCameraFrame(frame => {
				// 4、人脸识别,使用前需要通过 wx.initFaceDetect 进行一次初始化,推荐使用相机接口返回的帧数据
				wx.faceDetect({
					frameBuffer: frame.data,
					width: frame.width,
					height: frame.height,
					enablePoint: true,
					enableConf: true,
					enableAngle: true,
					enableMultiFace: true,
					success: faceData => {
						let face = faceData.faceInfo[0];
						//人脸位置校验
						var centerWidth = 150;
						var centerHeight = 150;

						if (faceData.x == -1 || faceData.y == -1) {
							that.tipsText = '检测不到人';
						}

						if (faceData.faceInfo.length > 1) {
							that.tipsText = '请保证只有一个人';
						} else {
							const { pitch, roll, yaw } = face.angleArray;
							const standard = 0.3;

							if (Math.abs(pitch) >= standard || Math.abs(roll) >= standard || Math.abs(yaw) >= standard) {
								that.tipsText = '请平视摄像头';
							} else if (
								face.x < (frame.width - centerWidth) / 2 ||
								face.x > (frame.width - centerWidth) / 2 + centerWidth ||
								face.y < (frame.height - centerHeight) / 2 ||
								face.y > (frame.height - centerHeight) / 2 + centerHeight
							) {
								this.tipsText = '请将人脸对准中心位置';
							} else if (
								face.confArray.global <= 0.8 ||
								face.confArray.leftEye <= 0.8 ||
								face.confArray.mouth <= 0.8 ||
								face.confArray.nose <= 0.8 ||
								face.confArray.rightEye <= 0.8
							) {
								that.tipsText = '请勿遮挡五官';
							} else {
								listener.stop();
								that.tipsText = '即将拍照,请保持!';
								setTimeout(function() {
									that.handleTakePhotoClick();
								}, 3000);
								return;
								let time = 3;
								that.tipsText = time + '秒后拍照,请保持!';
								let timer3 = setInterval(function() {
									time--;
									if (time <= 0) {
										clearInterval(timer3);
										// 拍照
										return that.handleTakePhotoClick();
									} else {
										that.tipsText = time + '秒后拍照,请保持!';
									}
								}, 1000);
							}
						}
					},
					fail: err => {
						if (err.x == -1 || err.y == -1) {
							that.tipsText = '检测不到人';
						} else {
							that.tipsText = err.errMsg || '网络错误,请退出页面重试';
						}
					}
				});
			});

			// 5、开始监听帧数据
			listener.start();
		},

		// 拍照
		handleTakePhotoClick() {
			this.tipsText = '正在上传...';
			setTimeout(()=>{
				this.tipsText = '上传成功';
			},2000)
			// 检测是否授权相机
			uni.getSetting({
				success: res => {
					if (!res.authSetting['scope.camera']) {
						this.isAuthCamera = false;
						uni.openSetting({
							success: res => {
								if (res.authSetting['scope.camera']) {
									this.isAuthCamera = true;
								}
							}
						});
					}
				}
			});

			this.cameraEngine.takePhoto({
				quality: 'low',
				success: ({ tempImagePath }) => {
					let mybase64 = wx.getFileSystemManager().readFileSync(tempImagePath, 'base64');
                    //拼接成base64格式
					let data = 'data:image/jpg' + ';base64,' + mybase64;
					console.log("上传",data);
					// 调用后端接口进行人脸识别,使用base64图片格式
					// 后端人脸识别可以使用阿里云,百度智能云这些
				}
			});
		}
	}
};
</script>
<style lang="scss" scoped>
	.content {
		position: absolute;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		
		.camera-box {
			position: relative;
			width: 100%;
			height: 100%;
		}
		
		.camera-bg-box{
			position: relative;
			width: 100%;
			height: 100%;
			overflow: hidden;
			
			&::after {
				content: '';
				position: absolute;
				left: 50%;
				top: 50%;
				transform: translateX(-50%) translateY(-50%);
				border-radius: 100%;
				width: 600rpx;
				height: 600rpx;
				// border: 1000rpx solid white;
				border: 1000rpx solid rgba(0, 0, 0, 0.5);
			}
		}
	
		.camera {
			width: 100%;
			height: 100%;
			border-top: 200rpx solid  black;
			border-bottom: 200rpx solid  black;
			box-sizing: border-box;
		}
	
		.camera-tip {
			position: absolute;
			bottom: 220rpx;
			left: 50%;
			transform: translateX(-50%);
			color: white;
		}
	}
</style>

  • 7
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值