0


毕设项目-人脸识别考勤签到系统

毕设项目-人脸识别考勤签到系统

人脸识别小程序、签到小程序,借助百度AI智能识别功能实现。

需求说明

学生信息管理,考勤管理,人脸识别处理大概这三个模块。

功能模块:

  • 登录与注册(两种身份 老师或学生)
  • 课程发布(老师可以发布课程信息 名称 地点 选课人数)
  • 课程查看(学生查看课程信息)
  • 人脸录入(学生登录后有录入人脸功能)
  • 考勤发布(老师发布考勤签到,课程名称,开始时间和结束时间)
  • 学生签到(人脸识别签到,签到失败,迟签,签到成功)
  • 考勤记录查看(学生ID,课程名称,签到状态)

功能分析

人脸识别调用百度智能云api就可以。做人脸识别签到,其实就是要拿识别的人脸和数据库里的人脸对比,相识度大于一定的值,就可以判定成功。

如我们识别的结果是98.295%,所以这里就可以认定为签到成功。

效果图

还没签到

签到

签到成功

接入人脸识别

人脸注册

我们要想实现人脸识别,就需要一开始先在百度的可视化人脸库里注册人脸,要调用的接口如下。

获取acess_token

在调用这个之前,我们需要先去获取对应的acess_token,所以接下来我们要做的第一步就是获取acess_token。我们后面做的所有操作,基本上都要获取这个。

  1. wx.request({
  2. url: 'https://aip.baidubce.com/oauth/2.0/token',
  3. data: {
  4. grant_type: 'client_credentials',
  5. client_id:, //应用的API Key
  6. client_secret: //应用的Secret Key
  7. },
  8. header: {
  9. 'Content-Type': 'application/json' // 默认值
  10. },
  11. success: res => {
  12. this.setData({
  13. token: res.data.access_token //获取到token
  14. })
  15. console.log('获取到的token', this.data.token)
  16. }
  17. })

拍人脸照,注册人脸到百度人脸库

我们在拍照以后,获取到图片,并通过 wx.getFileSystemManager().readFile()方法把图片转换为base64,因为百度需要这样格式的数据。

  1. var that = this;
  2. //拍照
  3. const ctx = wx.createCameraContext()
  4. ctx.takePhoto({
  5. quality: 'high',
  6. success: (res) => {
  7. that.setData({
  8. src: res.tempImagePath //获取图片
  9. })
  10. //图片base64编码
  11. wx.getFileSystemManager().readFile({
  12. filePath: that.data.src, //选择图片返回的相对路径
  13. encoding: 'base64', //编码格式
  14. success: res => { //成功的回调
  15. that.setData({
  16. base64: res.data
  17. })
  18. //第三步:上传人脸进行注册
  19. wx.request({
  20. url: 'https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add?access_token=' + that.data.token,
  21. method: 'POST',
  22. data: {
  23. image: that.data.base64,
  24. image_type: 'BASE64',
  25. group_id: 'users', //自己建的用户组id
  26. user_id: app.globalData.userInfo.phone, //学号
  27. user_info: app.globalData.userInfo.name //存储学生姓名
  28. },
  29. header: {
  30. 'Content-Type': 'application/json' // 默认值
  31. },
  32. success(res) {
  33. that.setData({
  34. msg: res.data.error_msg
  35. })
  36. console.log("人脸注册返回结果", res)
  37. //做成功判断
  38. if (that.data.msg == 'SUCCESS') { //微信js字符串使用单引号
  39. wx.showToast({
  40. title: '注册成功',
  41. icon: 'success',
  42. duration: 2000
  43. })
  44. // that.registerFace()
  45. }
  46. }
  47. }),
  48. //失败尝试
  49. wx.showToast({
  50. title: '请重试',
  51. icon: 'loading',
  52. duration: 500
  53. })
  54. }
  55. })
  56. } //拍照成功结束
  57. }) //调用相机结束

我们注册完以后,可以在百度人脸库里看到这条数据,可以看到我们创建的users表。

查看人脸库

人脸比对

我们上面注册好人脸以后,接下来就可以使用人脸打卡功能了。 使用之前还是第一步,获取acess_token。

获取acess_token

  1. // acess_token获取
  2. getTokenInfo() {
  3. var that = this
  4. wx.request({
  5. url: 'https://aip.baidubce.com/oauth/2.0/token',
  6. data: {
  7. grant_type: 'client_credentials',
  8. client_id: app.globalData.client_id, //应用的API Key
  9. client_secret: app.globalData.client_secret //Secret Key
  10. },
  11. header: {
  12. 'Content-Type': 'application/json' // 默认值
  13. },
  14. success(res) {
  15. that.setData({
  16. token: res.data.access_token //获取到token
  17. })
  18. console.log(that.data.token)
  19. }
  20. })
  21. },

人脸比对

  1. //拍照并编码
  2. takePhoto() {
  3. let that=this
  4. const ctx = wx.createCameraContext()
  5. ctx.takePhoto({
  6. quality: 'high',
  7. success: (res) => {
  8. //图片base64编码
  9. wx.getFileSystemManager().readFile({
  10. filePath: res.tempImagePath, //选择图片返回的相对路径
  11. encoding: 'base64', //编码格式
  12. success: res => { //成功的回调
  13. that.signInFace(res.data)
  14. }
  15. })
  16. }
  17. })
  18. },
  19. //上传人脸进行 比对
  20. signInFace(base64) {
  21. var that = this
  22. if (base64 != "") {
  23. wx.request({
  24. url: 'https://aip.baidubce.com/rest/2.0/face/v3/search?access_token=' + that.data.token,
  25. method: 'POST',
  26. data: {
  27. image: base64,
  28. image_type: 'BASE64',
  29. group_id_list: 'users' //自己建的用户组id
  30. },
  31. header: {
  32. 'Content-Type': 'application/json' // 默认值
  33. },
  34. success(res) {
  35. console.log("人脸对比返回结果", res)
  36. if (res.data.error_msg == "match user is not found") {
  37. wx.showModal({
  38. title: '签到失败',
  39. content: '请先注册人脸才可以人脸使用',
  40. })
  41. }
  42. if (res.data.error_msg == "SUCCESS") {
  43. that.setData({
  44. msg: res.data.result.user_list[0].score,
  45. })
  46. // console.log(res)
  47. if (that.data.msg > 80) { //相似度大于80
  48. console.log('人脸识别成功')
  49. } else {
  50. wx.showToast({
  51. title: '人脸识别失败',
  52. })
  53. }
  54. } else {
  55. wx.showToast({
  56. title: '人脸识别失败',
  57. })
  58. }
  59. }
  60. });
  61. }
  62. if (base64 == "") {
  63. wx.showToast({
  64. title: '请重试',
  65. icon: 'loading',
  66. duration: 500
  67. })
  68. }
  69. },

我们执行代码以后,会返回一个相识度,我这里规定相识度80%以上即为同一个人。看日志可以知道我们的相识度是98.295%,所以这里就可以认定为签到成功。


本文转载自: https://blog.csdn.net/qq_29528701/article/details/125002794
版权归原作者 mon_star° 所有, 如有侵权,请联系我们删除。

“毕设项目-人脸识别考勤签到系统”的评论:

还没有评论