Esempio n. 1
0
        void _trainBW_DoWork(object sender, DoWorkEventArgs e)
        {
            if (e == null)
            {
                return;
            }

            var arg = e.Argument as Object[];

            var name = arg[0] as String;

            var image = arg[1] as Bitmap;

            var imgName = Guid.NewGuid() + "kin.jpg";



            var detectionResult = _faceRest.faces_detect(null, imgName, null, "", image);

            var tags = detectionResult.photos[0].tags;

            var tagList = new List <string>();



            if (tags == null || tags.Count != 1)
            {
                e.Cancel = true;
                return;
            }

            var tag = tags[0];

            if (tag.attributes["face"].confidence < 80)
            {
                e.Cancel = true;
                return;
            }

            tagList.Add(tag.tid);
            if (!_users.Contains(name + "@" + _namespace[0]))
            {
                _users.Add(name + "@" + _namespace[0]);
            }

            _faceRest.tags_save(tagList, name + "@" + _namespace[0], "", "kinectHome");
            var trainRes = _faceRest.faces_train(new List <string> {
                name
            }, _namespace[0], string.Empty);

            var result = new object[2];

            result[0] = image;
            result[1] = detectionResult;
            e.Result  = result;

            var trainStatus = _faceRest.faces_status(_users, _namespace[0]);
        }
Esempio n. 2
0
        private void ExecuteTask(object state)
        {
            CloudQueueMessage queueMessage = (CloudQueueMessage)state;

            this.Log.DebugFormat("Executing render task {0}", queueMessage.AsString);
            Data.Comic comic = null;
            RenderTask task  = null;

            string            connectionString = ConfigurationManager.ConnectionStrings["ComicModelContext"].ConnectionString;
            ComicModelContext entityContext    = new ComicModelContext(connectionString);

            try
            {
                // Read task details from storage
                CloudBlobContainer container = this.BlobClient.GetContainerReference(ComicConfigSectionGroup.Blob.TaskContainer);
                CloudBlobDirectory directory = container.GetDirectoryReference(ComicConfigSectionGroup.Blob.RenderTaskDirectory);
                CloudBlob          blob      = directory.GetBlobReference(queueMessage.AsString);

                XmlSerializer serializer = new XmlSerializer(typeof(RenderTask));

                using (MemoryStream stream = new MemoryStream())
                {
                    blob.DownloadToStream(stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    task        = (RenderTask)serializer.Deserialize(stream);
                    task.Status = TaskStatus.Executing;
                    this.UpdateTask(task);
                }

                User user = entityContext.TryGetUser(task.OwnerUid);

                FacebookClient facebook = new FacebookClient(task.FacebookToken);

                // Get template
                Template            template      = entityContext.ListTemplates().First(t => t.TemplateId == task.TemplateId);
                List <TemplateItem> templateItems = template.TemplateItems.OrderBy(t => t.Ordinal).ToList();

                List <TextBubbleDirection> bubbles = entityContext.ListTextBubbles().SelectMany(b => b.TextBubbleDirections).ToList();

                TextBubble speechBubble = entityContext.ListTextBubbles().First(b => b.Title == "speech");
                TextBubble bubbleShout  = entityContext.ListTextBubbles().First(b => b.Title == "shout");
                TextBubble squareBubble = entityContext.ListTextBubbles().First(b => b.Title == "square");

                comic = new Data.Comic();
                entityContext.AddToComics(comic);

                comic.Author      = user;
                comic.Template    = template;
                comic.CreateTime  = DateTime.Now;
                comic.UpdateTime  = DateTime.Now;
                comic.PublishTime = null;
                comic.FeatureTime = null;
                comic.Title       = "";
                comic.Description = "";
                comic.ShareText   = "";
                comic.IsPublished = false;
                comic.IsPrivate   = false;
                comic.IsDeleted   = false;
                comic.Locale      = user.Locale ?? "en-US";
                comic.StorageKey  = Guid.NewGuid().ToString();

                if (task.RemixComicId.HasValue)
                {
                    comic.RemixedComic = entityContext.TryGetComic(task.RemixComicId.Value, user);
                }

                // Comic generator only used to size text
                ComicGenerator generator = new ComicGenerator(template.Width, template.Height);

                // Render effect parameters
                Dictionary <string, object> parameterValues = new Dictionary <string, object>();

                switch (task.Effect)
                {
                case ComicEffectType.ColorSketch:
                case ComicEffectType.PencilSketch:
                    parameterValues.Add("edging", 2);
                    parameterValues.Add("coloring", 35);
                    break;

                case ComicEffectType.Comic:
                default:
                    parameterValues.Add("coloring", 6);
                    break;
                }

                // Get photos for each frame
                for (int f = 0; f < task.Frames.Count && f < templateItems.Count; f++)
                {
                    Photo  photo    = null;
                    Bitmap image    = null;
                    string imageUrl = String.Empty;
                    ComicGenerator.ImageAlign imageAlignment = ComicGenerator.ImageAlign.Center;
                    Point tag          = Point.Empty;
                    bool  tagConfident = false;

                    if (task.PhotoSource == "Internal" && task.Frames[f].PhotoId.HasValue)
                    {
                        // Load image from database
                        photo           = entityContext.TryGetPhoto(task.Frames[f].PhotoId.Value);
                        photo.ImageData = this.GetStoredImage(photo.StorageKey);
                        image           = new Bitmap(new MemoryStream(photo.ImageData));
                    }
                    else
                    {
                        // Tagged facebook photos
                        if (task.PhotoSource == "Tagged")
                        {
                            try
                            {
                                // List photos of the user
                                Dictionary <string, object> args = new Dictionary <string, object>();
                                args.Add("limit", "50");

                                dynamic photoResult = facebook.Get(String.Format("/{0}/photos", task.Frames[f].Id), args);

                                if (photoResult.data.Count > 0)
                                {
                                    // Pick a random photo with 2 or fewer tags
                                    dynamic photoData = ((IList <dynamic>)photoResult.data)
                                                        .OrderBy(p => Guid.NewGuid())
                                                        .FirstOrDefault(p => p.tags.data.Count <= 2);

                                    if (photoData != null)
                                    {
                                        imageUrl = (string)photoData.source;

                                        // Look for user tag location
                                        int     id;
                                        dynamic tagData = ((IList <dynamic>)photoData.tags.data)
                                                          .FirstOrDefault(t => int.TryParse(t.id, out id) && id == task.Frames[f].Id);

                                        if (tagData != null)
                                        {
                                            tag          = new Point((int)Math.Round((double)tagData.x), (int)Math.Round((double)tagData.y));
                                            tagConfident = false;
                                        }
                                    }
                                }
                            }
                            catch (Exception x)
                            {
                                this.Log.Error("Unable to retrieve tagged photo from facebook.", x);
                            }
                        }

                        // Look for any photo of the user
                        else if (task.PhotoSource == "Any")
                        {
                            try
                            {
                                FaceRestAPI         faceApi   = this.CreateFaceApi(task.FacebookToken, user.Uid);
                                List <string>       ids       = new List <string>(new string[] { String.Format("{0}@facebook.com", task.Frames[f].Id) });
                                FaceRestAPI.FaceAPI anyResult = faceApi.facebook_get(ids, null, "1", null, "random");

                                if (anyResult.status == "success" && anyResult.photos.Count > 0)
                                {
                                    FaceRestAPI.Photo p = anyResult.photos[0];
                                    imageUrl     = p.url;
                                    tag          = new Point((int)Math.Round(p.tags.First().mouth_center.x), (int)Math.Round(p.tags.First().mouth_center.y));
                                    tagConfident = true;
                                }
                            }
                            catch (Exception x)
                            {
                                this.Log.Error("Unable to retrieve photo through face.com api.", x);
                            }
                        }

                        // Use profile photo as backup image
                        if (String.IsNullOrEmpty(imageUrl))
                        {
                            imageUrl = String.Format("https://graph.facebook.com/{0}/picture?access_token={1}&type=large", task.Frames[f].Id, facebook.AccessToken);
                        }

                        image = this.GetImage(imageUrl);


                        // Find faces when confidence in tag location is low
                        if (!tagConfident)
                        {
                            try
                            {
                                FaceRestAPI tagApi = this.CreateFaceApi(task.FacebookToken, user.Uid);
                                //List<string> tagIds = new List<string>(new string[] { String.Format("{0}@facebook.com", task.Frames[f].Id) });
                                List <string>       urls      = new List <string>(new string[] { imageUrl });
                                FaceRestAPI.FaceAPI tagResult = tagApi.faces_detect(urls, null, "Normal", null, null);

                                if (tagResult.status == "success" && tagResult.photos.Count > 0 && tagResult.photos[0].tags.Count > 0)
                                {
                                    FaceRestAPI.Tag t = tagResult.photos[0].tags.First();
                                    tag          = new Point((int)Math.Round(t.mouth_center.x), (int)Math.Round(t.mouth_center.y));
                                    tagConfident = true;
                                }
                            }
                            catch (Exception x)
                            {
                                this.Log.Error("Unable to detected faces.", x);
                            }
                        }

                        if (tag != Point.Empty && tag.Y <= image.Height / 3)
                        {
                            imageAlignment = ComicGenerator.ImageAlign.Top;
                        }
                    }

                    // Resize to fit frame
                    image = ComicGenerator.FitImage(new Size(templateItems[f].Width, templateItems[f].Height), image);

                    // Apply render effect
                    if (task.Effect != ComicEffectType.None)
                    {
                        RenderHelper    effectHelper = new RenderHelper(image.Size);
                        ImageRenderData renderResult = effectHelper.RenderEffect(image, task.Effect, parameterValues);
                        image = new Bitmap(renderResult.RenderStream);
                    }

                    // Read raw photo into memory
                    MemoryStream imageStream = new MemoryStream();
                    image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    imageStream.Seek(0, SeekOrigin.Begin);


                    // Frame text bubbles
                    if (!String.IsNullOrWhiteSpace(task.Frames[f].Message))
                    {
                        ComicTextBubble comicBubble = new ComicTextBubble();
                        entityContext.AddToComicTextBubbles(comicBubble);
                        comicBubble.Comic = comic;
                        comicBubble.Text  = task.Frames[f].Message;

                        // Remove newlines
                        comicBubble.Text = comicBubble.Text.Replace('\n', ' ');

                        // Font size
                        int fontSize = 7;
                        if (comicBubble.Text.Length > 160)
                        {
                            fontSize = 6;
                        }
                        if (comicBubble.Text.Length > 200)
                        {
                            fontSize = 5;
                        }
                        comicBubble.Font = new Font(ComicGenerator.ComicFont, fontSize, FontStyle.Regular, GraphicsUnit.Point);

                        // Shouting / excited?
                        TextBubble bubble = speechBubble;
                        if (comicBubble.Text.Contains('!') || Regex.Matches(comicBubble.Text, "[A-Z]").Count > comicBubble.Text.Length / 4)
                        {
                            bubble = bubbleShout;
                        }

                        // Calculate tag x/y coords relative to the whole comic
                        if (tag != Point.Empty)
                        {
                            Size      templateSize = new Size(templateItems[f].Width, templateItems[f].Height);
                            Rectangle cropArea     = ComicGenerator.GetCropImageSize(image.Size, templateSize, imageAlignment);
                            tag.X = image.Size.Width * tag.X / 100 - cropArea.X + templateItems[f].X;
                            tag.Y = image.Size.Height * tag.Y / 100 - cropArea.Y + templateItems[f].Y;
                        }

                        // Position text bubble
                        this.PositionFrameBubble(comicBubble, image, generator, bubble, squareBubble, templateItems[f], tag, imageAlignment);

                        // Add photo as template item
                        photo            = new Photo();
                        photo.User       = user;
                        photo.CreateTime = DateTime.Now;
                        photo.ImageData  = imageStream.ToArray();
                        photo.StorageKey = Guid.NewGuid().ToString();
                        photo.Width      = image.Width;
                        photo.Height     = image.Height;
                        entityContext.AddToPhotos(photo);
                    }

                    // Tag users
                    //if (task.Frames[f].Id > 0)
                    //{
                    //    try
                    //    {

                    //        // Lookup existing user
                    //        User taggedUser = entityContext.TryGetUser(task.Frames[f].Id, true);
                    //        if (taggedUser == null)
                    //        {
                    //            // User doesn't exist in the db yet - grab from facebook
                    //            dynamic facebookUser = facebook.Get(String.Format("/{0}", task.Frames[f].Id));
                    //            taggedUser = new User();
                    //            taggedUser.Uid = long.Parse(facebookUser.id);
                    //            taggedUser.IsDeleted = false;
                    //            taggedUser.IsSubscribed = false;
                    //            taggedUser.Locale = facebookUser.locale;
                    //            taggedUser.Name = facebookUser.name;
                    //            taggedUser.Nickname = facebookUser.name;
                    //            taggedUser.FbLink = facebookUser.link;
                    //            entityContext.AddToUsers(taggedUser);
                    //        }

                    //        ComicTag comicTag = new ComicTag();
                    //        comicTag.User = taggedUser;
                    //        comicTag.Comic = comic;
                    //        if (tag != Point.Empty)
                    //        {
                    //            comicTag.X = tag.X;
                    //            comicTag.Y = tag.Y;
                    //        }
                    //    }
                    //    catch (Exception x)
                    //    {
                    //        this.Log.ErrorFormat("Failed to tag user {0} in comic. {1}", task.Frames[f].Id, x.ToString());
                    //    }
                    //}


                    ComicPhoto comicPhoto = new ComicPhoto();
                    comicPhoto.Comic        = comic;
                    comicPhoto.Photo        = photo;
                    comicPhoto.TemplateItem = templateItems[f];
                    comicPhoto.Alignment    = imageAlignment;
                    comic.ComicPhotos.Add(comicPhoto);

                    // Update task progress
                    task.CompletedOperations++;
                    this.UpdateTask(task);
                }

                for (int b = 0; task.Bubbles != null && b < task.Bubbles.Count; b++)
                {
                    ComicTextBubble comicBubble = new ComicTextBubble();
                    entityContext.AddToComicTextBubbles(comicBubble);
                    comicBubble.Comic = comic;
                    comicBubble.Text  = task.Bubbles[b].Text;
                    comicBubble.Font  = new Font(ComicGenerator.ComicFont, 7, FontStyle.Regular, GraphicsUnit.Point);
                    comicBubble.TextBubbleDirection = bubbles.First(d => d.TextBubbleDirectionId == task.Bubbles[b].TextBubbleDirectionId);
                    comicBubble.Position            = new Rectangle(new Point(task.Bubbles[b].X, task.Bubbles[b].Y), generator.MeasureText(comicBubble.Text, comicBubble.Font).ToSize());
                }

                // Fix for position to x,y coordinates
                foreach (ComicTextBubble b in comic.ComicTextBubbles)
                {
                    b.X = b.Position.X;
                    b.Y = b.Position.Y;
                }

                this.SaveComic(comic, entityContext);

                task.CompletedOperations = task.TotalOperations;
                task.Status  = TaskStatus.Complete;
                task.ComicId = comic.ComicId;
                this.UpdateTask(task);
                this.Log.DebugFormat("Completed render task {0}", task.TaskId);
            }
            catch (Exception x)
            {
                this.Log.Error("Unable to complete render task.", x);

                if (task != null)
                {
                    task.Status = TaskStatus.Failed;
                    this.UpdateTask(task);
                }

                if (comic != null)
                {
                    this.Log.DebugFormat("Text bubble info [{0}] [{1}]",
                                         String.Join(",", comic.ComicTextBubbles.Select(b => b.ComicTextBubbleId.ToString()).ToArray()),
                                         String.Join(",", comic.ComicTextBubbles.Select(b => b.TextBubbleDirectionId.ToString()).ToArray()));
                }
            }
        }
Esempio n. 3
0
        private void ProcessImage(WriteableBitmap writeable)
        {
            var smaller = writeable.MakeSmallerCopy(AppConstants.MaxDimensionForUpload);
            smaller.SaveToFile(AppConstants.UploadFileName, AppConstants.JpegQualityForUpload, (stream) => { });

            var api = new FaceRestAPI(AppConstants.FaceApikey, AppConstants.FaceSecretkey, "", false, "json", "", "");
            api.faces_detect(null,
                            AppConstants.UploadFileName,
                            null,
                            string.Empty,
                             (result) => Dispatcher.BeginInvoke(() => DisplayResult(writeable, result)),
                             (error) => Dispatcher.BeginInvoke(() =>
                                                                   {
                                                                       MessageBox.Show("Sorry - we had a problem: " + error);
                                                                       Debug.WriteLine("Bummer " + error);
                                                                       ShowDisplay(DisplayState.Intro);
                                                                   }));
        }