0

我正在开发一个基本的面部识别程序,我正在尝试实现一个超时系统来锁定计算机。基本上它应该等待一秒钟,然后在计数器达到 10 时将 1 添加到计数器,然后它会激活一个 if 语句来锁定计算机。但是当运行它时,它不会运行代码的任何其他部分,直到它看到一张脸。我对此相当陌生,因此感谢您提供任何帮助。

        def facecheck():

            if matches == face_recognition.compare_faces(taylor_face_encoding, known_face_encodings):
                print("Confirmed")
                Confirm = True
                exit()
            else:
                print("Negative")
                correct-=1
                if correct <= -3:
                    print("User disallowed!")
                    subprocess.call('/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend',shell=True)
                    exit()
        while continue1 == 1:
            time.sleep(1)
            print("Counting")
            countdown+=1
            facecheck()
4

1 回答 1

2

您的代码似乎与您描述的过程并不完全相似,但您手头的问题是,您的代码在单个进程中运行,因此,在循环中时,没有其他内容执行。为此,有multiprocessing as well as the threading包。有了它,您将启动一个在它旁边运行的解耦进程。最小的例子:

from multiprocessing import Process

def facecheck():
  timer = 0
  while True:
    if timer > 10:
       if face_accepted:
         timer = 0
       else:
         lock()
    timer +=1
    sleep(1)
  [...]

proc = Process(target=facecheck, args=(client, addr))
proc.start()

以上将启动一个过程,等待 10 秒,然后进行面部检查(执行face_accepted所需的),如果接受面部,则再等待 10 秒重新检查。如果人脸不被接受,系统将被锁定(lock()必需)并每秒检查一次人脸,直到出现正确的人脸

于 2020-11-10T15:28:48.420 回答