private async Task AnalyzeFormFromFileAsync(StorageFile file) { if (file == null) { return; } try { IsReceipt = false; this.progressControl.IsActive = true; this.MultiPageResultCollection.Clear(); this.OverlayPresenter.TokenInfo = null; this.notFoundGrid.Visibility = Visibility.Collapsed; StorageFile imageFile = await file?.CopyAsync(ApplicationData.Current.TemporaryFolder, $"{Guid.NewGuid().ToString()}{file.FileType}"); if (imageFile != null) { IsImageSource = ImageExtensions.Contains(imageFile.FileType); if (IsImageSource) { this.OverlayPresenter.Source = new BitmapImage(new Uri(imageFile.Path)); } else { this.pdfViewerControl.Source = new Uri(imageFile.Path); } AnalyzeFormResult formResult = null; string fileType = IsImageSource ? FormRecognizerService.ImageJpegContentType : FormRecognizerService.PdfContentType; using (FileStream stream = new FileStream(imageFile.Path, FileMode.Open)) { formResult = await this.formRecognizerService.AnalyzeImageFormWithCustomModelAsync(CurrentFormModel.Id, stream, fileType); } ProcessFormResult(formResult); imageFile?.DeleteAsync(); } } catch (Exception ex) { await Util.GenericApiCallExceptionHandler(ex, "Form Recognizer error."); } finally { this.progressControl.IsActive = false; } }
private async Task <List <Tuple <TokenOverlayInfo, TokenOverlayInfo> > > AnalyzeFormFromFileAsync(StorageFile file) { if (file == null) { return(null); } try { AnalyzeFormResult result = null; string fileType = IsFormImageSource ? FormRecognizerService.ImageJpegContentType : FormRecognizerService.PdfContentType; using (FileStream stream = new FileStream(file.Path, FileMode.Open)) { result = await this.formRecognizerService.AnalyzeImageFormWithCustomModelAsync(FormRecognizerModelId, stream, fileType); } PageResult page = result?.PageResults.FirstOrDefault(); ReadResult pageInfo = result?.ReadResults.FirstOrDefault(); if (page != null && pageInfo != null) { double width = pageInfo.Width; double height = pageInfo.Height; var keyValuePairList = page.KeyValuePairs .Where(x => !string.IsNullOrEmpty(x.Key?.Text) && x.Key?.BoundingBox != null) // showing pairs with non-empty keys .Select(x => new Tuple <TokenOverlayInfo, TokenOverlayInfo>( new TokenOverlayInfo(x.Key, width, height), new TokenOverlayInfo(x.Value, width, height) )).ToList(); return(keyValuePairList); } } catch (Exception ex) { await Util.GenericApiCallExceptionHandler(ex, "Failure recognizing form"); } return(null); }
private void ProcessFormResult(AnalyzeFormResult result) { IList <PageResult> pages = result?.PageResults ?? new List <PageResult>(); this.MultiPageResultCollection.Clear(); foreach (var(page, index) in pages.Select((p, i) => (p, i))) { var keyValuePairList = page.KeyValuePairs .Where(x => !string.IsNullOrEmpty(x.Key?.Text) && x.Key?.BoundingBox != null) // showing pairs with non-empty keys .Select(x => new Tuple <List <TokenOverlayInfo>, List <TokenOverlayInfo> >( new List <TokenOverlayInfo>() { new TokenOverlayInfo(x.Key) { IsMuted = true } }, new List <TokenOverlayInfo>() { new TokenOverlayInfo(x.Value) { IsMuted = true } })); var multiPage = new MultiPageResultViewModel() { Title = $"Page {index + 1}", ExtractedKeyValuePairCollection = new ObservableCollection <Tuple <List <TokenOverlayInfo>, List <TokenOverlayInfo> > >(keyValuePairList), DataTableSourceCollection = new ObservableCollection <DataTable>(ListDataTableResult(page.Tables)) }; this.MultiPageResultCollection.Add(multiPage); } string jsonResult = result != null?JsonConvert.SerializeObject(result, Formatting.Indented) : string.Empty; this.PreparePageResult(jsonResult); }