İlker Balcılar
İlker Balcılar

Reputation: 9

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?

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")```

Upvotes: 0

Views: 237

Answers (1)

mmeisson
mmeisson

Reputation: 683

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.

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

Upvotes: 1

Related Questions