├── SAM-repo ├── MyDynamodbLambda │ ├── requirements.txt │ └── MyDynamodbLambda.py ├── MyCustomRekognitionLambda │ ├── requirements.txt │ └── MyCustomRekognitionLambda.py └── template.yaml ├── CODE_OF_CONDUCT.md ├── README.md ├── LICENSE └── CONTRIBUTING.md /SAM-repo/MyDynamodbLambda/requirements.txt: -------------------------------------------------------------------------------- 1 | requests -------------------------------------------------------------------------------- /SAM-repo/MyCustomRekognitionLambda/requirements.txt: -------------------------------------------------------------------------------- 1 | requests -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | ## Code of Conduct 2 | This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). 3 | For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact 4 | opensource-codeofconduct@amazon.com with any additional questions or comments. 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## AI Driven Visual Inspection of Wind Turbines based on Drones Pictures 2 | 3 | This repository provides the example SAM (Serverless Application Model) template and two AWS Lambda functions disucussed in the blog post AI Driven Visual Inspection of Wind Turbines based on Drones Pictures . 4 | 5 | This is an example business logic. Not intended for production. 6 | 7 | 8 | ## Security 9 | 10 | See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information. 11 | 12 | ## License 13 | 14 | This library is licensed under the MIT-0 License. See the LICENSE file. 15 | 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software is furnished to do so. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 10 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 11 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 12 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 13 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 14 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 15 | 16 | -------------------------------------------------------------------------------- /SAM-repo/MyCustomRekognitionLambda/MyCustomRekognitionLambda.py: -------------------------------------------------------------------------------- 1 | import json 2 | import boto3 3 | import os 4 | import io 5 | import datetime 6 | from urllib.parse import unquote_plus 7 | 8 | 9 | def lambda_handler(event, context): 10 | model_arn=os.environ["CustomLabels_ModelArn"] 11 | MinimumConfidence = int(os.environ["Confidence_Threshold"]) 12 | DynamoTable = os.environ["DynamoDB_Table"] 13 | 14 | client_dynamodb=boto3.client('dynamodb') 15 | client_rekognition=boto3.client('rekognition') 16 | 17 | for record in event['Records']: 18 | bucket = record['s3']['bucket']['name'] 19 | key = unquote_plus(record['s3']['object']['key']) 20 | location = "s3://" + bucket + "/" + key 21 | 22 | #process using S3 object 23 | response_custom = client_rekognition.detect_custom_labels(Image={'S3Object': {'Bucket': bucket, 'Name': key}}, 24 | MinConfidence=MinimumConfidence,ProjectVersionArn=model_arn) 25 | 26 | 27 | #Get the custom labels 28 | labels_custom=response_custom['CustomLabels'] 29 | 30 | 31 | response = client_dynamodb.put_item(TableName=DynamoTable, 32 | Item={ 33 | 'ID': {'S':key}, 34 | 'TimeStamp': {'S': str(datetime.datetime.now())}, 35 | 'Inference': {'S': json.dumps(labels_custom)}, 36 | 'Location': {'S': location} 37 | } 38 | ) 39 | 40 | 41 | 42 | return { 43 | 'statusCode': 200, 44 | 'body': 'CustomLabels: '+ json.dumps(labels_custom) 45 | } -------------------------------------------------------------------------------- /SAM-repo/MyDynamodbLambda/MyDynamodbLambda.py: -------------------------------------------------------------------------------- 1 | import json 2 | import os 3 | import boto3 4 | from urllib.parse import urlparse 5 | 6 | env1 = os.environ['AlarmingLabelList'] 7 | AlarmingLabelList = env1.split(",") 8 | SNSTopic = os.environ['TopicArn'] 9 | Threshold = float(os.environ['Threshold'])/100. 10 | 11 | 12 | def create_presigned_url(location, expiration=3600): 13 | """Generate a presigned URL to share an S3 object 14 | 15 | :param bucket_name: string 16 | :param object_name: string 17 | :param expiration: Time in seconds for the presigned URL to remain valid 18 | :return: Presigned URL as string. If error, returns None. 19 | """ 20 | lnk = urlparse(location, allow_fragments=True) 21 | bucket_name = lnk.netloc 22 | object_name = lnk.path.lstrip('/') 23 | 24 | # Generate a presigned URL for the S3 object 25 | s3_client = boto3.client('s3') 26 | response = s3_client.generate_presigned_url('get_object', 27 | Params={'Bucket': bucket_name, 28 | 'Key': object_name}, 29 | ExpiresIn=expiration) 30 | 31 | # The response contains the presigned URL 32 | return response 33 | 34 | def lambda_handler(event, context): 35 | 36 | for record in event['Records']: 37 | InferenceOutput = json.loads(record['dynamodb']['NewImage']['Inference']['S']) 38 | loc = record['dynamodb']['NewImage']['Location']['S'] 39 | link = create_presigned_url(loc) 40 | inf = 0 41 | message = '' 42 | for inference in InferenceOutput: 43 | inf += 1 44 | if inference['Name'] in AlarmingLabelList and inference['Confidence'] > Threshold: 45 | alarm = 'ALARM! :' + inference['Name'] + ' (Confidence: {})'.format(inference['Confidence']) 46 | message = message + '\n' + alarm 47 | if message: 48 | message = message + '\n' + 'Please see the photo: ' + link 49 | 50 | client = boto3.client('sns') 51 | response = client.publish( 52 | TopicArn=SNSTopic, 53 | Message= message, 54 | Subject= 'A Possible issue detected at Turbine', 55 | MessageStructure='string' 56 | ) 57 | 58 | return response -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing Guidelines 2 | 3 | Thank you for your interest in contributing to our project. Whether it's a bug report, new feature, correction, or additional 4 | documentation, we greatly value feedback and contributions from our community. 5 | 6 | Please read through this document before submitting any issues or pull requests to ensure we have all the necessary 7 | information to effectively respond to your bug report or contribution. 8 | 9 | 10 | ## Reporting Bugs/Feature Requests 11 | 12 | We welcome you to use the GitHub issue tracker to report bugs or suggest features. 13 | 14 | When filing an issue, please check existing open, or recently closed, issues to make sure somebody else hasn't already 15 | reported the issue. Please try to include as much information as you can. Details like these are incredibly useful: 16 | 17 | * A reproducible test case or series of steps 18 | * The version of our code being used 19 | * Any modifications you've made relevant to the bug 20 | * Anything unusual about your environment or deployment 21 | 22 | 23 | ## Contributing via Pull Requests 24 | Contributions via pull requests are much appreciated. Before sending us a pull request, please ensure that: 25 | 26 | 1. You are working against the latest source on the *main* branch. 27 | 2. You check existing open, and recently merged, pull requests to make sure someone else hasn't addressed the problem already. 28 | 3. You open an issue to discuss any significant work - we would hate for your time to be wasted. 29 | 30 | To send us a pull request, please: 31 | 32 | 1. Fork the repository. 33 | 2. Modify the source; please focus on the specific change you are contributing. If you also reformat all the code, it will be hard for us to focus on your change. 34 | 3. Ensure local tests pass. 35 | 4. Commit to your fork using clear commit messages. 36 | 5. Send us a pull request, answering any default questions in the pull request interface. 37 | 6. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation. 38 | 39 | GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and 40 | [creating a pull request](https://help.github.com/articles/creating-a-pull-request/). 41 | 42 | 43 | ## Finding contributions to work on 44 | Looking at the existing issues is a great way to find something to contribute on. As our projects, by default, use the default GitHub issue labels (enhancement/bug/duplicate/help wanted/invalid/question/wontfix), looking at any 'help wanted' issues is a great place to start. 45 | 46 | 47 | ## Code of Conduct 48 | This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). 49 | For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact 50 | opensource-codeofconduct@amazon.com with any additional questions or comments. 51 | 52 | 53 | ## Security issue notifications 54 | If you discover a potential security issue in this project we ask that you notify AWS/Amazon Security via our [vulnerability reporting page](http://aws.amazon.com/security/vulnerability-reporting/). Please do **not** create a public github issue. 55 | 56 | 57 | ## Licensing 58 | 59 | See the [LICENSE](LICENSE) file for our project's licensing. We will ask you to confirm the licensing of your contribution. 60 | -------------------------------------------------------------------------------- /SAM-repo/template.yaml: -------------------------------------------------------------------------------- 1 | AWSTemplateFormatVersion: '2010-09-09' 2 | Transform: AWS::Serverless-2016-10-31 3 | Description: The serverless services deployment for Amazon Rekognitin Custom Labels for wind turbines blog post 4 | Parameters: 5 | MyDynamoDBTable: 6 | Description: The Amazon DynamoDB table to be created for storing inference results 7 | Type: String 8 | Default: MyRekognitioNResultTable 9 | InputBucketNamePrefix: 10 | Description: The S3 bucket to continously receive turbine photos from drones 11 | Type: String 12 | Default: windturbine-pictures 13 | CustomLabelsEndpoint: 14 | Description: The ARN of the deployed Amazon Rekognition Custom Labels 15 | Type: String 16 | Default: arn:aws:rekognition:region-xxx-xx/xx/xx 17 | MyEmailAdress: 18 | Description: Your email adress to receive notifications 19 | Type: String 20 | Default: email@example.com 21 | MyLabelListToBeNotified: 22 | Description: Any set of issues (wear, icing, corrosion) separated by comma 23 | Type: String 24 | Default: "icing,wear,corrosion" 25 | ThresholdForNotification: 26 | Description: The percentage of threshold to get notification (enter 75 if 75%) 27 | Type: Number 28 | Default: 75 29 | Resources: 30 | InputBucket: 31 | Type: AWS::S3::Bucket 32 | Properties: 33 | BucketName: !Sub ${InputBucketNamePrefix}-get-photo 34 | PublicAccessBlockConfiguration: 35 | BlockPublicAcls: True 36 | BlockPublicPolicy: True 37 | IgnorePublicAcls: True 38 | RestrictPublicBuckets: True 39 | MyCustomRekognitionLambda: 40 | Type: AWS::Serverless::Function 41 | Properties: 42 | Handler: MyCustomRekognitionLambda.lambda_handler 43 | Runtime: python3.8 44 | CodeUri: ./MyCustomRekognitionLambda 45 | Description: An S3 Create Object that triggers this Lambda Function 46 | MemorySize: 128 47 | Timeout: 3 48 | Policies: 49 | - Statement: 50 | - Sid: MyCustomLabelsLabelsPolicy1 51 | Effect: Allow 52 | Action: 53 | - rekognition:CreateProject 54 | - rekognition:CreateProjectVersion 55 | - rekognition:StartProjectVersion 56 | - rekognition:StopProjectVersion 57 | - rekognition:DescribeProjects 58 | - rekognition:DescribeProjectVersions 59 | - rekognition:DetectCustomLabels 60 | - rekognition:DeleteProject 61 | - rekognition:DeleteProjectVersion 62 | Resource: !Ref CustomLabelsEndpoint 63 | - Sid: MyCustomLabelsLabelsPolicy2 64 | Effect: Allow 65 | Action: 66 | - s3:List* 67 | - s3:Get* 68 | Resource: "*" 69 | - DynamoDBWritePolicy: 70 | TableName: !Ref MyDynamoDBTable 71 | Environment: 72 | Variables: 73 | Confidence_Threshold: 0 74 | CustomLabels_ModelArn: 75 | Ref: CustomLabelsEndpoint 76 | DynamoDB_Table: 77 | Ref: MyDynamoDBTable 78 | Events: 79 | BucketEvent: 80 | Type: S3 81 | Properties: 82 | Bucket: !Ref InputBucket 83 | Events: 's3:ObjectCreated:*' 84 | MyDynamodbLambda: 85 | Type: AWS::Serverless::Function 86 | Properties: 87 | Handler: MyDynamodbLambda.lambda_handler 88 | Runtime: python3.8 89 | CodeUri: ./MyDynamodbLambda 90 | Description: An Dynamodb Create Object that triggers this Lambda Function 91 | MemorySize: 128 92 | Timeout: 3 93 | Policies: 94 | - DynamoDBReadPolicy: 95 | TableName: !Ref MyDynamoDBTable 96 | - SNSCrudPolicy: 97 | TopicName: !GetAtt MySNSTopic.TopicName 98 | - Statement: 99 | - Sid: MyDynamodLambdaS3PresignPolicy 100 | Effect: Allow 101 | Action: 102 | - s3:Get* 103 | - S3:Create* 104 | Resource: 105 | - !GetAtt InputBucket.Arn 106 | - !Join [ "/", [ !GetAtt InputBucket.Arn, '*'] ] 107 | Environment: 108 | Variables: 109 | AlarmingLabelList: !Ref MyLabelListToBeNotified 110 | TopicArn: !Ref MySNSTopic 111 | Threshold: !Ref ThresholdForNotification 112 | Events: 113 | TableEvent: 114 | Type: DynamoDB 115 | Properties: 116 | Stream: !GetAtt OutputTable.StreamArn 117 | StartingPosition: TRIM_HORIZON 118 | BatchSize: 1 119 | Enabled: true 120 | LambdaInvokePermission1: 121 | Type: 'AWS::Lambda::Permission' 122 | Properties: 123 | FunctionName: !GetAtt MyCustomRekognitionLambda.Arn 124 | Action: 'lambda:InvokeFunction' 125 | Principal: 's3.amazonaws.com' 126 | SourceAccount: !Sub ${AWS::AccountId} 127 | SourceArn: !GetAtt InputBucket.Arn 128 | LambdaInvokePermission2: 129 | Type: 'AWS::Lambda::Permission' 130 | Properties: 131 | FunctionName: !GetAtt MyDynamodbLambda.Arn 132 | Action: 'lambda:InvokeFunction' 133 | Principal: 'dynamodb.amazonaws.com' 134 | SourceAccount: !Sub ${AWS::AccountId} 135 | SourceArn: !GetAtt OutputTable.Arn 136 | OutputTable: 137 | Type: AWS::DynamoDB::Table 138 | Properties: 139 | TableName: 140 | Ref: MyDynamoDBTable 141 | AttributeDefinitions: 142 | - AttributeName: ID 143 | AttributeType: S 144 | - AttributeName: TimeStamp 145 | AttributeType: S 146 | KeySchema: 147 | - AttributeName: ID 148 | KeyType: HASH 149 | - AttributeName: TimeStamp 150 | KeyType: RANGE 151 | ProvisionedThroughput: 152 | ReadCapacityUnits: 5 153 | WriteCapacityUnits: 5 154 | StreamSpecification: 155 | StreamViewType: NEW_IMAGE 156 | MySubscription: 157 | Type: AWS::SNS::Subscription 158 | Properties: 159 | Endpoint: !Ref MyEmailAdress 160 | Protocol: email 161 | TopicArn: !Ref MySNSTopic 162 | MySNSTopic: 163 | Type : AWS::SNS::Topic --------------------------------------------------------------------------------