Esempio n. 1
0
        public static string GetPhotoEmotionScore(List <Emotion> emotionResults, int emotionResultNumber, EmotionType currentEmotionType)
        {
            double rawEmotionScore;

            var isInternetConnectionAvilable = Connectivity.NetworkAccess.Equals(NetworkAccess.Internet);

            if (!isInternetConnectionAvilable)
            {
                return(ErrorMessageDictionary[ErrorMessageType.ConnectionToCognitiveServicesFailed]);
            }

            if (emotionResults == null || emotionResults.Count < 1)
            {
                return(ErrorMessageDictionary[ErrorMessageType.NoFaceDetected]);
            }

            if (emotionResults.Count > 1)
            {
                OnMultipleFacesDetectedAlertTriggered();
                return(ErrorMessageDictionary[ErrorMessageType.MultipleFacesDetected]);
            }

            try
            {
                switch (currentEmotionType)
                {
                case EmotionType.Anger:
                    rawEmotionScore = emotionResults[emotionResultNumber].Anger;
                    break;

                case EmotionType.Contempt:
                    rawEmotionScore = emotionResults[emotionResultNumber].Contempt;
                    break;

                case EmotionType.Disgust:
                    rawEmotionScore = emotionResults[emotionResultNumber].Disgust;
                    break;

                case EmotionType.Fear:
                    rawEmotionScore = emotionResults[emotionResultNumber].Fear;
                    break;

                case EmotionType.Happiness:
                    rawEmotionScore = emotionResults[emotionResultNumber].Happiness;
                    break;

                case EmotionType.Neutral:
                    rawEmotionScore = emotionResults[emotionResultNumber].Neutral;
                    break;

                case EmotionType.Sadness:
                    rawEmotionScore = emotionResults[emotionResultNumber].Sadness;
                    break;

                case EmotionType.Surprise:
                    rawEmotionScore = emotionResults[emotionResultNumber].Surprise;
                    break;

                default:
                    return(ErrorMessageDictionary[ErrorMessageType.GenericError]);
                }

                var emotionScoreAsPercentage = ConversionService.ConvertDoubleToPercentage(rawEmotionScore);

                return(emotionScoreAsPercentage);
            }
            catch (Exception e)
            {
                AnalyticsHelpers.Report(e);
                return(ErrorMessageDictionary[ErrorMessageType.GenericError]);
            }
        }
Esempio n. 2
0
        public static async Task <string> GetPhotoEmotionScore(Emotion[] emotionResults, int emotionResultNumber, EmotionType currentEmotionType)
        {
            float rawEmotionScore;

            var isInternetConnectionAvilable = await ConnectionService.IsInternetConnectionAvailable().ConfigureAwait(false);

            if (!isInternetConnectionAvilable)
            {
                return(ErrorMessageDictionary[ErrorMessageType.ConnectionToCognitiveServicesFailed]);
            }

            if (emotionResults == null || emotionResults.Length < 1)
            {
                return(ErrorMessageDictionary[ErrorMessageType.NoFaceDetected]);
            }

            if (emotionResults.Length > 1)
            {
                OnMultipleFacesDetectedAlertTriggered();
                return(ErrorMessageDictionary[ErrorMessageType.MultipleFacesDetected]);
            }

            try
            {
                switch (currentEmotionType)
                {
                case EmotionType.Anger:
                    rawEmotionScore = emotionResults[emotionResultNumber].Scores.Anger;
                    break;

                case EmotionType.Contempt:
                    rawEmotionScore = emotionResults[emotionResultNumber].Scores.Contempt;
                    break;

                case EmotionType.Disgust:
                    rawEmotionScore = emotionResults[emotionResultNumber].Scores.Disgust;
                    break;

                case EmotionType.Fear:
                    rawEmotionScore = emotionResults[emotionResultNumber].Scores.Fear;
                    break;

                case EmotionType.Happiness:
                    rawEmotionScore = emotionResults[emotionResultNumber].Scores.Happiness;
                    break;

                case EmotionType.Neutral:
                    rawEmotionScore = emotionResults[emotionResultNumber].Scores.Neutral;
                    break;

                case EmotionType.Sadness:
                    rawEmotionScore = emotionResults[emotionResultNumber].Scores.Sadness;
                    break;

                case EmotionType.Surprise:
                    rawEmotionScore = emotionResults[emotionResultNumber].Scores.Surprise;
                    break;

                default:
                    return(ErrorMessageDictionary[ErrorMessageType.GenericError]);
                }

                var emotionScoreAsPercentage = ConversionService.ConvertFloatToPercentage(rawEmotionScore);

                return(emotionScoreAsPercentage);
            }
            catch (Exception e)
            {
                AnalyticsHelpers.Report(e);
                return(ErrorMessageDictionary[ErrorMessageType.GenericError]);
            }
        }