Example #1
0
        /// <summary>
        /// Detects faces in a jpg, gif, png or zip file.
        /// </summary>
        /// <returns><c>true</c>, if faces was detected, <c>false</c> otherwise.</returns>
        /// <param name="callback">Callback.</param>
        /// <param name="imagePath">Image path.</param>
        public bool DetectFaces(OnDetectFaces 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(DetectFaces(callback, imagePath, imageData));
        }
Example #2
0
        private bool DetectFaces(OnDetectFaces callback, string imagePath, byte[] imageData = default(byte[]))
        {
            RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, SERVICE_DETECT_FACES);

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

            req.Callback              = callback;
            req.Timeout               = REQUEST_TIMEOUT;
            req.OnResponse            = OnDetectFacesResp;
            req.Parameters["api_key"] = mp_ApiKey;
            req.Parameters["version"] = VisualRecognitionVersion.Version;

            if (imageData != null)
            {
                req.Headers["Content-Type"] = "application/x-www-form-urlencoded";
                req.Send = imageData;
            }

            return(connector.Send(req));
        }
Example #3
0
        /// <summary>
        /// Detects faces in a given image URL.
        /// </summary>
        /// <returns><c>true</c>, if faces was detected, <c>false</c> otherwise.</returns>
        /// <param name="url">URL.</param>
        /// <param name="callback">Callback.</param>
        public bool DetectFaces(string url, OnDetectFaces 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_DETECT_FACES);

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

            DetectFacesReq req = new DetectFacesReq();

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

            return(connector.Send(req));
        }