uniapp开发微信小程序-人脸采集功能、人脸检测功能

适用于人脸采集,人脸检测等功能。
uniapp开发微信小程序-人脸采集功能、人脸检测功能_第1张图片

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

<template>
	<view class="content">
			<view class="camera-box" v-if="showcamera == 1">
				<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() {
		// 初始化相机引擎
		this.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];
						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 ||
								face.x < 300 ||
								face.x > 400 ||
								face.y < 460 ||
								face.y > 600
							) {
								that.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();
								let time = 3;
								that.tipsText = time + '秒后拍照,请保持!';
								let timer = setInterval(function() {
									time--;
									if (time <= 0) {
										clearInterval(timer);
										// 拍照
										return that.handleTakePhotoClick();
									} else {
										that.tipsText = time + '秒后拍照,请保持!';
									}
								}, 1000);
								// listener.stop();
							}
						}
					},
					fail: err => {
						if (err.x == -1 || err.y == -1) {
							that.tipsText = '检测不到人';
						} else {
							that.tipsText = err.errMsg || '网络错误,请退出页面重试';
						}
					}
				});
			});

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

		// 拍照
		handleTakePhotoClick() {
			this.tipsText = '正在上传...';

			// 检测是否授权相机
			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图片格式
					// 后端人脸识别可以使用阿里云,百度智能云这些
				}
			});
		}
	}
};
</script>
.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 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%);
		.text-40-fff-600();
	}
}

扫码加q群
在这里插入图片描述

你可能感兴趣的:(uni-app,人脸检测,人脸识别,uni-app,小程序,图像处理)