没有使用Microsoft Computer Vision API

时间:2018-02-02 10:20:55

标签: android computer-vision microsoft-cognitive

我想使用微软的计算机视觉API创建一个Android应用程序,我想要做的是捕获图像并返回描述图像的标签或标题。我的问题是它确实返回了错误的json数据。到目前为止,我所做的一切......

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;
}

AnalyzeActivity下的onPostExecute方法

 @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");

    }

}

以下是来自logcat的错误消息

  

致命例外:主要   流程:myapp.capstone.com.lumineux,PID:1693
  java.lang.NullPointerException:尝试从字段读取   “的java.util.List   com.microsoft.projectoxford.vision.contract.Description.captions'上   null对象引用
  在   myapp.capstone.com.lumineux.AnalyzeActivity $ doRequest.onPostExecute(AnalyzeActivity.java:152)                                                                                          在   myapp.capstone.com.lumineux.AnalyzeActivity $ doRequest.onPostExecute(AnalyzeActivity.java:91)                                                                                          在android.os.AsyncTask.finish(AsyncTask.java:636)                                                                                          在android.os.AsyncTask.access $ 500(AsyncTask.java:177)                                                                                          在   android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:653)                                                                                          在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                          在android.os.Looper.loop(Looper.java:135)                                                                                          在android.app.ActivityThread.main(ActivityThread.java:5254)
  at java.lang.reflect.Method.invoke(Native Method)
  在java.lang.reflect.Method.invoke(Method.java:372)
  atcom.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:902)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)

提前致谢。

1 个答案:

答案 0 :(得分:1)

除非您提出要求,否则您的回复不会包含字幕。您有两种选择:

(1)调用/analyze?visualFeatures=Description端点。在您的示例中,您可以通过将一行更改为以下内容来实现此目的:

String[] features = {"Description"};

(2)调用/describe端点。对于相关SDK,您将调用DescribeActivity而不是 AnalyzeActivity 。换句话说,你打电话:

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

第二种方法更简单,也可以让你有机会获得多个候选句子(通过改变第二个参数。)如果你想要一些其他功能,那么第一种方法更方便描述,例如Faces