Search code examples
c#unity-game-enginewatsonvisual-recognition

Train own classifier IBM Watson Visual Recognition Unity3d


I already followed the steps from installing the SDK and configuring the service credentials. The problem is I cant train my own classifier. I'm getting this error: No overload for method 'TrainClassifier' takes 5 arguments.

void Start()
{
    string m_positiveExamplesPath = Application.dataPath + "/testData/cpu_positive_examples.zip";
    string m_negativeExamplesPath = Application.dataPath + "/testData/negative_examples.zip";
    if(!m_VisualRecognition.TrainClassifier("components", "cpu", m_positiveExamplesPath, m_negativeExamplesPath, OnTrainClassifier))
        Log.Debug("ExampleVisualRecognition", "Train classifier failed!");
}

private void OnTrainClassifier(GetClassifiersPerClassifierVerbose classifier)
{

    if(classifier != null)
    {
        Log.Debug("ExampleVisualRecognition", "Classifier is training! " + classifier);
    }
    else
    {
        Log.Debug("ExampleVisualRecognition", "Failed to train classifier!");
    }
}

Here's the link of the SDK in GitHub. Thanks!


Solution

  • You copied that code from the example page but looks like that everything on that page is outdated. It needs to be updated by IBM.

    The VisualRecognition class has 2 overloads of TrainClassifier:

    public bool TrainClassifier(OnTrainClassifier callback, string classifierName, Dictionary<string, string> positiveExamples, string negativeExamplesPath = default(string), string mimeType = "application/zip", string customData = default(string))
    

    and

    public bool TrainClassifier(OnTrainClassifier callback, string classifierName, Dictionary<string, byte[]> positiveExamplesData, byte[] negativeExamplesData = null, string mimeType = "application/zip", string customData = default(string))
    

    You have the SDK right in front of you. Next time you get an error like this, select the function, right click Go To Definition. It will show you the overload of the function then you will be able to pass the right parameter inside it.

    enter image description here

    Your code should be something like this:

    private VisualRecognition m_VisualRecognition = new VisualRecognition();
    
    void Start()
    {
        string m_positiveExamplesPath = Application.dataPath + "/testData/cpu_positive_examples.zip";
        string m_negativeExamplesPath = Application.dataPath + "/testData/negative_examples.zip";
    
        Dictionary<string, string> positiveExamples = new Dictionary<string, string>();
        positiveExamples.Add("giraffe", m_positiveExamplesPath);
    
        if (!m_VisualRecognition.TrainClassifier(OnTrainClassifier, "unity-test-classifier-example", positiveExamples, m_negativeExamplesPath))
            Log.Debug("ExampleVisualRecognition", "Train classifier failed!");
    }
    
    private void OnTrainClassifier(GetClassifiersPerClassifierVerbose classifier, string data)
    {
    
        if (classifier != null)
        {
            Log.Debug("ExampleVisualRecognition", "Classifier is training! " + classifier);
        }
        else
        {
            Log.Debug("ExampleVisualRecognition", "Failed to train classifier!");
        }
    }
    

    If you need any other example, don't get it from the example page. Get it from the Example folder that comes with the plugin.