Exemple #1
0
        static async Task _Main(string[] args)
        {
            InitLogging();
            log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            try
            {
                var ctx = new FCClient(ConfigurationManager.AppSettings["ApiKey"], ConfigurationManager.AppSettings["ApiSecret"]);
                var tagNamespace = ConfigurationManager.AppSettings["TagNamespace"];
                foreach (var file in Directory.GetFiles(ConfigurationManager.AppSettings["SourceDirectory"]))
                {
                    var nameParts = Path.GetFileNameWithoutExtension(file).Split('-');
                    var recognizeAs = nameParts.First();
                    log.DebugFormat("Preparing to recognize {0} as {1}", file, recognizeAs);

                    using (var fs = new FileStream(file, FileMode.Open))
                    {
                        var faces = await ctx.Faces.DetectAsync(new String[0], new[] {fs});
                        var tags = faces.Photos.SelectMany(i => i.Tags).Select(i => i.TagId).ToList();
                        if (tags.Count != 1)
                        {
                            log.WarnFormat("Expected to get 1 face, but got {0} for {1}", tags.Count, recognizeAs);
                            continue;
                        }

                        var userId = string.Format("{0}@{1}", recognizeAs, tagNamespace);
                        var res = await ctx.Tags.SaveAsync(tags, userId);
                        if (res.Status != Status.Success)
                        {
                            log.WarnFormat("Failed to save {0}: {1}{2}", recognizeAs, res.Message, res.ErrorMessage);
                            continue;
                        }
                        log.DebugFormat("Loaded {0}: {1}", recognizeAs, res.Message);

                        log.DebugFormat("Starting training for {0}", userId);
                        var train = await ctx.Faces.TrainAsync(new[] { userId });
                        if (train.Status != Status.Success)
                        {
                            log.WarnFormat("Failed to save {0}: {1}{2}", recognizeAs, train.Message, train.ErrorMessage);
                            continue;
                        }
                        log.DebugFormat("Result: {0}", train.Message);
                    }

                }
            }
            catch (Exception ex)
            {
                log.Error("Main", ex);
            }
        }
        public string[] ProcessImage(string rawDataUrl, out string intermediateUrl, out string finalUrl, out bool addedOverlay)
        {
            intermediateUrl = null;
            addedOverlay = false;
            var ctx = new FCClient(_apiKey, _apiSecret);

            string contentType;
            var data = GetDataFromUrl(rawDataUrl, out contentType);

            var dataToSend = ResizePictureForDetection(data);

            if (null == _possibleUsers)
            {
                _possibleUsers = Task.Run(async () => await ctx.Account.UsersAsync(new [] { _tagNamespace })).Result.Users[_tagNamespace].Select(i => i.Split('@')[0]).ToArray();
            }

            using (var ms = new MemoryStream(data))
            {
                var image = (Bitmap)Bitmap.FromStream(ms);
                new ContrastStretch().ApplyInPlace(image);

                List<Tag> faceTags = null;
                try
                {
                    var detectResult = Task.Run(async () =>
                    {
                        using (var msToSend = new MemoryStream(dataToSend))
                        {
                            return await ctx.Faces.RecognizeAsync(_possibleUsers, new string[0], new[] { msToSend }, _tagNamespace);
                        }
                    }).Result;
                    faceTags = detectResult.Photos.SelectMany(i => i.Tags).ToList();
                }
                catch (Exception ex)
                {
                    log.Error("Remote call", ex);
                }

                if (null == faceTags || faceTags.Count == 0)
                {
                    // our fancy web service can't figure out anything, use the local library :(
                    finalUrl = _imageCleanupService.CleanUpImage(rawDataUrl, out intermediateUrl);
                    return new string[0];
                }

                var intermediateImage = new Bitmap(image);
                var faces = faceTags.Select(i => GetTagBoundingBox(i, image)).ToList();
                addedOverlay = faceTags.Select(f => ProcessFace(f, intermediateImage, image)).ToList().Any(_ => _);

                var boundary = Math.Max(40, faces.Max(i => Math.Max(i.Height, i.Width)));
                var x1 = Math.Max(0, faces.Min(i => i.Left) - boundary);
                var y1 = Math.Max(0, faces.Min(i => i.Top) - boundary);
                var x2 = Math.Min(image.Width, faces.Max(i => i.Right) + boundary);
                var y2 = Math.Min(image.Height, faces.Max(i => i.Bottom) + boundary);

                var newBoundingBox = new Rectangle(x1, y1, x2 - x1, y2 - y1);
                new RectanglesMarker(new[] { newBoundingBox }, Color.Blue).ApplyInPlace(intermediateImage);

                using (var ms2 = new MemoryStream())
                {
                    intermediateImage.Save(ms2, ImageFormat.Jpeg);
                    intermediateUrl = string.Concat("data:image/jpeg;base64,", Convert.ToBase64String(ms2.ToArray()));
                }

                // save the original image without cropping @ JPG/100
                var finalImage = ImageHelper.GetBytes(s => image.Save(s, ImageHelper.JPEGEncoder(), ImageHelper.Quality(100)));
                finalUrl = string.Concat("data:image/jpeg;base64,", Convert.ToBase64String(finalImage));

                return faceTags.Select(GetMatch).Where(i => i != null).ToArray();
            }
        }
 /// <summary>
 /// Initialisiert den Rest Service.
 /// </summary>
 private void InitializeService()
 {
     _client = new FCClient("5f228f0a0ce14e86a7c901f62ca5a569", "1231e9f2e95d4d90adf436e3de20f0f6");
         _result = _client.Account.EndAuthenticate(_client.Account.BeginAuthenticate(null, null));
 }