private async Task <List <ReferenceRatio> > ocrImage(byte[] bytes) { // Analyze image to recognize text VisionServiceClient client = new VisionServiceClient(VISION_API_KEY, VISION_API_ENDPOINT); OcrResults results = null; try { results = await client.RecognizeTextAsync(new MemoryStream(bytes), LANGUAGE, false /* detectRotation */); } catch (Exception ex) { Debug.WriteLine($"Error analyzing image: {ex.Message}"); return(null); // Abort } // Print recognized text to the console Debug.WriteLine($"Got {results.Regions?.Count()} results"); List <ReferenceRatio> references = new List <ReferenceRatio>(); foreach (var region in results.Regions) { // Flatten lines of words into a list of words List <Word> words = region.Lines.ToList().SelectMany(line => line.Words).ToList(); Debug.WriteLine($"Words: {words.Select(word => word.Text).Aggregate((word1, word2) => word1 + " " + word2)}"); // Look for latitude or longitude reference text for (int i = 0; i < words.Count; ++i) { if (words[i].Text.ToLower() == "latitude" || words[i].Text.ToLower() == "longitude") { // Parse axis PolarAxis axis = words[i].Text.ToLower() == "latitude" ? PolarAxis.Latitude : PolarAxis.Longitude; // Parse pixel value int pixel; try { pixel = int.Parse(words[i + 1].Text); } catch { Debug.WriteLine($"Failed to parse pixel value: {words[i + 1].Text}"); continue; } // Parse polar value float polar; try { polar = float.Parse(words[i + 2].Text); } catch { Debug.WriteLine($"Failed to parse polar value: {words[i + 2].Text}"); continue; } // Create reference ratio from detected values var reference = new ReferenceRatio(pixel, polar, axis); Debug.WriteLine($"Created reference: {reference}"); references.Add(reference); } } } return(references); }
// Draw a reference line on the canvas for debugging purposes private static void drawReferenceLine(SKCanvas canvas, Point origin, Size size, ReferenceRatio reference, SKPaint paint) { switch (reference.Axis) { case ReferenceRatio.PolarAxis.Latitude: // Draw horizontal latitude line float y = (float)(origin.Y + reference.Pixel); canvas.DrawLine((float)origin.X, y, (float)(origin.X + size.Width), y, paint); canvas.DrawText(reference.Polar.ToString(), (float)((size.Width / 2.0f) + origin.X), y, paint); break; case ReferenceRatio.PolarAxis.Longitude: // Draw vertical longitude line float x = (float)(origin.X + reference.Pixel); canvas.DrawLine(x, (float)origin.Y, x, (float)(origin.Y + size.Height), paint); canvas.DrawText(reference.Polar.ToString(), x, (float)((size.Height / 2.0f) + origin.Y), paint); break; default: throw new ArgumentException($"Unknown polar axis: {reference.Axis}. When did that get invented?!"); } }