1
votes

I created a program with windows builder to make a custom classifier using IBM Watson services and everything works fine but I have problems with the classification of an image using that classifier's ID from a text fie.

When I put the custom classifiers ID inside the code it works fine but when I am trying to take that ID from a TextField it won't work.

Here is the code inside the button's action event. The variable String id outputs exactly What is inside the parameters method but it replaces the id (cats_599303326) with the id that is in the TextField but when I put id as an argument in the parameters method, program won't run successfully.

On the other hand, if I comment everything and just output the id String, copy and paste it inside the parameters method, It worked fine.

Why it won't work when I pass the variable id through?

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

Error log when putting the variable id as an argument in the parameters method, and as I said before, if I just print the id String, copy it and paste it as an argument in the parameters method program will run successfully but it won't run if i put it as a variable:

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)
2

2 Answers

0
votes

This error indicates the service was unable to parse your JSON parameters object. I suspect you are either missing the quotation marks or not submitting the classifier_ids as an array when taking the value from your textfield, but cannot tell without more code for how you do that.

0
votes

If stackoverflow guys ban me for life and then come to my house and murder me, i would totally understand!! Solution was so simple it's embarrassing i did't think of it. Basically the String I created had extra quotation marks. The solution is :

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