原标题:给你的电脑做个简单的“人脸识别认证”
Simple “Face ID” for your PC
作者 | German Gensetskiy
翻译 | callofduty890
校对 | 约翰逊·李加薪 审核 | 酱番梨 整理 | 立鱼王
https://medium.com/gowombat/simple-face-id-for-your-pc-780168b95321
在我们的办公室,锁定屏幕是您需要快速开发的习惯。 因为如果你将你的计算机解锁,有人会玩得开心并改变你的壁纸或别名你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运行终端命令并不是什么大问题:
fromsubprocess importcall
LOCK_ARGS = {
True: '--activate',
False: '--deactivate',
}
deflock_screen(lock):
call(( 'cinnamon-screensaver-command', LOCK_ARGS[lock]))
lock_screen( True) # will lock the screen
lock_screen( False) # will unlock the screen
设置face_recognition
下一步是认出你可爱的脸。 我们将使用人脸识别库。 你可以在数据库中找到很多很好的例子,我相信一个对我们很有用。
它使用OpenCV从相机捕获流。 我还决定使用构造神经网络来定位框架中的面部。 要有更好的准确性。
fromthreading importTimer
importcv2
importface_recognition
defload_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]
returnuser_image_face_encoding
deffind_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)
forface_encoding inface_encodings:
matches = face_recognition.compare_faces((user_encoding, ), face_encoding, tolerance= 0.9)
returnany(matches)
if__name__ == '__main__':
user_encoding = load_user_encoding()
video_capture = cv2.VideoCapture( 0) # get a reference to webcam #0 (the default one)
lock_timer = None
process_this_frame = True
whileTrue:
ret, frame = video_capture.read()
small_frame = cv2.resize(frame, ( 0, 0), fx= 0.25, fy= 0.25)
rgb_small_frame = small_frame[:, :, :: -1]
ifprocess_this_frame:
user_found = find_user_in_frame(rgb_small_frame, user_encoding)
ifuser_found:
print( 'user found')
lock_screen( False)
iflock_timer isnotNone: # cancel lock timer if it exists
lock_timer.cancel()
lock_timer = None
else:
print( 'user not found')
iflock_timer isNone: # start timer if it's not started already
lock_timer = Timer( 5, lock_screen, ( True,))
lock_timer.start()
process_this_frame = notprocess_this_frame
如你所见,我使用threading.Timer在5秒后锁定屏幕,以防用户找不到。 我建议在锁定屏幕之前稍等一下,因为有时它无法识别某些画面上的脸部。 或者你可以暂时离开。
优化
使用该解决方案,它有一个令人讨厌的延迟用于读取帧和坏帧。 所以我决定对其进行优化,并使用多处理将识别过程移到单独的过程中
首先,我们需要重写我们的函数来查找用户,以便它能够被Process和Pipe 调用代替返回:
deffind_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
forface_encoding inface_encodings:
matches = face_recognition.compare_faces((user_encoding, ), face_encoding, tolerance= 0.9)
found_user = any(matches)
iffound_user:
break
conn.send(found_user)
在此之后我们需要调用该函数multiprocessing.Process在main中的循环当中
现在它工作更顺畅,延迟很小。
if__name__ == '__main__':
user_encoding = load_user_encoding()
video_capture = cv2.VideoCapture( 0) # get a reference to webcam #0 (the default one)
lock_timer = None
parent_conn, child_conn = Pipe()
find_user_process = None
whileTrue:
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 of finding user is not working - start new one
iffind_user_process isNone:
find_user_process = Process(target=find_user_in_frame, args=(child_conn, rgb_small_frame, user_encoding))
find_user_process.start()
# if process of finding user is already working - check is it done
eliffind_user_process isnotNoneandnotfind_user_process.is_alive():
user_found = parent_conn.recv()
find_user_process = None
ifuser_found:
print( 'user found')
lock_screen( False)
iflock_timer isnotNone:
lock_timer.cancel()
lock_timer = None
else:
print( 'user not found')
iflock_timer isNone:
lock_timer = Timer(LOCK_TIMEOUT, lock_screen, ( True,))
lock_timer.start()
谢谢阅读! 希望它很有趣,你很喜欢它。
源代码可以在github上找到:https://github.com/Ignisor/face-screenlock
https://ai.yanxishe.com/page/TextTranslation/1258
盘点图像分类的窍门生成模型:基于单张图片找到物体位置注意力的动画解析(以机器翻译为例)如使用深度学习玩Pong游戏教程:使用iPhone相机和openCV来完成3D重建(第三部分)返回搜狐,查看更多
责任编辑: