Search code examples
javaandroidibm-cloudibm-watsonvisual-recognition

Watson visual recognition, classify against multiple classifiers in java


I found this curl command in API document that can classify an image against multiple classifiers:

    curl -u "{username}":"{password}" \
-X POST \
-F "images_file=@batch1.zip" \
-F "classifier_ids=<classifierlist.json" \
"https://gateway.watsonplatform.net/visual-recognition-beta/api/v2/classify?version=2015-12-02"

I wondered if it is possible to do this in java since I'm working on an android program using Watson's visual recognition service.

thank you


Solution

  • Use this tutorial to set your Java environment

    https://developer.ibm.com/recipes/tutorials/bluemix-watson-apis-quickstart-using-java-sdk/

    Then take a look on this other tutorial that shows how to use multiple classifiers using Java code

    https://developer.ibm.com/recipes/tutorials/estimate-a-childs-age-based-on-photos-using-watson-visual-recognition/

    Briefly speaking, your code will look like this

    Step #1 - create the classifiers

     VisualRecognition service = new VisualRecognition(VisualRecognition.VERSION_DATE_2015_12_02);
     service.setUsernameAndPassword("*******", "********");
    
     File p1 = new File("/home/leoks/Desktop/models/pos2010-2011.zip");
     File n1 = new File("/home/leoks/Desktop/models/pos2014-2015.zip");
     VisualClassifier c1 = service.createClassifier("2010", p1, n1);
    
     File p3 = new File("/home/leoks/Desktop/models/pos2014-2015.zip");
     File n3 = new File("/home/leoks/Desktop/models/pos2010-2011.zip");
     VisualClassifier c3 = service.createClassifier("2014", p3, n3);
    
    System.out.println(service.getClassifiers());
    

    Step #2 - use them

    File image = new File("...");
    
     VisualClassifier vc1 = new VisualClassifier("2010_633980596");
     VisualClassifier vc2 = new VisualClassifier("2014_450835300");
     VisualClassification result = service.classify(image, vc1,vc2);
     System.out.println(result);
    

    If your image is identified by the classifier, it will return the score, otherwise, no answer will be returned. E.g.

    {
     "images": [
     {
     "image": "2012.jpg",
     "scores": [
     {
     "classifier_id": "2010_633980596",
     "name": "2010",
     "score": 0.992153
     },
     {
     "classifier_id": "2014_450835300",
     "name": "2014",
     "score": 0.833185
     }
     ]
     }
     ]
    }
    

    check the tutorials, they're step-by-step instructions. Good luck.