简体   繁体   English

Python OpenCV 人脸检测锁定电脑

[英]Python OpenCV face detection to lock pc

I have prepared some code for it to lock when I get up from computer and get away from it but when I use it with a casual algorithm, it turns off immediately because it does not detect my face in some movements.我已经准备了一些代码,当我从计算机上起来并远离它时,它会被锁定,但是当我用一个随意的算法使用它时,它会立即关闭,因为它在某些动作中没有检测到我的脸。 For this, I want it to wait 3 seconds when it does not detect my face, check it again and if it still does not detect my face, I want it to lock but when I use the time.sleep method, webcam video freezes and works as face does not exist even my face at camera, what kind of working algorithm do you suggest for this?为此,我希望它在未检测到我的脸时等待 3 秒,再次检查,如果仍然未检测到我的脸,我希望它锁定,但是当我使用 time.sleep 方法时,网络摄像头视频冻结并且工作因为即使我的脸在相机上也不存在,你为此建议什么样的工作算法?

from multiprocessing.connection import wait
import cv2
import time
import pyautogui
import ctypes
from math import sin, cos, radians
camera =  cv2.VideoCapture(0)
face = cv2.CascadeClassifier("haarcascade_frontalface_alt2.xml")

settings = {
    'scaleFactor': 1.3, 
    'minNeighbors': 3, 
    'minSize': (50, 50), 
    'flags': cv2.CASCADE_FIND_BIGGEST_OBJECT|cv2.CASCADE_DO_ROUGH_SEARCH
}

def rotate_image(image, angle):
    if angle == 0: return image
    height, width = image.shape[:2]
    rot_mat = cv2.getRotationMatrix2D((width/2, height/2), angle, 0.9)
    result = cv2.warpAffine(image, rot_mat, (width, height), flags=cv2.INTER_LINEAR)
    return result

def rotate_point(pos, img, angle):
    if angle == 0: return pos
    x = pos[0] - img.shape[1]*0.4
    y = pos[1] - img.shape[0]*0.4
    newx = x*cos(radians(angle)) + y*sin(radians(angle)) + img.shape[1]*0.4
    newy = -x*sin(radians(angle)) + y*cos(radians(angle)) + img.shape[0]*0.4
    return int(newx), int(newy), pos[2], pos[3]

while True:
    ret, img = camera.read()

    for angle in [0, -25, 25]:
        rimg = rotate_image(img, angle)
        detected = face.detectMultiScale(rimg, **settings)
        if len(detected):
            detected = [rotate_point(detected[-1], img, -angle)]
            break

    for x, y, w, h in detected[-1:]:
     cv2.rectangle(img, (x, y), (x+w, y+h), (255,0,0), 2)

    cv2.imshow('facedetect', img) 

    if cv2.waitKey(5) != -1:
        break

    if 0==(len(detected)):
        time.sleep(3)
        if 1==(len(detected)):
            pass
        else:
            ctypes.windll.user32.LockWorkStation()

cv2.destroyWindow("facedetect")```

set a variable with the last timestamp where you wouldn't have detected a face.设置一个带有最后一个时间戳的变量,您不会在其中检测到人脸。 On every loop, if you detect your face again, set this variable to None, if this variable is not None and variable + 3secondes <= current timestamp, lock your station.在每个循环中,如果你再次检测到你的脸,将这个变量设置为 None,如果这个变量不是 None 并且变量 + 3secondes <= 当前时间戳,锁定你的站。

import time

unseen_from = None
while True:

  # etc etc
  detected = bool(detected) # empty list == False, True otherwise
  if unseen_from is None:
    detected = None if detected else time.time()
  elif detected:
    unseen_from = None
  else if detected_from + 3 < time.time():
    ctypes.windll.user32.LockWorkStation()

live coding, I don't have a windows to test this on, but the idea is there现场编码,我没有 windows 来测试这个,但想法就在那里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM