Clarifai/clarifai-java

Clarifai with offline images

PAMAZ opened this issue · 3 comments

PAMAZ commented

Hi!

I'm trying to take some tags from an image which is stored in my pc.

Everytime i'm trying to get the tags I get this exception:

Exception in thread "main" java.util.NoSuchElementException: This API call was not successful. Details about this error: ClarifaiStatus{networkErrorOccurred=false, statusCode=10020, description=Failure, errorDetails=null}

What's the problem with it? Thank you in advance!

public static List<String> recognize(String imageUrl) {

        // Defining List Object
        List<String> resultList = new ArrayList<String>();

        if(imageUrl != null) {

            final ClarifaiClient client = new ClarifaiBuilder("<apiKey>").buildSync();

            final List<ClarifaiOutput<Concept>> predictionResults =
                    client.getDefaultModels().generalModel() // You can also do client.getModelByID("id") to get custom models
                            .predict()
                            .withInputs(
                                    ClarifaiInput.forImage(ClarifaiImage.of(imageUrl))
                            )
                            .executeSync()
                            .get();

            if (predictionResults != null && predictionResults.size() > 0) {

                // Prediction List Iteration
                for (int i = 0; i < predictionResults.size(); i++) {

                    ClarifaiOutput<Concept> clarifaiOutput = predictionResults.get(i);

                    List<Concept> concepts = clarifaiOutput.data();

                    if(concepts != null && concepts.size() > 0) {
                        for (int j = 0; j < concepts.size(); j++) {

                            resultList.add(concepts.get(j).name());
                        }
                    }
                }
            }

        }
        return resultList;
    }

    public static void main(String[] args) {

        // You can change the Image URL accordingly.
        String imageUrl = "file:///home/paolo/Scrivania/Clarifai/pippo.png";

        // List of Recognized Result from Image
        List<String> resultList = ImageRecognizer.recognize(imageUrl);

        // Iteration of Result
        for(String result : resultList) {

            System.out.println(result);
        }
    }

@PAMAZ I don't think this works with file:// URL's like that.

Can you please give it a shot using the example Java code here?

Hi @PAMAZ,

To do a prediction on a local file, use ClarifaiImage.of(new File(imageUrl)) instead of ClarifaiImage.of(imageUrl). In this case you don't need to (or better said: must not) prepend file:/.

I'd also suggest you to rename your imageUrl variable to something like imageFilePath.

Hope this helps! If you have any more questions, feel free to ask.

PAMAZ commented

Hi @rok-povsic and @eddiezane!

I tried the solution suggested by @rok-povsic and it worked!

Thank you very much,
Paolo