public AsyncOCR(SImage image, frmBabel Form, Action <AsyncOCR> callback = null) { this.image = image.Copy(); this.Form = Form; this.callback += callback; if (Properties.Settings.Default.dummyData) { _bigBox = OCRBox.DummyBigBox(); _smallBoxes = OCRBox.DummySmallBoxes(); _timeStamp = "[dummy]"; isDone = true; callback?.Invoke(this); } else if (image == null) { _bigBox = null; _smallBoxes = new OCRBox[0]; _timeStamp = "[empty]"; isDone = true; callback?.Invoke(this); } else { task = Task.Run(DoOCR); } }
private async Task DoOCR() { try { string Identifer = Utility.RandomHex(); DebugLog.Log("Making OCR request [" + Identifer + "]"); if (!File.Exists(Properties.Settings.Default.apiKeyPath)) { throw new FileNotFoundException("Keyfile not present at " + Properties.Settings.Default.apiKeyPath); } // Wait for rate limiter before starting the clock GoogleAsyncStatic.rate.Check(); Stopwatch sw = new Stopwatch(); // Dump the provided image to a memory stream var stream = new MemoryStream(); image.Save(stream, ImageFormat.Png); stream.Position = 0; // Load the stream as a gimage GImage gimage = GImage.FromStream(stream); // Make our connection client ImageAnnotatorClient client = new ImageAnnotatorClientBuilder { CredentialsPath = Properties.Settings.Default.apiKeyPath, }.Build(); // Ask for OCR sw.Start(); var response = await client.DetectTextAsync(gimage); sw.Stop(); // If we didn't get anything back if (response.Count == 0) { _bigBox = OCRBox.ErrorBigBox(); _smallBoxes = new OCRBox[] { }; } else { // First result is the big box _bigBox = new OCRBox(response.First()); // Following results are the small boxes _smallBoxes = response.Skip(1) .Select(ann => new OCRBox(ann)) .ToArray(); } _timeStamp = string.Format("{0:00}:{1:00}:{2:00}.{3:000}", sw.Elapsed.Hours, sw.Elapsed.Minutes, sw.Elapsed.Seconds, sw.Elapsed.Milliseconds); isDone = true; callback?.Invoke(this); DebugLog.Log("Finished OCR request [" + Identifer + "]"); } catch (Grpc.Core.RpcException e) { string url = ""; // Define a regular expression for repeated words. Regex rx = new Regex(@"(http\S*)", RegexOptions.Compiled | RegexOptions.IgnoreCase); // Find matches. MatchCollection matches = rx.Matches(e.Message); if (matches.Count > 0) { url = matches[0].Groups[0].Value; } Form.Invoke(Form.SafeLogWorkerError, new object[] { e.Message, url }); } catch (Exception e) { Form.Invoke(Form.SafeLogWorkerError, new object[] { e.Message, "" }); } }