# 相关库导入
import face_recognition
import cv2
import os
# 限制只读取文件格式'.png'或'.jpg'或'.jpeg'的图片
from cv2 import namedWindow
unknow_people_list = [i for i in os.listdir('unknow_people') if
(i.endswith('.jpg')) or (i.endswith('.png')) or (i.endswith('.jpeg'))]
know_people_list = [i for i in os.listdir('know_people') if
(i.endswith('.jpg')) or (i.endswith('.png')) or (i.endswith('.jpeg'))]
def face_select():
# 定义flag
flag = 0
for unknow_people in unknow_people_list:
# 读取待识别图片
unknow = face_recognition.load_image_file('unknow_people/' + unknow_people)
# 将待识别图片转化为特征向量
unknow_encode = face_recognition.face_encodings(unknow)[0]
for know_people in know_people_list:
# 读取计算机已经认识的图片
know = face_recognition.load_image_file('know_people/' + know_people)
# 获得面部位置
face_location1 = face_recognition.face_locations(know)
face_location2 = face_recognition.face_locations(unknow)
# 提取面部关键点
face_landmarks_list1 = face_recognition.face_landmarks(know)
face_landmarks_list2 = face_recognition.face_landmarks(unknow)
# 图片转化为特征向量
know_encode = face_recognition.face_encodings(know)[0]
# 两张图片进行比较的结果
res = face_recognition.compare_faces([know_encode], unknow_encode, tolerance=0.5)
if res[0]:
flag = 1
break
else:
flag = 0
if flag == 1:
print(f'{know_people.split(".")[0]}匹配成功!')
else:
print(f'匹配失败')
if res == [True]:
name = "PASS"
else:
name = "NO"
# 绘制人脸特征点
for face_landmarks in face_landmarks_list1:
for facial_feature in face_landmarks.keys():
for pt_pos in face_landmarks[facial_feature]:
cv2.circle(know, pt_pos, 1, (192, 192, 192), 2)
for face_landmarks in face_landmarks_list2:
for facial_feature in face_landmarks.keys():
for pt_pos in face_landmarks[facial_feature]:
cv2.circle(unknow, pt_pos, 1, (192, 192, 192), 2)
# 脸部区域绘图
for (x1, y1, w1, h1) in face_location1:
img1 = cv2.rectangle(know, (y1, w1), (h1, x1), (255, 0, 0), 2) # 脸部框图绘制
cv2.putText(know, name, (y1 - 10, w1 - 10), cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 255, 0), 2)
frame1 = cv2.cvtColor(know, cv2.COLOR_BGR2RGB)
for (x1, y1, w1, h1) in face_location2:
img2 = cv2.rectangle(unknow, (y1, w1), (h1, x1), (255, 0, 0), 2)
cv2.putText(unknow, name, (y1 - 10, w1 - 10), cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 255, 0), 2)
frame2 = cv2.cvtColor(unknow, cv2.COLOR_BGR2RGB)
# 结果显示
namedWindow("1", 0)
cv2.imshow("1", frame1)
namedWindow("2", 0)
cv2.imshow("2", frame2)
if flag == 1:
cv2.waitKey(0)
else:
cv2.waitKey(1)
if __name__ == '__main__':
face_select()
2.MD5算法文件加密功能的实现
import os
import hashlib
import time
def mine():
DATA_1 = "C:/Users/Admin/Desktop/人脸识别/unknow_people"
str = "10"
PassWord_1 = input('请输入要加密或解密的密码:')
# 判断是否填写
if PassWord_1 == '':
print('密码不得为空,请重新输入!!!')
time.sleep(2)
os.system('cls')
mine()
else:
pass
def iterbrowse(path):
for home, dirs, files in os.walk(path):
for filename in files:
yield os.path.join(home, filename)
for fullname in iterbrowse(DATA_1):
NAME = fullname
NAME = NAME.replace("\\", "/") # 替换
def get_FileSize(filePath):
fsize = os.path.getsize(filePath)
fsize = fsize / float(1024 * 1024)
size = "%.0f" % fsize
if int(size) >= int(str):
print('[-!-]: ', NAME, '\t- - - - 内存过大!!!')
pass
else:
# 写入的文件
if NAME.split(".")[-1][-10:] == 'DATAAES-AI': # 检测是否是已经加密后的文件,用于检测后缀的DATAAES-AI
# 解密
F = NAME.split(os.path.sep)[0].replace("DATAAES-AI", "") # 去除DATAAES-AI
print(f'[-.·J·.-]: ', NAME, '\t- - - - 解密成功!!!')
else:
# 加密
F = NAME.split(os.path.sep)[0] + "DATAAES-AI"
print(f'[+·J·+]: ', NAME, '\t+ + + + 加密成功!!!')
a = open(NAME, "rb") # 读取文件
b = open(F, "wb") # 写入文件
# 使用MD5进行加密(双层加密)
hl = hashlib.md5()
hl.update(PassWord_1.encode(encoding='utf-8'))
password_list = hl.hexdigest()
# 使用MD5进行加密(双层加密)
hl.update(password_list.encode(encoding='utf-8'))
password_list2 = hl.hexdigest()
password_data = password_list + password_list2
# 加密
def Encryption_and_decryption():
count = 0 # 索引
for now in a:
for nowByte in now:
newByte = nowByte ^ ord(password_data[count % len(password_data)]) # 循环遍历出密码的ord值,单个循环
count += 1
b.write(bytes([newByte])) # 转换
Encryption_and_decryption()
a.close()
b.close()
os.remove(f'{NAME}')
get_FileSize(NAME)
print('操作完成!!!')
print('正在返回!!!')
time.sleep(2)
os.system('cls')
mine()
mine()
在unknow文件夹录入人脸识别照片数据库
由于照片数据库保管着大量照片信息,不应该让用户轻易查看或修改,我们可以通过加密来避免这些问题。
(解密与加密同理,使用同一密钥)
对于待识别的图像,主要实现了对人脸特征向量的编码,对识别点的标识,对图片中人脸部分的框图标识,以及文字提示最终比较结果(比对成功为PASS,比对失败为NO)*
PASS
NO
华为云服务器实现
!!!得买Ubuntu的服务器才好用,一开始买的openEuler属实不好用,好多指令操作都很复杂,许多python库的安装都需要进行许多额外的操作。
(真的太多啦,写几个还没有关掉的吧)
https://www.linuxidc.com/Linux/2017-01/139241.htm
https://blog.csdn.net/Nothing_227/article/details/107670329
https://blog.csdn.net/xd782292778/article/details/50372688?utm_source=itdadao&utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_title~default-1-50372688-blog-50372688.pc_relevant_default&spm=1001.2101.3001.4242.2&utm_relevant_index=4