Microsoft ProjectOxford Vision API 错误

Posted

技术标签:

【中文标题】Microsoft ProjectOxford Vision API 错误【英文标题】:Microsoft ProjectOxford Vision API Error 【发布时间】:2018-06-23 17:45:54 【问题描述】:

我是安卓新手。我正在尝试开发 android 应用程序,我在 youtube 中寻找教程。我遵循 youtube Analyze image 上的 android 教程中的每一步。但是由于我是开发android的初学者,所以在运行应用程序时遇到错误,它会自动停止。

有人愿意帮忙吗?在此先感谢

这是我的代码

public class MainActivity extends AppCompatActivity 

public VisionServiceClient visionServiceClient = new VisionServiceRestClient("96a818770f0e45dfa09fc5cce0f1ce1e");

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.apple);
    ImageView imageView = (ImageView)findViewById(R.id.imageView);
    Button btnIdentify = (Button)findViewById(R.id.btnIdentify);

    imageView.setImageBitmap(mBitmap);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    mBitmap.compress(Bitmap.CompressFormat.JPEG, 100,outputStream);
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());

    btnIdentify.setOnClickListener(new View.OnClickListener() 

        @Override
        public void onClick(View v)
           final AsyncTask<InputStream,String,String> visionTask = new AsyncTask<InputStream, String, String>() 
                ProgressDialog mDialog = new ProgressDialog(MainActivity.this);

                @Override
                protected String doInBackground(InputStream... params) 
                    try 
                        publishProgress("Recognizing...");
                        String[] features = "Description";
                        String[] details = ;

                        AnalysisResult result = visionServiceClient.analyzeImage(params[0], features, details);

                        String strResult = new Gson().toJson(result);
                        return strResult;
                     catch (Exception e) 
                       return  null;
                    
                

                @Override
                protected void onPreExecute() 
                    mDialog.show();
                

                @Override
                protected void onPostExecute(String s) 
                    mDialog.dismiss();

                    AnalysisResult result = new Gson().fromJson(s,AnalysisResult.class);
                    TextView textView = (TextView)findViewById(R.id.txtDescription);
                    StringBuilder stringBuilder = new StringBuilder();
                    for(Caption caption:result.description.captions)
                    
                        stringBuilder.append(caption.text);
                    
                    textView.setText(stringBuilder);
                

                @Override
                protected void onProgressUpdate(String... values) 
                    mDialog.setMessage(values[0]);
                
            ;

            visionTask.execute(inputStream);
        
    );


这是我遇到的错误

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.dev.sample, PID: 455
              java.lang.NullPointerException: Attempt to read from field 'com.microsoft.projectoxford.vision.contract.Description com.microsoft.projectoxford.vision.contract.AnalysisResult.description' on a null object reference
                  at com.dev.sample.MainActivity$1$1.onPostExecute(MainActivity.java:81)
                  at com.dev.sample.MainActivity$1$1.onPostExecute(MainActivity.java:50)
                  at android.os.AsyncTask.finish(AsyncTask.java:651)
                  at android.os.AsyncTask.access$500(AsyncTask.java:180)
                  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:7325)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

【问题讨论】:

【参考方案1】:

该错误表示 API 调用失败,您正在尝试解析 null json。

将您的代码更改为:

@Override
protected String doInBackground(InputStream...params) 
  try 
    publishProgress("Recognizing...");
    String[] features = 
      "Description"
    ;
    String[] details = ;

    AnalysisResult result = visionServiceClient.analyzeImage(params[0], features, details);

    String strResult = new Gson().toJson(result);
    return strResult;
   catch (Exception e) 
    return e.getMessage();
  


@Override
protected void onPostExecute(String s) 
  mDialog.dismiss();
  StringBuilder stringBuilder = new StringBuilder();
  TextView textView = findViewById(R.id.txtDescription);
  try 
    AnalysisResult result = new Gson().fromJson(s, AnalysisResult.class);
    for (Caption caption: result.description.captions) 
      stringBuilder.append(caption.text);
    
   catch (Exception e) 
    stringBuilder.append(e.getCause());
  
  textView.setText(stringBuilder);

【讨论】:

谢谢。我会试一试,我会告诉你会发生什么..你可以尝试做这个教程,如果它有效的话。 在尝试您的代码时,它返回错误 java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path$。请帮忙

以上是关于Microsoft ProjectOxford Vision API 错误的主要内容,如果未能解决你的问题,请参考以下文章

Project oxford vision API ocr异常

Microsoft 认知服务视觉 API ClientException 错误

按学题搜索Microsoft Academic API

使用微软认知服务进行人脸识别

未找到导入的项目“C:\Program Files\dotnet\sdk\2.1.201\Microsoft\VisualStudio\v15.0\WebApplications\Microsoft.

Microsoft.ReportViewer.WinForms.V15 与 .NET Core 3.1 不兼容 - 如何在 WPF Core 中显示 RDLC?