Exemple #1
0
        /// <summary>
        /// Recognizes text in a given image.
        /// </summary>
        /// <returns><c>true</c>, if text was recognized, <c>false</c> otherwise.</returns>
        /// <param name="callback">Callback.</param>
        /// <param name="imagePath">Image path.</param>
        public bool RecognizeText(OnRecognizeText callback, string imagePath)
        {
            if (string.IsNullOrEmpty(imagePath))
            {
                throw new ArgumentNullException("Define an image path to classify!");
            }
            if (string.IsNullOrEmpty(mp_ApiKey))
            {
                mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
            }
            if (string.IsNullOrEmpty(mp_ApiKey))
            {
                throw new WatsonException("FindClassifier - VISUAL_RECOGNITION_API_KEY needs to be defined in config.json");
            }

            byte[] imageData = null;
            if (imagePath != default(string))
            {
                if (LoadFile != null)
                {
                    imageData = LoadFile(imagePath);
                }
                else
                {
                    #if !UNITY_WEBPLAYER
                    imageData = File.ReadAllBytes(imagePath);
                    #endif
                }

                if (imageData == null)
                {
                    Log.Error("VisualRecognition", "Failed to upload {0}!", imagePath);
                }
            }

            return(RecognizeText(callback, imagePath, imageData));
        }
Exemple #2
0
        private bool RecognizeText(OnRecognizeText callback, string imagePath, byte[] imageData = default(byte[]))
        {
            RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, SERVICE_RECOGNIZE_TEXT);

            if (connector == null)
            {
                return(false);
            }
            RecognizeTextReq req = new RecognizeTextReq();

            req.Callback   = callback;
            req.Timeout    = REQUEST_TIMEOUT;
            req.OnResponse = OnRecognizeTextResp;

            req.Parameters["api_key"] = mp_ApiKey;
            req.Parameters["version"] = VisualRecognitionVersion.Version;

            if (imageData != null)
            {
                req.Send = imageData;
            }

            return(connector.Send(req));
        }
Exemple #3
0
        /// <summary>
        /// Recognizes text given an image url.
        /// </summary>
        /// <returns><c>true</c>, if text was recognized, <c>false</c> otherwise.</returns>
        /// <param name="url">URL.</param>
        /// <param name="callback">Callback.</param>
        public bool RecognizeText(string url, OnRecognizeText callback)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException("url");
            }
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }
            if (string.IsNullOrEmpty(mp_ApiKey))
            {
                mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
            }
            if (string.IsNullOrEmpty(mp_ApiKey))
            {
                throw new WatsonException("FindClassifier - VISUAL_RECOGNITION_API_KEY needs to be defined in config.json");
            }

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

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

            RecognizeTextReq req = new RecognizeTextReq();

            req.Callback              = callback;
            req.OnResponse            = OnRecognizeTextResp;
            req.Parameters["api_key"] = mp_ApiKey;
            req.Parameters["url"]     = url;
            req.Parameters["version"] = VisualRecognitionVersion.Version;

            return(connector.Send(req));
        }