private async void startButton_Click(object sender, RoutedEventArgs e) { startButton.IsEnabled = false; startButton.Content = "Please Wait..."; //Perform initialization for facial detection. await FacialSimilarity.TrainDetectionAsync(); //Open the camera changeCameraState(); listenAgainButton.Visibility = Visibility.Visible; stopButton.Visibility = Visibility.Visible; startButton.Visibility = Visibility.Collapsed; startButton.IsEnabled = true; startButton.Content = "Start"; ////Set up the timer checkThisPhraseTimer = ThreadPoolTimer.CreatePeriodicTimer(async(source) => { checkVoiceFeedback(); // Update the UI thread by using the UI core dispatcher. await Dispatcher.RunAsync(CoreDispatcherPriority.High, () => { if (thisPhrase.Contains("stranger")) { listenAgainButton.Content = "Stranger"; } }); }, checkThisPhraseTimeSpan); }
/// <summary> /// This method is called when the app first appears, but it also checks if it has been launched /// using Cortana's voice commands, and takes appropriate action if this is the case. /// </summary> protected async override void OnNavigatedTo(NavigationEventArgs e) { _dispatcher = CoreWindow.GetForCurrentThread().Dispatcher; _presence = new UserPresence(_dispatcher, _unfilteredName); _pageParameters = e.Parameter as VoiceCommandObjects.VoiceCommand; if (_pageParameters != null) { switch (_pageParameters.VoiceCommandName) { case "addNewNote": _activeNote = CreateNote(App.EVERYONE); break; case "addNewNoteForPerson": _activeNote = CreateNote(_pageParameters.NoteOwner); break; default: break; } } //Perform initialization for facial detection. if (AppSettings.FaceApiKey != "") { await FacialSimilarity.TrainDetectionAsync(); } // Perform initialization for speech recognition. _speechManager = new SpeechManager(FamilyModel); _speechManager.PhraseRecognized += speechManager_PhraseRecognized; _speechManager.StateChanged += speechManager_StateChanged; await _speechManager.StartContinuousRecognition(); }
private async void AddNewPersonDialog(Person currentPerson) { var dialog = new AddPersonContentDialog(); dialog.ProvideExistingPerson(currentPerson); await dialog.ShowAsync(); Person newPerson = dialog.AddedPerson; // If there is a valid person to add, add them if (newPerson != null) { // Get or create a directory for the user (we do this regardless of whether or not there is a profile picture) StorageFolder userFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(("Users\\" + newPerson.FriendlyName), CreationCollisionOption.OpenIfExists); // See if we have a profile photo if (dialog.TemporaryFile != null) { // Save off the profile photo and delete the temporary file await dialog.TemporaryFile.CopyAsync(userFolder, "ProfilePhoto.jpg", NameCollisionOption.ReplaceExisting); await dialog.TemporaryFile.DeleteAsync(); // Update the profile picture for the person newPerson.IsProfileImage = true; newPerson.ImageFileName = userFolder.Path + "\\ProfilePhoto.jpg"; if (AppSettings.FaceApiKey != "") { await FacialSimilarity.AddTrainingImageAsync(newPerson.FriendlyName, new Uri($"ms-appdata:///local/Users/{newPerson.FriendlyName}/ProfilePhoto.jpg")); } } // Add the user if it is new now that changes have been made if (currentPerson == null) { await FamilyModel.AddPersonAsync(newPerson); } // Otherwise we had a user, so update the current one else { //await FamilyModel.UpdatePersonImageAsync(newPerson); Person personToUpdate = FamilyModel.PersonFromName(currentPerson.FriendlyName); if (personToUpdate != null) { personToUpdate.IsProfileImage = true; personToUpdate.ImageFileName = userFolder.Path + "\\ProfilePhoto.jpg"; } } } }
private async void AddNewPersonFromDB(Person person, ObservableCollection <Face> faces) { //var dialog = new AddPersonContentDialog(); //dialog.ProvideExistingPerson(currentPerson); //await dialog.ShowAsync(); //dialog.AddedPerson = person; //Person newPerson = dialog.AddedPerson; Person newPerson = person; // If there is a valid person to add, add them if (newPerson != null) { try { // Get or create a directory for the user (we do this regardless of whether or not there is a profile picture) StorageFolder userFolder; if (person.FriendlyName != null && person.FriendlyName != "" && person.FriendlyName.Contains("stranger")) { userFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(("Users\\" + newPerson.FriendlyName), CreationCollisionOption.FailIfExists); } else { userFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(("Users\\" + newPerson.Name), CreationCollisionOption.FailIfExists); } StorageFile userFile = await userFolder.CreateFileAsync("ProfilePhoto.jpg", CreationCollisionOption.OpenIfExists); foreach (Face face in faces) { await AzureBlobService.DownloadFaceImageAsFile(face, userFile); } newPerson.IsProfileImage = true; newPerson.ImageFileName = userFolder.Path + "\\ProfilePhoto.jpg"; } catch { } // See if we have a profile photo //if (dialog.TemporaryFile != null) //{ // Save off the profile photo and delete the temporary file //await dialog.TemporaryFile.CopyAsync(userFolder, "ProfilePhoto.jpg", NameCollisionOption.GenerateUniqueName); //await dialog.TemporaryFile.DeleteAsync(); // Update the profile picture for the person await FacialSimilarity.AddTrainingImageAsync(newPerson.FriendlyName, new Uri($"ms-appdata:///local/Users/{newPerson.FriendlyName}/ProfilePhoto.jpg")); person = newPerson; //} // Add the user if it is new now that changes have been made //if (currentPerson == null) //{ // await FamilyModel.AddPersonAsync(newPerson); //} // Otherwise we had a user, so update the current one //else //{ // //await FamilyModel.UpdatePersonImageAsync(newPerson); // Person personToUpdate = FamilyModel.PersonFromName(currentPerson.FriendlyName); // if (personToUpdate != null) // { // personToUpdate.IsProfileImage = true; // personToUpdate.ImageFileName = userFolder.Path + "\\ProfilePhoto.jpg"; // } //} } }
/// <summary> /// Retrieves the count of detected faces /// </summary> /// <param name="faces">The list of detected faces from the FaceDetected event of the effect</param> private async void CountDetectedFaces(IReadOnlyList <DetectedFace> faces) { FaceCount = $"{_detectionString} {faces.Count.ToString()}"; // If we detect any faces, kill our no faces timer if (faces.Count != 0) { if (_noFacesTimer != null) { _noFacesTimer.Dispose(); } } // Otherwise, if we are filtering and don't have a timer else if (_currentlyFiltered && (_noFacesTimer == null)) { // Create a callback TimerCallback noFacesCallback = (object stateInfo) => { _dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { OnFilterOnFace(_unfilteredName); _noFacesTimer = null; }); _noFacesTimer.Dispose(); }; // Set our timer _noFacesTimer = new Timer(noFacesCallback, null, NoFacesTime, Timeout.Infinite); } // We are also going to take an image the first time that we detect exactly one face. // Sidenote - to avoid a race-condition, I had to use a boolean. Just checking for _faceCaptureStill == null could produce an error. if ((faces.Count == 1) && !_holdForTimer) // && !_currentlyFiltered { // Kick off the timer so we don't keep taking pictures, but will resubmit if we are not filtered _holdForTimer = true; //MainPage.isOnTime = false; // Take the picture _faceCaptureStill = await ApplicationData.Current.LocalFolder.CreateFileAsync("FaceDetected.jpg", CreationCollisionOption.ReplaceExisting); await _mediaCapture.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateJpeg(), _faceCaptureStill); if (((App)Application.Current).AppSettings.FaceApiKey != "" && FacialSimilarity.InitialTrainingPerformed) { var UserName = await FacialSimilarity.CheckForUserAsync(new Uri("ms-appdata:///local/FaceDetected.jpg")); if (UserName != "") { OnFilterOnFace(UserName); } else //strange face is detected { #region a strange face is detected //Voice Feedback //await MainPage._speechManager.SpeakAsync("Stranger! Sending the enquiry", MainPage.mediaElement); //Stranger Notice MainPage.thisPhrase = "stranger! Sending the enquiry!"; //stranger1 //Create a new Person thisPerson = new Person(); thisPerson.Name = "stranger"; thisPerson.FriendlyName = "stranger"; thisPerson.Relation = ""; thisPerson.PatientId = MainPage.PatientId; await AzureDatabaseService.UploadPersonInfo(thisPerson); thisPerson = new Person(); thisPerson = await AzureDatabaseService.GetLatestPerson(); thisPerson.FriendlyName += thisPerson.Id; await AzureDatabaseService.UploadPersonInfo(thisPerson); //Create a new Face thisFace = new Face(); // Prepare the captured image StorageFile newImageFile = await ApplicationData.Current.LocalFolder.GetFileAsync("FaceDetected.jpg"); //StorageFolder newPersonFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(("Users\\" + thisPerson.FriendlyName), CreationCollisionOption.OpenIfExists); //await newImageFile.CopyAsync(newPersonFolder, "ProfilePhoto.jpg", NameCollisionOption.ReplaceExisting); string faToken = null; IRandomAccessStream irandom = await newImageFile.OpenAsync(FileAccessMode.Read); faToken = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(newImageFile); thisFace.Image = new BitmapImage(); await thisFace.Image.SetSourceAsync(irandom); thisFace.ImageToken = faToken; thisFace.IsDefault = true; //Upload the Face //addFaceList.Add(thisFace); await AzureDatabaseService.UploadOneFaceInfo(thisPerson.Id, thisFace); //addFaceList.Clear(); thisFace = new Face(); thisFace = await AzureDatabaseService.GetLatestFace(); ObservableCollection <Face> newFaces = new ObservableCollection <Face>(); newFaces.Add(thisFace); MainPage.Persons.Add(thisPerson); //MainPage.Faces.Add(newFaces); //Upload the image //await AzureBlobService.UploadImageFromImageToken(faToken, thisFace.Id + ".jpg"); Face updatedFace = await AzureDatabaseService.UpdateFaceImageAddress(thisFace); thisFace = updatedFace; await AzureDatabaseService.UpdatePersonDefaultImageAddress(thisPerson.Id, thisFace); thisPerson = await AzureDatabaseService.GetLatestPerson(); MainPage.Persons.Add(thisPerson); //Upload a Stranger Warning Enquiry thisWarning.PersonId = thisPerson.Id; await AzureDatabaseService.UploadWarning(thisWarning); // Update the profile picture for the person //await FacialSimilarity.AddTrainingImageAsync(thisPerson.FriendlyName, new Uri($"ms-appdata:///local/Users/{thisPerson.FriendlyName}/ProfilePhoto.jpg")); AddNewPersonFromDB(thisPerson, newFaces); MainPage.thisPhrase = "Stranger! Be careful!"; //stranger2 //StorageFolder folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Media"); //StorageFile file = await folder.GetFileAsync("dong.wav"); //var soundStream = await file.OpenAsync(FileAccessMode.Read); //MainPage.mediaElement.SetSource(soundStream, file.ContentType); //MainPage.mediaElement.Play(); //await MainPage._speechManager.SpeakAsync("Stranger", MainPage.mediaElement); ////Perform initialization for facial detection. //await FacialSimilarity.TrainDetectionAsync(); #endregion } } // Allow the camera to take another picture in 10 seconds TimerCallback callback = (Object stateInfo) => { // Now that the timer is expired, we no longer need to hold // Nothing else to do since the timer will be restarted when the picture is taken _holdForTimer = false; if (_pictureTimer != null) { _pictureTimer.Dispose(); } }; _pictureTimer = new Timer(callback, null, 5000, Timeout.Infinite); }//end if ((faces.Count == 1) && !_holdForTimer && !_currentlyFiltered) }
/// <summary> /// Retrieves the count of detected faces /// </summary> /// <param name="faces">The list of detected faces from the FaceDetected event of the effect</param> private async void CountDetectedFaces(IReadOnlyList <DetectedFace> faces) { FaceCount = $"{_detectionString} {faces.Count.ToString()}"; // If we detect any faces, kill our no faces timer if (faces.Count != 0) { if (_noFacesTimer != null) { _noFacesTimer.Dispose(); } } // Otherwise, if we are filtering and don't have a timer else if (_currentlyFiltered && (_noFacesTimer == null)) { // Create a callback TimerCallback noFacesCallback = (object stateInfo) => { _dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { OnFilterOnFace(_unfilteredName); _noFacesTimer = null; }); _noFacesTimer.Dispose(); }; // Set our timer _noFacesTimer = new Timer(noFacesCallback, null, NoFacesTime, Timeout.Infinite); } // We are also going to take an image the first time that we detect exactly one face. // Sidenote - to avoid a race-condition, I had to use a boolean. Just checking for _faceCaptureStill == null could produce an error. if ((faces.Count == 1) && !_holdForTimer && !_currentlyFiltered) { // Kick off the timer so we don't keep taking pictures, but will resubmit if we are not filtered _holdForTimer = true; // Take the picture _faceCaptureStill = await ApplicationData.Current.LocalFolder.CreateFileAsync("FaceDetected.jpg", CreationCollisionOption.ReplaceExisting); await _mediaCapture.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateJpeg(), _faceCaptureStill); if (((App)Application.Current).AppSettings.FaceApiKey != "" && FacialSimilarity.InitialTrainingPerformed) { var UserName = await FacialSimilarity.CheckForUserAsync(new Uri("ms-appdata:///local/FaceDetected.jpg")); if (UserName != "") { OnFilterOnFace(UserName); } } // Allow the camera to take another picture in 10 seconds TimerCallback callback = (Object stateInfo) => { // Now that the timer is expired, we no longer need to hold // Nothing else to do since the timer will be restarted when the picture is taken _holdForTimer = false; if (_pictureTimer != null) { _pictureTimer.Dispose(); } }; _pictureTimer = new Timer(callback, null, 10000, Timeout.Infinite); } }
/// <summary> /// Test Internet Connection and Load Persons and Faces /// </summary> private async void testInternetConnection() { //Test Internet Connection try { showProgressDialog("Checking the Internet Connection and Loading the data..."); progressDialog.Title = "Processing: "; progressDialog.PrimaryButtonText = "Ok"; //statusTextBlock.Text = "Checking the Internet Connection..."; //Get Person List for this patient Persons.Clear(); ObservableCollection <Person> persons = await AzureDatabaseService.GetPersonList(PatientId); //statusTextBlock.Text = persons.Count + " Persons: "; //Clear Local User Folder StorageFolder userFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(("Users\\"), CreationCollisionOption.OpenIfExists); await userFolder.DeleteAsync(); userFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(("Users\\"), CreationCollisionOption.OpenIfExists); //Perform initialization for facial detection. await FacialSimilarity.TrainDetectionAsync(); //Download each person foreach (Person person in persons) { //Update Info person.DefaultIcon = await AzureBlobService.DisplayImageFile(person.DefaultImageAddress); Persons.Add(person); //Get Face List for each person ObservableCollection <Face> faces = await AzureDatabaseService.GetFaceList(person.Id); foreach (Face face in faces) { face.Image = await AzureBlobService.DisplayImageFile(face.ImageAddress); } Faces.Add(faces); AddNewPersonFromDB(person, faces); //statusTextBlock.Text += faces.Count + " faces "; } } catch (System.Net.Http.HttpRequestException) { internetConnection = false; //statusTextBlock.Text = " "; //showProgressDialog("Internet Connection Error! Please check your Internet connection."); progressDialog.Title = "Internet Connection Error!"; progressDialog.PrimaryButtonText = "Ok, I know"; progressDialog.IsPrimaryButtonEnabled = true; //await progressDialog.ShowAsync(); return; //Test Internet Connection Again //testInternetConnection(); } internetConnection = true; //statusTextBlock.Text = "Internet Connection Success!"; progressDialog.Content = "Success!"; progressDialog.IsPrimaryButtonEnabled = true; return; }