Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

auto_lock

Automatically locks your Windows workstation when you leave your desk, using your external webcam and face detection.


How it works

  1. Webcam check — the program polls for a USB-connected external webcam. If none is plugged in, monitoring is suspended (nothing is locked).
  2. Face detection — once the webcam is found, frames are captured every few seconds and analysed with OpenCV's Haar cascade face detector.
  3. Proximity filter — only faces that occupy ≥ 12 % of the frame width are counted as "you". Colleagues sitting further away appear much smaller in the frame and are ignored.
  4. Auto-lock — if no qualifying face is detected for 30 seconds, the Windows lock screen is triggered (Win + L equivalent).
  5. Resume — once you unlock and sit back down, monitoring resumes automatically.

Requirements

  • Windows 10 / 11
  • Python 3.8 or later
  • An external USB webcam

Installation

pip install -r requirements.txt

Configuration

Open auto_lock.py and edit the constants near the top of the file:

Constant Default Description
CAMERA_INDEX None Camera index to use. None = auto-detect (highest available index). Set to 0 on desktops with no built-in camera, or 1 on a laptop.
ABSENCE_TIMEOUT 30 Seconds without a face before the screen locks.
CHECK_INTERVAL 2 Seconds between face-detection checks.
MIN_FACE_SIZE_RATIO 0.12 Minimum face width as a fraction of frame width. Raise this if colleagues are triggering false detections; lower it if your own face isn't being detected.
DEBUG_WINDOW False Set to True to open a live video window showing the camera feed and detection boxes. Useful for tuning MIN_FACE_SIZE_RATIO.

Running

python auto_lock.py

Press Ctrl + C to stop.

Run automatically on Windows startup (optional)

Open Task Scheduler and create a new task:

  • Trigger: At log on
  • Action: Start a program
    • Program: pythonw.exe (runs without a console window)
    • Arguments: "C:\Users\michael.katsoulis\Documents\tools\auto_lock\auto_lock.py"
  • General: Check "Run only when user is logged on"

Or create a shortcut in your Startup folder (Win + Rshell:startup):

pythonw "C:\Users\michael.katsoulis\Documents\tools\auto_lock\auto_lock.py"

Troubleshooting

The program says "External webcam disconnected" even though my camera is plugged in

  • The PowerShell USB camera query may fail on some configurations. In this case, set CAMERA_INDEX to the specific index of your webcam (try 0, 1, 2 …) — the program will then skip the USB check and go straight to opening that camera.

My face is not being detected

  • Enable DEBUG_WINDOW = True and check the live feed. If no green box appears around your face, try lowering MIN_FACE_SIZE_RATIO (e.g. 0.08).
  • Make sure the room is reasonably lit — Haar cascade detectors struggle in dim light.
  • The cascade works best with frontal, upright faces. Avoid extreme angles.

Colleagues are causing the screen to lock / stay unlocked

  • Enable DEBUG_WINDOW = True and observe bounding box sizes for distant faces.
  • Raise MIN_FACE_SIZE_RATIO (e.g. 0.18) until only your face — the closest to the camera — triggers detection.

I want a longer grace period before locking

Increase ABSENCE_TIMEOUT to e.g. 60 (one minute).

Finding the right camera index

Run this snippet in a Python shell to list available cameras:

import cv2
for i in range(6):
    cap = cv2.VideoCapture(i, cv2.CAP_DSHOW)
    if cap.isOpened():
        print(f"Camera found at index {i}")
    cap.release()

About

Auto locks your windows computer when you're not present. Uses Yolo instead of facial recognition for privacy.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages