gpt4 book ai didi

android - 没有使用 Microsoft Computer Vision API 返回的描述或标签

转载 作者:行者123 更新时间:2023-11-30 00:08:25 24 4
gpt4 key购买 nike

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

AnalyzeActivity.java

下的

process 方法

 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 的错误信息

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)

提前致谢。

最佳答案

除非您要求,否则您的回复将不会包含标题。您有两个选择:

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

String[] features = {"Description"};

(2) 调用/describe端点。对于有问题的 SDK,您将调用 DescribeActivity而不是 AnalyzeActivity。换句话说,你会调用:

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

第二种方法更简单,并且还可以让您有机会获得多个候选句子(通过更改第二个参数。)如果您想要一些其他功能,第一种方法更方便除了 em> 描述,例如 Faces

关于android - 没有使用 Microsoft Computer Vision API 返回的描述或标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48580546/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com