-
Notifications
You must be signed in to change notification settings - Fork 6
/
BirdLambda.py
executable file
·87 lines (69 loc) · 3.63 KB
/
BirdLambda.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
import boto3
import datetime
CONFIDENCE = 65
WEBSITE_BUCKET = 'www.docugain.com'
BIRD_TOPIC = 'arn:aws:sns:us-east-1:389195416133:bird-topic'
SQUIRREL_TOPIC = 'arn:aws:sns:us-east-1:389195416133:squirrel-topic'
# list of labels to ignore
IGNORE = ['Bird','Bird Feeder','Beak','Animal', 'Mammal', 'Chair', 'Furniture','Brick','Windshield', 'Winter',\
'Bench', 'Grass', 'Plant', 'Lawn','Lamp','Hydrant','Plastic','Plastic Wrap','Stove','Sticker','Jay', \
'Fire Hydrant', 'Lamp Post', 'Tree', 'Conifer', 'Water', 'Spruce','Traffic Light','Light', \
'Concrete','Letterbox','Mailbox', 'Post Box','Public Mailbox','Aircraft','Vehicle','Transportation', \
'Airplane', 'Fir','Abies','Cross','Symbol','Hat','Ceiling Fan','Human','Person','Fish', \
'Light Fixture', 'Appliance','Outdoors','Window','Trash Can', 'Tin','Can','Hole','Oven', \
'Porthole','Home Decor','Statue', 'Sculpture','Art','Ornament','Label','Text','Hardhat', \
'Helmet','Apparel','Clothing','Aluminium','Forge','Nature','Ice','Snow','Bluebird', 'Blue Jay','Vegetation','Fire Hydrant']
def detect_labels(bucket, key, min_confidence=CONFIDENCE):
client=boto3.client('rekognition')
dt = datetime.datetime.now().strftime('%Y-%m-%d-%H:%M:%S')
response = client.detect_labels(Image={'S3Object': {'Bucket': bucket, 'Name': key}},
MinConfidence=min_confidence)
outstring = ''
primary = ''
print(response['Labels'])
for Label in response['Labels']:
if not (Label['Name'] in IGNORE):
if primary == '':
primary = Label['Name']
outstring += str(Label['Name']) + ' (Confidence ' + str(Label['Confidence']) + ')\r\n'
if outstring != '':
outstring = 'Detected labels for photo at time ' + dt + '\r\n' + outstring + ' in photo ' + key
print(outstring)
else:
print('No labels detected')
return primary,outstring
def send_to_sns(topic_arn,msg):
# Create an SNS client
sns = boto3.client('sns')
# Publish labels as a message to the SNS topic
response = sns.publish(TopicArn=topic_arn,Message=msg)
return response
def lambda_handler(event, context):
# Get the S3 bucket name and file from the event
bucket = event['Records'][0]['s3']['bucket']['name']
key = event['Records'][0]['s3']['object']['key']
print('bucket =',bucket)
print('key =',key)
try:
# Call rekognition DetectLabels API to detect labels in S3 object
primary_label,label_text = detect_labels(bucket, key)
#print(labels)
s3_resource = boto3.resource('s3')
if ('Squirrel' in label_text) or ('Rodent' in label_text) :
send_to_sns(SQUIRREL_TOPIC,label_text)
elif label_text != '': #we'll assume it's a bird, since we already ignored other items which aren't interesting
send_to_sns(BIRD_TOPIC,label_text)
#if it's a bird pic, move the picture to the website bucket
new_path = 'Latest/_'+primary_label+'_'+key
old_path = bucket+'/'+key
print(old_path,' , ',new_path)
s3_resource.Object(WEBSITE_BUCKET,new_path).copy_from(CopySource=old_path)
#delete the original picture
print('deleting file: ',bucket+key)
s3_resource.Object(bucket,key).delete()
return label_text
except Exception as e:
print(e)
print("Error processing object {} from bucket {}. ".format(key, bucket) +
"Make sure your object and bucket exist and your bucket is in the same region as this function.")
raise e