【问题标题】:IBM Watson Visual Recognition parameters method won't accept String implicitlyIBM Watson Visual Recognition 参数方法不会隐式接受字符串
【发布时间】:2018-01-10 03:24:28
【问题描述】:

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

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

这是按钮动作事件中的代码。变量String id 准确输出parameters 方法中的内容,但它将id (cats_599303326) 替换为TextField 中的id,但是当我将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 字符串,请将其复制并粘贴为@987654332 中的参数@method程序会运行成功,但如果我把它作为变量它就不会运行:

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)

【问题讨论】:

    标签: java eclipse api ibm-watson visual-recognition


    【解决方案1】:

    此错误表示服务无法解析您的 JSON 参数对象。我怀疑您在从文本字段中获取值时要么缺少引号,要么没有将分类器 ID 作为数组提交,但是如果没有更多代码来说明您是如何做到这一点的,则无法判断。

    【讨论】:

    • 感谢您的反馈。我做错的是在我的字符串中添加了额外的引号。这太尴尬了。基本上我所做的不是创建 String = "Hello world!",而是 String = ""Hello world!"" 。
    【解决方案2】:

    如果 stackoverflow 的人永远禁止我,然后来我家谋杀我,我会完全理解的!解决方案是如此简单,以至于我没有想到它的尴尬。基本上我创建的字符串有额外的引号。解决办法是:

    String id = String.format("{\"classifier_ids\": [\"%s\"]}", textField_1.getText());

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多