/** * This is just a convenience function. * It's equivalent to {@code ClarifaiInput.forInputValue(ClarifaiImage.of(imageBytes))}. * @param imageBytes the image to represent * @return a {@link ClarifaiInput} that represents the given image */ @NotNull public static ClarifaiInput forImage(@NotNull byte[] imageBytes) { return ClarifaiInput.forInputValue(ClarifaiImage.of(imageBytes)); }
/** * This is just a convenience function. * It's equivalent to {@code ClarifaiInput.forInputValue(ClarifaiImage.of(imageURL))}. * @param imageURL the image to represent * @return a {@link ClarifaiInput} that represents the given image */ @NotNull public static ClarifaiInput forImage(@NotNull String imageURL) { return ClarifaiInput.forInputValue(ClarifaiImage.of(imageURL)); }
@NotNull public static ClarifaiURLImage of(@NotNull String imageURL) { final URL result; try { result = new URL(imageURL); } catch (MalformedURLException e) { throw new ClarifaiException("Could not parse URL " + imageURL, e); } return of(result); }
/** * This is just a convenience function. * It's equivalent to {@code ClarifaiInput.forInputValue(ClarifaiImage.of(imageFile))}. * @param imageFile the image to represent * @return a {@link ClarifaiInput} that represents the given image */ @NotNull public static ClarifaiInput forImage(@NotNull File imageFile) { return ClarifaiInput.forInputValue(ClarifaiImage.of(imageFile)); }
/** * This is just a convenience function. * It's equivalent to {@code ClarifaiInput.forInputValue(ClarifaiImage.of(imageURL))}. * @param imageURL the image to represent * @return a {@link ClarifaiInput} that represents the given image */ @NotNull public static ClarifaiInput forImage(@NotNull URL imageURL) { return ClarifaiInput.forInputValue(ClarifaiImage.of(imageURL)); }
@Nullable @Override public ClarifaiFileImage deserialize( @NotNull JsonElement json, @NotNull TypeToken<ClarifaiFileImage> type, @NotNull Gson gson) { final JsonObject root = InternalUtil.assertJsonIs(json, JsonObject.class); String base64 = root.get("base64").getAsString(); byte[] bytes = ByteString.decodeBase64(base64).toByteArray(); return ClarifaiImage.of(bytes) .withCrop(gson.fromJson(root.get("crop"), Crop.class)); } };
public static ClarifaiFileImage deserializeInner(ImageOuterClass.Image imageResponse) { ClarifaiFileImage image = ClarifaiImage.of(imageResponse.getBase64().toByteArray()); if (!imageResponse.getCropList().isEmpty()) { image.withCrop(Crop.deserialize(imageResponse.getCropList())); } return image; }
@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(); }
@Test public void searchByReverseImage() { // Search by image (image URL represented by: String or java.net.URL) client.searchInputs(SearchClause.matchImageVisually(ClarifaiImage.of("@@sampleTrain"))) .getPage(1) .executeSync(); // Search by image (image represented by: java.io.File or byte[]) client.searchInputs(SearchClause.matchImageVisually(ClarifaiImage.of(new File("image.png")))) .getPage(1) .executeSync(); }
@Retry @Test public void t17a_searchInputsWithModel() { assertSuccess(client.searchInputs( SearchClause.matchImageURL(ClarifaiImage.of(METRO_NORTH_IMAGE_URL)) )); }
@Test public void reverseImageSearch() { client.searchInputs(SearchClause.matchImageVisually(ClarifaiImage.of("http://i.imgur.com/HEoT5xR.png"))) .getPage(1) .executeSync(); }
@Test public void quickStartSearch() { final ClarifaiResponse<SearchInputsResult> trainImages = client.searchInputs( // Finds images that match this picture of a train OR images that match the "train" concept tag SearchClause.matchImageVisually(ClarifaiImage.of("@@sampleTrain")) ) .getPage(1) .executeSync(); }
@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 searchMatchURL() { // Lookup images with this URL client.searchInputs(SearchClause.matchImageURL(ClarifaiImage.of(""))) .getPage(1) .executeSync(); }
@Retry @Test public void t17d_searchInputsWithModel_multi_language() { assertSuccess(client.searchInputs( SearchClause.matchImageURL(ClarifaiImage.of(METRO_NORTH_IMAGE_URL))).withLanguage("zh")); }
@Retry @Test public void t17b_searchInputsWithModel_complexSearch() { assertSuccess( client.searchInputs(matchConcept(Concept.forID("outdoors23").withValue(true))) .and(SearchClause.matchImageURL(ClarifaiImage.of(METRO_NORTH_IMAGE_URL))) .build() ); }
@Test public void searchANDing() { client.searchInputs() .ands( SearchClause.matchUserTaggedConcept(Concept.forName("cat")), SearchClause.matchConcept(Concept.forName("dog").withValue(false)), SearchClause.matchImageVisually(ClarifaiImage.of("@@sampleTrain")) ) .getPage(1) .executeSync(); }
@Retry @Test public void t16b_predictWithModel_00() { assertSuccess(client.getDefaultModels().generalModel().predict() .withInputs(ClarifaiInput.forInputValue(ClarifaiImage.of(METRO_NORTH_IMAGE_URL) .withCrop(Crop.create() .top(0.1F) .bottom(0.8F) ) ))); }
@Test public void allDataShouldBeExposedAndNotNull() { SearchInputsRequest request = client.searchInputs(SearchClause.matchImageURL( ClarifaiImage.of(imageWithNoSearchHits))); ClarifaiResponse<SearchInputsResult> response = request.getPage(1).executeSync(); assertNotNull(response.get().id()); assertNotNull(response.getStatus()); assertNotNull(response.get().searchHits()); } }
@Test public void t17e_searchInputsWithModel_geo() { assertSuccess(client.addInputs().plus( ClarifaiInput.forImage(METRO_NORTH_IMAGE_URL) .withGeo(PointF.at(90F, 23F)) )); assertSuccess( client.searchInputs(matchConcept(Concept.forID("outdoors23").withValue(true))) .and(SearchClause.matchImageURL(ClarifaiImage.of(METRO_NORTH_IMAGE_URL))) .and(SearchClause.matchGeo(PointF.at(90F, 23F), Radius.of(5, Radius.Unit.MILE))) .build() ); }