-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpc_idle.py
125 lines (103 loc) · 3.34 KB
/
pc_idle.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import os
import time
from requests import get, post
import cv2
import win32api
import yaml
# import sys
# print("%x" % sys.maxsize, sys.maxsize > 2**32)
rp = realpath = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(rp, "config.yaml"), "r") as f:
config = yaml.load(f)
def getIdleTime():
return (win32api.GetTickCount() - win32api.GetLastInputInfo()) / 1000.0
def trigger(action):
print(action)
headers = {
'Authorization': 'Bearer {}'.format(config['token']),
'content-type': 'application/json',
}
for entity_id in config['home_assistant_booleans']:
url = "{}/services/input_boolean/{}".format(config['endpoint_url'], action)
data = {"entity_id": "input_boolean.{}".format(entity_id)}
# print (" ", action, url, data)
response = post(url, headers=headers, json=data)
# print(" ", response.text)
use_camera = config['use_camera']
camera_id = config['camera_id']
idle = False
def do_sleep():
time.sleep(config['interval'])
def detect_faces(camera_id, cascades):
cam = cv2.VideoCapture(camera_id)
# Check if the webcam is opened correctly
if not cam.isOpened():
print("Cannot open webcam, check ID, or maybe camera is used by another app.")
do_sleep()
return False
ret, frame = cam.read()
cam.release()
face_detected = False
for cascade in cascades:
faceCascade = cv2.CascadeClassifier(os.path.join(rp, cascade))
# print("Getting camera image...")
if not ret:
print("Cannot get image from camera, possibly wrong ID?")
do_sleep()
break
# print(ret, frame)
# cv2.imshow("test", frame)
# cv2.waitKey(10)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces in the image
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE
)
if len(faces) > 0:
print("Faces detected with {}: {}".format(cascade, len(faces)))
do_sleep()
return True
return False
cascades = config['cascades']
offset = 0
face_detected = False
while True:
idle_for = getIdleTime()
if idle_for-offset < 0:
offset = 0
# print(idle_for-offset, idle_for, offset)
if idle_for - offset > config['idle_if_seconds']:
if not idle:
if use_camera:
face_detected = detect_faces(camera_id, cascades)
if not face_detected:
time.sleep(5)
face_detected = detect_faces(camera_id, cascades)
if face_detected:
offset = getIdleTime()
continue
if not face_detected:
print("No faces.")
trigger('turn_on')
idle = True
offset = 0
do_sleep()
continue
else:
trigger('turn_on')
offset = 0
idle = True
do_sleep()
continue
else:
if idle:
trigger('turn_off')
idle = False
offset = 0
# config['idle_if_seconds']
#print ("Sleep in end")
do_sleep()