import cv2 import time import logging import dbus session_bus = dbus.SessionBus() def tryLock(): DBUS_DEST = "org.mate.ScreenSaver" DBUS_PATH = "/org/mate/ScreenSaver" DBUS_IFACE = "org.mate.ScreenSaver" proxy = session_bus.get_object(DBUS_DEST, DBUS_PATH) screensaver = dbus.Interface(proxy, DBUS_IFACE) if screensaver.Hello() == DBUS_DEST: if not screensaver.GetActive(): logging.info("call Lock()") screensaver.Lock() else: logging.debug("Is locked already") else: logging.error("Mate-Screensaver doesn't answer our call") def detect(show_window=False): face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') capture = cv2.VideoCapture(0) last_seen = time.time() try: while True: ret, frame = capture.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) if len(faces) == 1: last_seen = time.time() elif time.time() - last_seen > 10: # sec tryLock() for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2) roi_gray = gray[y:y+h, x:x+w] roi_color = frame[y:y+h, x:x+w] if show_window: cv2.imshow("img", frame) if cv2.waitKey(500) >= 0: break else: time.sleep(0.3) except KeyboardInterrupt: pass capture.release() if show_window: cv2.destroyAllWindows() if __name__ == "__main__": detect(True)