@Override protected ClarifaiResponse<List<ClarifaiOutput<Concept>>> doInBackground(Void... params) { // The default Clarifai model that identifies concepts in images final ConceptModel generalModel = App.get().clarifaiClient().getDefaultModels().generalModel(); // Use this model to predict, with the image that the user just selected as the input return generalModel.predict() .withInputs(ClarifaiInput.forImage(ClarifaiImage.of(imageBytes))) .executeSync(); }
/** * If added, only these concepts will be considered in the prediction. * @param concepts the concepts * @return PredictRequest instance */ @NotNull public PredictRequest<PREDICTION> selectConcepts(@NotNull Concept... concepts) { return selectConcepts(Arrays.asList(concepts)); }
@Test public void shouldBeSuccessfulWhenSpecifyingModelVersionID() { ConceptModel model = client.getDefaultModels().generalModel(); // It's reasonably safe to have this hard-coded in a test as this is the general's model version ID which is // unlikely to be ever removed. String modelVersionID = "aa9ca48295b37401f8af92ad1af0d91d"; ClarifaiResponse<List<ClarifaiOutput<Prediction>>> response = client.predict(model.id()) .withInputs(ClarifaiInput.forImage(METRO_NORTH_IMAGE_URL)) .withVersion(modelVersionID) .executeSync(); Assert.assertTrue(response.isSuccessful()); }
@Test public void shouldReturnCorrectValuesWhenValid() { PredictRequest<Prediction> request = makePredictRequest() .withMaxConcepts(3); ClarifaiResponse<List<ClarifaiOutput<Prediction>>> predictionsResponse = request.executeSync(); List<Prediction> predictions = predictionsResponse.get().get(0).data(); assertTrue(predictions.size() <= 3); }
@Test public void videoURLShouldBeSuccessfulWhenSpecifyingSampleMs() { VideoModel model = client.getDefaultModels().generalVideoModel(); ClarifaiResponse<List<ClarifaiOutput<Prediction>>> response = client.predict(model.id()) .withInputs(ClarifaiInput.forVideo(VIDEO_URL)) .withSampleMs(2000) .executeSync(); Assert.assertTrue(response.isSuccessful()); for (Prediction prediction : response.get().get(0).data()) { Frame frame = prediction.asFrame(); Assert.assertEquals(0, frame.time() % 2000); } }
@Test public void shouldReturnCorrectConceptsWhenSelectConcepts() { String catConceptId = "ai_mFqxrph2"; String dogConceptId = "ai_8S2Vq3cR"; PredictRequest<Prediction> request = client.predict(client.getDefaultModels().generalModel().id()) .withInputs( ClarifaiInput.forImage(CAT_IMAGE_URL)) .selectConcepts(Concept.forID(dogConceptId), Concept.forID(catConceptId)); ClarifaiResponse<List<ClarifaiOutput<Prediction>>> predictionsResponse = request.executeSync(); List<Prediction> predictions = predictionsResponse.get().get(0).data(); // Since we selected only 2 concepts, no more than 2 should be returned. assertTrue(predictions.size() <= 2); // Cat should be in the prediction response. assertTrue(predictions.stream().anyMatch(prediction -> prediction.asConcept().id().equals(catConceptId))); }
@NotNull public PredictRequest<PREDICTION> withInputs(@NotNull ClarifaiInput... inputData) { return withInputs(Arrays.asList(inputData)); }
@Retry @Test public void t16f_predictWithModel_multi_lang() { assertSuccess(client.predict(client.getDefaultModels().generalModel().id()) .withInputs(ClarifaiInput.forImage(KOTLIN_LOGO_IMAGE_FILE)) .withLanguage("zh") ); }
@Test public void shouldReturnErrorWhenMinValueOutOfRange() { PredictRequest<Prediction> request = makePredictRequest() .withMinValue(1.01); ClarifaiResponse<List<ClarifaiOutput<Prediction>>> predictionsResponse = request.executeSync(); assertFalse(predictionsResponse.isSuccessful()); assertEquals(21202, predictionsResponse.getStatus().statusCode()); assertEquals("Invalid model argument", predictionsResponse.getStatus().description()); assertEquals("'min_value' must be within the interval [0,1].", predictionsResponse.getStatus().errorDetails() ); }
@NotNull public final PredictRequest<PREDICTION> predict() { PredictRequest<Prediction> predict = client().predict(id()); if (_modelVersion() != null) { predict.withVersion(_modelVersion()); } return (PredictRequest<PREDICTION>) predict; }
@NotNull @Override public PredictRequest<Prediction> predict(@NotNull String modelID) { return new PredictRequest<>(this, modelID); }
return stub().postModelOutputs(requestBuilder.build());
@Test public void videoFileShouldBeSuccessfulWhenSpecifyingSampleMs() { VideoModel model = client.getDefaultModels().generalVideoModel(); ClarifaiResponse<List<ClarifaiOutput<Prediction>>> response = client.predict(model.id()) .withInputs(ClarifaiInput.forVideo(BEER_VIDEO_FILE)) .withSampleMs(2000) .executeSync(); Assert.assertTrue(response.isSuccessful()); for (Prediction prediction : response.get().get(0).data()) { Frame frame = prediction.asFrame(); Assert.assertEquals(0, frame.time() % 2000); } } }
@NotNull private PredictRequest<Prediction> makePredictRequest() { return client.predict(client.getDefaultModels().travelModel().id()) .withInputs( ClarifaiInput.forInputValue( ClarifaiImage.of(FAMILY_IMAGE_URL) .withAllowDuplicateUrl(false))); } }
@Test public void shouldReturnErrorWhenTooManyMaxConcepts() { PredictRequest<Prediction> request = makePredictRequest() .withMaxConcepts(201); ClarifaiResponse<List<ClarifaiOutput<Prediction>>> predictionsResponse = request.executeSync(); assertFalse(predictionsResponse.isSuccessful()); assertEquals(21202, predictionsResponse.getStatus().statusCode()); assertEquals("Invalid model argument", predictionsResponse.getStatus().description()); assertEquals("'max_concepts' must be within the interval (0, 200]", predictionsResponse.getStatus().errorDetails()); }
@Test public void predictWithTheModel() { client.predict("{{model_id}}") .withInputs( ClarifaiInput.forImage("@@samplePuppy") ) .executeSync(); }
@Retry @Test public void t16a_predictWithModel() { assertSuccess(client.predict(client.getDefaultModels().generalModel().id()) .withInputs(ClarifaiInput.forImage(KOTLIN_LOGO_IMAGE_FILE)) ); }
@Test public void predictViaImageBytes() { client.getDefaultModels().generalModel().predict() .withInputs( ClarifaiInput.forImage(new File("/home/user/image.jpeg")) ) .executeSync(); }
@Retry @Test public void t21_testApparelModel() { assertSuccess(client.predict(client.getDefaultModels().apparelModel().id()) .withInputs(ClarifaiInput.forImage(FAMILY_IMAGE_URL)) ); }
@Test public void quickStartPredict() { final ClarifaiResponse<List<ClarifaiOutput<Concept>>> predictionResults = client.getDefaultModels().generalModel() // You can also do client.getModelByID("id") to get custom models .predict() .withInputs( ClarifaiInput.forImage("@@sampleTrain") ) .executeSync(); }