public void Execute(Arguments arguments) { var rectangle = arguments.Area.Value; if (!rectangle.IsValidRectangle()) { throw new ArgumentException("Argument Area is not a valid rectangle"); } if (arguments.Relative.Value) { var foregroundWindowRect = new RobotWin32.Rect(); RobotWin32.GetWindowRectangle(RobotWin32.GetForegroundWindow(), ref foregroundWindowRect); rectangle = new Rectangle(rectangle.X + foregroundWindowRect.Left, rectangle.Y + foregroundWindowRect.Top, Math.Min(rectangle.Width, foregroundWindowRect.Right - foregroundWindowRect.Left) - rectangle.X, Math.Min(rectangle.Height, foregroundWindowRect.Bottom - foregroundWindowRect.Top) - rectangle.Y); } var partOfScreen = RobotWin32.GetPartOfScreen(rectangle); var imgToParse = OcrOfflineHelper.RescaleImage(partOfScreen, arguments.Sensitivity.Value); var language = arguments.Language.Value; var dataPath = OcrOfflineHelper.GetResourcesFolder(language); try { using (var tEngine = new TesseractEngine(dataPath, language, EngineMode.TesseractAndCube)) using (var img = PixConverter.ToPix(imgToParse)) using (var page = tEngine.Process(img)) { var text = page.GetText(); if (string.IsNullOrEmpty(text)) { throw new NullReferenceException("Ocr was unable to find any text"); } Scripter.Variables.SetVariableValue(arguments.Result.Value, new Language.TextStructure(text)); } } catch (TesseractException) { throw new ApplicationException("Ocr engine exception, possibly missing language data in folder: " + dataPath); } catch (Exception e) { throw e; } }
public TesseractAddon() : base() { OcrOfflineHelper.UnpackNeededAssemblies(); }
public void Execute(Arguments arguments) { var rectangle = arguments.Area.Value; if (!rectangle.IsValidRectangle()) { throw new ArgumentException("Argument Area is not a valid rectangle"); } if (arguments.Relative.Value) { var foregroundWindowRect = new RobotWin32.Rect(); RobotWin32.GetWindowRectangle(RobotWin32.GetForegroundWindow(), ref foregroundWindowRect); rectangle = new Rectangle(rectangle.X + foregroundWindowRect.Left, rectangle.Y + foregroundWindowRect.Top, Math.Min(rectangle.Width, foregroundWindowRect.Right - foregroundWindowRect.Left - rectangle.X), Math.Min(rectangle.Height, foregroundWindowRect.Bottom - foregroundWindowRect.Top - rectangle.Y)); } var partOfScreen = RobotWin32.GetPartOfScreen(rectangle); var language = arguments.Language.Value; var imgToParse = OcrOfflineHelper.RescaleImage(partOfScreen, arguments.Sensitivity.Value); var search = arguments.Search.Value.ToLower().Trim(); var dataPath = OcrOfflineHelper.GetResourcesFolder(language); try { using (var tEngine = new TesseractEngine(dataPath, language, EngineMode.TesseractAndCube)) using (var img = PixConverter.ToPix(imgToParse)) using (var page = tEngine.Process(img)) { var rectResult = new Rectangle(-1, -1, -1, -1); var rectanglesWithWords = GetWords(page.GetHOCRText(0), arguments.Sensitivity.Value); var searchWords = search.Split(' '); List <Rectangle> rectangleList = new List <Rectangle>(); if (searchWords.Length > 1) { rectangleList = rectanglesWithWords.Where(x => searchWords.Contains(x.Value)).Select(a => a.Key).ToList(); if (rectangleList.Count() == searchWords.Length) { rectResult = UniteRectangles(rectangleList); } } else if (rectanglesWithWords.ContainsValue(search)) { rectResult = rectanglesWithWords.Where(x => x.Value == search).First().Key; } if (Equals(rectResult, new Rectangle(-1, -1, -1, -1)) || (searchWords.Length > 1 && rectangleList.Count() != searchWords.Length)) { throw new NullReferenceException("Ocr was unable to find text"); } Scripter.Variables.SetVariableValue(arguments.Result.Value, new RectangleStructure(rectResult)); } } catch (TesseractException) { throw new ApplicationException("Ocr engine exception, possibly missing language data in folder: " + dataPath); } catch (Exception e) { throw e; } }