-
Notifications
You must be signed in to change notification settings - Fork 25
/
watch.py
executable file
·72 lines (62 loc) · 2.32 KB
/
watch.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
#!/usr/bin/env python
import sys
import os
import time
import boto3 as b3
from botocore.exceptions import ClientError
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
client = b3.client('rekognition')
class MotionEventHandler(PatternMatchingEventHandler):
def __init__(self, collection=None):
super(MotionEventHandler, self).__init__()
self.collection = collection
patterns = ["*.jpg"]
def on_created(self, event):
#print(event.src_path, event.event_type)
event_file = open(event.src_path, 'rb')
image = event_file.read()
response_check = client.detect_faces(Image={'Bytes': image})
if not response_check['FaceDetails']:
#print 'No face detected, deleting file...'
event_file.flush()
event_file.close()
try:
os.remove(event.src_path)
except OSError:
print('Cannot delete file, check permissions')
else:
#print 'Face detected, identifying...'
try:
resp = client.search_faces_by_image(
CollectionId=self.collection,
Image={'Bytes': image},
MaxFaces=1,
FaceMatchThreshold=85)
with open('event.log', 'a+') as check:
person = ('Unknown' if not resp['FaceMatches']
else resp['FaceMatches'][0]['Face']['ExternalImageId'])
check.write('%s | %s | %s\n' % (
time.strftime('%Y-%m-%d %H:%M:%S'),
person,
event.src_path))
except ClientError as exception:
print('Error: %s' % exception.response['Error']['Code'])
event_file.flush()
event_file.close()
def main():
path = sys.argv[1] if len(sys.argv) > 1 else '.'
collection = sys.argv[2] if len(sys.argv) > 2 else 'camerapi'
observer = Observer()
event_handler = MotionEventHandler(collection)
observer.schedule(event_handler, path)
print('Starting observer on %s' % path)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == '__main__':
main()