vue+express人脸识别

1.腾讯云注册与登录

登录 - 腾讯云

2.你需要注册注册一个人员

点击新建人员库------> 添加基本信息

3.API Explorer

腾讯云

要记得设置完成参数后获取秘钥

4.下载安装包

使用npm i即可

//生成token
var jwt = require("jsonwebtoken")
//腾讯人脸识别
const tencentcloud = require("tencentcloud-sdk-nodejs-iai");

express 

//生成token包(npm i jsonwebtoken)
var jwt = require("jsonwebtoken")
var express = require("express")
var router = express.Router();
//腾讯人脸识别包(npm i tencentcloud-sdk-nodejs-iai)
const tencentcloud = require("tencentcloud-sdk-nodejs-iai");
 
router.get('/test', (req, res) => {
    res.send({
        code: 200,
        msg: 'success'
    })
})
//人脸识别登录
router.post('/faceLogin', async (req, res) => {
    //获取前端传来的base64
    let b64 = req.body.b64
 
    const IaiClient = tencentcloud.iai.v20200303.Client;
 
    const clientConfig = {
        credential: {
            //自己的腾讯secretId
            secretId: "自己的腾讯secretId",
            //自己的腾讯密匙
            secretKey: "自己的腾讯秘钥",
        },
        region: "ap-beijing",  //地域参数(华北地区北京)
        profile: {
            httpProfile: {
                endpoint: "iai.tencentcloudapi.com",
            },
        },
    };
 
    const client = new IaiClient(clientConfig);
 
    const params = {
        "GroupIds": [  //你创建的人员库ID
            "你创建的人员库ID"
        ],
        "Image": b64,  //图片的base64格式编码
        "NeedPersonInfo": 1,  //是否返回人员具体信息。0 为关闭,1 为开启。默认为 0。
        "QualityControl": 0,  //图片质量控制。 0: 不进行控制; 1:较低的质量要求
        "FaceMatchThreshold": 85,  //人脸对比度设置为大于85才可
    };
 
    let doc = await client.SearchFaces(params)
 
    //doc为人脸识别后的返回信息
    console.log(doc, 'doc');
 
    if (doc.Results[0].Candidates.length != 0) {  //表示当前人脸库有该人员
        let personName = doc.Results[0].Candidates[0].PersonName  //拿到该人员的名称
        console.log(personName, 'personNume');
 
        //生成token
        let token = jwt.sign({ "name": personName }, "face", { expiresIn: '1d' })
        
        res.send({
            code: 200,
            msg: "登录成功!",
            token
        })
 
    } else {
        res.send({
            code: 401,
            msg: '人脸库无此人!'
        })
        return false
    }
 
})
 
module.exports = router;
 5.vue
https://pan.baidu.com/s/1PaluS8Pz1kLYO5ZrWG29_A?pwd=yfvn
提取码: yfvn 

将拿到的js文件夹放在src中的assets中

<template>
    <div class="login-wrapper">
 
        <div class="big-box" v-show="isShowV">
            <div class="video-box">
                <video id="video" width="320" height="240" preload autoplay loop muted></video>
                <canvas id="canvas" width="320" height="240"></canvas>
            </div>
 
            <div class="success-face">
                <canvas id="screenshotCanvas" width="320" height="240"></canvas>
                <div class="switch-button">
                    <el-row>
                        <el-button type="primary" @click="destroyed">关闭摄像头</el-button>
                        <el-button type="primary" @click="init">开始识别</el-button>
                    </el-row>
                </div>
            </div>
        </div>
 
 
        <div class="login-form">
            <div class="title-container">
                <div class="t_wel">欢迎登录</div>
                <div class="t_site">李小黑</div>
            </div>
 
            <div class="form-box">
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
                    <el-form-item prop="username">
                        <el-input v-model="ruleForm.username" prefix-icon="el-icon-user-solid" placeholder="请输入用户名"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input show-password v-model="ruleForm.password" prefix-icon="el-icon-lock" placeholder="请输入密码"></el-input>
                    </el-form-item>
 
                    <el-form-item>
                        <el-button @keyup.enter="keyDown" type="primary" @click="submitForm('ruleForm')" style="width: 100%;padding: 15px 0">立即登录</el-button>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="text" @click="faceLoginBtn">人脸识别登录</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>
 
<script>
import tracking from '@/assets/js/tracking-min'
import '@/assets/js/face-min.js'
 
export default {
    name: "Login2",
    data() {
        return {
            ruleForm: {
                username: 'admin',
                password: '123',
            },
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                ],
            },
			
            //人脸识别用到的变量
            trackerTask: null,
            mediaStreamTrack: null,
            video: null,
            screenshotCanvas: null,
            uploadLock: true, // 上传锁
            isShowV:false
            
        };
    },
    methods: {
        //人脸识别登录
        faceLoginBtn(){
            this.isShowV = this
            // this.init()
        },
        // 初始化设置
        init() {
            this.video = this.mediaStreamTrack = document.getElementById('video');
            this.screenshotCanvas = document.getElementById('screenshotCanvas');
 
            let canvas = document.getElementById('canvas');
            let context = canvas.getContext('2d');
 
            // 固定写法
            let tracker = new window.tracking.ObjectTracker('face');
            tracker.setInitialScale(4);
            tracker.setStepSize(2);
            tracker.setEdgesDensity(0.1);
            //摄像头初始化
            this.trackerTask = window.tracking.track('#video', tracker, {
                camera: true
            });
 
            let _this = this;
            
            //监听图像变化
            tracker.on('track', function(event) {
 
                // 检测出人脸 绘画人脸位置
                context.clearRect(0, 0, canvas.width, canvas.height);
                event.data.forEach(function(rect) {
                    context.strokeStyle = '#76e535';
                    context.strokeRect(rect.x, rect.y, rect.width, rect.height);
                });
                // event.data.length长度为多少代表检测几张人脸
                if(_this.uploadLock && event.data.length){
                    //上传图片
                    _this.screenshotAndUpload();
                }
            });
        },
        // 上传图片
        screenshotAndUpload() {
            // 上锁避免重复发送请求
            this.uploadLock = false;
 
            // 绘制当前帧图片转换为base64格式
            let canvas = this.screenshotCanvas;
            let video = this.video;
            let ctx = canvas.getContext('2d');
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
            
            //base64Img为最终人像数据
            let base64Img = canvas.toDataURL('image/jpeg');
            console.log(base64Img.split(',')[1]);
            //发送给后端
			this.$http.post("http://127.0.0.1:3000/api/faceLogin",{b64:base64Img.split(',')[1]}).then(res=>{
                if(res.data.code===200){
                    //登录成功把token存在浏览器会话存储中
                    sessionStorage.setItem("token",res.data.token)
 
                    this.$message.success(res.data.msg)
                    this.destroyed();
                    this.$router.push('index')
                }else if(res.data.code===401){
                    console.log(res.data,'res.data')
                    this.$message.error(res.data.msg)
 
                    this.destroyed();
                    this.$router.push('/kong')
                }else if(res.data.code===251){
                    console.log(res.data,'res.data')
                    this.$message.error(res.data.msg)
                    this.destroyed();
                }
            })
            								
            // 请求接口成功以后打开锁
            // this.uploadLock = true;
        },
        
        //关闭摄像头
        destroyed(){
            if(!this.mediaStreamTrack){
                return
            }
            this.mediaStreamTrack.srcObject.getTracks()[0].stop();
            this.trackerTask.stop()
            this.isShowV = false
        },
 
        //键盘事件
        keyDown(e){
            //如果是回车则执行登录方法
            if(e.keyCode === 13){
                //需要执行的登录方法
                this.submitForm('ruleForm');
            }
        },
        //表单登录按钮
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.$message.success('本登录只能使用人脸登录!')
                } else {
                    console.log('error submit!!');
                    this.$message.error('请填写登录信息!!!')
                    return false;
                }
            });
        },
    },
    mounted() {
        this.$http.get('http://127.0.0.1:3000/api/test').then(res=>{
            console.log(res.data)
        })
        //绑定事件
        window.addEventListener('keydown',this.keyDown);
    },
    //销毁事件
    destroyed(){
        window.removeEventListener('keydown',this.keyDown,false);
    }
}
</script>
 
<style lang="scss" scoped>
html,body{
    width: 100%;
    height: 100%;
}
*{
    box-sizing: border-box;
}
.login-wrapper{
    background-image: url("../assets/image/1.jpeg");//背景图片自行更改
    width: 100%;
    height: 100%;
    background-repeat: no-repeat;
    background-size: 100% 100%;
    background-position: center center;
    overflow: hidden;
    background-size: cover;
    .login-form{
        position: absolute;
        left: 80%;
        top: 50%;
        -webkit-transform: translate(-50%,-50%);
        transform: translate(-50%,-50%);
        width: 480px;
        height: 465px;
        max-width: 100%;
        margin-right: 120px;
        padding: 40px 70px 0;
        overflow: hidden;
        background-color: rgba(255,255,255,.1);
        border-radius: 15px;
        .title-container{
            position: relative;
            text-align: center;
            .t_wel{
                font-size: 26px;
                line-height: 1;
                color: #eee;
                margin-bottom: 10px;
            }
            .t_site{
                font-size: 14px;
                line-height: 1;
                color: #bebebe;
                margin-bottom: 26px;
            }
        }
        .form-box{
            .el-form{
                .el-form-item{
                    .el-input{
                        margin-top: 10px;
                    }
                    .el-button{
                        margin-top: 10px;
                    }
                }
            }
        }
    }
    .big-box{
        position: absolute;
        top: 5%;
        left: 50%;
        width: 320px;
        //border: 1px solid #ddd;
        //height: 700px;
        transform: translate(-50%);
        .video-box{
            width: 320px;
            height: 240px;
            #canvas{
                position: absolute;
                top: 0;
                left: 0;
            }
        }
        .success-face{
            width: 320px;
            height: 240px;
            //border: #ddd solid 1px;
        }
    }
 
}
</style>
 

作为AI智能领域的重要应用之一,人脸识别已经被广泛应用于很多领域,如安防监控、智能门锁、出行服务等。在人脸识别的实现过程中,后端的框架技术、数据库的存储技术和前端的展示技术都是至关重要的。本文将介绍如何使用SpringBoot、Vue、MyBatis实现人脸识别应用。 一、技术架构与选型 1、技术架构 前端使用Vue框架,实现数据的展示和用户交互;后端使用SpringBoot框架,实现接口的创建和数据的处理;数据库使用MySQL,存储用户信息和人脸数据;人脸识别引擎使用Face++,实现人脸检测、识别等功能。 2、技术选型 Vue:轻量级、MVVM架构、组件化开发。 SpringBoot:基于Spring、快速开发、自动配置。 MyBatis:ORM框架、减少SQL代码量、提高效率。 MySQL:流行的关系型数据库、易于使用。 Face++:全球领先的人脸识别技术提供商、稳定可靠、安全性高。 二、技术实现 1、前端实现 前端的实现主要包括如下两个方面: (1)页面展示 使用Vue.js创建页面,并通过axios发起请求与后端进行交互。通过展示人脸照片、关键信息列表、系统设置等功能,可以帮助用户更好地进行人脸检索。 (2)人脸识别 通过Face++提供的API接口,可以实现人脸检测、剪裁、人脸搜索等功能。具体实现过程如下: 1)人脸检测和剪裁:通过调用Face++提供的detect接口,可以检测上传的图片是否包含人脸,如果有则返回人脸在图片中的位置和关键点位置,再根据位置信息和关键点位置将人脸从原始图片中剪裁出来。 2)人脸搜索:通过调用Face++提供的search接口,可以将剪裁出来的人脸与存储在MySQL数据库中的人脸库图片逐一比对,返回最相似的人脸信息。 2、后端实现 后端的实现主要包括如下两个方面: (1)接口创建 利用SpringBoot提供的@RestController注解,可以创建多个RESTful风格的接口,如上传图片、搜索人脸信息、查询人员名单等。 (2)数据处理 利用MyBatis提供的ORM功能,可以将请求的数据与MySQL数据库中的表进行增删查改等操作。将人脸库的图片存储在云端服务器中,并将图片的地址存储在MySQL数据库中,保证数据的安全性和备份性。 三、技术难点 1、人脸检测和剪裁 在必须保证人脸剪裁的准确性和效率的前提下,如何确保系统的稳定性和性能优化是人脸检测和剪裁的两个难点。 2、人脸搜索 在实现人脸搜索的过程中,如何确保人脸识别的准确性和系统的稳定性是人脸搜索的两个关键难点。 四、总结 通过使用SpringBoot、Vue、MyBatis等技术,本文成功实现了一个基于Face++人脸识别引擎的人脸识别应用。该应用可以在安防监控、智能门锁、出行服务等领域广泛应用,为社会的发展和人们的生活提供了更加安全和便捷的保障。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值