import dlib
import numpy as np
import cv2
import os
import pandas as pd
import time
def run():
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('data/data_dlib/shape_predictor_68_face_landmarks.dat')
face_reco_model = dlib.face_recognition_model_v1("data/data_dlib/dlib_face_recognition_resnet_model_v1.dat")
class Face_Recognizer:
def __init__(self):
self.font = cv2.FONT_ITALIC
# For FPS
self.frame_time = 0
self.frame_start_time = 0
self.fps = 0
# cnt for frame
self.frame_cnt = 0
self.features_known_list = []
self.name_known_list = []
self.last_frame_centroid_list = []
self.current_frame_centroid_list = []
self.last_frame_names_list = []
self.current_frame_face_name_list = []
self.last_frame_face_cnt = 0
self.current_frame_face_cnt = 0
self.current_frame_face_X_e_distance_list = []
self.current_frame_face_position_list = []
self.current_frame_face_features_list = []
self.last_current_frame_centroid_e_distance = 0
def get_face_database(self):
if os.path.exists("data/features_all.csv"):
path_features_known_csv = "data/features_all.csv"
csv_rd = pd.read_csv(path_features_known_csv, header=None)
for i in range(csv_rd.shape[0]):
features_someone_arr = []
for j in range(0, 128):
if csv_rd.iloc[i][j] == '':
features_someone_arr.append('0')
else:
features_someone_arr.append(csv_rd.iloc[i][j])
self.features_known_list.append(features_someone_arr)
self.name_known_list.append("Person_" + str(i + 1))
print("Faces in Database:", len(self.features_known_list))
return 1
else:
print('##### Warning #####', '\n')
print("'features_all.csv' not found!")
print(
"Please run 'get_faces_from_camera.py' and 'features_extraction_to_csv.py' before 'face_reco_from_camera.py'",
'\n')
print('##### End Warning #####')
return 0
# Get the fps of video stream
def update_fps(self):
now = time.time()
self.frame_time = now - self.frame_start_time
self.fps = 1.0 / self.frame_time
self.frame_start_time = now
# Compute the e-distance between two 128D features
@staticmethod
def return_euclidean_distance(feature_1, feature_2):
feature_1 = np.array(feature_1)
feature_2 = np.array(feature_2)
dist = np.sqrt(np.sum(np.square(feature_1 - feature_2)))
return dist
# / Use centroid tracker to link face_x in current frame with person_x in last frame
def centroid_tracker(self):
for i in range(len(self.current_frame_centroid_list)):
e_distance_current_frame_person_x_list = []
# For object 1 in current_frame, compute e-distance with object 1/2/3/4/... in last frame
for j in range(len(self.last_frame_centroid_list)):
self.last_current_frame_centroid_e_distance = self.return_euclidean_distance(
self.current_frame_centroid_list[i], self.last_frame_centroid_list[j])
e_distance_current_frame_person_x_list.append(
self.last_current_frame_centroid_e_distance)
last_frame_num = e_distance_current_frame_person_x_list.index(
min(e_distance_current_frame_person_x_list))
self.current_frame_face_name_list[i] = self.last_frame_face_name_list[last_frame_num]
# putText on cv2 window
def draw_note(self, img_rd):
# Add some statements
cv2.putText(img_rd, "Face recognizer with OT", (20, 40), self.font, 1, (255, 255, 255), 1, cv2.LINE_AA)
cv2.putText(img_rd, "FPS: " + str(self.fps.__round__(2)), (20, 100), self.font, 0.8, (0, 255, 0), 1,
cv2.LINE_AA)
cv2.putText(img_rd, "Faces: " + str(self.current_frame_face_cnt), (20, 130), self.font, 0.8, (0, 255, 0), 1,
cv2.LINE_AA)
cv2.putText(img_rd, "Q: Quit", (20, 450), self.font, 0.8, (255, 255, 255), 1, cv2.LINE_AA)
for i in range(len(self.current_frame_face_name_list)):
cv2.putText(img_rd, "Face " + str(i + 1), tuple(
[int(self.current_frame_centroid_list[i][0]), int(self.current_frame_centroid_list[i][1])]), self.font,
0.8, (255, 190, 0),
1,
cv2.LINE_AA)
# Face detection and recognition wit OT from input video stream
def process(self, stream):
if self.get_face_database():
while stream.isOpened():
self.frame_cnt += 1
print(">>> Frame " + str(self.frame_cnt) + " starts")
flag, img_rd = stream.read()
kk = cv2.waitKey(1)
faces = detector(img_rd, 0)
if self.current_frame_face_name_list == ['Person_2', 'Person_2']:
break
# Update cnt for faces in frames
self.last_frame_face_cnt = self.current_frame_face_cnt
self.current_frame_face_cnt = len(faces)
# Update the face name list in last frame
self.last_frame_face_name_list = self.current_frame_face_name_list[:]
# update frame centroid list
self.last_frame_centroid_list = self.current_frame_centroid_list
self.current_frame_centroid_list = []
print(" >>> current_frame_face_cnt: ", self.current_frame_face_cnt)
# 2.1. if cnt not changes
if self.current_frame_face_cnt == self.last_frame_face_cnt:
print(" >>> scene 1: 当前帧和上一帧相比没有发生人脸数变化 / no faces cnt changes in this frame!!!")
self.current_frame_face_position_list = []
if self.current_frame_face_cnt != 0:
# 2.1.1 Get ROI positions
for k, d in enumerate(faces):
self.current_frame_face_position_list.append(tuple(
[faces[k].left(), int(faces[k].bottom() + (faces[k].bottom() - faces[k].top()) / 4)]))
self.current_frame_centroid_list.append(
[int(faces[k].left() + faces[k].right()) / 2,
int(faces[k].top() + faces[k].bottom()) / 2])
height = (d.bottom() - d.top())
width = (d.right() - d.left())
hh = int(height / 2)
ww = int(width / 2)
cv2.rectangle(img_rd,
tuple([d.left() - ww, d.top() - hh]),
tuple([d.right() + ww, d.bottom() + hh]),
(255, 255, 255), 2)
# multi-faces in current frames, use centroid tracker to track
if self.current_frame_face_cnt != 1:
self.centroid_tracker()
for i in range(self.current_frame_face_cnt):
# 6.2 w
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于python的在线课堂考勤系统.zip (98个子文件)
code
.idea
code.iml 324B
misc.xml 294B
modules.xml 267B
workspace.xml 12KB
.gitignore 184B
inspectionProfiles
profiles_settings.xml 174B
code
face_recognize_FK
face_reco_from_camera_ot_single_person.py 13KB
how_to_use_camera.py 2KB
face_descriptor_from_camera.py 3KB
dancheng-onet_epoch.pt 879KB
assemble_rnet_imglist-dancheng.py 958B
face_reco_from_camera.py 11KB
data
data_dlib
shape_predictor_68_face_landmarks.dat 95.08MB
dlib_face_recognition_resnet_model_v1.dat 21.43MB
data_faces_from_camera
person_8
person_7
person_9
img_face_1.jpg 60KB
person_4
person_6
person_3
person_11
img_face_2.jpg 50KB
img_face_4.jpg 50KB
img_face_7.jpg 48KB
img_face_5.jpg 38KB
img_face_1.jpg 66KB
img_face_3.jpg 46KB
img_face_6.jpg 64KB
person_5
person_10
person_2
img_face_1.jpg 62KB
person_1
img_face_2.jpg 45KB
img_face_4.jpg 45KB
img_face_7.jpg 45KB
img_face_5.jpg 45KB
img_face_18.jpg 47KB
img_face_12.jpg 44KB
img_face_19.jpg 48KB
img_face_1.jpg 45KB
img_face_16.jpg 48KB
img_face_17.jpg 47KB
img_face_9.jpg 45KB
img_face_3.jpg 45KB
img_face_11.jpg 41KB
img_face_14.jpg 43KB
img_face_13.jpg 43KB
img_face_10.jpg 43KB
img_face_15.jpg 43KB
img_face_8.jpg 45KB
img_face_6.jpg 45KB
features_all.csv 12KB
__pycache__
get_faces_from_camera.cpython-36.pyc 5KB
face_reco_from_camera_ot_single_person.cpython-36.pyc 8KB
face_reco_GUI.cpython-36.pyc 2KB
face_reco_from_camera_ot_multi_people.cpython-36.pyc 7KB
features_extraction_to_csv.cpython-36.pyc 2KB
dancheng-rnet_epoch.pt 237KB
face_reco_GUI.py 2KB
is_my_face-dancheng.py 4KB
get_my_faces-dancheng.py 2KB
.idea
misc.xml 195B
workspace.xml 9KB
Dlib_face_recognition_from_camera.iml 527B
assemble_onet_imglist-dancheng.py 946B
dancheng-pnet_epoch.pt 29KB
face_reco_GUI.exe 151.04MB
set_other_faces-dancheng.py 1KB
features_extraction_to_csv.py 3KB
assemble_pnet_imglist-dancheng.py 958B
simsun.ttc 10.01MB
assemble-dancheng.py 979B
face_reco_from_camera_ot_multi_people.py 15KB
BGP.png 69KB
train_faces-dancheng.py 5KB
get_faces_from_camera.py 6KB
.idea
code.iml 412B
misc.xml 188B
modules.xml 267B
workspace.xml 7KB
.gitignore 50B
inspectionProfiles
profiles_settings.xml 174B
attentionDetect_FK
convert_to_onnx.py 1KB
HDR.py 8KB
save_user_face
face_reco_from_camera.py 8KB
model
shape_predictor_68_face_landmarks.dat 95.08MB
dlib_face_recognition_resnet_model_v1.dat 21.43MB
shape_predictor_5_face_landmarks.dat 8.73MB
__pycache__
register_face.cpython-36.pyc 3KB
HDR.cpython-36.pyc 5KB
save_NoPerson
1648903620.6945288.png 5KB
1648902140.0732043.png 350KB
1648902165.2882795.png 447KB
1648902168.3040736.png 398KB
1648902143.2445138.png 367KB
detect.py 7KB
get_my_faces-dancheng.py 2KB
.idea
dbnavigator.xml 23KB
misc.xml 188B
.name 9B
人脸识别门禁.iml 291B
modules.xml 295B
workspace.xml 7KB
.gitignore 50B
inspectionProfiles
profiles_settings.xml 174B
detect.exe 67.07MB
face_reco_from_camera_ot_multi_people.py 12KB
共 98 条
- 1
[D]regs
- 粉丝: 66
- 资源: 24
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页