基于vscode人脸识别考勤系统代码
时间: 2023-08-01 14:15:37 浏览: 112
基于人脸识别的考勤系统
基于VSCode的人脸识别考勤系统的代码需要分为两部分:前端和后端。前端主要以HTML、CSS和JavaScript为主,后端主要以Python为主。
前端代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>人脸考勤系统</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/layui/2.5.6/layui.js"></script>
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/layui/2.5.6/css/layui.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h2>人脸考勤系统</h2>
<div class="row">
<div class="col-md-6">
<div class="card">
<div class="card-header">考勤签到</div>
<div class="card-body">
<form>
<div class="form-group">
<label for="username">用户名:</label>
<input type="text" class="form-control" id="username" placeholder="请输入用户名">
</div>
<div class="form-group">
<label for="password">密码:</label>
<input type="password" class="form-control" id="password" placeholder="请输入密码">
</div>
<div class="form-group">
<button type="button" class="btn btn-primary" onclick="signIn()">签到</button>
</div>
</form>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card">
<div class="card-header">考勤记录</div>
<div class="card-body">
<table class="table table-striped">
<thead>
<tr>
<th>用户名</th>
<th>签到时间</th>
</tr>
</thead>
<tbody id="attendance-list">
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<script src="main.js"></script>
</body>
</html>
```
```css
body {
background-color: #f2f2f2;
}
.container {
margin-top: 50px;
}
.card-header {
font-weight: bold;
font-size: 20px;
}
.card-body {
padding: 20px;
}
.form-group {
margin-bottom: 20px;
}
.table {
margin-top: 20px;
}
.table th,
.table td {
text-align: center;
}
.table th {
font-weight: bold;
font-size: 16px;
}
```
后端代码:
```python
import cv2
import os
import numpy as np
import datetime
import face_recognition
import base64
from flask import Flask, jsonify, request
app = Flask(__name__)
# 加载已知人脸编码
known_face_encodings = []
known_face_names = []
for file in os.listdir("known_faces"):
if file.endswith(".jpg"):
image = face_recognition.load_image_file("known_faces/" + file)
encoding = face_recognition.face_encodings(image)[0]
known_face_encodings.append(encoding)
known_face_names.append(os.path.splitext(file)[0])
attendance_records = []
@app.route("/")
def home():
return "Welcome to Face Recognition Attendance System!"
@app.route("/api/signIn", methods=["POST"])
def signIn():
# 提取请求中的用户名和密码
username = request.form.get("username")
password = request.form.get("password")
# 验证用户名和密码
if username != "admin" or password != "123456":
return jsonify({"status": "fail", "message": "用户名或密码错误"})
# 打开摄像头
camera = cv2.VideoCapture(0)
# 循环读取视频流
while True:
ret, frame = camera.read()
if not ret:
break
# 将图片大小缩小为原来的1/4,加快识别速度
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
# 将图片从BGR格式转换为RGB格式
rgb_small_frame = small_frame[:, :, ::-1]
# 在图片中识别人脸
face_locations = face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
# 遍历所有检测到的人脸
for face_encoding in face_encodings:
# 判断是否为已知人脸
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"
if True in matches:
first_match_index = matches.index(True)
name = known_face_names[first_match_index]
# 绘制人脸识别框
top, right, bottom, left = face_locations[0]
top *= 4
right *= 4
bottom *= 4
left *= 4
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
# 绘制人脸名称
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
# 记录考勤记录
now = datetime.datetime.now()
attendance_records.append({"name": name, "time": now.strftime("%Y-%m-%d %H:%M:%S")})
# 保存考勤记录到文件
with open("attendance_records.txt", "a") as f:
f.write("{}\t{}\n".format(name, now.strftime("%Y-%m-%d %H:%M:%S")))
# 将考勤记录转换为Base64编码,以便前端显示
attendance_list = []
for record in attendance_records:
attendance_list.append({"name": record["name"], "time": record["time"]})
attendance_list_str = str(attendance_list)
attendance_list_bytes = attendance_list_str.encode("utf-8")
attendance_list_base64 = base64.b64encode(attendance_list_bytes).decode("utf-8")
# 返回考勤记录给前端
return jsonify({"status": "success", "attendance_list": attendance_list_base64})
# 显示视频流
cv2.imshow("Video", frame)
# 按下q键退出循环
if cv2.waitKey(1) & 0xFF == ord("q"):
break
# 释放摄像头并关闭窗口
camera.release()
cv2.destroyAllWindows()
return jsonify({"status": "fail", "message": "未检测到人脸"})
if __name__ == "__main__":
app.run(debug=True)
```
阅读全文