-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageRecognitionFunction.yml
More file actions
67 lines (62 loc) · 2.24 KB
/
ImageRecognitionFunction.yml
File metadata and controls
67 lines (62 loc) · 2.24 KB
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
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
BucketName:
Type: String
Description: Name of the S3 bucket to create for image uploads
Resources:
ImageBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: 564474-custom-bucket-fun # Modify this with your desired bucket name
ImageRecognitionFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: ImageRecognitionFunction # Modify this with your desired function name
Runtime: python3.8
Handler: index.handler
Timeout: 60
Role: !GetAtt LambdaExecutionRole.Arn
Code:
ZipFile: |
import boto3
import os
rekognition = boto3.client('rekognition')
def handler(event, context):
# Retrieve the uploaded image object information
bucket_name = event['Records'][0]['s3']['bucket']['name']
object_key = event['Records'][0]['s3']['object']['key']
# Perform object detection using Rekognition
response = rekognition.detect_labels(
Image={'S3Object': {'Bucket': bucket_name, 'Name': object_key}},
MaxLabels=10
)
# Print the detected labels
for label in response['Labels']:
print('Detected label: ' + label['Name'])
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
RoleName: LambdaExecutionRole
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
LambdaPermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref ImageRecognitionFunction
Action: lambda:InvokeFunction
Principal: s3.amazonaws.com
SourceArn: !Sub arn:aws:s3:::${BucketName}
Outputs:
ImageBucketName:
Description: Name of the S3 bucket for image uploads
Value: !Ref ImageBucket
ImageRecognitionFunctionArn:
Description: ARN of the Lambda function for image recognition
Value: !GetAtt ImageRecognitionFunction.Arn