public MainWindow() { InitializeComponent(); //StackPanel imgPanel = this.imgStackPanel; List<Image> imgList = getImages(@"C:\pics"); foreach (Image img in imgList) { //imgPanel.Children.Add(img); } obj = new FaceRestAPI("3728359974280cbfe98c90e4c4cc7e1d", "a7057564ad99fd57cabe3f286a5a1f63", "", true, "xml", "", ""); }
public FaceRestAPIWrapper(string apiKey, string apiSecret, string password, string fb_user, string fb_oauth_token, string nameSpace, string tempFolder) { this.apiKey = apiKey; this.apiSecret = apiSecret; this.fb_user = fb_user; this.fb_oauth_token = fb_oauth_token; this.password = password; this.nameSpace = nameSpace; fra = new FaceRestAPI(apiKey, apiSecret, password, false, "json", fb_user, fb_oauth_token); roamingPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + Path.DirectorySeparatorChar + "SpaRCMap" + (tempFolder == null ? "" : (Path.DirectorySeparatorChar + tempFolder)); if (!Directory.Exists(roamingPath)) Directory.CreateDirectory(roamingPath); }
public Recognizer() { _faceRest = new FaceRestAPI("2460a8f47f65651ea1fb039f7782a55a", "90dfe4135e408911cd9acf72fb7b1f6b", "desaster", false, "json", "", ""); var authResult = _faceRest.account_authenticate(); var users = _faceRest.account_users(_namespace); foreach(var user in users.users[_namespace[0]]) { _users.Add(user); Console.Out.WriteLine(user); } _trainBW = new BackgroundWorker(); _trainBW.DoWork += _trainBW_DoWork; _trainBW.RunWorkerCompleted += _trainBW_RunWorkerCompleted; _recognizeBW = new BackgroundWorker(); _recognizeBW.DoWork += _recognizeBW_DoWork; _recognizeBW.RunWorkerCompleted += _recognizeBW_RunWorkerCompleted; }
public Recognizer() { _faceRest = new FaceRestAPI("2460a8f47f65651ea1fb039f7782a55a", "90dfe4135e408911cd9acf72fb7b1f6b", "desaster", false, "json", "", ""); var authResult = _faceRest.account_authenticate(); var users = _faceRest.account_users(_namespace); foreach (var user in users.users[_namespace[0]]) { _users.Add(user); Console.Out.WriteLine(user); } _trainBW = new BackgroundWorker(); _trainBW.DoWork += _trainBW_DoWork; _trainBW.RunWorkerCompleted += _trainBW_RunWorkerCompleted; _recognizeBW = new BackgroundWorker(); _recognizeBW.DoWork += _recognizeBW_DoWork; _recognizeBW.RunWorkerCompleted += _recognizeBW_RunWorkerCompleted; }
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())); } } }
public FaceRecognitionEventArgs(FaceRestAPI.FaceAPI faceAPI) { this.faceAPI = faceAPI; }
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); })); }
private void DisplaySmilieResult(WriteableBitmap writeableBitmap, FaceRestAPI.FaceAPI result) { int countHappy = 0; int countSad = 0; foreach (var tag in result.photos[0].tags) { // Uses the segment's center and half width, height var c = tag.center; Debug.WriteLine("Center ({0} ,{1}), size ({2}, {3})", c.x, c.y, tag.width, tag.height); var destRect = new Rect(writeableBitmap.PixelWidth * 0.01 * (tag.center.x - tag.width / 2), writeableBitmap.PixelHeight * 0.01 * (tag.center.y - tag.height / 2), writeableBitmap.PixelWidth * 0.01 * tag.width, writeableBitmap.PixelHeight * 0.01 * tag.height); var whichFace = ChooseFace(tag.attributes); if (whichFace == _smilieWriteableBitmap) countHappy++; else countSad++; var sourceRect = new Rect(0, 0, whichFace.PixelWidth, whichFace.PixelHeight); writeableBitmap.Blit(destRect, whichFace, sourceRect); } writeableBitmap.Invalidate(); SelectedImage.Source = writeableBitmap; writeableBitmap.SaveToFile(AppConstants.IsoFileName, (stream) => { /* do nothing */ }); ShowDisplay(DisplayState.Finished); NavigateToPhotoPage(countHappy, countSad); }
private void DisplayResult(WriteableBitmap writeableBitmap, FaceRestAPI.FaceAPI result) { DisplaySmilieResult(writeableBitmap, result); }
public static void parseFaces(FaceRestAPI.FaceAPI fd) { foreach (var photo in fd.photos) { // http://graph.facebook.com/4926921/picture?type=large var url = photo.url; string id = url.Substring (26); id = id.Substring (0, id.IndexOf ("/")); int count = 0; lock (Database.Main) { Database.Main.Execute("Delete from Face where FriendId = ?",id); } foreach (var tag in photo.tags) { Face face = new Face (); var increase = 1.1f; face.FriendId = id; var width = Math.Max ((photo.height * (tag.height / 100)) * increase, (photo.width * (tag.width / 100)) * increase); face.Height = width; face.Width = width; var file = id + (count > 0 ? " -" + count : "") + ".png"; face.OrgImage = id + ".png"; face.Img = file; face.Cx = photo.width * ((tag.center.x) / 100); face.Cy = photo.height * ((tag.center.y) / 100); face.Roll = tag.roll; lock (Database.Main) Database.Main.Insert (face); Graphics.SaveFace (face); count++; } } }