Appendix C: Customizing the Solution - Media Analysis Solution

Appendix C: Customizing the Solution

The Media Analysis Solution includes a default set of features, but you can extend and customize the solution to include additional features and functionalities for your specific use case. Follow the step-by-step instructions in this section to add functionality that will automatically detect text in images uploaded to the solution.

Step 1. Update the State Machine

To coordinate text detection in concurrence with the existing image analysis, add another branch to the image analysis state machine. Follow the example below:

{ "StartAt": "Image-Text Params", "States": { "Image-Text Params": { "Type": "Pass", "Result": { "service_name": "image", "function_name": "get_text" }, "ResultPath": "$.lambda", "Next": "Image-Get Text" }, "Image-Get Text": { "Type": "Task", "Resource":"arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:name", "InputPath": "$", "ResultPath": "$.results.text", "End": true } } }

Step 2: Add Logic to AWS Lambda Code

When the MediaAnalysisFunction AWS Lambda function is invoked by the state machine, the function will parse the event, invoke the appropriate Amazon Rekognition API, process and store the response, and return the status of the job to the state machine. Update the MediaAnalysisFunction Lambda function.

In the source/analysis/lib/image/index.js file, add the following code.

else if (event.lambda.function_name == 'get_text') { _image.getText(event, function(err, data) { if (err) { return cb(err, null); } else { return cb(null, data); } }); }

In the source/analysis/lib/image/image.js file, add the following code.

image.prototype.getText = function(image_info, cb){ let rek_params = { Image: { S3Object: { Bucket: s3Bucket, Name: image_info.key } } }; let rekognition = new AWS.Rekognition(); rekognition.detectText(rek_params, function(err, data) { if (err) { return cb(err, null); } else { var text = []; for (var t in data.TextDetections) { if (data.TextDetections[t].Confidence >= confidence_score) { text.push(data.TextDetections[t].DetectedText); } } let text_key = ['private',image_info.owner_id,'media',image_info.object_id,'results','text.json'].join("/"); let s3_params = { Bucket: s3Bucket, Key: text_key, Body: JSON.stringify(data), ContentType: 'application/json' }; upload.respond(s3_params, function(err, response) { if (err){ return cb(err, null); } else { let text_response = {'key': text_key, 'text': text, 'status': "COMPLETE"}; return cb(null,text_response); } }); } }); };

Step 3. Update the AWS IAM Policy

The Media Analysis Solution uses AWS Identity and Access Management (IAM) policies to ensure least privilege access to AWS resources. By default, the solution does not allow the MediaAnalysisFunction AWS Lambda function to access the Amazon Rekognition DetectText API. If you want to access the API you must update the IAM policy to provide access. Add the following permission:

{ "Action": [ "rekognition:DetectText" ], "Resource": "*", "Effect": "Allow" }

You can quickly and easily add new functionality to the Media Analysis Solution, enabling you to extract even more valuable data from your media files. In addition to the above changes, you should consider updating the MediaAnalysisApiFunction Lambda function to enable API access to the metadata extracted from your media files.

Customers who want to write their own code using a different language or logic structure can easily coordinate those functions from the Media Analysis Solution state machine. You can also use the solution to coordinate analysis using their own machine learning models that they write or host by specifying an activity in the state machine's Task resource.