in packages/ekyc-api/src/ekyc-api/DocumentDefinitions/RekognitionDocumentChecker.cs [30:95]
public async Task<DocumentTypeCheckResponse> GetDocumentType(string s3Key)
{
AWSXRayRecorder.Instance.BeginSubsegment("RekognitionDocumentChecker:GetDocumentType");
Debug.WriteLine($"Getting document type for {s3Key}");
if (!string.IsNullOrEmpty(Globals.RekognitionCustomLabelsProjectArn) && !string.IsNullOrEmpty(Globals.RekognitionCustomLabelsProjectVersionArn))
{
// Can't check this document as there is no Rekognition Custom Labels project running
var projects = await _rekognition.DescribeProjectVersionsAsync(new DescribeProjectVersionsRequest()
{ ProjectArn = Globals.RekognitionCustomLabelsProjectArn });
var projectVersion = projects.ProjectVersionDescriptions.Where(a => a.Status == "RUNNING")
.OrderByDescending(a => a.CreationTimestamp).FirstOrDefault();
if (projectVersion==null)
return new DocumentTypeCheckResponse { Type = null, BoundingBox = null };
}
else
{
return new DocumentTypeCheckResponse { Type = null, BoundingBox = null };
}
var response = await _rekognition.DetectCustomLabelsAsync(new DetectCustomLabelsRequest
{
Image = new Image
{
S3Object = new S3Object
{
Bucket = Globals.StorageBucket,
Name = s3Key
}
},
MinConfidence = Convert.ToSingle(Globals.GetMinimumConfidence()),
ProjectVersionArn = Globals.RekognitionCustomLabelsProjectVersionArn
});
var uniqueLabels = response?.CustomLabels?
.OrderByDescending(a => a.Confidence)
.ThenByDescending(a => a.Geometry.BoundingBox.Height)
.ThenByDescending(a => a.Geometry.BoundingBox.Width)
.GroupBy(a => a.Name)
.Select(a => a.First())
.ToList();
if (uniqueLabels == null || uniqueLabels.Count == 0)
return new DocumentTypeCheckResponse { Type = null, BoundingBox = null };
foreach (var label in uniqueLabels)
{
DocumentTypes detectedLabelName;
if (Enum.TryParse(label.Name, true, out detectedLabelName))
{
var bb = BoundingBox.GetBoundingBoxFromRekognition(label.Geometry.BoundingBox);
return new DocumentTypeCheckResponse { Type = detectedLabelName, BoundingBox = bb };
;
}
}
AWSXRayRecorder.Instance.EndSubsegment();
// No label found
return new DocumentTypeCheckResponse { Type = null, BoundingBox = null };
}