gpt4 book ai didi

java - IBM Watson Visual Recognition 参数方法不会隐式接受字符串

转载 作者:太空宇宙 更新时间:2023-11-04 10:48:05 26 4
gpt4 key购买 nike

我使用 Windows 构建器创建了一个程序,以使用 IBM Watson 服务创建自定义分类器,一切正常,但我在使用文本文件中的分类器 ID 进行图像分类时遇到问题。

当我将自定义分类器 ID 放入代码中时,它工作正常,但当我尝试从 TextField 获取该 ID 时,它无法工作。

这是按钮操作事件中的代码。变量 String id 准确输出 parameters 方法中的内容,但它用 TextField 中的 id 替换 id (cats_599303326),但是当我将 id 作为参数放入 parameters 方法中时,程序将无法成功运行。

另一方面,如果我注释所有内容并仅输出 id 字符串,将其复制并粘贴到 parameters 方法中,则效果很好。

为什么我传递变量id时不起作用?

VisualRecognition service = new VisualRecognition(
VisualRecognition.VERSION_DATE_2016_05_20
);
service.setEndPoint("https://gateway-a.watsonplatform.net/visual-recognition/api");
service.setApiKey("{api-key}");
File imagesStream = new File(textField.getText());
ClassifyOptions classifyOptions = null;
String id = String.format("\"{\\\"classifier_ids\\\": [\\\"%s\\\"]}\"", textField_1.getText());
System.out.println(id);

try {
classifyOptions = new ClassifyOptions.Builder()
.imagesFile(imagesStream)
.imagesFilename("Image ")
.parameters("{\"classifier_ids\": [\"cats_599303326\"]}") //inside the parameters method, the goal is to replace cats_599303326 with an id given from a textfield
.build();
} catch (FileNotFoundException e1) {

e1.printStackTrace();
}
ClassifiedImages result = service.classify(classifyOptions).execute();
System.out.println(result);

将变量id作为参数放入parameters方法中时出现错误日志,正如我之前所说,如果我只是打印id字符串,将其复制并粘贴到parameters方法中作为参数,程序将成功运行,但如果将其作为变量,则不会运行:

Jan 10, 2018 6:07:55 AM okhttp3.internal.platform.Platform log
INFO: --> POST https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?version=2016-05-20&api_key=aca4433597018de62edafdeebceb2bdc1482496a http/1.1 (-1-byte body)
Jan 10, 2018 6:08:06 AM okhttp3.internal.platform.Platform log
INFO: <-- 400 Bad Request https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?version=2016-05-20&api_key=aca4433597018de62edafdeebceb2bdc1482496a (10214ms, 167-byte body)
Jan 10, 2018 6:08:06 AM com.ibm.watson.developer_cloud.service.WatsonService processServiceCall
SEVERE: POST https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?version=2016-05-20&api_key=aca4433597018de62edafdeebceb2bdc1482496a, status: 400, error: {
"images_processed": 0,
"error": {
"code": 400,
"description": "Invalid form data 'parameters'",
"error_id": "parameter_error"
}
}
Exception in thread "AWT-EventQueue-0" com.ibm.watson.developer_cloud.service.exception.BadRequestException: {
"images_processed": 0,
"error": {
"code": 400,
"description": "Invalid form data 'parameters'",
"error_id": "parameter_error"
}
}
at com.ibm.watson.developer_cloud.service.WatsonService.processServiceCall(WatsonService.java:408)
at com.ibm.watson.developer_cloud.service.WatsonService$1.execute(WatsonService.java:174)
at visualRecognitionSecondTry.ClassifyInterface$3.actionPerformed(ClassifyInterface.java:129)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

最佳答案

此错误表示服务无法解析您的 JSON 参数对象。我怀疑您在从文本字段获取值时缺少引号或未将 classifier_ids 作为数组提交,但如果没有更多代码就无法判断如何执行此操作。

关于java - IBM Watson Visual Recognition 参数方法不会隐式接受字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48179843/

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