qwerty
qwerty

Reputation: 51

No description or tags returned using Microsoft Computer Vision API

I want to make an android app using Microsoft's computer vision API, and what I wanted to do is to capture an image and return tags or caption which describes the image.My problem is that it does return wrong json data. Here is so far what I have done...

process method under AnalyzeActivity.java

 private String process() throws VisionServiceException, IOException {
    Gson gson = new Gson();
    String[] features = {"ImageType", "Color", "Faces", "Adult", "Categories"};
    String[] details = {};


    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bitmapPicture.compress(Bitmap.CompressFormat.JPEG, 100, output);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(output.toByteArray());

    AnalysisResult v = this.client.analyzeImage(inputStream, features, details);

    String result = gson.toJson(v);
    Log.d("result", result);

    return result;
}

onPostExecute method under AnalyzeActivity

 @Override
protected void onPostExecute(String data) {
    super.onPostExecute(data);

    mEditText.setText("");
    if (e != null) {
        mEditText.setText("Error: " + e.getMessage());
        this.e = null;
    } else {
        Gson gson = new Gson();
        AnalysisResult result = gson.fromJson(data, AnalysisResult.class);
        mEditText.append("Definition: ");
        mEditText.append("Image format: " + result.metadata.format + "\n");
        mEditText.append("Image width: " + result.metadata.width + ", height:" + result.metadata.height + "\n");

        for (Category category: result.categories) {
            mEditText.append("Category: " + category.name + ", score: " + category.score + "\n");
        }


       for (Caption caption: result.description.captions) {
            mEditText.append("Caption: " + caption.text + ", confidence: " + caption.confidence + "\n");
        }
        mEditText.append("\n");

        for (String tag: result.description.tags) {
            mEditText.append("Tag: " + tag + "\n");
        }
        mEditText.append("\n");

    }

}

Here is the error message from logcat

FATAL EXCEPTION: main
Process: myapp.capstone.com.lumineux, PID: 1693
java.lang.NullPointerException: Attempt to read from field 'java.util.List com.microsoft.projectoxford.vision.contract.Description.captions' on a null object reference
at myapp.capstone.com.lumineux.AnalyzeActivity$doRequest.onPostExecute(AnalyzeActivity.java:152) at myapp.capstone.com.lumineux.AnalyzeActivity$doRequest.onPostExecute(AnalyzeActivity.java:91) at android.os.AsyncTask.finish(AsyncTask.java:636) at android.os.AsyncTask.access$500(AsyncTask.java:177) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)

Thanks in advance.

Upvotes: 1

Views: 247

Answers (1)

cthrash
cthrash

Reputation: 2973

Your response will not contain captions unless you request it. You have two options:

(1) Call the /analyze?visualFeatures=Description endpoint. In your example you would achive this by changing the one line to the following:

String[] features = {"Description"};

(2) Call the /describe endpoint. For the SDK in question you would invoke the DescribeActivity instead of the AnalyzeActivity. In other words you'd call:

AnalysisResult v = this.client.describe(inputStream, 1);

The second method is simpler, and will also give you the opportunity to get more than one candidate sentence (by changing the second argument.) The first is more convenient if you want some other feature in addition to the description, such as Faces.

Upvotes: 1

Related Questions