Skip to content
This repository was archived by the owner on Jan 22, 2019. It is now read-only.

Commit 3be26cb

Browse files
committed
bump
1 parent 790f915 commit 3be26cb

File tree

8 files changed

+227
-99
lines changed

8 files changed

+227
-99
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Distribution / packaging
2+
*.pyc
23
.Python
34
.env
45
node_modules
-232 Bytes
Binary file not shown.
-1.96 KB
Binary file not shown.
-1.86 KB
Binary file not shown.
-1.42 KB
Binary file not shown.

find_person.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import os
2+
from threading import Timer
3+
import time
4+
import datetime
5+
import awscam
6+
import cv2
7+
from botocore.session import Session
8+
from threading import Thread
9+
10+
# Setup the S3 client
11+
session = Session()
12+
s3 = session.create_client('s3')
13+
s3_bucket = 'doorman-faces'
14+
15+
# setup the camera and frame
16+
ret, frame = awscam.getLastFrame()
17+
ret,jpeg = cv2.imencode('.jpg', frame)
18+
Write_To_FIFO = True
19+
class FIFO_Thread(Thread):
20+
def __init__(self):
21+
''' Constructor. '''
22+
Thread.__init__(self)
23+
24+
def run(self):
25+
# write to tmp file for local debugging purpose
26+
fifo_path = "/tmp/results.mjpeg"
27+
if not os.path.exists(fifo_path):
28+
os.mkfifo(fifo_path)
29+
f = open(fifo_path,'w')
30+
31+
# yay, succesful, let's start streaming to the file
32+
while Write_To_FIFO:
33+
try:
34+
f.write(jpeg.tobytes())
35+
except IOError as e:
36+
continue
37+
38+
def greengrass_infinite_infer_run():
39+
try:
40+
modelPath = "/opt/awscam/artifacts/mxnet_deploy_ssd_resnet50_300_FP16_FUSED.xml"
41+
modelType = "ssd"
42+
input_width = 300
43+
input_height = 300
44+
max_threshold = 0.60 # raise/lower this value based on your conditions
45+
outMap = { 1: 'aeroplane', 2: 'bicycle', 3: 'bird', 4: 'boat', 5: 'bottle', 6: 'bus', 7 : 'car', 8 : 'cat', 9 : 'chair', 10 : 'cow', 11 : 'dinning table', 12 : 'dog', 13 : 'horse', 14 : 'motorbike', 15 : 'person', 16 : 'pottedplant', 17 : 'sheep', 18 : 'sofa', 19 : 'train', 20 : 'tvmonitor' }
46+
results_thread = FIFO_Thread()
47+
results_thread.start()
48+
49+
# Load model to GPU
50+
mcfg = {"GPU": 1}
51+
model = awscam.Model(modelPath, mcfg)
52+
53+
# try to get a frame from the camera
54+
ret, frame = awscam.getLastFrame()
55+
if ret == False:
56+
raise Exception("Failed to get frame from the stream")
57+
58+
yscale = float(frame.shape[0]/input_height)
59+
xscale = float(frame.shape[1]/input_width)
60+
61+
doInfer = True
62+
while doInfer:
63+
# Get a frame from the video stream
64+
ret, frame = awscam.getLastFrame()
65+
66+
# Raise an exception if failing to get a frame
67+
if ret == False:
68+
raise Exception("Failed to get frame from the stream")
69+
70+
# Resize frame to fit model input requirement
71+
frameResize = cv2.resize(frame, (input_width, input_height))
72+
73+
# Run model inference on the resized frame
74+
inferOutput = model.doInference(frameResize)
75+
76+
# Output inference result to the fifo file so it can be viewed with mplayer
77+
parsed_results = model.parseResult(modelType, inferOutput)['ssd']
78+
label = '{'
79+
for obj in parsed_results:
80+
if obj['prob'] > max_threshold:
81+
xmin = int( xscale * obj['xmin'] ) + int((obj['xmin'] - input_width/2) + input_width/2)
82+
ymin = int( yscale * obj['ymin'] )
83+
xmax = int( xscale * obj['xmax'] ) + int((obj['xmax'] - input_width/2) + input_width/2)
84+
ymax = int( yscale * obj['ymax'] )
85+
86+
# if a person was found, upload the target area to S3 for further inspection
87+
if outMap[obj['label']] == 'person':
88+
89+
# get the person image
90+
person = frame[ymin:ymax, xmin:xmax]
91+
92+
# create a nice s3 file key
93+
s3_key = datetime.datetime.utcnow().strftime('%Y-%m-%d_%H_%M_%S.%f') + '.jpg'
94+
encode_param=[int(cv2.IMWRITE_JPEG_QUALITY), 90] # 90% should be more than enough
95+
_, jpg_data = cv2.imencode('.jpg', person, encode_param)
96+
filename = "incoming/%s" % s3_key # the guess lambda function is listening here
97+
response = s3.put_object(ACL='public-read', Body=jpg_data.tostring(),Bucket=s3_bucket,Key=filename)
98+
99+
# draw a rectangle around the designated area, and tell what label was found
100+
cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), (255, 165, 20), 4)
101+
label += '"{}": {:.2f},'.format(outMap[obj['label']], obj['prob'] )
102+
label_show = "{}: {:.2f}%".format(outMap[obj['label']], obj['prob']*100 )
103+
cv2.putText(frame, label_show, (xmin, ymin-15),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 165, 20), 4)
104+
label += '"null": 0.0'
105+
label += '}'
106+
107+
global jpeg
108+
ret,jpeg = cv2.imencode('.jpg', frame)
109+
110+
except Exception as e:
111+
print "Crap, something failed: %s" % str(e)
112+
113+
# Asynchronously schedule this function to be run again in 15 seconds
114+
Timer(15, greengrass_infinite_infer_run).start()
115+
116+
# Execute the function above
117+
greengrass_infinite_infer_run()
118+
119+
120+
# This is a dummy handler and will not be invoked
121+
# Instead the code above will be executed in an infinite loop for our example
122+
def function_handler(event, context):
123+
return

handler.py

Lines changed: 99 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -16,105 +16,105 @@
1616

1717

1818
if __name__ == "__main__":
19-
# print(unknown(
20-
# {
21-
# "Records": [
22-
# {
23-
# "eventVersion": "2.0",
24-
# "eventTime": "1970-01-01T00:00:00.000Z",
25-
# "requestParameters": {
26-
# "sourceIPAddress": "127.0.0.1"
27-
# },
28-
# "s3": {
29-
# "configurationId": "testConfigRule",
30-
# "object": {
31-
# "eTag": "0123456789abcdef0123456789abcdef",
32-
# "sequencer": "0A1B2C3D4E5F678901",
33-
# "key": "detected/U033PFSFB/3aabdb9e2e5c4da13ec13b670cf4f574.jpg",
34-
# "size": 1024
35-
# },
36-
# "bucket": {
37-
# "arn": "arn:aws:s3:::doorman-faces",
38-
# "name": "doorman-faces",
39-
# "ownerIdentity": {
40-
# "principalId": "EXAMPLE"
41-
# }
42-
# },
43-
# "s3SchemaVersion": "1.0"
44-
# },
45-
# "responseElements": {
46-
# "x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH",
47-
# "x-amz-request-id": "EXAMPLE123456789"
48-
# },
49-
# "awsRegion": "us-east-1",
50-
# "eventName": "ObjectCreated:Put",
51-
# "userIdentity": {
52-
# "principalId": "EXAMPLE"
53-
# },
54-
# "eventSource": "aws:s3"
55-
# }
56-
# ]
57-
# }, {})
58-
# )
19+
print(unknown(
20+
{
21+
"Records": [
22+
{
23+
"eventVersion": "2.0",
24+
"eventTime": "1970-01-01T00:00:00.000Z",
25+
"requestParameters": {
26+
"sourceIPAddress": "127.0.0.1"
27+
},
28+
"s3": {
29+
"configurationId": "testConfigRule",
30+
"object": {
31+
"eTag": "0123456789abcdef0123456789abcdef",
32+
"sequencer": "0A1B2C3D4E5F678901",
33+
"key": "detected/U033PFSFB/1ef4dfe223eec2b6801aa4873cd3e350.jpg",
34+
"size": 1024
35+
},
36+
"bucket": {
37+
"arn": "arn:aws:s3:::doorman-faces",
38+
"name": "doorman-faces",
39+
"ownerIdentity": {
40+
"principalId": "EXAMPLE"
41+
}
42+
},
43+
"s3SchemaVersion": "1.0"
44+
},
45+
"responseElements": {
46+
"x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH",
47+
"x-amz-request-id": "EXAMPLE123456789"
48+
},
49+
"awsRegion": "us-east-1",
50+
"eventName": "ObjectCreated:Put",
51+
"userIdentity": {
52+
"principalId": "EXAMPLE"
53+
},
54+
"eventSource": "aws:s3"
55+
}
56+
]
57+
}, {})
58+
)
5959

60-
print(train({
61-
"resource": "/",
62-
"path": "/",
63-
"httpMethod": "POST",
64-
"headers": {
65-
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
66-
"Accept-Encoding": "gzip, deflate, br",
67-
"Accept-Language": "en-GB,en-US;q=0.8,en;q=0.6,zh-CN;q=0.4",
68-
"cache-control": "max-age=0",
69-
"CloudFront-Forwarded-Proto": "https",
70-
"CloudFront-Is-Desktop-Viewer": "true",
71-
"CloudFront-Is-Mobile-Viewer": "false",
72-
"CloudFront-Is-SmartTV-Viewer": "false",
73-
"CloudFront-Is-Tablet-Viewer": "false",
74-
"CloudFront-Viewer-Country": "GB",
75-
"content-type": "application/x-www-form-urlencoded",
76-
"Host": "j3ap25j034.execute-api.eu-west-2.amazonaws.com",
77-
"origin": "https://j3ap25j034.execute-api.eu-west-2.amazonaws.com",
78-
"Referer": "https://j3ap25j034.execute-api.eu-west-2.amazonaws.com/dev/",
79-
"upgrade-insecure-requests": "1",
80-
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36",
81-
"Via": "2.0 a3650115c5e21e2b5d133ce84464bea3.cloudfront.net (CloudFront)",
82-
"X-Amz-Cf-Id": "0nDeiXnReyHYCkv8cc150MWCFCLFPbJoTs1mexDuKe2WJwK5ANgv2A==",
83-
"X-Amzn-Trace-Id": "Root=1-597079de-75fec8453f6fd4812414a4cd",
84-
"X-Forwarded-For": "50.129.117.14, 50.112.234.94",
85-
"X-Forwarded-Port": "443",
86-
"X-Forwarded-Proto": "https"
87-
},
88-
"queryStringParameters": "",
89-
"pathParameters": "None",
90-
"stageVariables": "None",
91-
"requestContext": {
92-
"path": "/dev/",
93-
"accountId": "125002137610",
94-
"resourceId": "qdolsr1yhk",
95-
"stage": "dev",
96-
"requestId": "0f2431a2-6d2f-11e7-b75152aa497861",
97-
"identity": {
98-
"cognitoIdentityPoolId": None,
99-
"accountId": None,
100-
"cognitoIdentityId": None,
101-
"caller": None,
102-
"apiKey": "",
103-
"sourceIp": "50.129.117.14",
104-
"accessKey": None,
105-
"cognitoAuthenticationType": None,
106-
"cognitoAuthenticationProvider": None,
107-
"userArn": None,
108-
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36",
109-
"user": None
110-
},
111-
"resourcePath": "/",
112-
"httpMethod": "POST",
113-
"apiId": "j3azlsj0c4"
114-
},
115-
"body": "payload=%7B%22type%22%3A%22interactive_message%22%2C%22actions%22%3A%5B%7B%22name%22%3A%22discard%22%2C%22type%22%3A%22select%22%2C%22selected_options%22%3A%5B%7B%22value%22%3A%22U033PFSFB%22%7D%5D%7D%5D%2C%22callback_id%22%3A%22unknown%5C%2Ftest.jpg%22%2C%22team%22%3A%7B%22id%22%3A%22T02HUP4V7%22%2C%22domain%22%3A%22unitt%22%7D%2C%22channel%22%3A%7B%22id%22%3A%22C8HLA7R63%22%2C%22name%22%3A%22whodis%22%7D%2C%22user%22%3A%7B%22id%22%3A%22U033PFSFB%22%2C%22name%22%3A%22svdgraaf%22%7D%2C%22action_ts%22%3A%221513682920.702541%22%2C%22message_ts%22%3A%221513654753.000074%22%2C%22attachment_id%22%3A%221%22%2C%22token%22%3A%22QfG0e3Guj5VqB8FYRu6t6hsG%22%2C%22is_app_unfurl%22%3Afalse%2C%22original_message%22%3A%7B%22text%22%3A%22Whodis%3F%22%2C%22username%22%3A%22Doorman%22%2C%22bot_id%22%3A%22B8GHY4N9G%22%2C%22attachments%22%3A%5B%7B%22fallback%22%3A%22Nope%3F%22%2C%22image_url%22%3A%22https%3A%5C%2F%5C%2Fs3.amazonaws.com%5C%2Fdoorman-faces%5C%2Funknown%5C%2Ftest.jpg%22%2C%22image_width%22%3A720%2C%22image_height%22%3A480%2C%22image_bytes%22%3A85516%2C%22callback_id%22%3A%22unknown%5C%2Ftest.jpg%22%2C%22id%22%3A1%2C%22color%22%3A%223AA3E3%22%2C%22actions%22%3A%5B%7B%22id%22%3A%221%22%2C%22name%22%3A%22username%22%2C%22text%22%3A%22Select+a+username...%22%2C%22type%22%3A%22select%22%2C%22data_source%22%3A%22users%22%7D%5D%7D%5D%2C%22type%22%3A%22message%22%2C%22subtype%22%3A%22bot_message%22%2C%22ts%22%3A%221513654753.000074%22%7D%2C%22response_url%22%3A%22https%3A%5C%2F%5C%2Fhooks.slack.com%5C%2Factions%5C%2FT02HUP4V7%5C%2F288116281232%5C%2FqaNuNww7z6pywPrk3jIHHcHF%22%2C%22trigger_id%22%3A%22288701768947.2606786993.b007d1a39a5076df879809512ef1d063%22%7D",
116-
"isBase64Encoded": False
117-
}, {}))
60+
# print(train({
61+
# "resource": "/",
62+
# "path": "/",
63+
# "httpMethod": "POST",
64+
# "headers": {
65+
# "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
66+
# "Accept-Encoding": "gzip, deflate, br",
67+
# "Accept-Language": "en-GB,en-US;q=0.8,en;q=0.6,zh-CN;q=0.4",
68+
# "cache-control": "max-age=0",
69+
# "CloudFront-Forwarded-Proto": "https",
70+
# "CloudFront-Is-Desktop-Viewer": "true",
71+
# "CloudFront-Is-Mobile-Viewer": "false",
72+
# "CloudFront-Is-SmartTV-Viewer": "false",
73+
# "CloudFront-Is-Tablet-Viewer": "false",
74+
# "CloudFront-Viewer-Country": "GB",
75+
# "content-type": "application/x-www-form-urlencoded",
76+
# "Host": "j3ap25j034.execute-api.eu-west-2.amazonaws.com",
77+
# "origin": "https://j3ap25j034.execute-api.eu-west-2.amazonaws.com",
78+
# "Referer": "https://j3ap25j034.execute-api.eu-west-2.amazonaws.com/dev/",
79+
# "upgrade-insecure-requests": "1",
80+
# "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36",
81+
# "Via": "2.0 a3650115c5e21e2b5d133ce84464bea3.cloudfront.net (CloudFront)",
82+
# "X-Amz-Cf-Id": "0nDeiXnReyHYCkv8cc150MWCFCLFPbJoTs1mexDuKe2WJwK5ANgv2A==",
83+
# "X-Amzn-Trace-Id": "Root=1-597079de-75fec8453f6fd4812414a4cd",
84+
# "X-Forwarded-For": "50.129.117.14, 50.112.234.94",
85+
# "X-Forwarded-Port": "443",
86+
# "X-Forwarded-Proto": "https"
87+
# },
88+
# "queryStringParameters": "",
89+
# "pathParameters": "None",
90+
# "stageVariables": "None",
91+
# "requestContext": {
92+
# "path": "/dev/",
93+
# "accountId": "125002137610",
94+
# "resourceId": "qdolsr1yhk",
95+
# "stage": "dev",
96+
# "requestId": "0f2431a2-6d2f-11e7-b75152aa497861",
97+
# "identity": {
98+
# "cognitoIdentityPoolId": None,
99+
# "accountId": None,
100+
# "cognitoIdentityId": None,
101+
# "caller": None,
102+
# "apiKey": "",
103+
# "sourceIp": "50.129.117.14",
104+
# "accessKey": None,
105+
# "cognitoAuthenticationType": None,
106+
# "cognitoAuthenticationProvider": None,
107+
# "userArn": None,
108+
# "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36",
109+
# "user": None
110+
# },
111+
# "resourcePath": "/",
112+
# "httpMethod": "POST",
113+
# "apiId": "j3azlsj0c4"
114+
# },
115+
# "body": "payload=%7B%22type%22%3A%22interactive_message%22%2C%22actions%22%3A%5B%7B%22name%22%3A%22discard%22%2C%22type%22%3A%22select%22%2C%22selected_options%22%3A%5B%7B%22value%22%3A%22U033PFSFB%22%7D%5D%7D%5D%2C%22callback_id%22%3A%22unknown%5C%2Ftest.jpg%22%2C%22team%22%3A%7B%22id%22%3A%22T02HUP4V7%22%2C%22domain%22%3A%22unitt%22%7D%2C%22channel%22%3A%7B%22id%22%3A%22C8HLA7R63%22%2C%22name%22%3A%22whodis%22%7D%2C%22user%22%3A%7B%22id%22%3A%22U033PFSFB%22%2C%22name%22%3A%22svdgraaf%22%7D%2C%22action_ts%22%3A%221513682920.702541%22%2C%22message_ts%22%3A%221513654753.000074%22%2C%22attachment_id%22%3A%221%22%2C%22token%22%3A%22QfG0e3Guj5VqB8FYRu6t6hsG%22%2C%22is_app_unfurl%22%3Afalse%2C%22original_message%22%3A%7B%22text%22%3A%22Whodis%3F%22%2C%22username%22%3A%22Doorman%22%2C%22bot_id%22%3A%22B8GHY4N9G%22%2C%22attachments%22%3A%5B%7B%22fallback%22%3A%22Nope%3F%22%2C%22image_url%22%3A%22https%3A%5C%2F%5C%2Fs3.amazonaws.com%5C%2Fdoorman-faces%5C%2Funknown%5C%2Ftest.jpg%22%2C%22image_width%22%3A720%2C%22image_height%22%3A480%2C%22image_bytes%22%3A85516%2C%22callback_id%22%3A%22unknown%5C%2Ftest.jpg%22%2C%22id%22%3A1%2C%22color%22%3A%223AA3E3%22%2C%22actions%22%3A%5B%7B%22id%22%3A%221%22%2C%22name%22%3A%22username%22%2C%22text%22%3A%22Select+a+username...%22%2C%22type%22%3A%22select%22%2C%22data_source%22%3A%22users%22%7D%5D%7D%5D%2C%22type%22%3A%22message%22%2C%22subtype%22%3A%22bot_message%22%2C%22ts%22%3A%221513654753.000074%22%7D%2C%22response_url%22%3A%22https%3A%5C%2F%5C%2Fhooks.slack.com%5C%2Factions%5C%2FT02HUP4V7%5C%2F288116281232%5C%2FqaNuNww7z6pywPrk3jIHHcHF%22%2C%22trigger_id%22%3A%22288701768947.2606786993.b007d1a39a5076df879809512ef1d063%22%7D",
116+
# "isBase64Encoded": False
117+
# }, {}))
118118

119-
# print(guess({'Records': [{'eventVersion': '2.0', 'eventSource': 'aws:s3', 'awsRegion': 'us-east-1', 'eventTime': '2017-12-19T12:04:09.653Z', 'eventName': 'ObjectCreated:Put', 'userIdentity': {'principalId': 'AWS:AROAIHE5RTNGNONLAZCZS:AssumeRoleSession'}, 'requestParameters': {'sourceIPAddress': '63.228.166.237'}, 'responseElements': {'x-amz-request-id': '839246E5C0E2300C', 'x-amz-id-2': 'nZdfLBl9JptArE5YNYgD5vJhHjXSXZHPD8jFKSneIIJ8HWM4wiFvETRixxOVSJGenFGpqCFjckw='}, 's3': {'s3SchemaVersion': '1.0', 'configurationId': 'ca169f58-28da-4b01-a2c2-cdbbad9081c7', 'bucket': {'name': 'doorman-faces', 'ownerIdentity': {'principalId': 'AUAL9TOIHMDDI'}, 'arn': 'arn:aws:s3:::doorman-faces'}, 'object': {'key': 'detected/U033PFSFB/08e80c1689a77cd57818556f963f38c4.jpg', 'size': 81715, 'eTag': '112ba09a09910f3d45508e31398b0517', 'sequencer': '005A39003941D63E04'}}}]}
119+
# print(guess({'Records': [{'eventVersion': '2.0', 'eventSource': 'aws:s3', 'awsRegion': 'us-east-1', 'eventTime': '2017-12-19T12:04:09.653Z', 'eventName': 'ObjectCreated:Put', 'userIdentity': {'principalId': 'AWS:AROAIHE5RTNGNONLAZCZS:AssumeRoleSession'}, 'requestParameters': {'sourceIPAddress': '63.228.166.237'}, 'responseElements': {'x-amz-request-id': '839246E5C0E2300C', 'x-amz-id-2': 'nZdfLBl9JptArE5YNYgD5vJhHjXSXZHPD8jFKSneIIJ8HWM4wiFvETRixxOVSJGenFGpqCFjckw='}, 's3': {'s3SchemaVersion': '1.0', 'configurationId': 'ca169f58-28da-4b01-a2c2-cdbbad9081c7', 'bucket': {'name': 'doorman-faces', 'ownerIdentity': {'principalId': 'AUAL9TOIHMDDI'}, 'arn': 'arn:aws:s3:::doorman-faces'}, 'object': {'key': 'detected/U033PFSFB/ac8b9fe608d6ec59871f5d2e2bcb8edd.jpg', 'size': 81715, 'eTag': '112ba09a09910f3d45508e31398b0517', 'sequencer': '005A39003941D63E04'}}}]}
120120
# ,{}))

serverless.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ functions:
9292
path: faces/train
9393
method: post
9494

95+
# find person, function for greengrass device
96+
find-person:
97+
handler: find_person.function_handler
98+
9599
# resources:
96100
# Resources:
97101
# S3BucketDoormanfaces:

0 commit comments

Comments
 (0)