在我们的办公室,锁定屏幕是您需要快速开发的习惯。 因为如果你将你的计算机解锁,有人会玩得开心并改变你的壁纸或别名你sudo( linux系统管理指令,注*文章作者使用Linux系统)的东西。
有一天,我开始思考,为什么我不能自动化呢? 在这里,我来到Face Recognition python库。 它的设置和使用非常简单。
但首先要做的事情。 我们需要检查是否可以从python锁定屏幕以及如何操作。
锁定屏幕
我在Cinnamon桌面环境中使用Linux Mint。 幸运的是在Cinnamon案例中,使用screensaver命令锁定或解锁屏幕非常容易。
- cinnamon-screensaver-command --activate # to lock the screen
- cinnamon-screensaver-command --deactivate # to unlock the screen
复制代码从python运行终端命令并不是什么大问题: - from subprocess import call
- LOCK_ARGS = {
- True: '--activate',
- False: '--deactivate',
- }
- def lock_screen(lock):
- call(('cinnamon-screensaver-command', LOCK_ARGS[lock]))
- lock_screen(True) lock_screen(False)
复制代码
设置face_recognition
下一步是认出你可爱的脸。 我们将使用人脸识别库。 你可以在数据库中找到很多很好的例子,我相信一个对我们很有用。
它使用OpenCV从相机捕获流。 我还决定使用构造神经网络来定位框架中的面部。 要有更好的准确性。 - from threading import Timer
- import cv2
- import face_recognition
- def load_user_encoding():
- user_image = face_recognition.load_image_file(os.path.join(BASE_DIR, 'user.jpg'))
- user_image_face_encoding = face_recognition.face_encodings(user_image, num_jitters=10)[0]
- return user_image_face_encoding
- def find_user_in_frame(frame, user_encoding):
- face_locations = face_recognition.face_locations(frame, model='cnn')
- face_encodings = face_recognition.face_encodings(frame, face_locations, num_jitters=2)
- for face_encoding in face_encodings:
- matches = face_recognition.compare_faces((user_encoding, ), face_encoding, tolerance=0.9)
- return any(matches)
复制代码- if __name__ == '__main__':
- user_encoding = load_user_encoding()
- video_capture = cv2.VideoCapture(0) lock_timer = None
- process_this_frame = True
-
- while True:
- ret, frame = video_capture.read()
- small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
- rgb_small_frame = small_frame[:, :, ::-1]
- if process_this_frame:
- user_found = find_user_in_frame(rgb_small_frame, user_encoding)
- if user_found:
- print('user found')
- lock_screen(False)
- if lock_timer is not None: lock_timer.cancel()
- lock_timer = None
- else:
- print('user not found')
- if lock_timer is None: lock_timer = Timer(5, lock_screen, (True,))
- lock_timer.start()
- process_this_frame = not process_this_frame
复制代码如你所见,我使用threading.Timer在5秒后锁定屏幕,以防用户找不到。 我建议在锁定屏幕之前稍等一下,因为有时它无法识别某些画面上的脸部。 或者你可以暂时离开。
优化使用该解决方案,它有一个令人讨厌的延迟用于读取帧和坏帧。 所以我决定对其进行优化,并使用多处理将识别过程移到单独的过程中
首先,我们需要重写我们的函数来查找用户,以便它能够被Process和Pipe 调用代替返回;
- def find_user_in_frame(conn, frame, user_encoding):
- face_locations = face_recognition.face_locations(frame, model='cnn')
- face_encodings = face_recognition.face_encodings(frame, face_locations, num_jitters=2)
- found_user = False
- for face_encoding in face_encodings:
- matches = face_recognition.compare_faces((user_encoding, ), face_encoding, tolerance=0.9)
- found_user = any(matches)
- if found_user:
- break
- conn.send(found_user)
复制代码
在此之后我们需要调用该函数multiprocessing.Process在main中的循环当中
- if __name__ == '__main__':
- user_encoding = load_user_encoding()
- video_capture = cv2.VideoCapture(0) lock_timer = None
- parent_conn, child_conn = Pipe()
- find_user_process = None
- while True:
- ret, frame = video_capture.read()
- small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
- rgb_small_frame = small_frame[:, :, ::-1]
- if find_user_process is None:
- find_user_process = Process(target=find_user_in_frame, args=(child_conn, rgb_small_frame, user_encoding))
- find_user_process.start()
- elif find_user_process is not None and not find_user_process.is_alive():
- user_found = parent_conn.recv()
- find_user_process = None
- if user_found:
- print('user found')
- lock_screen(False)
- if lock_timer is not None:
- lock_timer.cancel()
- lock_timer = None
- else:
- print('user not found')
- if lock_timer is None:
- lock_timer = Timer(LOCK_TIMEOUT, lock_screen, (True,))
- lock_timer.start()
复制代码现在它工作更顺畅,延迟很小。
谢谢阅读! 希望它很有趣,你很喜欢它。 英文原文:Simple “Face ID” for your PC
翻译:callofduty890
来源:AI研习社
|