@@ -0,0 +1,3034 @@
+%0Afrom __future__ import print_function%0Aimport boto3%0Aimport json%0Aimport urllib%0Afrom wordcloud import WordCloud%0Aimport matplotlib.pyplot as plt%0A%0Arekognition = boto3.client('rekognition')%0A%0A%0Adef detect_labels(bucket, key):%0A    response = rekognition.detect_labels(Image=%7B%22S3Object%22: %7B%22Bucket%22: bucket, %22Name%22: key%7D%7D)%0A    return response%0A%0A%0Adef create_wordcloud(payload, key):%0A    keywords = ''%0A    json_obj = json.loads(payload)%0A%0A    for i in json_obj%5B'Labels'%5D:%0A        for key, value in i.iteritems():%0A            if key == %22Name%22:%0A                print(value)%0A                keywords += str(value) + ' '%0A%0A    # Generate a word cloud image%0A    wc = WordCloud().generate(keywords)%0A%0A    # Display the generated image:%0A    # the matplotlib way:%0A    plt.imshow(wc, interpolation='bilinear')%0A    keysplit = key.split('.')%0A    filename = keysplit%5B0%5D + '.png'%0A    wc.to_file('/tmp/' + filename)%0A%0A    return filename%0A%0A%0Adef push_to_sqs(body, bucket, key):%0A    s3 = boto3.client('s3')%0A    url = s3.generate_presigned_url(%0A        ClientMethod='get_object',%0A        Params=%7B%0A            'Bucket': bucket,%0A            'Key': key%0A        %7D%0A    )%0A    json_dump = json.dumps(body)  # SQS MessageBody expects string, not dict%0A    sqs = boto3.client('sqs')%0A    try:%0A        response = sqs.send_message(%0A            QueueUrl='https://sqs.us-east-1.amazonaws.com/831555608520/MetaMuleQueue',%0A            DelaySeconds=1,%0A            MessageBody=json_dump,%0A            MessageAttributes=%7B%0A                'bucket': %7B%0A                    'DataType': 'String',%0A                    'StringValue': bucket%0A                %7D,%0A                'key': %7B%0A                    'DataType': 'String',%0A                    'StringValue': key%0A                %7D,%0A                'url': %7B%0A                    'DataType': 'String',%0A                    'StringValue': url%0A                %7D%0A            %7D%0A        )%0A    except Exception as e:%0A        print(e)%0A        raise e%0A    print(response)%0A%0A%0Adef lambda_handler(event, context):%0A    # Demonstrates S3 trigger that uses Rekognition APIs to detect faces, labels and index faces in S3 Object.%0A    print(%22Received event: %22 + json.dumps(event, indent=2))%0A%0A    # Get the object from the event%0A    bucket = event%5B'Records'%5D%5B0%5D%5B's3'%5D%5B'bucket'%5D%5B'name'%5D%0A    key = urllib.unquote_plus(event%5B'Records'%5D%5B0%5D%5B's3'%5D%5B'object'%5D%5B'key'%5D.encode('utf8'))%0A%0A    try:%0A        # Calls rekognition DetectLabels API to detect labels in S3 object%0A        response = detect_labels(bucket, key)%0A        # Print response to console.%0A        print(response)%0A        worldcloud_key = create_wordcloud(response, key)%0A        s3_wc = boto3.client('s3')%0A        s3_wc.uploadfile('/tmp/' + worldcloud_key, 'ehog-wordclouds', worldcloud_key)%0A        push_to_sqs(response, bucket, key)%0A%0A        return response%0A    except Exception as e:%0A        print(e)%0A        print(%22Error processing object %7B%7D from bucket %7B%7D. %22.format(key, bucket) +%0A              %22Make sure your object and bucket exist and your bucket is in the same region as this function.%22)%0A        raise e%0A

Create getRekognition.py