Skip to content

Commit

Permalink
Updated Rekognition with Text Detection
Browse files Browse the repository at this point in the history
  • Loading branch information
brianklaas committed Aug 6, 2018
1 parent bc6f054 commit 55cae67
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
48 changes: 47 additions & 1 deletion model/rekognitionLib.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,53 @@ component output="false" hint="A utility for making requests to AWS Rekognition.
}

/**
* @description Returns a structure of labels for the image, with the label as the key and the confidence level as the value
* @description Returns an structure of both the lines of text found in the image and the individual words
* @requiredArguments
* - awsBucketName = Name of the bucket where the images reside
* - pathToImage = Path to the image in the provided bucket
*/
public struct function detectText(required string awsBucketName, required string pathToImage) {
var returnStruct = structNew();
var thisDetectionObj = 0;
var linesCounter = 0;
var wordsCounter = 0;
var detectTextRequest = CreateObject('java', 'com.amazonaws.services.rekognition.model.DetectTextRequest').init();
var imageToScan = CreateObject('java', 'com.amazonaws.services.rekognition.model.Image').init();
var imageS3Object = CreateObject('java', 'com.amazonaws.services.rekognition.model.S3Object').init();
imageS3Object.setBucket(arguments.awsBucketName);
imageS3Object.setName(arguments.pathToImage);
imageToScan.setS3Object(imageS3Object);
detectTextRequest.setImage(imageToScan);

var detectTextResult = variables.rekognitionService.detectText(detectTextRequest);
var detectionsArray = detectTextResult.getTextDetections();

returnStruct.lines = arrayNew(1);
returnStruct.words = arrayNew(1);

detectionsArray.each(function(thisDetectionObj, index) {
if (thisDetectionObj.getType() is "LINE") {
linesCounter++;
returnStruct.lines[linesCounter] = structNew();
returnStruct.lines[linesCounter]['label'] = thisDetectionObj.getDetectedText();
returnStruct.lines[linesCounter]['confidence'] = Int(thisDetectionObj.getConfidence());
returnStruct.lines[linesCounter]['id'] = thisDetectionObj.getID();
returnStruct.lines[linesCounter]['geometry'] = thisDetectionObj.getGeometry().toString();
} else {
wordsCounter++;
returnStruct.words[wordsCounter] = structNew();
returnStruct.words[wordsCounter]['label'] = thisDetectionObj.getDetectedText();
returnStruct.words[wordsCounter]['confidence'] = Int(thisDetectionObj.getConfidence());
returnStruct.words[wordsCounter]['id'] = thisDetectionObj.getID();
returnStruct.words[wordsCounter]['parentID'] = thisDetectionObj.getParentID();
returnStruct.words[wordsCounter]['geometry'] = thisDetectionObj.getGeometry().toString();
}
});
return returnStruct;
}

/**
* @description Returns an array of labels for the image as structures, with the label as the key and the confidence level as the value
* @requiredArguments
* - awsBucketName = Name of the bucket where the images reside
* - pathToImage = Path to the image in the provided bucket
Expand Down
34 changes: 32 additions & 2 deletions rekognition.cfm
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
<!--- Put the URLs to your images in S3 here --->
<cfset s3images = structNew()>
<cfset s3images.awsBucketName = "YOUR BUCKET NAME GOES HERE" />
<cfset s3images.facesForMatching = [ "ARRAY","OF","PATHS","TO","IMAGES","IN","THE","BUCKET","LISTED","ABOVE"] />
<cfset s3images.imagesForLabels = [ "ARRAY","OF","PATHS","TO","IMAGES","IN","THE","BUCKET","LISTED","ABOVE] />
<cfset s3images.facesForMatching = [ "ARRAY","OF","PATHS","TO","IMAGES","IN","THE","BUCKET","LISTED","ABOVE" ] />
<cfset s3images.imagesForLabels = [ "ARRAY","OF","PATHS","TO","IMAGES","IN","THE","BUCKET","LISTED","ABOVE" ] />
<cfset s3images.imagesWithText = [ "ARRAY","OF","PATHS","TO","IMAGES","IN","THE","BUCKET","LISTED","ABOVE" ] />

<cfif structKeyExists(URL, "rekogRequest")>
<cfscript>
Expand Down Expand Up @@ -32,6 +33,11 @@
detectSentimentResult = rekognitionLib.detectSentiment(s3images.awsBucketName, sourceImage);
break;
case 'detectText':
sourceImage = s3images.imagesWithText[randRange(1, arrayLen(s3images.imagesWithText))];
detectTextResult = rekognitionLib.detectText(s3images.awsBucketName, sourceImage);
break;
default:
throw(message="Unsupported service requested", detail="You have requested a method (#URL.rekogRequest#) which is not supported at this time.");
break;
Expand Down Expand Up @@ -108,12 +114,36 @@
<br clear="all">
</cfoutput>
</cfcase>
<cfcase value="detectText">
<cfoutput>
<div>
<div style="width:50%; float:left;">
<p>Here is the image used:</p>
<p>
<img src="http://#s3images.awsBucketName#.s3.amazonaws.com/#sourceImage#" width="450" height="350" border="1" />
</p>
</div>
<div style="width:50%; float:right;">
<p>Lines of text:</p>
<cfloop array="#detectTextResult.lines#" index="idxThisLine">
#idxThisLine.id# | #idxThisLine.label# | (#idxThisLine.confidence#%)<br/>
</cfloop>
<p>Individual words:</p>
<cfloop array="#detectTextResult.words#" index="idxThisWord">
Line: #idxThisWord.parentID# &mdash; #idxThisWord.label# (#idxThisWord.confidence#%)<br/>
</cfloop>
</div>
</div>
<br clear="all">
</cfoutput>
</cfcase>
</cfswitch>
</cfif> <!--- End if a Rekognition request was made --->

<p><a href="rekognition.cfm?rekogRequest=compareFaces">Compare Two Faces</a></p>
<p><a href="rekognition.cfm?rekogRequest=detectLabels">Label the Properties of an Image</a></p>
<p><a href="rekognition.cfm?rekogRequest=detectSentiment">Detect Facial Sentiment of an Image</a></p>
<p><a href="rekognition.cfm?rekogRequest=detectText">Detect Text in an Image</a></p>

<p align="right" ><a href="index.cfm" class="homeButton">Home</a></p>
</div>
Expand Down

0 comments on commit 55cae67

Please sign in to comment.