From fdd4c6267e683ec107f3df43f02140f0ac01d63f Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Sat, 21 Mar 2015 15:38:56 +0100 Subject: autolock --- cv-head-lock/autolock.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 cv-head-lock/autolock.py (limited to 'cv-head-lock/autolock.py') diff --git a/cv-head-lock/autolock.py b/cv-head-lock/autolock.py new file mode 100644 index 0000000..a4f4bf9 --- /dev/null +++ b/cv-head-lock/autolock.py @@ -0,0 +1,61 @@ +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) -- cgit v1.2.1