diff options
author | Yves Fischer <yvesf-git@xapek.org> | 2015-03-21 23:32:10 +0100 |
---|---|---|
committer | Yves Fischer <yvesf-git@xapek.org> | 2015-03-21 23:32:10 +0100 |
commit | 3d1048d79ad09d156836ff22edb9909d038fce87 (patch) | |
tree | dd9cd035f3e3537e2404208bf101be6bdbdfa865 /cv-head-lock/autolock.py | |
parent | fdd4c6267e683ec107f3df43f02140f0ac01d63f (diff) | |
download | scripts-3d1048d79ad09d156836ff22edb9909d038fce87.tar.gz scripts-3d1048d79ad09d156836ff22edb9909d038fce87.zip |
notifications
Diffstat (limited to 'cv-head-lock/autolock.py')
-rwxr-xr-x[-rw-r--r--] | cv-head-lock/autolock.py | 104 |
1 files changed, 79 insertions, 25 deletions
diff --git a/cv-head-lock/autolock.py b/cv-head-lock/autolock.py index a4f4bf9..acbbbc0 100644..100755 --- a/cv-head-lock/autolock.py +++ b/cv-head-lock/autolock.py @@ -1,3 +1,4 @@ +#!/usr/bin/python import cv2 import time import logging @@ -5,48 +6,101 @@ import dbus session_bus = dbus.SessionBus() +notif_id = id(__name__) % (2**32) + + +def get_dbus_object(dest, path, iface): + proxy = session_bus.get_object(dest, path) + return dbus.Interface(proxy, iface) + + +def notify(summary, body): + notif = get_dbus_object("org.freedesktop.Notifications", + "/org/freedesktop/Notifications", + "org.freedesktop.Notifications") + notif.Notify("autolock", notif_id, "", summary, body, [], {}, 1) + + +def close_notification(): + notif = get_dbus_object("org.freedesktop.Notifications", + "/org/freedesktop/Notifications", + "org.freedesktop.Notifications") + notif.CloseNotification(notif_id) 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() + try: + screensaver = get_dbus_object("org.mate.ScreenSaver", + "/org/mate/ScreenSaver", + "org.mate.ScreenSaver") + if screensaver.Hello() == "org.mate.ScreenSaver": + if not screensaver.GetActive(): + logging.info("call Lock()") + screensaver.Lock() + else: + logging.debug("Is locked already") else: - logging.debug("Is locked already") - else: - logging.error("Mate-Screensaver doesn't answer our call") + logging.error("Mate-Screensaver doesn't answer our call") + except: + pass def detect(show_window=False): - face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') + haar_cascade = cv2.CascadeClassifier("xml/haarcascade_eye.xml") capture = cv2.VideoCapture(0) last_seen = time.time() + last_analysis = last_seen try: while True: - ret, frame = capture.read() + ret = capture.grab() + + if time.time() - last_analysis < 0.5: + continue + + ret, frame = capture.retrieve() + last_analysis = time.time() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) - faces = face_cascade.detectMultiScale(gray, 1.3, 5) - if len(faces) == 1: + matches = haar_cascade.detectMultiScale(gray, 1.3, 5) + if len(matches) >= 1: + close_notification() 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] + else: + timeout = (last_seen - time.time()) + 12 + if timeout <= 10: + notify("Autolocker:", + "Activate screenlock in {:.0f}s".format(timeout)) + if timeout <= 0: + tryLock() if show_window: + gray = cv2.GaussianBlur(gray, (5, 5), 0) + gray = cv2.adaptiveThreshold(gray, 255, + cv2.ADAPTIVE_THRESH_GAUSSIAN_C, + cv2.THRESH_BINARY, 11, 2) + for (x, y, w, h) in matches: + cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2) + +# roi = gray[y:(y+h), x:(x+w)] +# if not roi != []: continue +# print (x, y, w, h) +# det = cv2.SimpleBlobDetector() +# for blob in det.detect(roi): +# cv2.circle(frame, (x+int(blob.pt[0]), y+int(blob.pt[1])), int(blob.size), (255,0,0)) +# circles = cv2.HoughCircles(roi, cv2.cv.CV_HOUGH_GRADIENT, 1, 20, +# param1=40,param2=15,minRadius=4,maxRadius=min(w,h)/2) +# if circles != None and len(circles) > 0: +# circles = np.uint16(np.around(circles)) +# for circle in circles[0]: +# center = (x + int(circle[0]), y + int(circle[1])) +# cv2.circle(frame, center, int(circle[2]), (0,255,255)) + cv2.imshow("img", frame) - if cv2.waitKey(500) >= 0: + cv2.imshow("gray", gray) + + key = cv2.waitKey(1) + ctrl_mask = 0x40000 + if key in [ctrl_mask | ord("q"), ctrl_mask | ord("w"), 27]: break else: time.sleep(0.3) |