栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python与Face++实现简易人脸识别考勤系统

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python与Face++实现简易人脸识别考勤系统

文章目录
  • 一、注册Face++用户
  • 二、仿真步骤
    • 1.系统体系结构
    • 2.仿真环境
    • 3.安装依赖环境
    • 4.配置文件
    • 5.创建云端人脸集
    • 6.上传人脸并且进行人脸检测得到Face_token并将其存入CSV文件中
    • 7.主程序
  • 三、总结
  • 四、参考和拓展


一、注册Face++用户

本系统采用的是旷视旗下的Face++人工智能开放平台,首先需要注册一个Face++用户(地址为:https://console.faceplusplus.com.cn/register),在其平台中创建新应用,获得对应的API Key和API Secret

二、仿真步骤 1.系统体系结构

2.仿真环境
     本文采用的语言为Python语言,代码编译器采用的是Visual Studio Code
     Python版本为:IDLE (Python 3.9 64-bit)      
3.安装依赖环境
        直接可以用pip指令去安装

opencv-contrib-python == 4.5.5.62
pencv-python == 4.5.5.62
requests == 2.27.1

4.配置文件
#!usr/bin/
# -*- coding: utf-8 -*-

#配置文件
key = "你申请的API Key"
secret = "申请的API Screct"
outer_id = '*'#人脸库名称
5.创建云端人脸集
#!/usr/bin/env/ python
# _*_ coding:utf-8 _*_
  
import requests
from json import JSONDecoder
import config

#每个人脸组只需要执行一次
http_url = "https://api-cn.faceplusplus.com/facepp/v3/faceset/create"
get_url = "https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets"
getdetails_url = "https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail"
key = config.key
secret = config.secret
outer_id = config.outer_id

data = {"api_key":key,"api_secret":secret,"outer_id":outer_id}
  
repn = requests.post(http_url,data = data)
print(repn)
print(repn.text)
data = {"api_key":key,"api_secret":secret}
repn = requests.post(get_url,data = data)
print(repn)
print(repn.text)
data = {"api_key":key,"api_secret":secret,"outer_id":outer_id}
repn = requests.post(getdetails_url,data = data)
print(repn)
print(repn.text)
6.上传人脸并且进行人脸检测得到Face_token并将其存入CSV文件中
#!usr/bin/
# -*- coding: utf-8 -*-
import requests
from json import JSONDecoder
import csv
import config
#开始学号
#图片用学号命名放在picture文件夹下
id = '学号1'

http_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'
key = config.key
secret = config.secret

faceID_List = []
face_token_List = []
for i in range(0, 5):
    faceID = str(id + i)
    print(faceID)
    file = "./picture/" + faceID + ".jpg"
    data = {"api_key": key, "api_secret": secret, "return_landmark": 1, }
    file = {"image_file": open(file, 'rb')}

    response = requests.post(http_url, data=data, files=file)
    req_con = response.content.decode('utf-8')
    req_dict = JSONDecoder().decode(req_con)
    face_token = req_dict["faces"][0]["face_token"]
    faceID_List.append(faceID)
    face_token_List.append(face_token)

print(faceID_List)
print(face_token_List)

csvfile = open('face_token.csv','a',newline='' )
fwriter = csv.writer(csvfile)
for i in range(len(faceID_List)):
    fwriter.writerow([faceID_List[i], face_token_List[i]])
csvfile.close()
7.主程序
#!usr/bin/
# -*- coding: utf-8 -*-

#识别程序
import config
import requests
from json import JSONDecoder
import csv
import cv2
import time
import tkinter as tk
  
search_url = "https://api-cn.faceplusplus.com/facepp/v3/search"
getdetail_url = "https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail"
key = config.key
secret = config.secret

filename = time.time()
filepath = "photo/" + str(filename) + ".jpg"
b = time.localtime(filename);
nowTime = time.strftime("%Y-%m-%d %H:%M:%S", b)
cap = cv2.VideoCapture(0)
while(1):
  # get a frame
  ret, frame = cap.read()
  # show a frame
  cv2.imshow("capture", frame)
  if cv2.waitKey(1) & 0xFF == ord('1'):
    cv2.imwrite(filepath, frame)
    break
cap.release()
cv2.destroyAllWindows()

print("waiting...")

csvfile = open('face_token.csv','r')
freader = csv.reader(csvfile)
dic = dict(freader)
csvfile.close()
faceID_dict = {v:k for k,v in dic.items()}
print("...")

data = {"api_key": key, "api_secret": secret, "outer_id":config.outer_id}
files = {"image_file": open(filepath, "rb")}
print(files)
response = requests.post(search_url, data=data, files=files)
# s = requests.session()
# s.keep_alive = False
req_con = response.content.decode('utf-8')
req_dict = JSONDecoder().decode(req_con)
print(type(req_dict))
print(req_dict)
pre_face_token = req_dict["results"][0]["face_token"]
csvfile = open('face_token.csv', 'r', newline='')
fread = csv.reader(csvfile);
def getStudentId(pre_face_token):
  for item in fread:
    if item[1] == pre_face_token:
      fp=open('考勤统计txt文件绝对路径','a+',encoding='utf-8')
      print("学号为:" + item[0] + "  考勤时间:" + nowTime,file=fp)
      fp.close()
      return
  print("考勤失败,查无此人")
getStudentId(pre_face_token)
三、总结

本设计还有很多不足,是一个简易人脸识别系统,在人脸识别过程中没有考虑到活体检测这一功能,在后续考勤统计过程中没有加入数据库整合。

四、参考和拓展

源码参考来源(本文代码参考了这位大佬的源码)
本文源码下载

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/968346.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号