Exemplo n.º 1
0
        /// <summary>
        /// Trains a classifier. Training requires a zip file of 10 positive examples and a zip file of 10 negative examples.
        /// The name of the positive examples shoud be prepended with the class name (ie className_positive_examples).
        /// Negative examples should be named negative_examples.
        /// </summary>
        /// <returns><c>true</c>, if classifier was trained, <c>false</c> otherwise.</returns>
        /// <param name="classifierName">Classifier name.</param>
        /// <param name="className">Class name.</param>
        /// <param name="positiveExamplesPath">Positive examples path.</param>
        /// <param name="negativeExamplesPath">Negative examples path.</param>
        /// <param name="callback">Callback.</param>
        public bool TrainClassifier(string classifierName, string className, string positiveExamplesPath, string negativeExamplesPath, OnTrainClassifier callback)
        {
            if (string.IsNullOrEmpty(mp_ApiKey))
            {
                mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
            }
            if (string.IsNullOrEmpty(mp_ApiKey))
            {
                throw new WatsonException("GetClassifier - VISUAL_RECOGNITION_API_KEY needs to be defined in config.json");
            }
            if (string.IsNullOrEmpty(classifierName))
            {
                throw new ArgumentNullException("ClassifierName");
            }
            if (string.IsNullOrEmpty(positiveExamplesPath))
            {
                throw new ArgumentNullException("positiveExamplesPath");
            }
            if (string.IsNullOrEmpty(negativeExamplesPath))
            {
                throw new ArgumentNullException("negativeExamplesPath");
            }
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }

            byte[] positiveExamplesData = null;
            byte[] negativeExamplesData = null;
            if (LoadFile != null)
            {
                positiveExamplesData = LoadFile(positiveExamplesPath);
                negativeExamplesData = LoadFile(negativeExamplesPath);
            }
            else
            {
                #if !UNITY_WEBPLAYER
                positiveExamplesData = File.ReadAllBytes(positiveExamplesPath);
                negativeExamplesData = File.ReadAllBytes(negativeExamplesPath);
                #endif
            }

            if (positiveExamplesData == null || negativeExamplesData == null)
            {
                Log.Error("VisualRecognition", "Failed to upload {0} or {1}!", positiveExamplesPath, negativeExamplesPath);
            }

            return(UploadClassifier(classifierName, className, positiveExamplesData, negativeExamplesData, callback));
        }
Exemplo n.º 2
0
        private bool UploadClassifier(string classifierName, string className, byte[] positiveExamplesData, byte[] negativeExamplesData, OnTrainClassifier callback)
        {
            if (string.IsNullOrEmpty(mp_ApiKey))
            {
                mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
            }
            if (string.IsNullOrEmpty(mp_ApiKey))
            {
                throw new WatsonException("GetClassifier - VISUAL_RECOGNITION_API_KEY needs to be defined in config.json");
            }
            if (string.IsNullOrEmpty(classifierName))
            {
                throw new ArgumentNullException("ClassifierName");
            }
            if (positiveExamplesData == null)
            {
                throw new ArgumentNullException("positiveExamplesData");
            }
            if (negativeExamplesData == null)
            {
                throw new ArgumentNullException("negativeExamplesData");
            }
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }

            RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, SERVICE_CLASSIFIERS);

            if (connector == null)
            {
                return(false);
            }

            TrainClassifierReq req = new TrainClassifierReq();

            req.Callback              = callback;
            req.OnResponse            = OnTrainClassifierResp;
            req.Timeout               = REQUEST_TIMEOUT;
            req.Parameters["api_key"] = mp_ApiKey;
            req.Parameters["version"] = VisualRecognitionVersion.Version;
            req.Forms         = new Dictionary <string, RESTConnector.Form>();
            req.Forms["name"] = new RESTConnector.Form(classifierName);
            req.Forms[className + "_positive_examples"] = new RESTConnector.Form(positiveExamplesData, className + "_positive_examples.zip", "application/zip");
            req.Forms["negative_examples"] = new RESTConnector.Form(negativeExamplesData, "negative_examples.zip", "application/zip");

            return(connector.Send(req));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Train a new classifier.
        /// </summary>
        /// <param name="classifierName">A name to give the classifier.</param>
        /// <param name="language">Language of the classifier.</param>
        /// <param name="trainingData">CSV training data.</param>
        /// <param name="callback">Callback to invoke with the results.</param>
        /// <returns>Returns true if training data was submitted correctly.</returns>
        public bool TrainClassifier(string classifierName, string language, string trainingData, OnTrainClassifier callback)
        {
            if (string.IsNullOrEmpty(classifierName))
            {
                throw new ArgumentNullException("classifierId");
            }
            if (string.IsNullOrEmpty(language))
            {
                throw new ArgumentNullException("language");
            }
            if (string.IsNullOrEmpty(trainingData))
            {
                throw new ArgumentNullException("trainingData");
            }
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }

            RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, "/v1/classifiers");

            if (connector == null)
            {
                return(false);
            }

            Dictionary <string, object> trainingMetaData = new Dictionary <string, object>();

            trainingMetaData["language"] = language;
            trainingMetaData["name"]     = classifierName;

            TrainClassifierReq req = new TrainClassifierReq();

            req.Callback   = callback;
            req.OnResponse = OnTrainClassifierResp;
            req.Forms      = new Dictionary <string, RESTConnector.Form>();
            req.Forms["training_metadata"] = new RESTConnector.Form(Encoding.UTF8.GetBytes(Json.Serialize(trainingMetaData)));
            req.Forms["training_data"]     = new RESTConnector.Form(Encoding.UTF8.GetBytes(trainingData));

            return(connector.Send(req));
        }