private void UpdateDemographics(ImageAnalyzer img) { if (this.lastSimilarPersistedFaceSample != null) { bool demographicsChanged = false; // Update the Visitor collection (either add new entry or update existing) int temp_count = 0; foreach (var item in this.lastSimilarPersistedFaceSample) { Visitor visitor; var CurTime = DateTime.Now; if (this.visitors.TryGetValue(item.SimilarPersistedFace.PersistedFaceId, out visitor)) { try { visitor.Date = CurTime.Date.ToString("yyyy/MM/dd"); visitor.Hour = CurTime.Hour; visitor.Count++; if (this.lastIdentifiedPersonSample != null && this.lastIdentifiedPersonSample.Count() > temp_count && this.lastIdentifiedPersonSample.ElementAt(temp_count) != null && this.lastIdentifiedPersonSample.ElementAt(temp_count).Item2 != null && this.lastIdentifiedPersonSample.ElementAt(temp_count).Item2.Person != null) { visitor.Name = this.lastIdentifiedPersonSample.ElementAt(temp_count).Item2.Person.Name; } if (this.lastEmotionSample != null && this.lastEmotionSample.Count() > temp_count && this.lastEmotionSample.ElementAt(temp_count) != null) { Emotion emo = this.lastEmotionSample.ElementAt(temp_count); visitor.Smile = Math.Round(emo.Scores.Happiness, 4); } var messageString = JsonConvert.SerializeObject(visitor); Task.Run(async() => { await AzureIoTHub.SendSQLToCloudMessageAsync(messageString); }); } catch (NullReferenceException e) { this.debugText.Text = string.Format("NullRefenrenceException source at 1: {0}", e.Source); log.WriteToFile(DateTime.Now.ToString("G", tw) + " Error at getting visitor informations\n"); } } else { try { demographicsChanged = true; double smile = 0; if (this.lastEmotionSample != null && this.lastEmotionSample.Count() > temp_count && this.lastEmotionSample.ElementAt(temp_count) != null) { Emotion emo = this.lastEmotionSample.ElementAt(temp_count); smile = Math.Round(emo.Scores.Happiness, 4); } else { smile = 0; } int male = 1; double age = item.Face.FaceAttributes.Age; if (string.Compare(item.Face.FaceAttributes.Gender, "male", StringComparison.OrdinalIgnoreCase) == 0) { male = 0; } if (this.lastIdentifiedPersonSample != null && this.lastIdentifiedPersonSample.Count() > temp_count && this.lastIdentifiedPersonSample.ElementAt(temp_count) != null && this.lastIdentifiedPersonSample.ElementAt(temp_count).Item2 != null && this.lastIdentifiedPersonSample.ElementAt(temp_count).Item2.Person != null) { string name = this.lastIdentifiedPersonSample.ElementAt(temp_count).Item2.Person.Name; if (name != null) { visitor = new Visitor { UniqueId = item.SimilarPersistedFace.PersistedFaceId, Count = 1, Gender = male, Age = age, Smile = smile, Date = CurTime.Date.ToString("yyyy/MM/dd"), Hour = CurTime.Hour, Name = name, Device = deviceName }; } else { visitor = new Visitor { UniqueId = item.SimilarPersistedFace.PersistedFaceId, Count = 1, Gender = male, Age = age, Smile = smile, Date = CurTime.Date.ToString("yyyy/MM/dd"), Hour = CurTime.Hour, Name = name, Device = deviceName }; } } else { visitor = new Visitor { UniqueId = item.SimilarPersistedFace.PersistedFaceId, Count = 1, Gender = male, Age = age, Smile = smile, Date = CurTime.Date.ToString("yyyy/MM/dd"), Hour = CurTime.Hour, Name = null, Device = deviceName }; } this.visitors.Add(visitor.UniqueId, visitor); this.demographics.Visitors.Add(visitor); // Update the demographics stats. We only do it for new visitors to avoid double counting. AgeDistribution genderBasedAgeDistribution = null; if (string.Compare(item.Face.FaceAttributes.Gender, "male", StringComparison.OrdinalIgnoreCase) == 0) { this.demographics.OverallMaleCount++; genderBasedAgeDistribution = this.demographics.AgeGenderDistribution.MaleDistribution; } else { this.demographics.OverallFemaleCount++; genderBasedAgeDistribution = this.demographics.AgeGenderDistribution.FemaleDistribution; } if (item.Face.FaceAttributes.Age < 16) { genderBasedAgeDistribution.Age0To15++; } else if (item.Face.FaceAttributes.Age < 20) { genderBasedAgeDistribution.Age16To19++; } else if (item.Face.FaceAttributes.Age < 30) { genderBasedAgeDistribution.Age20s++; } else if (item.Face.FaceAttributes.Age < 40) { genderBasedAgeDistribution.Age30s++; } else if (item.Face.FaceAttributes.Age < 50) { genderBasedAgeDistribution.Age40s++; } else { genderBasedAgeDistribution.Age50sAndOlder++; } var messageString = JsonConvert.SerializeObject(visitor); Task.Run(async() => { await AzureIoTHub.SendSQLToCloudMessageAsync(messageString); }); } catch (NullReferenceException e) { this.debugText.Text = string.Format("NullRefenrenceException source at 2: {0}", e.Source); log.WriteToFile(DateTime.Now.ToString("G", tw) + " Error at getting visitor informations\n"); } } temp_count++; } if (demographicsChanged) { this.ageGenderDistributionControl.UpdateData(this.demographics); } this.overallStatsControl.UpdateData(this.demographics); } }