public async Task DescribeImageAsync() { IsBusy = true; StatusMessage = null; string baseDescription = null; string facesRecognizedDescription = null; string emotionDescription = null; MessengerInstance.Send(new NotificationMessage(Constants.TakingPhoto)); try { StatusMessage = AppResources.QueryingVisionService; using (var stream = await streamingService.GetCurrentFrameAsync()) { if (stream != null) { var imageBytes = await stream.ToArrayAsync(); MessengerInstance.Send(new NotificationMessage <byte[]>(imageBytes, Constants.PhotoTaken)); if (await Network.IsInternetAvailableAsync()) { var result = await cognitiveClient.RecognizeAsync(stream, Language, RecognitionType.Vision | RecognitionType.Emotion, OnRecognitionProgress); var visionResult = result.VisionResult; if (visionResult.IsValid) { baseDescription = visionResult.Description; if (visionResult.IsTranslated) { if (Settings.ShowOriginalDescriptionOnTranslation) { baseDescription = $"{visionResult.TranslatedDescription} ({visionResult.Description})"; } else { baseDescription = visionResult.TranslatedDescription; } } if (Settings.ShowDescriptionConfidence) { baseDescription = $"{baseDescription} ({Math.Round(visionResult.Confidence, 2)})"; } // Analyzes emotion results. var emotionResults = result.EmotionResults; if (emotionResults.Any()) { var emotionMessages = new StringBuilder(); foreach (var emotionResult in emotionResults) { var emotionMessage = SpeechHelper.GetEmotionMessage(emotionResult.Gender, emotionResult.Age, emotionResult.Emotion); if (!string.IsNullOrWhiteSpace(emotionMessage)) { emotionMessages.Append(emotionMessage); } } // Describes how many faces have been recognized. if (emotionResults.Count() == 1) { facesRecognizedDescription = AppResources.FaceRecognizedSingular; } else { facesRecognizedDescription = $"{string.Format(AppResources.FacesRecognizedPlural, emotionResults.Count())} {Constants.SentenceEnd}"; } emotionDescription = emotionMessages.ToString(); } } else { if (Settings.ShowRawDescriptionOnInvalidRecognition && visionResult.RawDescription != null) { baseDescription = $"{AppResources.RecognitionFailed} ({visionResult.RawDescription}, {Math.Round(visionResult.Confidence, 2)})"; } else { baseDescription = AppResources.RecognitionFailed; } } } else { // Connection isn't available, the service cannot be reached. baseDescription = AppResources.NoConnection; } } else { baseDescription = AppResources.UnableToTakePhoto; } } } catch (Microsoft.ProjectOxford.Vision.ClientException) { // Unable to access the service (tipically, due to invalid registration keys). baseDescription = AppResources.UnableToAccessService; } catch (Microsoft.ProjectOxford.Common.ClientException ex) when(ex.Error.Code.ToLower() == "unauthorized") { // Unable to access the service (tipically, due to invalid registration keys). baseDescription = AppResources.UnableToAccessService; } catch (WebException) { // Internet isn't available, the service cannot be reached. baseDescription = AppResources.NoConnection; } catch (Exception ex) { var error = AppResources.RecognitionError; if (Settings.ShowExceptionOnError) { error = $"{error} ({ex.Message})"; } baseDescription = error; } // Shows and speaks the result. var message = $"{baseDescription}{Constants.SentenceEnd} {facesRecognizedDescription} {emotionDescription}"; StatusMessage = this.GetNormalizedMessage(message); await SpeechHelper.TrySpeechAsync(message); IsBusy = false; }
private async Task TakePhotoAsync() { IsBusy = true; string recognizeText = null; try { using (var stream = await mediaPicker.TakePhotoAsync()) { if (stream != null) { var imageBytes = await stream.ToArrayAsync(); MessengerInstance.Send(new NotificationMessage <byte[]>(imageBytes, Constants.PhotoTaken)); Message = null; if (await Network.IsInternetAvailableAsync()) { var result = await cognitiveClient.RecognizeAsync(stream, Language, RecognitionType.Text); var ocrResult = result.OcrResult; if (ocrResult.IsValid) { recognizeText = ocrResult.Text; } else { recognizeText = AppResources.UnableToRecognizeText; } } else { // Internet isn't available, the service cannot be reached. recognizeText = AppResources.NoConnection; } } else { // If message is null at this point, this is the first request. If we cancel it, turns automatically to the // previous page. if (message == null) { Navigator.GoBack(); } IsBusy = false; return; } } } catch (WebException) { // Internet isn't available, the service cannot be reached. recognizeText = AppResources.NoConnection; } catch (ClientException) { // Unable to access the service (tipically, due to invalid registration keys). recognizeText = AppResources.UnableToAccessService; } catch (Exception ex) { var error = AppResources.RecognitionError; if (Settings.ShowExceptionOnError) { error = $"{error} ({ex.Message})"; } recognizeText = error; } // Shows the result. Message = this.GetNormalizedMessage(recognizeText); IsBusy = false; }