Templates/Blueprints/DetectLabels/detectlabels.ps1.txt

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
# PowerShell script file to be executed as a AWS Lambda function.
#
# When executing in Lambda the following variables will be predefined.
# $LambdaInput - A PSObject that contains the Lambda function input data.
# $LambdaContext - An Amazon.Lambda.Core.ILambdaContext object that contains information about the currently running Lambda environment.
#
# The last item in the PowerShell pipeline will be returned as the result of the Lambda function.
#
# To include PowerShell modules with your Lambda function, like the AWSPowerShell.NetCore module, add a "#Requires" statement
# indicating the module and version.

#Requires -Modules @{ModuleName='AWSPowerShell.NetCore';ModuleVersion='3.3.335.0'}

# Uncomment to send the input event to CloudWatch Logs
# Write-Host (ConvertTo-Json -InputObject $LambdaInput -Compress -Depth 5)

$imageExtensions = @(".jpg",".jpeg",".png",".gif")

foreach ($record in $LambdaInput.Records) {
    $bucket = $record.s3.bucket.name
    $key = $record.s3.object.key

    Write-Host "Processing event for: bucket = $bucket, key = $key"

    $ext = [System.IO.Path]::GetExtension($key)
    if ($imageExtensions.Contains($ext)) {

        $confidence = $env:MinConfidence
        if ($confidence) {
            Write-Host "MinConfidence environment variable set to $confidence%"
        } else {
            Write-Host "MinConfidence environment variable not set, defaulting to 70%"
            $confidence = 70
        }

        Write-Host "Inspecting for labels with confidence equal or higher than $confidence%"
        $labels = (Find-REKLabel -ImageBucket $bucket -ImageName $key -MinConfidence $confidence).Labels  | Select -First 10
        Write-Host 'Labels Found ' $labels.Count

        Write-Host "Posting up to 10 labels to the object as tags"

        $tags = @()
        $labels | ForEach-Object {

            Write-Host $_.Name '('$_.Confidence')'

            $tag = New-Object Amazon.S3.Model.Tag
            $tag.Key = $_.Name
            $tag.Value = $_.Confidence.ToString()
            $tags += $tag
        }

        Write-S3ObjectTagSet -BucketName $bucket -Key $key -Tagging_TagSet $tags
    } else {
        Write-Host "Skipped processing: object does not match a known image extension ($imageExtensions -join ',')"
    }
}