前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >给你的电脑做个简单的“人脸识别认证”

给你的电脑做个简单的“人脸识别认证”

作者头像
AI研习社
发布2019-03-14 15:34:11
1.7K0
发布2019-03-14 15:34:11
举报
文章被收录于专栏:AI研习社AI研习社

本文为 AI 研习社编译的技术博客,原标题 : 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命令锁定或解锁屏幕非常容易。

代码语言:javascript
复制
cinnamon-screensaver-command --activate  # to lock the screen
cinnamon-screensaver-command --deactivate  # to unlock the screen

从python运行终端命令并不是什么大问题:

代码语言:javascript
复制
from subprocess import call
LOCK_ARGS = {
    True: '--activate',
    False: '--deactivate',
}

def lock_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从相机捕获流。 我还决定使用构造神经网络来定位框架中的面部。 要有更好的准确性。

代码语言:javascript
复制
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)  # get a reference to webcam #0 (the default one)

    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:  # cancel lock timer if it exists
                    lock_timer.cancel()
                    lock_timer = None
            else:
                print('user not found')

                if lock_timer is None:  # start timer if it's not started already
                    lock_timer = Timer(5, lock_screen, (True,))
                    lock_timer.start()

        process_this_frame = not process_this_frame

如你所见,我使用threading.Timer在5秒后锁定屏幕,以防用户找不到。 我建议在锁定屏幕之前稍等一下,因为有时它无法识别某些画面上的脸部。 或者你可以暂时离开。

优化

使用该解决方案,它有一个令人讨厌的延迟用于读取帧和坏帧。 所以我决定对其进行优化,并使用多处理将识别过程移到单独的过程中

首先,我们需要重写我们的函数来查找用户,以便它能够被Process和Pipe 调用代替返回:

代码语言:javascript
复制
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中的循环当中

现在它工作更顺畅,延迟很小。

代码语言:javascript
复制
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
    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 of finding user is not working - start new one
        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()
        # if process of finding user is already working - check is it done
        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()

谢谢阅读! 希望它很有趣,你很喜欢它。

源代码可以在github上找到:https://github.com/Ignisor/face-screenlock

想要继续查看该篇文章相关链接和参考文献?

长按链接点击打开或点击底部【阅读原文】:

https://ai.yanxishe.com/page/TextTranslation/1258

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-02-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 AI研习社 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 锁定屏幕
  • 设置face_recognition
  • 优化
相关产品与服务
人脸识别
腾讯云神图·人脸识别(Face Recognition)基于腾讯优图强大的面部分析技术,提供包括人脸检测与分析、比对、搜索、验证、五官定位、活体检测等多种功能,为开发者和企业提供高性能高可用的人脸识别服务。 可应用于在线娱乐、在线身份认证等多种应用场景,充分满足各行业客户的人脸属性识别及用户身份确认等需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档