pythonpython-3.xmachine-learningibm-watsonvisual-recognition

How to create a class (not a classifier) with a variable in IBM Watson Visual Recognition with Python?


I'm trying to create a new class with the API like it's shown in the API Documentation https://cloud.ibm.com/apidocs/visual-recognition?code=python#update-a-classifier

The part that Watson uses as the name of the class is the prefix of "_positive_examples". I need to use the content of a variable as the prefix, but it's a keyword and I can't add any variable there (variableNotWorking in the code).

I have tried to format as a string and also to create a dictionary in Python and introduce it there but I always obtain an error either from Python or Watson.

    positive = nuevaClase + '_positive_examples'
    visual_recognition = credencials()
    with open(aprDirFitxer1, 'rb') as nuevaClase:
            updated_model = visual_recognition.update_classifier(
                classifier_id=classifId,
                positive_examples={positive : nuevaClase}).get_result()

The error from Watson that I always obtain is:

watson_developer_cloud.watson_service.WatsonApiException: Error: Cannot execute learning task.  Did not find any files to process in the POST request body. , Code: 400 

Solution

  • I think your problem may be that you are using an old version of the SDK because the package name changed in Version 3 to "ibm-watson". You need to install the new SDK package with

    pip install --upgrade ibm-watson
    

    and then in your Python code, import from this new package:

    from ibm_watson import VisualRecognitionV3