利用opencv实现一个电脑人脸解锁效果
本帖最后由 bean0283 于 2022-8-19 15:07 编辑学了3天python,看上了一个opencv的玩意,想着就利用这个功能自己摸索实现一个功能,主要效果是当摄像头前不存在人,或者存在的人不是你事先录入的人,我设定的阈值是10s,屏幕就弹出一下全屏窗口,保护隐私
台式电脑没有摄像头的可以用DroidCam.Client,使用手机来当电脑的摄像头
项目架构很简单,主要是在img文件夹内放入你自己的自拍照即可
下面是源码,代码很多是网上cv拼凑然后微调出来了,大佬不要喷我
import os
import time
import cv2
import face_recognition
import numpy as np
import win32con
import win32gui
video_capture = cv2.VideoCapture(0)
img = cv2.imread("res/img.png")
path = os.getcwd()
os.chdir(path)
images_file = os.listdir(path+'/img')
known_face_encodings = []
known_face_names = []
for each in images_file:
image_path = path + '/img/'+each
known_face_encodings.append(face_recognition.face_encodings(face_recognition.load_image_file(image_path)))
name = os.path.splitext(each)
known_face_names.append(name)
face_names = []
face_locations = []
face_encodings = []
process_this_frame = True
cascade = cv2.CascadeClassifier("./res/haarcascade_frontalface_default.xml")## 读入分类器数据
i = 0
statTime = time.time()
endTime = time.time()
ck1 = False
while True:
if endTime - statTime > 10:
print("人脸消失超过10s,自动锁屏")
out_win = "output_style_full_screen"
cv2.namedWindow(out_win, cv2.WINDOW_NORMAL)
cv2.setWindowProperty(out_win, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow(out_win, img)
#window前置
if ck1 == False:
hwnd = win32gui.FindWindow(None, out_win)# 获取句柄,然后置顶
CVRECT = cv2.getWindowImageRect(out_win)
win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 0, 0, CVRECT, CVRECT, win32con.SWP_SHOWWINDOW)
# cv2.waitKey(0)
ck1 = True
elif ck1 and endTime - statTime < 10:
cv2.destroyWindow(out_win)
ck1 = False
ret, frame = video_capture.read()
sample_image =cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)## 图片地址
faces = cascade.detectMultiScale(sample_image, scaleFactor=1.1, minNeighbors=5, minSize=(50, 50))
if len(faces) > 0:
print("存在" + str(len(faces)) + "张人脸")
else:
#print("不存在人脸")
endTime = time.time()
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_small_frame = small_frame[:, :, ::-1]
if process_this_frame:
face_locations = face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches:
name = known_face_names
face_names.append(name)
for name in face_names:
#print(name)
print(name)
if name in known_face_names:
statTime = time.time()
else:
endTime = time.time()
process_this_frame = not process_this_frame
for (top, right, bottom, left), name in zip(face_locations, face_names):
top *= 4
left *= 4
right *= 4
bottom *= 4
font = cv2.FONT_HERSHEY_DUPLEX
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
模块涉及的主要包,需要按顺序安装,不然可能报错
pip install cmake -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install dlib -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install face_recognition -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple
其中dlib包有些电脑安装可能会报错,建议百度
最后源码上传:FaceMonitoring.rar
提醒:常规安装dlib需要cmake,vs等复杂操作。最简单的办法是安装Python2.7或3.4~3.6。另外,dlib的清华源可能下载不了,建议更换。 BUCTPJP 发表于 2022-10-7 18:03
为什么放入自己的照片后别人的人脸也会解锁
这个代码是练手项目,需要自己改一改判断逻辑,这样效果更好,我现在改用虹软的sdk了,那个识别效率更高,想要研究的可以去看看 这个真不错,可以让电脑也实现面容解锁了,赞,收藏了,感谢分享 搞个图片能过吗? iawyxkdn8 发表于 2022-8-19 12:18
搞个图片能过吗?
能过的{:1_896:}{:1_896:}{:1_896:},不然就不是这个水平了 摸鱼神器 看起来很强大的感觉嗯 对电脑性能影响大吗 更安全点就在上锁后加上手势识别解锁
页:
[1]
2