Automatically locks your Windows workstation when you leave your desk, using your external webcam and face detection.
- Webcam check — the program polls for a USB-connected external webcam. If none is plugged in, monitoring is suspended (nothing is locked).
- Face detection — once the webcam is found, frames are captured every few seconds and analysed with OpenCV's Haar cascade face detector.
- 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.
- Auto-lock — if no qualifying face is detected for 30 seconds, the Windows lock screen is triggered (
Win + Lequivalent). - Resume — once you unlock and sit back down, monitoring resumes automatically.
- Windows 10 / 11
- Python 3.8 or later
- An external USB webcam
pip install -r requirements.txtOpen 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. |
python auto_lock.pyPress Ctrl + C to stop.
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"
- Program:
- General: Check "Run only when user is logged on"
Or create a shortcut in your Startup folder (Win + R → shell:startup):
pythonw "C:\Users\michael.katsoulis\Documents\tools\auto_lock\auto_lock.py"
- The PowerShell USB camera query may fail on some configurations. In this case, set
CAMERA_INDEXto the specific index of your webcam (try0,1,2…) — the program will then skip the USB check and go straight to opening that camera.
- Enable
DEBUG_WINDOW = Trueand check the live feed. If no green box appears around your face, try loweringMIN_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.
- Enable
DEBUG_WINDOW = Trueand 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.
Increase ABSENCE_TIMEOUT to e.g. 60 (one minute).
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()