1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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)
|