public void Basic1() { var(isBuying, items) = AzureOcr.ParseItems(ITEMS_EXAMPLE_1); Assert.AreEqual(true, isBuying); Assert.AreEqual(6, items.Length); }
public Task Execute() { var dispatcher = Dispatcher.CurrentDispatcher; var imageViewModel = mImages.Current.Value !; imageViewModel.IsAnalyzing.Value = true; return(Task.Run(async() => { var fullImage = SKImage.FromEncodedData(imageViewModel.FilePath); var attempts = 1; while (fullImage == null && attempts < 10) { // Errored.. Image probably not fully written yet await Task.Delay(attempts * 100).ConfigureAwait(false); fullImage = SKImage.FromEncodedData(imageViewModel.FilePath); attempts++; } if (fullImage == null) { imageViewModel.IsAnalyzing.Value = false; // TODO: Report return; } using var itemsImageStream = new MemoryStream(); fullImage.Subset(mImages.ItemsRectangle.Value) .Encode(SKEncodedImageFormat.Png, 100) .SaveTo(itemsImageStream); itemsImageStream.Position = 0; using var ocr = new AzureOcr(mSettings.ApiKey.Value, mSettings.ApiEndPoint.Value); var itemsText = ocr.ExtractTextAsync(itemsImageStream); using var locationImageStream = new MemoryStream(); fullImage.Subset(mImages.LocationRectangle.Value) .Encode(SKEncodedImageFormat.Png, 100) .SaveTo(locationImageStream); locationImageStream.Position = 0; try { var locationText = await ocr.ExtractTextAsync(locationImageStream).ConfigureAwait(false); var(isBuying, items) = AzureOcr.ParseItems(await itemsText.ConfigureAwait(false)); await dispatcher.BeginInvoke(() => { imageViewModel.IsAnalyzing.Value = false; imageViewModel.IsAnalyzed.Value = true; mLocations.AddLocation(locationText, items, isBuying); }); } catch (TaskCanceledException) { imageViewModel.IsAnalyzing.Value = false; imageViewModel.IsAnalyzed.Value = false; } catch (Exception) { imageViewModel.IsAnalyzing.Value = false; imageViewModel.IsAnalyzed.Value = false; } })); }