-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
160 lines (115 loc) · 4.74 KB
/
main.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# import the necessary packages
from imutils.video import VideoStream
from imutils.video import FPS
import face_recognition as fr
import imutils
import pickle
import time
import cv2
import datetime
print("[INFO] loading encodings + face detector...")
data = pickle.loads(open('encodings.pickle', "rb").read())
# initialize the video stream and allow the camera sensor to warm up
print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()
# vs = VideoStream(usePiCamera=True).start()
time.sleep(2.0)
# start the FPS counter
fps = FPS().start()
screen_time = False # is looking towards the screen?
screen_up_time = datetime.timedelta(0, 0, 0)
init_time = datetime.datetime.now()
users = {None: {'screentime': datetime.timedelta(0)}}
def add_user(name):
users[name] = {'screentime': datetime.timedelta(0)}
for nms in data["names"]:
add_user(nms)
def display_screen_time():
for user in users.keys():
if user is None:
continue
print(f'{user}: {users[user]["screentime"]}')
def save_data():
with open('./data/log.csv', 'w') as datafile:
for name in users.keys():
# if name is None: continue
datafile.write(f"{name},{users[name]['screentime'].seconds}\n")
def get_data():
with open('./data/log.csv') as datafile:
data = datafile.readlines()
for row in data:
row = row.replace('\n', ' ')
d = row.split(',')
users[d[0]]['screentime'] = datetime.timedelta(seconds=int(d[1]))
last_iter_people=set()
while True:
try:
frame = vs.read()
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
rgb = imutils.resize(frame, width=750)
r = frame.shape[1] / float(rgb.shape[1])
# detect the (x, y)-coordinates of the bounding boxes
# corresponding to each face in the input frame, then compute
# the facial embeddings for each face
boxes = fr.face_locations(rgb, model='hog')
num_faces=len(boxes)
print('Counting' if num_faces > 0 else 'Ideal 😴 ', end='\r')
encodings = fr.face_encodings(rgb, boxes)
names = set()
people = set()
for encoding in encodings:
# attempt to match each face in the input image to our known
# encodings
matches = fr.compare_faces(data["encodings"], encoding)
name = "Unknown"
# check to see if we have found a match
if True in matches:
# find the indexes of all matched faces then initialize a
# dictionary to count the total number of times each face
# was matched
matchedIdxs = [i for (i, b) in enumerate(matches) if b]
counts = {}
# loop over the matched indexes and maintain a count for
# each recognized face face
for i in matchedIdxs:
name = data["names"][i]
counts[name] = counts.get(name, 0) + 1
# determine the recognized face with the largest number
# of votes (note: in the event of an unlikely tie Python
# will select first entry in the dictionary)
dname = max(counts, key=counts.get)
# update the list of names
names.add(dname)
if not users[name].get('screen_time'):
users[name]['screen_time'] = True
users[name]['start_time'] = datetime.datetime.now()
for p in list(last_iter_people - names):
print(p)
if users[p]['screen_time']:
users[p]['screen_time'] = False
users[p]['screentime'] += datetime.datetime.now() - users[p]['start_time']
last_iter_people = names.copy() # helper to make the logic work
# all the people detected are stored in people:set
# the names of people is copied to last_iter_people:set
# in the next iteration we can calclate the missing people
del frame
time.sleep(1)
except KeyboardInterrupt:
# just a check before closing the programme
for p in list(names):
if users[p]['screen_time']:
users[p]['screen_time'] = False
users[p]['screentime'] += datetime.datetime.now() - users[p]['start_time']
print('*-'*20)
print(users)
save_data()
print('Thank you for Using Face Screen Time')
print('*-'*20)
vs.stop()
break
except Exception as e:
print('some unknown error occured')
print(e)
break
# do a bit of cleanup
vs.stop()