Skip to content

added rekognition customlabels feature to amplify predictions category #7328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import {
IdentifyTextOutput,
IdentifyLabelsInput,
IdentifyLabelsOutput,
IdentifyCustomLabelsInput,
IdentifyCustomLabelsOutput,
} from '../../src/types';
import { BlockList } from '../../src/types/AWSTypes';
import { AmazonAIIdentifyPredictionsProvider } from '../../src/Providers';
import {
RekognitionClient,
DetectLabelsCommandOutput,
DetectLabelsCommand,
DetectCustomLabelsCommandOutput,
DetectCustomLabelsCommand,
DetectModerationLabelsCommandOutput,
DetectModerationLabelsCommand,
DetectFacesCommandOutput,
Expand Down Expand Up @@ -260,6 +264,13 @@ const options = {
type: 'LABELS',
},
},
identifyCustomLabels: {
proxy: false,
region: 'us-west-2',
defaults: {
type: 'LABELS',
},
},
};

describe('Predictions identify provider test', () => {
Expand Down Expand Up @@ -448,6 +459,48 @@ describe('Predictions identify provider test', () => {
});
});
});
describe('identifyCustomLabels tests', () => {
describe('identifyCustomlabels tests', () => {
const detectCustomLabelInput: IdentifyCustomLabelsInput = {
customlabels: {
source: { key: 'key' },
projectVersionArn: 'arn://',
type: 'LABELS',
},
};
test('happy case credentials exist', () => {
const expected: IdentifyCustomLabelsOutput = {
customlabels: [
{
name: 'test',
metadata: [{ confidence: 100 }],
},
],
};
return expect(
predictionsProvider.identify(detectCustomLabelInput)
).resolves.toMatchObject(expected);
});
test('error case credentials do not exist', () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
return null;
});
return expect(
predictionsProvider.identify(detectCustomLabelInput)
).rejects.toMatch('No credentials');
});
test('error case credentials exist but service fails', () => {
jest
.spyOn(RekognitionClient.prototype, 'send')
.mockImplementationOnce(() => {
return Promise.reject('error');
});
return expect(
predictionsProvider.identify(detectCustomLabelInput)
).rejects.toMatch('error');
});
});
});
describe('identifyFaces tests', () => {
describe('identifyEntities::detctFaces tests', () => {
const detectFacesInput: IdentifyEntitiesInput = {
Expand Down Expand Up @@ -682,4 +735,167 @@ describe('Predictions identify provider test', () => {
).rejects.toMatch('not configured correctly');
});
});

describe('identify input source transformation tests for custom labels', () => {
const detectcustomlabelsResponse: DetectCustomLabelsCommandOutput = {
CustomLabels: [
{
Confidence: 100,

Name: 'test',

Geometry: {},
},
],

$metadata: null,
};

test('happy case input source valid public s3object', () => {
const detectCustomLabelInput: IdentifyCustomLabelsInput = {
customlabels: {
source: { level: 'public', key: 'key' },
projectVersionArn: 'arn://',
type: 'LABELS',
},
};
jest
.spyOn(RekognitionClient.prototype, 'send')
.mockImplementationOnce(command => {
expect(
(command as DetectCustomLabelsCommand).input.Image.S3Object.Name
).toMatch('public/key');
return Promise.resolve(detectcustomlabelsResponse);
});
predictionsProvider.identify(detectCustomLabelInput);
});

test('happy case input source valid private s3object', async () => {
const detectCustomLabelInput: IdentifyCustomLabelsInput = {
customlabels: {
source: { key: 'key', level: 'private' },
projectVersionArn: 'arn://',
type: 'LABELS',
},
};
jest
.spyOn(RekognitionClient.prototype, 'send')
.mockImplementationOnce(command => {
expect(
(command as DetectCustomLabelsCommand).input.Image.S3Object.Name
).toMatch('private/identityId/key');
return {};
});
await predictionsProvider.identify(detectCustomLabelInput);
});

test('happy case input source valid protected s3object', () => {
const detectCustomLabelInput: IdentifyCustomLabelsInput = {
customlabels: {
source: {
key: 'key',
identityId: credentials.identityId,
level: 'protected',
},
projectVersionArn: 'arn://',
type: 'LABELS',
},
};
jest
.spyOn(RekognitionClient.prototype, 'send')
.mockImplementationOnce(command => {
expect(
(command as DetectCustomLabelsCommand).input.Image.S3Object.Name
).toMatch('protected/identityId/key');
return Promise.resolve(detectcustomlabelsResponse);
});
predictionsProvider.identify(detectCustomLabelInput);
});

test('happy case input source valid bytes', () => {
const detectCustomLabelInput: IdentifyCustomLabelsInput = {
customlabels: {
source: { bytes: 'bytes' },
projectVersionArn: 'arn://',
type: 'LABELS',
},
};
jest
.spyOn(RekognitionClient.prototype, 'send')
.mockImplementationOnce(command => {
expect(
(command as DetectCustomLabelsCommand).input.Image.Bytes
).toMatch('bytes');
return Promise.resolve(detectcustomlabelsResponse);
});
predictionsProvider.identify(detectCustomLabelInput);
});

test('happy case input source valid bytes', async () => {
const fileInput = new Blob([Buffer.from('file')]);
const detectCustomLabelInput: IdentifyCustomLabelsInput = {
customlabels: {
source: { bytes: fileInput },
projectVersionArn: 'arn://',
type: 'LABELS',
},
};
jest
.spyOn(RekognitionClient.prototype, 'send')
.mockImplementationOnce(command => {
expect(
(command as DetectCustomLabelsCommand).input.Image.Bytes
).toMatchObject(fileInput);
return {};
});
await predictionsProvider.identify(detectCustomLabelInput);
});

// Failing test in CircleCI TODO fix
test.skip('happy case input source valid file', async () => {
const fileInput = new File([Buffer.from('file')], 'file');
const detectCustomLabelInput: IdentifyCustomLabelsInput = {
customlabels: {
source: { file: fileInput },
projectVersionArn: 'arn://',
type: 'LABELS',
},
};
jest
.spyOn(RekognitionClient.prototype, 'send')
.mockImplementationOnce(command => {
expect(
(command as DetectCustomLabelsCommand).input.Image.Bytes
).toMatchObject(fileInput);
return {};
});
await predictionsProvider.identify(detectCustomLabelInput);
});

test('error case invalid input source', () => {
const detectCustomLabelInput: IdentifyCustomLabelsInput = {
customlabels: {
source: null,
projectVersionArn: 'arn://',
type: 'LABELS',
},
};
return expect(
predictionsProvider.identify(detectCustomLabelInput)
).rejects.toMatch('not configured correctly');
});
test('error case ARN invalid', async () => {
const fileInput = new Blob([Buffer.from('file')]);
const detectCustomLabelInput: IdentifyCustomLabelsInput = {
customlabels: {
source: { bytes: fileInput },
projectVersionArn: null,
type: 'LABELS',
},
};
return expect(
predictionsProvider.identify(detectCustomLabelInput)
).rejects.toMatch('projectVersionArn not configured correctly');
});
});
});
17 changes: 15 additions & 2 deletions packages/predictions/src/Predictions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
IdentifyTextOutput,
IdentifyLabelsOutput,
IdentifyLabelsInput,
IdentifyCustomLabelsOutput,
IdentifyCustomLabelsInput,
IdentifyEntitiesInput,
IdentifyEntitiesOutput,
InterpretTextOutput,
Expand Down Expand Up @@ -168,15 +170,26 @@ export class PredictionsClass {
input: IdentifyLabelsInput,
options?: ProviderOptions
): Promise<IdentifyLabelsOutput>;
public identify(
input: IdentifyCustomLabelsInput,
options?: ProviderOptions
): Promise<IdentifyCustomLabelsOutput>;
public identify(
input: IdentifyEntitiesInput,
options?: ProviderOptions
): Promise<IdentifyEntitiesOutput>;
public identify(
input: IdentifyTextInput | IdentifyLabelsInput | IdentifyEntitiesInput,
input:
| IdentifyTextInput
| IdentifyLabelsInput
| IdentifyCustomLabelsInput
| IdentifyEntitiesInput,
options: ProviderOptions
): Promise<
IdentifyTextOutput | IdentifyLabelsOutput | IdentifyEntitiesOutput
| IdentifyTextOutput
| IdentifyLabelsOutput
| IdentifyCustomLabelsOutput
| IdentifyEntitiesOutput
> {
const pluggableToExecute = this.getPluggableToExecute(
this._identifyPluggables,
Expand Down
Loading