SoFunction
Updated on 2025-03-01

Implementing interesting lock screen widgets with Python

Write in front

I was in the company before, after all, I was doing safety. The department had such a requirement. If I was found to not lock the screen, I would like to ask the whole department to drink milk tea. Unfortunately, I also forgot to lock the screen and then was discovered. Since then, I have formed muscle memory and are also very sensitive to others not locking the screen.

Why emphasize lock screen? You don’t want your computer to be operated by others, and you don’t want your information to be obtained by others. After all, you must be wary of others.

Since I switched to a new company, I saw someone on the way to the toilet without locking the screen. It really made me extremely confused. Is it that difficult to lock a screen? It's really difficult. Sometimes I'll forget it when I'm busy, so I want to implement a program that automatically locks the screen without the computer.

analyze

This thing is not difficult to implement. Just think about it simply, let the computer detect whether the person is in front of the computer. That is to try capturing the camera, then set an interval, capture pictures every once in a while, and do face recognition. Just lock the screen if there is no face.

When it comes to camera image processing, it directly reminds people of opencv, and then uses python to implement the above logic, and then it is done.

Code

Install opencv library

pip install opencv-python

Directly upload the code:

import cv2
import time
import os
import platform

# Detect the operating systemdef detect_os():
    os_name = ()
    if os_name == 'Windows':
        return 'windows'
    elif os_name == 'Darwin':
        return 'mac'
    else:
        return 'other'

# Execute the lock screen commanddef lock_screen(os_type):
    if os_type == 'windows':
        (' , LockWorkStation')
    elif os_type == 'mac':
        ('/System/Library/CoreServices/"Menu Extras"//Contents/Resources/CGSession -suspend')
    

# Initialize the cameracap = (0)

# Loading the face detection model of OpenCVface_cascade = ( + 'haarcascade_frontalface_default.xml')

# Unmanned State Timerno_person_timer = 0
# Set the time threshold for unmanned stateNO_PERSON_THRESHOLD = 3

# Detect operating system typeos_type = detect_os()

while True:
    ret, frame = ()
    if not ret:
        break

    # Convert to grayscale image    gray = (frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.1, 4)

    if len(faces) == 0:
        no_person_timer += 1
    else:
        no_person_timer = 0

    # If the threshold is exceeded, the screen lock is locked    if no_person_timer > NO_PERSON_THRESHOLD:
        lock_screen(os_type)
        no_person_timer = 0

    (1)

()

The code is commented, which is very simple. Because the lock screen instructions of Windows and macOS are different, a simple system platform judgment was made.

It can be executed perfectly, that is, it has to keep calling the camera, and no one will really use this thing, hhh.

This is the end of this article about using Python to implement interesting lock screen widgets. For more related Python lock screen content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!