face++与python实现人脸识别签到(考勤)功能

yipeiwu_com5年前Python基础

项目实现利用face++开发一个课堂签到的软件,实现面向摄像头即可完成记录学号、姓名和时间的签到工作。

项目架构

项目使用场景

代码:

流程代码,主文件

#!usr/bin/
# -*- coding: utf-8 -*-
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 = "***************"
secret = "*********************"
 
filename = time.time()
filepath = "photo/" + str(filename) + ".jpg"
 
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":'zbpm'}
files = {"image_file": open(filepath, "rb")}
response = requests.post(search_url, data=data, files=files)
req_con = response.content.decode('utf-8')
req_dict = JSONDecoder().decode(req_con)
pre_face_token = req_dict["results"][0]["face_token"]
pre_confidence = req_dict["results"][0]["confidence"]
pre_thresholds = req_dict["thresholds"]["1e-5"]
print("...")
 
data = {"api_key": key, "api_secret": secret,"outer_id":'zbpm'}
response = requests.post(getdetail_url, data=data)
req_con = response.content.decode('utf-8')
req_dict = JSONDecoder().decode(req_con)
faces_token = req_dict["face_tokens"]
print("...")
 
if pre_face_token in faces_token and pre_confidence >= pre_thresholds:
  labaltext = faceID_dict[pre_face_token] + "\n\n\n" +str(time.asctime(time.localtime()))
  window = tk.Tk()
  window.title = ('FaceID')
  window.geometry = ('200x200')
  var = tk.StringVar() 
  l = tk.Label(window,bg = 'yellow',text = labaltext,font=("黑体",20 ,"bold"),width = 30,height = 20)
  l.pack()
  l.config(text=labaltext+var.get())
  #tk.messagebox.askokcancel('faceId', faceID_dict[pre_face_token] + "\n" +str(time.asctime(time.localtime())))
  print(faceID_dict[pre_face_token])
else:
  tkinter.messagebox.askokcancel('提示', '未找到')
  print("未找到")

建立云脸数据集的模块:

#!usr/bin/
# -*- coding: utf-8 -*-
import requests
import time
from json import JSONDecoder
import csv
 
 
http_url ='https://api-cn.faceplusplus.com/facepp/v3/faceset/addface'
key = "z_qkMMqK1efq8ikgAPOEn89A7And-lAa"
secret = "***********************"
faceset_token = '******************************'
 
face_tokens_str = ''
csvfile = open('face_token.csv','r')
freader = csv.reader(csvfile)
facedata = []
i = 0
for item in freader:
  i = i + 1
  face_tokens_str = face_tokens_str + item[1] + ','
  if i%5 == 0:
    face_tokens_str = face_tokens_str[:-1]
    facedata.append (face_tokens_str)
    face_tokens_str = ''
face_tokens_str = face_tokens_str[:-1]
facedata.append (face_tokens_str)
csvfile.close()
 
for item in facedata:
  print(item)
  data = {"api_key":key,"api_secret":secret,"faceset_token":faceset_token,"face_tokens":item}
  response = requests.post(http_url, data=data)
  print(response)
  print(response.text)

初始建立云联数据集的模块: 

#!/usr/bin/env/ python
# _*_ coding:utf-8 _*_
 
import requests
from json import JSONDecoder
 
 
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 = "**********************"
secret = "*********************"
 
data = {"api_key":key,"api_secret":secret,"display_name":'SEUers',"outer_id":'zbpm'}
 
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":'zbpm'}
repn = requests.post(getdetails_url,data = data)
print(repn)
print(repn.text)

生成存储facetoken:

这里从171860学号开始生成我的facetoken

#!usr/bin/
# -*- coding: utf-8 -*-
import requests
from json import JSONDecoder
import csv
 
id = 171860
 
http_url ='https://api-cn.faceplusplus.com/facepp/v3/detect'
key = "*******************"
secret = "*******************"
 
 
 
faceID_List = []
face_token_List = []
for i in range (0,10):
  faceID = str(id + i)
  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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Django框架之中间件MiddleWare的实现

Django中的中间件是一个轻量级、底层的插件系统,可以介入Django的请求和响应处理过程,修改Django的输入或输出。 中间件的设计为开发者提供了一种无侵入式的开发方式,增强了Dj...

Python 在OpenCV里实现仿射变换—坐标变换效果

Python 在OpenCV里实现仿射变换—坐标变换效果

在现实的图像操作软件中,经常碰到的不是给出放大多少倍,而是由用户在软件的界面上选择多大的区域,或者选择几个点,那么这样情况下,怎么样来计算出变换矩阵呢?从前面知道变换矩阵是2X3的矩阵,...

Python XlsxWriter模块Chart类用法实例分析

Python XlsxWriter模块Chart类用法实例分析

本文实例讲述了Python XlsxWriter模块Chart类用法。分享给大家供大家参考,具体如下: 一 点睛 Chart类是XlsxWriter模块中图表组件的基类,支持的图表类型包...

Python 一键获取百度网盘提取码的方法

Python 一键获取百度网盘提取码的方法

该 GIF 图来自于官网,文末有给出链接。 描述 依托于百度网盘巨大的的云存储空间,绝大数人会习惯性的将一些资料什么的存储到上面,但是有的私密链接需要提取码,但是让每个想下载私密资源的...

python取余运算符知识点详解

python取余运算符是什么? python取余运算符是%,即表示取模,返回除法的余数。 假设变量: a=10,b=20: 那么b % a 输出结果 0 注: Python语言支持以下类...