0

I was making an Android Studio app and I need help integrating Clarifai in it. I started by setting up the client as per the Quick Start-Up Guide on Clarifai website. My code looks something like this:

 public static void main(String[] arg) {

    new ClarifaiBuilder("c719daa395fe42f2a1385ace59592496").buildSync();

    ClarifaiClient client = new ClarifaiBuilder("c719daa395fe42f2a1385ace59592496")
            .client(new OkHttpClient()) // OPTIONAL. Allows customization of OkHttp by the user
            .buildSync();

    client.getDefaultModels().generalModel().predict()
            .withInputs(ClarifaiInput.forImage("https://samples.clarifai.com/metro-north.jpg"))
            .executeSync();

}

From what I understand it is taking the sample image metro-north and predicting what it is. What I want to know is how you could write/print the top result (or the list of results either works) on a screen in your app so it is visible.

If someone could help me with this problem I would greatly appreciate it :D

2 Answers2

0

You can print the top result with the following code:

client.getDefaultModels().generalModel().predict()
  .withInputs(ClarifaiInput.forImage("https://samples.clarifai.com/metro-north.jpg"))
            .executeAsync(
                outputs -> System.out.println("First output of this prediction is " + outputs.get(0))
            ),
        code -> System.err.println("Error code: " + code + ". Error msg: " + message),
        e -> { throw new ClarifaiException(e); }
    );
Jared
  • 111
  • 1
  • 8
-1

Essentially you can do this :

//Get the results from the api:

 new AsyncTask<Void, Void, ClarifaiResponse<List<ClarifaiOutput<Concept>>>>() {
      @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();
      }

      @Override protected void onPostExecute(ClarifaiResponse<List<ClarifaiOutput<Concept>>> response) {
        setBusy(false);
        if (!response.isSuccessful()) {
          showErrorSnackbar(R.string.error_while_contacting_api);
          return;
        }
        final List<ClarifaiOutput<Concept>> predictions = response.get();
        if (predictions.isEmpty()) {
          showErrorSnackbar(R.string.no_results_from_api);
          return;
        }else{

            //Do something with the results or get the first element.
        }

      }

Taken from this project

Dadep
  • 2,796
  • 5
  • 27
  • 40