forked from panorays/DishSnitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_loop.py
188 lines (158 loc) · 7.64 KB
/
main_loop.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import datetime
import os
import pprint
import time
import cv2
import face_recognition
import numpy as np
import pygame as pg
import pymongo
import global_vars
import config
from part1_capture_image import take_sink_photo
from part2_detect_dishes import check_if_dishes_exist
from part3_notify import post_slack
mongo_client = pymongo.MongoClient(host=config.mongo_host, port=config.mongo_port)
targets_db = mongo_client.targets
employees_pull = targets_db.employees
known_face_encodings = []
known_face_names = []
last_sin = {}
# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
total_time = []
cooldown = 10 # 3600 seconds between detection of targets = 1 hour
def play_music(music_file, volume=0.8):
'''
stream music with mixer.music module in a blocking manner
this will stream the sound from disk while playing
'''
# set up the mixer
freq = 44100 # audio CD quality
bitsize = -16 # unsigned 16 bit
channels = 2 # 1 is mono, 2 is stereo
buffer = 2048 # number of samples (experiment to get best sound)
pg.mixer.init(freq, bitsize, channels, buffer)
# volume value 0.0 to 1.0
pg.mixer.music.set_volume(volume)
clock = pg.time.Clock()
try:
pg.mixer.music.load(music_file)
print("Music file {} loaded!".format(music_file))
except pg.error:
print("File {} not found! ({})".format(music_file, pg.get_error()))
return
pg.mixer.music.play()
while pg.mixer.music.get_busy():
# check if playback has finished
clock.tick(30)
for employee in employees_pull.distinct("full_name"):
for fetch_info in employees_pull.find({"full_name": employee}):
last_sin[fetch_info.get("full_name")] = (time.time() - cooldown)
known_face_names.append(fetch_info.get("full_name"))
#print ('starting to encode {} '.format(employee))
start_time = time.time()
known_face_encodings.append(
face_recognition.face_encodings(face_recognition.load_image_file(fetch_info.get("image")))[0])
end_time = time.time()
finish_time = end_time - start_time
#print ('{} has been encoded and took {} '.format(employee, finish_time))
total_time.append(finish_time)
print (known_face_names)
print ('total tine for the process {} '.format(round(sum(total_time))))
# Get a reference to webcam for capturing people
video_capture = cv2.VideoCapture(config.person_camera_index)
while True:
# Grab a single frame of video
ret, frame = video_capture.read()
# Resize frame of video to 1/2 size for faster face recognition processing
small_frame = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5)
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
rgb_small_frame = small_frame[:, :, ::-1]
# Only process every other frame of video to save time
if process_this_frame:
# Find all the faces and face encodings in the current frame of video
face_locations = face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
# See if the face is a match for the known face(s)
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"
# # If a match was found in known_face_encodings, just use the first one.
# if True in matches:
# first_match_index = matches.index(True)
# name = known_face_names[first_match_index]
# Or instead, use the known face with the smallest distance to the new face
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
for check in known_face_names:
if check == name:
pprint.pprint("We found you..." + name)
# optional volume 0 to 1.0
volume = 1.0
play_music(config.found_someone_mp3_path, volume)
if face_distances[best_match_index] < 0.49:
if time.time() > (last_sin[name] + cooldown):
last_sin[name] = time.time()
print ("target found at {} ".format(check) + time.strftime("(%H:%M:%S - %d/%m/%Y)"))
global_vars.current_suspect = name
pprint.pprint("current_suspect after detected:" + global_vars.current_suspect)
#get sink status
#check_if_dishes_exist()
pprint.pprint("sink status when face detected:" + global_vars.current_sink_status)
pprint.pprint("current suspect when face detected:" + global_vars.current_suspect)
else:
cooldown_remain = ((last_sin[name] + cooldown) - time.time())
print ('{} you have more {} for cool-down'.format(name, str(
datetime.timedelta(seconds=cooldown_remain))))
face_names.append(name)
else:
pprint.pprint("unknown person or person left... did someone leave the sink?")
# take sink photo
pprint.pprint("taking photo of sink..")
take_sink_photo()
# get sink status
check_if_dishes_exist()
pprint.pprint("sink status with unknwon:" + global_vars.current_sink_status)
pprint.pprint("current suspect with unknwon:" + global_vars.current_suspect)
if global_vars.current_sink_status == "dirty":
pprint.pprint("Someone left DIRTY dishes!")
pprint.pprint("last person: " + global_vars.current_suspect)
if global_vars.current_suspect != "unknown":
post_slack(global_vars.current_suspect)
global_vars.current_sink_status = "dirty_notification_sent"
global_vars.current_sink_status = "unknown"
pprint.pprint("sink status after notification:" + global_vars.current_sink_status)
pprint.pprint("current suspect after notification:" + global_vars.current_suspect)
else:
pprint.pprint("Someone left CLEAN dishes!")
pprint.pprint("last person: " + global_vars.current_suspect)
global_vars.current_suspect = "unknown"
process_this_frame = not process_this_frame
#Display the results
for (top, right, bottom, left), name in zip(face_locations, face_names):
# Scale back up face locations since the frame we detected in was scaled to 0.5 size
top *= 2
right *= 2
bottom *= 2
left *= 2
# Draw a box around the face
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
# Draw a label with a name below the face
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 3, bottom - 3), font, 0.5, (255, 255, 255), 1)
# Display the resulting image
cv2.imshow('Video', frame)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()