예제 #1
0
        private async void addPersonButton_Click(object sender, RoutedEventArgs e)
        {
            testInternetConnection();

            if (internetConnection)
            {
                thisPerson = new Person();

                var result = await PersonDialog.ShowAsync();

                // Value 1 indicates primary button was selected by the user, which adds a new Person
                if ((int)result == 1)
                {
                    //Apply the Data
                    showProgressDialog("Apply the Data...");
                    thisPerson.Name       = PersonNameInput.Text;
                    thisPerson.Relation   = PersonRelationInput.Text;
                    thisPerson.RiskFactor = (int)riskSlider.Value;
                    thisPerson.PatientId  = PatientId;

                    //Upload Person Info
                    progressDialog.Content = "Upload Person Info...";
                    await AzureDatabaseService.UploadPersonInfo(thisPerson);

                    thisPerson = await AzureDatabaseService.GetPersonFromNameAndRelation(thisPerson.Name, thisPerson.Relation, thisPerson.PatientId);

                    //statusTextBlock.Text = "thisPerson.Id = " + thisPerson.Id;

                    //Upload Face Info
                    progressDialog.Content = "Upload Faces and Images...";
                    foreach (Face face in addFaceList)
                    {
                        face.PersonId = thisPerson.Id;
                    }
                    await AzureDatabaseService.UploadFaceInfo(thisPerson.Id, addFaceList);

                    foreach (Face face in deleteFaceList)
                    {
                        await AzureDatabaseService.DeleteFace(face);
                    }

                    //Reload Persons List
                    loadPersonsAndCheckEnquiry();

                    //Add a Person Finished!
                    progressDialog.Content = "Add a Person Finished !";
                    progressDialog.IsPrimaryButtonEnabled = true;
                } //end result == 1
            }     //end internetConnection

            initializePersonInfoDialog();
        }//end addPersonButton_Click
예제 #2
0
        /// <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)
        }