public void DrawInfo(DrawingContext drawingContext, Face f)
        {
            int[] coords = { 100, 100, 50, 75 };
            if (f != null) {
                // create box params
                double scale = 640.0 / 768.0;
                coords = f.Coords;
                for (int i = 0; i < coords.Length; i++)
                    coords[i] = (int)Math.Floor(coords[i] / scale);
                coords = FaceTracking.expandBy(coords, 150, 719, 499);
            }
            double width = coords[2] / 2.0;
            double height = coords[3] / 2.0;
            //Point topleft = new Point(coords[0] + width - 171.0 / 2.0, coords[1] - 199);
            //Point bottomright = new Point(coords[0] + width + 171.0 / 2.0, coords[1]);
            Point topleft = new Point(coords[0] + width - 256.5 / 2.0, coords[1] - 298.5);
            Point bottomright = new Point(coords[0] + width + 256.5 / 2.0, coords[1]);
            System.Windows.Rect rect = new System.Windows.Rect(topleft, bottomright);
            //Point textpointtop = new Point(coords[0] + width, coords[1] - 180);
            //Point textpointmid1 = new Point(coords[0] + width, coords[1] - 150);
            //Point textpointmid2 = new Point(coords[0] + width, coords[1] - 90);
            //Point textpointbottom = new Point(coords[0] + width, coords[1] - 70);
            Point textpointtop = new Point(coords[0] + width, coords[1] - 270);
            Point textpointmid1 = new Point(coords[0] + width, coords[1] - 225);
            Point textpointmid2 = new Point(coords[0] + width, coords[1] - 135);
            Point textpointbottom = new Point(coords[0] + width, coords[1] - 105);

            DrawChip(drawingContext, rect, f);
            DrawText(drawingContext, textpointtop, textpointmid1, textpointmid2, textpointbottom, f);
        }
Пример #2
0
 public void enqueue(Face f)
 {
     lock (faceQueue) {
         faceQueue.Enqueue(f);
         if (faceQueue.Count == 1) {
             Monitor.PulseAll(faceQueue);
         }
     }
 }
Пример #3
0
 public static Face findDuplicate(Face face)
 {
     foreach (Face f in list) {
         if (face.Id == f.Id) {
             return f;
         }
     }
     return null;
 }
Пример #4
0
 public Impression(Face f, Guid ad)
 {
     this.Age = f.Age;
     this.AgeConf = f.AgeConf;
     this.Gender = f.Gender;
     this.GenderConf = f.GenderConf;
     this.FacialHairConf = f.FacialHairConf;
     this.GlassesConf = f.GlassesConf;
     this.SecondsFacing = f.SecondsFacing[ad];
     this.Ad = ad;
     this.PersonId = f.Id;
 }
Пример #5
0
 // returns null if new face was added, or updated duplicate if duplicate was found
 public static Face AddOrReplace(Face face)
 {
     Face dup = findDuplicate(face);
     if (dup != null) {
         dup.Merge(face);
         return dup;
     }
     lock (list) {
         list.Add(face);
     }
     return null;
 }
Пример #6
0
        public static void DumpToDB(Face f)
        {
            BackgroundWorker bw = new BackgroundWorker();

            // what to do in the background thread
            bw.DoWork += new DoWorkEventHandler(
            delegate(object o, DoWorkEventArgs args) {
                HashSet<Guid> adIds = new HashSet<Guid>();
                foreach (Guid ad in f.SecondsFacing.Keys) {
                    adIds.Add(ad);
                    if (!Double.IsNaN(f.SecondsFacing[ad])) {
                        Impression i = new Impression(f, ad);
                        DatabaseManager.addImpression(i);
                    }
                }
                lock (list) {
                    list.Remove(f);
                }
                ChartView.UpdateCurrentAdChart = true;
                ChartView.UpdateHistoricalCharts = true;
                if (AdChooser.USE_ML) {
                    foreach (Guid id in adIds) {
                        AdChooser.updateAgeRange(id);
                        AdChooser.updateGenderStats(id);
                    }
                }

            });

            /*// what to do when worker completes its task (notify the user)
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
            delegate(object o, RunWorkerCompletedEventArgs args) {
                label1.Text = "Finished!";
            });*/

            bw.RunWorkerAsync();
        }
Пример #7
0
        public static bool getBetafaceResults(Face face)
        {
            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // connect to webservice

            Console.Out.WriteLine("Connecting to webservice...");
            HttpClient client = new HttpClient(ServiceUri);

            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // upload images

            Console.Out.WriteLine("Uploading images...");
            String guid = null;

            //Dictionary<Guid, string> images = new Dictionary<Guid, string>();

            if (!String.IsNullOrEmpty(face.Path) && File.Exists(face.Path)) {
                byte[] data = ReadFile(face.Path);
                if (null != data) {
                    Console.Out.WriteLine("transfer file " + face.Path);

                    //transfer file
                    ImageRequestBinary request = new ImageRequestBinary();
                    request.api_key = Username;
                    request.api_secret = Password;
                    request.detection_flags = "23"; //binary representing flags detection|recognition|bestface|classifiers
                    request.imagefile_data = Convert.ToBase64String(data);
                    request.original_filename = face.Path;

                    HttpContent body = HttpContentExtensions.CreateXmlSerializable(request);
                    System.Net.ServicePointManager.Expect100Continue = false;
                    using (HttpResponseMessage response = client.Post("UploadNewImage_File", body)) {
                        // Throws an exception if not OK
                        response.EnsureStatusIsSuccessful();

                        BetafaceImageResponse ret = response.Content.ReadAsXmlSerializable<BetafaceImageResponse>();
                        if (ret.int_response == 0) {
                            face.BetafaceId = ret.img_uid; // add betafaceid to face
                            guid = ret.img_uid;
                            Console.Out.WriteLine("transfer ok");
                        } else {
                            Console.Out.WriteLine("error " + ret.int_response + " " + ret.string_response);
                            return false;
                        }
                    }
                }

            } else {
                Console.Out.WriteLine("File not found " + face.Path);
                // if filepath invalid, return true bc don't want to keep in queue
                return true;
            }

            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // retreive results
            Console.Out.WriteLine("Waiting for results...");

            ImageInfoRequestUid req = new ImageInfoRequestUid();
            req.api_key = Username;
            req.api_secret = Password;
            req.img_uid = guid;

            HttpContent bod = HttpContentExtensions.CreateXmlSerializable(req);
            System.Net.ServicePointManager.Expect100Continue = false;

            using (HttpResponseMessage response = client.Post("GetImageInfo", bod)) {
                // Throws an exception if not OK
                response.EnsureStatusIsSuccessful();

                response.Content.LoadIntoBuffer();
                string str = response.Content.ReadAsString();

                BetafaceImageInfoResponse ret = response.Content.ReadAsXmlSerializable<BetafaceImageInfoResponse>();
                 if (ret.int_response == 0) {
                    string xmlFilepath = "..\\..\\Images\\BetafaceResults\\" + guid + ".xml";
                    Console.Out.WriteLine("writing result to " + xmlFilepath);
                    using (StreamWriter sw = new StreamWriter(xmlFilepath)) {
                        sw.WriteLine(str);
                    }

                    updateFacesFromXML(face, xmlFilepath);
                    return true;
                } else {
                    Console.Out.WriteLine("error " + ret.int_response + " " + ret.string_response);
                    return false;
                }
            }
        }
Пример #8
0
        public static void updateFacesFromXML(Face face, string xmlFilepath)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlFilepath);
            List<Face> faces = new List<Face>();
            //TODO test if count = 0
            if (xmlDoc.GetElementsByTagName("age").Count == 0) {
                face.ProcessingBetaface = false;
                return;
            }
            string age = xmlDoc.GetElementsByTagName("age")[0].InnerText;
            string age_confidence = xmlDoc.GetElementsByTagName("age_confidence")[0].InnerText;
            string gender = xmlDoc.GetElementsByTagName("gender")[0].InnerText;
            string gender_confidence = xmlDoc.GetElementsByTagName("gender_confidence")[0].InnerText;
            string betafaceId = xmlDoc.GetElementsByTagName("image_uid")[0].InnerText;
            string glasses_conf = xmlDoc.GetElementsByTagName("glasses_confidence")[0].InnerText;
            string beard_conf = xmlDoc.GetElementsByTagName("beard_confidence")[0].InnerText;
            string mustache_conf = xmlDoc.GetElementsByTagName("mustache_confidence")[0].InnerText;
            double facialhair_conf = Math.Max(Convert.ToDouble(beard_conf), Convert.ToDouble(mustache_conf));

            face.AddBetafaceInfo(Convert.ToDouble(age), Convert.ToDouble(age_confidence),
                gender, Convert.ToDouble(gender_confidence), facialhair_conf, Convert.ToDouble(glasses_conf));
        }
        private void DrawText(DrawingContext drawingContext, Point pointtop, Point pointmid1, Point pointmid2, Point pointbottom, Face f)
        {
            string wordstop = "thinking";
            int ms = DateTime.Now.Millisecond;
            if (ms >= 250 && ms < 500)
                wordstop += ".";
            else if (ms >= 500 && ms < 750)
                wordstop += "..";
            else if (ms >= 750)
                wordstop += "...";
            string wordsmid1 = "";
            string wordsmid2 = "facing ad:";
            string wordsbottom = "";
            SolidColorBrush brush = new SolidColorBrush(Color.FromRgb(79, 79, 79));
            if (f != null) {
                string[] data = new string[7];
                if (!Double.IsNaN(f.SecondsFacing[AdView.currentAd.id])) {
                    data[0] = String.Format("{0:0.00}", f.SecondsFacing[AdView.currentAd.id]);
                }
                if (f.BetafaceReturned) {
                    data[1] = String.Format("{0:0}-{1:0}", f.Age - 4, f.Age + 4);
                    if (f.Gender == "m") {
                        data[2] = "male";
                        brush = new SolidColorBrush(Color.FromRgb(28, 72, 130));
                    } else {
                        data[2] = "female";
                        brush = new SolidColorBrush(Color.FromRgb(138, 46, 90));
                    }
                    data[3] = String.Format("{0:0.00}", f.LowestConfidence);
                    //data[4] = /*f.FacialHairConf > 0.0 ? "yes" : "no"*/ "no";
                    //data[5] = /*f.GlassesConf > 0.0 ? "yes" : "no"*/ "yes";
                    wordstop = "age:";// + data[1]; +
                    //"\nbeard: " + data[4] +
                    //"\nglasses: " + data[5];
                    //+"\nid: " + f.Id;
                    wordsmid1 = data[1];

                }
                wordsbottom = data[0] == null ? "" : data[0];
            } else {
                wordstop = "age:";
                wordsmid1 = "18-26";
                wordsbottom = "29.91";
            }

            //Brush brush = Brushes.White;

            //FormattedText texttop = new FormattedText(wordstop, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Segoe WP"), 19.0, brush);
            //FormattedText textmid1 = new FormattedText(wordsmid1, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Segoe WP"), 42.0, brush);
            //FormattedText textmid2 = new FormattedText(wordsmid2, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Segoe WP"), 19.0, brush);
            //FormattedText textbottom = new FormattedText(wordsbottom, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Segoe WP"), 27.0, brush);
            FormattedText texttop = new FormattedText(wordstop, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Segoe WP"), 28.5, brush);
            FormattedText textmid1 = new FormattedText(wordsmid1, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Segoe WP"), 63, brush);
            FormattedText textmid2 = new FormattedText(wordsmid2, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Segoe WP"), 28.5, brush);
            FormattedText textbottom = new FormattedText(wordsbottom, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Segoe WP"), 40.5, brush);
            texttop.TextAlignment = TextAlignment.Center;
            textbottom.TextAlignment = TextAlignment.Center;
            textmid1.TextAlignment = TextAlignment.Center;
            textmid2.TextAlignment = TextAlignment.Center;
            drawingContext.DrawText(texttop, pointtop);
            drawingContext.DrawText(textmid1, pointmid1);
            drawingContext.DrawText(textmid2, pointmid2);
            drawingContext.DrawText(textbottom, pointbottom);
        }
        private void DrawChip(DrawingContext drawingContext, System.Windows.Rect r, Face f)
        {
            if (f == null) {
                drawingContext.DrawImage(ChartView.graychip, r);
                //drawingContext.PushOpacity(.1);
                //drawingContext.DrawImage(ChartView.pinkchipBorder, r);
                //drawingContext.Pop();
                return;
            }
            if (f.Finalized) {
                if (f.Gender == "m")
                    drawingContext.DrawImage(ChartView.bluechipBorder, r);
                else if (f.Gender == "f")
                    drawingContext.DrawImage(ChartView.pinkchipBorder, r);
                return;
            }

            drawingContext.DrawImage(ChartView.graychip, r);
            if (f.BetafaceReturned) {
                double percentColor = f.GenderTotalConf / Face.CONF_THRESH;
                if (f.Gender == "m") {
                    drawingContext.PushOpacity(percentColor);
                    drawingContext.DrawImage(ChartView.bluechip, r);
                    drawingContext.Pop();
                } else if (f.Gender == "f") {
                    drawingContext.PushOpacity(percentColor);
                    drawingContext.DrawImage(ChartView.pinkchip, r);
                    drawingContext.Pop();
                }
            }
        }
Пример #11
0
 public static bool Remove(Face f)
 {
     bool result = false;
     lock (list) {
         result = list.Remove(f);
     }
     return result;
 }
Пример #12
0
        public void LoadAdjacentAds()
        {
            Face targeted_face = null;
            bool should_update = false;

            List<Face> curFaces = new List<Face>();
            foreach (Face f in FaceList.toList()) {
                f.isTargeted = false;
                if (f.BetafaceReturned) {
                    double closest_z = double.MaxValue;
                    curFaces.Add(f);
                    if (f.TotalTimeLooking > 1.0)
                    {
                        currentState = State.Invidividual;
                        if (f.Distance < closest_z){
                            targeted_face = f;
                        }

                    }
                }
            }
            if (targeted_face != null)
                targeted_face.isTargeted = true;

            if (targeted_face == null)
                currentState = State.Group;
            if (curFaces.Count == 0)
                currentState = State.Slideshow;

            //Check to see if the faces are the same from the last run
            if (curFaces.Count == oldIds.Count)
            {
                foreach (Face f in curFaces)
                {
                    int id = f.Id;
                    if (!oldIds.Contains(id))
                    {
                        should_update = true;
                        break;
                    }
                }
            }
            else {
                should_update = true;
            }

            //Check to see if we are targeting the same person
            if (prev_targeted != targeted_face && currentState == State.Invidividual)
                should_update = true;

            //If the list has changed, update the old lists so they are the same
            if(should_update){
                oldIds = new List<int>();
                foreach (Face f in curFaces)
                {
                    oldIds.Add(f.Id);
                }

                prev_targeted = targeted_face;

                //Get the ads that we need to show;

                if (currentState == State.Invidividual)
                {
                    List<Face> target = new List<Face>();
                    target.Add(targeted_face);
                    orderedAds = AdChooser.chooseAd(target);
                    //Select the least relevant ad so we load the most relavent one on the top
                    currentAd = DatabaseManager.getAd(orderedAds[orderedAds.Count-1]);
                }
                else
                {
                    orderedAds = AdChooser.chooseAd(curFaces);
                }
            }

            //Update the text for ad order
            /*object text = AdGrid.FindName("AdOrder");
            System.Windows.Controls.TextBlock t = ((System.Windows.Controls.TextBlock)text);
            t.Text = string.Join(", ", Demystify(orderedAds).ToArray());*/

            int orderedSize = orderedAds.Count;

            List<Guid> relatedAds = DatabaseManager.getAdsInCategory(currentAd.category);
            int relSize = relatedAds.Count;

            int rel_curr_i = relatedAds.IndexOf(currentAd.id);
            int ordered_curr_i = orderedAds.IndexOf(currentAd.id);

            if(ordered_curr_i == -1){
                List<Guid> adsInCategory = DatabaseManager.getAdsInCategory(currentAd.category);
                foreach (Guid g in adsInCategory) {
                    ordered_curr_i = orderedAds.IndexOf(g);
                    if (ordered_curr_i != -1)
                        break;
                }
            }

            int new_top_i = (ordered_curr_i + 1) % orderedSize;
            int new_bottom_i = (ordered_curr_i - 1 + orderedSize) % orderedSize;
            int new_left_i = (rel_curr_i - 1 + relSize) % relSize;
            int new_right_i = (rel_curr_i + 1) % relSize;

            Guid new_top = orderedAds[new_top_i];
            Guid new_bottom = orderedAds[new_bottom_i];
            Guid new_left = relatedAds[new_left_i];
            Guid new_right = relatedAds[new_right_i];

            topAd = DatabaseManager.getAd(new_top);
            TopPicture = LoadPicture(topAd);
            this.changeProperty("TopPicture");

            bottomAd = DatabaseManager.getAd(new_bottom);
            BottomPicture = LoadPicture(bottomAd);
            this.changeProperty("BottomPicture");

            leftAd = DatabaseManager.getAd(new_left);
            LeftPicture = LoadPicture(leftAd);
            this.changeProperty("LeftPicture");

            rightAd = DatabaseManager.getAd(new_right);
            RightPicture = LoadPicture(rightAd);
            this.changeProperty("RightPicture");
        }
Пример #13
0
        public void Merge(Face faceNew)
        {
            foreach (Guid ad in faceNew.SecondsFacing.Keys) {
                if (!Double.IsNaN(faceNew.SecondsFacing[ad])) {
                    if (Double.IsNaN(this.SecondsFacing[ad])) {
                        this.SecondsFacing[ad] = 0;
                    }
                    this.SecondsFacing[ad] += faceNew.SecondsFacing[ad];
                }
            }

            if (this.IsFacingAd && faceNew.IsFacingAd) {
                double newSeconds = (faceNew.LastTimeFacingAd - this.LastTimeFacingAd).TotalSeconds;
                if (Double.IsNaN(this.SecondsFacing[AdView.currentAd.id]))
                    this.SecondsFacing[AdView.currentAd.id] = 0;
                this.SecondsFacing[AdView.currentAd.id] += newSeconds;
            }
            double secs = (faceNew.LastVectorTime - this.LastVectorTime).TotalSeconds;
            if (faceNew.Frame != null && secs > 0.15) {
                Vector3DF newVector = getVector(faceNew.Frame);
                this.Velocity = getDistance(this.Vector, newVector) / secs;
                this.Vector = newVector;
                this.LastVectorTime = faceNew.LastVectorTime;
            }
            this.Frame = faceNew.Frame;
            // just added this stuff, need to check
        }