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人脸识别与专注度检测源码 (161个子文件)
features_all.csv 3KB
shape_predictor_68_face_landmarks.dat 95.08MB
shape_predictor_68_face_landmarks.dat 95.08MB
dlib_face_recognition_resnet_model_v1.dat 21.43MB
dlib_face_recognition_resnet_model_v1.dat 21.43MB
shape_predictor_5_face_landmarks.dat 8.73MB
face_reco_GUI.exe 151.04MB
detect.exe 67.07MB
.gitignore 184B
.gitignore 50B
Dlib_face_recognition_from_camera.iml 527B
face_recognize_FK.iml 455B
人脸识别门禁.iml 426B
img_face_5.jpg 23KB
img_face_6.jpg 23KB
img_face_7.jpg 23KB
img_face_3.jpg 23KB
img_face_2.jpg 22KB
img_face_4.jpg 22KB
img_face_1.jpg 22KB
img_face_8.jpg 22KB
img_face_11.jpg 22KB
img_face_10.jpg 22KB
img_face_9.jpg 22KB
img_face_12.jpg 22KB
.name 9B
1649995377.1937618.png 405KB
1649995528.9703574.png 405KB
1649995522.9075015.png 404KB
1649995525.930145.png 404KB
1649995513.7870731.png 404KB
1649995453.063886.png 404KB
1649995374.1360073.png 404KB
1649995422.69742.png 404KB
1649995410.568796.png 404KB
1649995450.0085173.png 404KB
1649995413.5777328.png 404KB
1649995419.6404474.png 404KB
1649995407.5121431.png 404KB
1649995416.633044.png 404KB
1649995459.1296577.png 404KB
1649995404.5037174.png 404KB
1649995538.0890713.png 403KB
1649995519.8508968.png 403KB
1649995446.9534342.png 403KB
1649995516.813058.png 403KB
1649995532.0139074.png 403KB
1649995462.1393607.png 403KB
1649995535.03438.png 403KB
1649995365.0167217.png 403KB
1649995425.7512748.png 403KB
1649995547.1628861.png 403KB
1649995428.7607145.png 403KB
1649995568.4101722.png 403KB
1649995562.3463445.png 402KB
1649995565.4029915.png 402KB
1649995559.338066.png 402KB
1649995541.097715.png 402KB
1649995571.4666872.png 402KB
1649995456.071407.png 402KB
1649995556.2819228.png 402KB
1649995577.531368.png 402KB
1649995574.4763236.png 402KB
1649995550.2181685.png 402KB
1649995544.153583.png 402KB
1649995553.2267087.png 401KB
1649995580.5371687.png 401KB
1649995389.3203351.png 401KB
1649995437.8797307.png 401KB
1649995443.9446177.png 401KB
1649995440.8905134.png 401KB
1649995368.073147.png 401KB
1649995386.264638.png 401KB
1649995380.2018392.png 401KB
1649995431.8162894.png 400KB
1649995395.3898263.png 400KB
1649995383.2564235.png 400KB
1649995434.8247302.png 400KB
1649995392.327554.png 400KB
1649995398.4405527.png 400KB
1649995401.4488633.png 400KB
1649995371.1289132.png 398KB
1649995254.6716235.png 387KB
1649995303.6724987.png 381KB
1649995294.4730756.png 380KB
1649995361.9592378.png 380KB
1649995263.768297.png 380KB
1649995285.273577.png 379KB
1649995334.3600352.png 379KB
1649995269.9641874.png 379KB
1649995257.6741178.png 379KB
1649995355.7691634.png 379KB
1649995279.1686406.png 379KB
1649995272.9685948.png 379KB
1649995260.7654824.png 379KB
1649995340.4722002.png 379KB
1649995309.7766728.png 379KB
1649995282.2178943.png 379KB
1649995337.3686383.png 379KB
1649995291.369395.png 379KB
共 161 条
- 1
- 2
xifenglie123321
- 粉丝: 626
- 资源: 23
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页