Search in sources :

Example 1 with IdentifyEntitiesConfiguration

use of com.amplifyframework.predictions.aws.configuration.IdentifyEntitiesConfiguration in project amplify-android by aws-amplify.

the class AWSRekognitionService method recognizeCelebrities.

void recognizeCelebrities(@NonNull ByteBuffer imageData, @NonNull Consumer<IdentifyResult> onSuccess, @NonNull Consumer<PredictionsException> onError) {
    final IdentifyEntitiesConfiguration config;
    try {
        config = pluginConfiguration.getIdentifyEntitiesConfiguration();
        if (config.isCelebrityDetectionEnabled()) {
            List<CelebrityDetails> celebrities = detectCelebrities(imageData);
            onSuccess.accept(IdentifyCelebritiesResult.fromCelebrities(celebrities));
        } else {
            onError.accept(new PredictionsException("Celebrity detection is disabled.", "Please enable celebrity detection via Amplify CLI. This feature " + "should be accessible by running `amplify update predictions` " + "in the console and updating entities detection resource with " + "advanced configuration setting."));
        }
    } catch (PredictionsException exception) {
        onError.accept(exception);
    }
}
Also used : IdentifyEntitiesConfiguration(com.amplifyframework.predictions.aws.configuration.IdentifyEntitiesConfiguration) CelebrityDetails(com.amplifyframework.predictions.models.CelebrityDetails) PredictionsException(com.amplifyframework.predictions.PredictionsException)

Example 2 with IdentifyEntitiesConfiguration

use of com.amplifyframework.predictions.aws.configuration.IdentifyEntitiesConfiguration in project amplify-android by aws-amplify.

the class AWSPredictionsPluginConfigurationTest method testIdentifyEntityMatchesConfiguration.

/**
 * Test that configuration with explicit "identifyEntities" section creates
 * customized entities identification configuration instance for entity
 * match detection if max-entities and collection ID are both valid.
 * @throws Exception if configuration fails
 */
@Test
public void testIdentifyEntityMatchesConfiguration() throws Exception {
    JSONObject json = Resources.readAsJson("configuration-with-identify-entity-matches.json");
    AWSPredictionsPluginConfiguration pluginConfig = AWSPredictionsPluginConfiguration.fromJson(json);
    // Custom identify entities configuration with valid match detection settings
    IdentifyEntitiesConfiguration identifyConfig = pluginConfig.getIdentifyEntitiesConfiguration();
    assertNotNull(identifyConfig);
    assertFalse(identifyConfig.isCelebrityDetectionEnabled());
    assertEquals(NetworkPolicy.AUTO, identifyConfig.getNetworkPolicy());
    assertEquals(10, identifyConfig.getMaxEntities());
    assertEquals("some-collection-id", identifyConfig.getCollectionId());
    assertFalse(identifyConfig.isGeneralEntityDetection());
}
Also used : IdentifyEntitiesConfiguration(com.amplifyframework.predictions.aws.configuration.IdentifyEntitiesConfiguration) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Example 3 with IdentifyEntitiesConfiguration

use of com.amplifyframework.predictions.aws.configuration.IdentifyEntitiesConfiguration in project amplify-android by aws-amplify.

the class AWSRekognitionService method detectEntities.

void detectEntities(@NonNull ByteBuffer imageData, @NonNull Consumer<IdentifyResult> onSuccess, @NonNull Consumer<PredictionsException> onError) {
    final IdentifyEntitiesConfiguration config;
    try {
        config = pluginConfiguration.getIdentifyEntitiesConfiguration();
        if (config.isGeneralEntityDetection()) {
            List<EntityDetails> entities = detectEntities(imageData);
            onSuccess.accept(IdentifyEntitiesResult.fromEntityDetails(entities));
        } else {
            int maxEntities = config.getMaxEntities();
            String collectionId = config.getCollectionId();
            List<EntityMatch> matches = detectEntityMatches(imageData, maxEntities, collectionId);
            onSuccess.accept(IdentifyEntityMatchesResult.fromEntityMatches(matches));
        }
    } catch (PredictionsException exception) {
        onError.accept(exception);
    }
}
Also used : IdentifyEntitiesConfiguration(com.amplifyframework.predictions.aws.configuration.IdentifyEntitiesConfiguration) EntityDetails(com.amplifyframework.predictions.models.EntityDetails) EntityMatch(com.amplifyframework.predictions.models.EntityMatch) PredictionsException(com.amplifyframework.predictions.PredictionsException)

Example 4 with IdentifyEntitiesConfiguration

use of com.amplifyframework.predictions.aws.configuration.IdentifyEntitiesConfiguration in project amplify-android by aws-amplify.

the class AWSPredictionsPluginConfigurationTest method testIdentifyEntitiesConfiguration.

/**
 * Test that configuration with explicit "identifyEntities" section creates
 * customized entities identification configuration instance for general
 * entity detection if max-entities or collection ID is invalid.
 * @throws Exception if configuration fails
 */
@Test
public void testIdentifyEntitiesConfiguration() throws Exception {
    JSONObject json = Resources.readAsJson("configuration-with-identify-entities.json");
    AWSPredictionsPluginConfiguration pluginConfig = AWSPredictionsPluginConfiguration.fromJson(json);
    // Custom identify entities configuration with invalid match detection settings
    IdentifyEntitiesConfiguration identifyConfig = pluginConfig.getIdentifyEntitiesConfiguration();
    assertNotNull(identifyConfig);
    assertTrue(identifyConfig.isCelebrityDetectionEnabled());
    assertEquals(NetworkPolicy.AUTO, identifyConfig.getNetworkPolicy());
    assertEquals(0, identifyConfig.getMaxEntities());
    assertTrue(identifyConfig.getCollectionId().isEmpty());
    assertTrue(identifyConfig.isGeneralEntityDetection());
}
Also used : IdentifyEntitiesConfiguration(com.amplifyframework.predictions.aws.configuration.IdentifyEntitiesConfiguration) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Example 5 with IdentifyEntitiesConfiguration

use of com.amplifyframework.predictions.aws.configuration.IdentifyEntitiesConfiguration in project amplify-android by aws-amplify.

the class AWSPredictionsPluginConfiguration method fromJson.

/**
 * Constructs an instance of {@link AWSPredictionsPluginConfiguration} from
 * the plugin configuration JSON object.
 * @param configurationJson the plugin configuration
 * @return the configuration object for AWS Predictions Plugin
 * @throws PredictionsException if configuration is missing or malformed
 */
@NonNull
static AWSPredictionsPluginConfiguration fromJson(JSONObject configurationJson) throws PredictionsException {
    if (configurationJson == null) {
        throw new PredictionsException("Could not locate predictions configuration for AWS Predictions Plugin.", "Verify that amplifyconfiguration.json contains a section for \"awsPredictionsPlugin\".");
    }
    final Region defaultRegion;
    final SpeechGeneratorConfiguration speechGeneratorConfiguration;
    final TranslateTextConfiguration translateTextConfiguration;
    final IdentifyLabelsConfiguration identifyLabelsConfiguration;
    final IdentifyEntitiesConfiguration identifyEntitiesConfiguration;
    final IdentifyTextConfiguration identifyTextConfiguration;
    final InterpretTextConfiguration interpretConfiguration;
    try {
        // Get default region
        String regionString = configurationJson.getString(ConfigKey.DEFAULT_REGION.key());
        defaultRegion = Region.getRegion(regionString);
        if (configurationJson.has(ConfigKey.CONVERT.key())) {
            JSONObject convertJson = configurationJson.getJSONObject(ConfigKey.CONVERT.key());
            speechGeneratorConfiguration = SpeechGeneratorConfiguration.fromJson(convertJson);
            translateTextConfiguration = TranslateTextConfiguration.fromJson(convertJson);
        } else {
            speechGeneratorConfiguration = null;
            translateTextConfiguration = null;
        }
        if (configurationJson.has(ConfigKey.IDENTIFY.key())) {
            JSONObject identifyJson = configurationJson.getJSONObject(ConfigKey.IDENTIFY.key());
            identifyLabelsConfiguration = IdentifyLabelsConfiguration.fromJson(identifyJson);
            identifyEntitiesConfiguration = IdentifyEntitiesConfiguration.fromJson(identifyJson);
            identifyTextConfiguration = IdentifyTextConfiguration.fromJson(identifyJson);
        } else {
            identifyLabelsConfiguration = null;
            identifyEntitiesConfiguration = null;
            identifyTextConfiguration = null;
        }
        if (configurationJson.has(ConfigKey.INTERPRET.key())) {
            JSONObject interpretJson = configurationJson.getJSONObject(ConfigKey.INTERPRET.key());
            interpretConfiguration = InterpretTextConfiguration.fromJson(interpretJson);
        } else {
            interpretConfiguration = null;
        }
    } catch (JSONException | IllegalArgumentException exception) {
        throw new PredictionsException("Issue encountered while parsing configuration JSON", exception, "Check the attached exception for more details.");
    }
    return new AWSPredictionsPluginConfiguration(defaultRegion, speechGeneratorConfiguration, translateTextConfiguration, identifyLabelsConfiguration, identifyEntitiesConfiguration, identifyTextConfiguration, interpretConfiguration);
}
Also used : IdentifyEntitiesConfiguration(com.amplifyframework.predictions.aws.configuration.IdentifyEntitiesConfiguration) SpeechGeneratorConfiguration(com.amplifyframework.predictions.aws.configuration.SpeechGeneratorConfiguration) TranslateTextConfiguration(com.amplifyframework.predictions.aws.configuration.TranslateTextConfiguration) JSONException(org.json.JSONException) InterpretTextConfiguration(com.amplifyframework.predictions.aws.configuration.InterpretTextConfiguration) JSONObject(org.json.JSONObject) IdentifyLabelsConfiguration(com.amplifyframework.predictions.aws.configuration.IdentifyLabelsConfiguration) IdentifyTextConfiguration(com.amplifyframework.predictions.aws.configuration.IdentifyTextConfiguration) Region(com.amazonaws.regions.Region) PredictionsException(com.amplifyframework.predictions.PredictionsException) NonNull(androidx.annotation.NonNull)

Aggregations

IdentifyEntitiesConfiguration (com.amplifyframework.predictions.aws.configuration.IdentifyEntitiesConfiguration)5 PredictionsException (com.amplifyframework.predictions.PredictionsException)3 JSONObject (org.json.JSONObject)3 Test (org.junit.Test)2 NonNull (androidx.annotation.NonNull)1 Region (com.amazonaws.regions.Region)1 IdentifyLabelsConfiguration (com.amplifyframework.predictions.aws.configuration.IdentifyLabelsConfiguration)1 IdentifyTextConfiguration (com.amplifyframework.predictions.aws.configuration.IdentifyTextConfiguration)1 InterpretTextConfiguration (com.amplifyframework.predictions.aws.configuration.InterpretTextConfiguration)1 SpeechGeneratorConfiguration (com.amplifyframework.predictions.aws.configuration.SpeechGeneratorConfiguration)1 TranslateTextConfiguration (com.amplifyframework.predictions.aws.configuration.TranslateTextConfiguration)1 CelebrityDetails (com.amplifyframework.predictions.models.CelebrityDetails)1 EntityDetails (com.amplifyframework.predictions.models.EntityDetails)1 EntityMatch (com.amplifyframework.predictions.models.EntityMatch)1 JSONException (org.json.JSONException)1