-
Notifications
You must be signed in to change notification settings - Fork 1
/
detector.py
78 lines (63 loc) · 2.43 KB
/
detector.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
from gpiozero import MotionSensor
from picamera import PiCamera
from datetime import datetime
import csv
import boto3
from time import sleep
class Detector(object):
def __init__(self):
# 4 = the pin on the Rasberry pi that the MotionSensor is connected to
self.pir = MotionSensor(4, threshold=0.5)
self.camera = PiCamera()
self.source_photo = 'test.jpg'
with open('new_user_credentials.csv', 'r') as input:
csvreader = csv.DictReader(input)
for row in csvreader:
self.access_key_id = row['Access key ID']
self.secret_key = row['Secret access key']
def start(self):
self.wait_for_motion()
self.take_picture()
self.wait_for_no_motion()
photo = self.covert_img_to_bytes()
results = self.aws_rekognition_image(photo)
self.print_results(results)
def wait_for_motion(self):
self.pir.wait_for_no_motion()
self.pir.wait_for_motion()
print("Motion detect!")
def wait_for_no_motion(self):
self.pir.wait_for_no_motion()
print("No Motion")
def take_picture(self):
self.camera.resolution = (1920, 1080)
self.camera.rotation = 180
self.camera.start_preview()
sleep(2)
self.camera.capture(self.source_photo)
self.camera.stop_preview()
def stop_camera(self):
self.camera.stop_recording()
def start_camera(self):
datename = "{0:%Y}-{0:%m}-{0:%d}:{0:%H}:{0:%M}:{0:%S}".format(datetime.now())
filename = str(datename) + "video.h264"
self.camera.resolution = (640, 480)
self.camera.rotation = 180
self.camera.start_recording(filename)
def aws_rekognition_image(self, photo):
client = boto3.client('rekognition',
aws_access_key_id=self.access_key_id,
aws_secret_access_key=self.secret_key,
region_name='us-west-2')
return client.detect_labels(Image={'Bytes': photo})
def covert_img_to_bytes(self):
with open(self.source_photo, 'rb') as photo:
return photo.read()
def print_results(self, results):
for each in results['Labels']:
print(each['Name'] + ": " + str(each['Confidence']))
def main():
obj = Detector()
obj.start()
if __name__ == "__main__":
main()