async void OnTakePhotoButtonClicked(object sender, EventArgs e)
        {
            await CrossMedia.Current.Initialize();

            // Take photo
            if (CrossMedia.Current.IsCameraAvailable || CrossMedia.Current.IsTakePhotoSupported)
            {
                // Select an image from camera roll (if using a emulator)
                photo = await CrossMedia.Current.PickPhotoAsync();

                // Take a photo with your device instead

                /*
                 * photo = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
                 * {
                 *  Name = "emotion.jpg",
                 *  PhotoSize = PhotoSize.Small
                 * });
                 */

                if (photo != null)
                {
                    image.Source = ImageSource.FromStream(photo.GetStream);
                }
            }
            else
            {
                await DisplayAlert("No Camera", "Camera unavailable.", "OK");
            }

            // Recognize emotion
            try
            {
                if (photo != null)
                {
                    var faceAttributes = new FaceAttributeType[] { FaceAttributeType.Emotion, FaceAttributeType.Age, FaceAttributeType.Gender };
                    using (var photoStream = photo.GetStream())
                    {
                        ((Button)sender).IsEnabled  = false;
                        activityIndicator.IsRunning = true;

                        Face[] faces = await _faceRecognitionService.DetectAsync(photoStream, true, false, faceAttributes);

                        if (faces.Any())
                        {
                            // Emotions detected are happiness, sadness, surprise, anger, fear, contempt, disgust, or neutral.
                            emotionResultLabel.Text = faces.FirstOrDefault().FaceAttributes.Emotion.ToRankedList().FirstOrDefault().Key;

                            ((Button)sender).IsEnabled  = true;
                            activityIndicator.IsRunning = false;
                        }
                        photo.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
Exemplo n.º 2
0
        async void OnTakePhotoButtonClicked(object sender, EventArgs e)
        {
            await CrossMedia.Current.Initialize();

            if (CrossMedia.Current.IsCameraAvailable || CrossMedia.Current.IsTakePhotoSupported)
            {
                photo = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
                {
                    Name      = "emotion.jpg",
                    PhotoSize = PhotoSize.Small
                });

                if (photo != null)
                {
                    image.Source = ImageSource.FromStream(photo.GetStream);
                }
            }
            else
            {
                await DisplayAlert("No Camera", "Camera unavailable", "Ok");
            }

            ((Button)sender).IsEnabled  = false;
            activityIndicator.IsRunning = true;

            try
            {
                if (photo != null)
                {
                    var faceAttributes = new FaceAttributeType[] { FaceAttributeType.Emotion };
                    using (var photoStream = photo.GetStream())
                    {
                        Face[] faces = await faceRecognitionService.DetectAsync(photoStream, true, false, faceAttributes);

                        if (faces.Any())
                        {
                            emotionResultLabel.Text = faces.FirstOrDefault().FaceAttributes.Emotion.ToRankedList().FirstOrDefault().Key;
                        }
                        photo.Dispose();
                    }
                }
            }
            catch (FaceAPIException fx)
            {
                Debug.WriteLine(fx.Message);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            activityIndicator.IsRunning = false;
            ((Button)sender).IsEnabled  = true;
        }
Exemplo n.º 3
0
        async void OnTakePhotoButtonClicked(object sender, EventArgs e)
        {
            await CrossMedia.Current.Initialize();

            // Take photo
            if (CrossMedia.Current.IsCameraAvailable || CrossMedia.Current.IsTakePhotoSupported)
            {
                emotionResultLabel.Text = null;
                photo = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
                {
                    Name      = "emotion.jpg",
                    PhotoSize = PhotoSize.Small
                });

                if (photo != null)
                {
                    image.Source = ImageSource.FromStream(photo.GetStream);
                }
            }
            else
            {
                await DisplayAlert("No Camera", "Camera unavailable.", "OK");
            }

            ((Button)sender).IsEnabled  = false;
            activityIndicator.IsRunning = true;

            // Recognize emotion
            try
            {
                if (photo != null)
                {
                    var faceAttributes = new FaceAttributeType[] { FaceAttributeType.Emotion };
                    var photoStream    = photo.GetStream();

                    try
                    {
                        Face[] faces = await _faceRecognitionService.DetectAsync(photoStream, true, false, faceAttributes);

                        while (!faces.Any())
                        {
                            faces = await _faceRecognitionService.DetectAsync(photoStream, true, false, faceAttributes);

                            emotionResultLabel.Text = faces.FirstOrDefault().FaceAttributes.Emotion.ToRankedList().FirstOrDefault().Key;
                        }
                        emotionResultLabel.Text = faces.FirstOrDefault().FaceAttributes.Emotion.ToRankedList().FirstOrDefault().Key;


                        if (faces.Any())
                        {
                            emotionResultLabel.Text = faces.FirstOrDefault().FaceAttributes.Emotion.ToRankedList().FirstOrDefault().Key;
                        }

                        photo.Dispose();
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                    }


                    //using (var photoStream = photo.GetStream())
                    //{
                    //    Face[] faces = await _faceRecognitionService.DetectAsync(photoStream, true, false, faceAttributes);
                    //    await DisplayAlert("Photo using", "Début de traitement de votre photo.", "OK");
                    //    if (faces.Any())
                    //    {
                    //        await DisplayAlert("faces Any", "test.", "OK");
                    //        // Emotions detected are happiness, sadness, surprise, anger, fear, contempt, disgust, or neutral.
                    //        emotionResultLabel.Text = faces.FirstOrDefault().FaceAttributes.Emotion.ToRankedList().FirstOrDefault().Key;
                    //    }
                    //    if (emotionResultLabel.Text == null)
                    //    {
                    //        await DisplayAlert("No Photo", "Désolé on n'a pas pu traiter votre photo.", "OK");
                    //        emotionResultLabel.Text = "Désolé on n'a pas pu traiter votre photo";
                    //    }
                    //    photo.Dispose();
                    //}
                }
                if (emotionResultLabel.Text == null)
                {
                    emotionResultLabel.Text = "Désolé on n'a pas pu traiter votre photo";
                }
            }
            catch (FaceAPIException fx)
            {
                Debug.WriteLine(fx.Message);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            activityIndicator.IsRunning = false;
            ((Button)sender).IsEnabled  = true;
        }