//Use the camera to take the picture and call microsoft cognitive service emotion api to find out the emotion of user private async void TakePicture_Clicked(object sender, System.EventArgs e) { //Check permission of using camera var cameraStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Camera); var storageStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage); if (cameraStatus != PermissionStatus.Granted || storageStatus != PermissionStatus.Granted) { var results = await CrossPermissions.Current.RequestPermissionsAsync(new[] { Permission.Camera, Permission.Storage }); cameraStatus = results[Permission.Camera]; storageStatus = results[Permission.Storage]; } //successfully taken the photo if (cameraStatus == PermissionStatus.Granted && storageStatus == PermissionStatus.Granted) { button.IsEnabled = false; button2.IsEnabled = false; var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions { DefaultCamera = Plugin.Media.Abstractions.CameraDevice.Front, Directory = "Phase2", Name = $"{DateTime.UtcNow}.jpg", CompressionQuality = 92 }); //if photo has not been taken, enable the buttons if (file == null) { button.IsEnabled = true; button2.IsEnabled = true; return; } //use the api key to call the external api to find out the emotion try { string emotionKey = "ff39772d2a764944a93eee520503eb4b"; EmotionServiceClient emotionClient = new EmotionServiceClient(emotionKey); //get the unprocessed result. var emotionResults = await emotionClient.RecognizeAsync(file.GetStream()); var temp = emotionResults[0].Scores; //store the result emotion of the client var clientEmotion = temp.ToRankedList().First().Key; //build the emotion model using the user facebook id and their emotion and the time it is taken. EmotionModel model = new EmotionModel() { Name = profile.Name, Emotion = clientEmotion, updateTime = DateTime.Now.ToString(), facebookId = profile.id }; //change to food page to confirm if the client wants to order the dish. ChangeToFoodPage(model); button.IsEnabled = true; button2.IsEnabled = true; } catch (Exception ex) { errorLabel.Text = ex.Message; } } else { await DisplayAlert("Permissions Denied", "Unable to take photos.", "OK"); } }
public async Task AddTimeLine(EmotionModel timeline) { await emotionTable.InsertAsync(timeline); }
//Change page after photo has been taken private async void ChangeToFoodPage(EmotionModel _emotionModel) { await Navigation.PushAsync(new FoodPage(_emotionModel, profile)); }
//basic layout of the page public FoodPage(EmotionModel _emotionModel, FaceBookModel profile) { InitializeComponent(); NavigationPage.SetHasBackButton(this, false); this._emotionModel = _emotionModel; this.profile = profile; var image = new Image { Aspect = Aspect.AspectFit }; var layout = new StackLayout { Padding = new Thickness(5, 10) }; //preview of the dishes based on the emotion if (_emotionModel.Emotion.ToLower() == "happiness") { image.Source = ImageSource.FromFile("happyfood.jpg"); label = new Label { Text = "You look happy. Do you want it?", TextColor = Color.FromHex("#77d065"), FontSize = 20 }; } else if (_emotionModel.Emotion.ToLower() == "anger") { image.Source = ImageSource.FromFile("angryfood.jpg"); label = new Label { Text = "You look angry. Do you want it?", TextColor = Color.FromHex("#77d065"), FontSize = 20 }; } else if (_emotionModel.Emotion.ToLower() == "contempt") { image.Source = ImageSource.FromFile("contemptfood.jpg"); label = new Label { Text = "You look contempt. Do you want it?", TextColor = Color.FromHex("#77d065"), FontSize = 20 }; } else if (_emotionModel.Emotion.ToLower() == "disgust") { image.Source = ImageSource.FromFile("disgustfood.jpg"); label = new Label { Text = "You look disgust. Do you want it?", TextColor = Color.FromHex("#77d065"), FontSize = 20 }; } else if (_emotionModel.Emotion.ToLower() == "fear") { image.Source = ImageSource.FromFile("fearfood.jpg"); label = new Label { Text = "You look fear. Do you want it?", TextColor = Color.FromHex("#77d065"), FontSize = 20 }; } else if (_emotionModel.Emotion.ToLower() == "neutral") { image.Source = ImageSource.FromFile("neutralfood.jpg"); label = new Label { Text = "You look alright. Do you want it?", TextColor = Color.FromHex("#77d065"), FontSize = 20 }; } else if (_emotionModel.Emotion.ToLower() == "sadness") { image.Source = ImageSource.FromFile("sadnessfood.jpg"); label = new Label { Text = "You look sad. Do you want it?", TextColor = Color.FromHex("#77d065"), FontSize = 20 }; } else if (_emotionModel.Emotion.ToLower() == "surprise") { image.Source = ImageSource.FromFile("surprisefood.jpg"); label = new Label { Text = "You look surprised. Do you want it?", TextColor = Color.FromHex("#77d065"), FontSize = 20 }; } this.Content = layout; button = new Button { Text = "Order the food", BackgroundColor = Color.Silver, TextColor = Color.White, BorderRadius = 0 }; button.Clicked += SendOrder; layout.Children.Add(image); layout.Children.Add(label); layout.Children.Add(button); }