-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathget-rekognition-result-recap.py
64 lines (59 loc) · 2.19 KB
/
get-rekognition-result-recap.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
#! /usr/bin/python
import boto3
import json
import sys
import subprocess
import time
import datetime
foundedNames = []
def actions(name):
if not (name in foundedNames):
foundedNames.append(name)
# subprocess.call(["python", "say_hi.py", name])
subprocess.call(["python", "sns-publish.py", name])
else:
print "Founded %d peoples" % len(foundedNames)
# def putFaces(number):
# data = {}
# data["faces"] = number
# data["timeStamp"] = datetime.datetime.now().isoformat()
# payload = json.dumps(data)
# response = kinesis.put_record(StreamName=kinesisFaceCountStreamName, Data=payload, PartitionKey="Camera01")
# print response
with open('config.json') as json_data_file:
config = json.load(json_data_file)
region = config['region']
kinesisDataStream = config['kinesisDataStreamName']
kinesisFaceCountStreamName = config['kinesisFaceCountStreamName']
kinesis = boto3.client('kinesis', region_name=region)
stream = kinesis.describe_stream(StreamName=kinesisDataStream)
shardId = stream['StreamDescription']['Shards'][0]['ShardId']
shardIterator = kinesis.get_shard_iterator(StreamName=kinesisDataStream,
ShardId=shardId,
ShardIteratorType="LATEST")
shard_it = shardIterator["ShardIterator"]
while(True):
try:
recs = kinesis.get_records(ShardIterator=shard_it, Limit=1)
# print recs
shard_it = recs["NextShardIterator"]
if len(recs['Records']) > 0:
data = json.loads(recs['Records'][0]['Data'])
# print data['FaceSearchResponse']
if len(data['FaceSearchResponse']) > 0:
detectedFaces = len(data['FaceSearchResponse'])
subprocess.Popen(["python", "put-face-count.py", str(detectedFaces)])
print 'detect faces: %d' % detectedFaces
for faceSearchResponse in data['FaceSearchResponse']:
if len(faceSearchResponse['MatchedFaces']) > 0:
print 'match faces: %d' % len(faceSearchResponse['MatchedFaces'])
for face in faceSearchResponse['MatchedFaces']:
name = face['Face']['ExternalImageId']
confidence = face['Face']['Confidence']
print 'match face: %s confidence: %d' % (name, confidence)
actions(name)
except Exception as e:
print e.message
time.sleep(1)
# avoid ProvisionedThroughputExceededException
time.sleep(0.2)