public static void Run() { // ExStart:UserDefinedRecognitionBlocks // The path to the documents directory. string dataDir = RunExamples.GetDataDir_OMR(); // Initialize an instance of OcrEngine OcrEngine ocrEngine = new OcrEngine(); // Clear notifier list ocrEngine.ClearNotifies(); // Clear recognition blocks ocrEngine.Config.ClearRecognitionBlocks(); // Add 3 rectangle blocks to user defined recognition blocks ocrEngine.Config.AddRecognitionBlock(RecognitionBlock.CreateTextBlock(0, 10, 20, 40)); ocrEngine.Config.AddRecognitionBlock(RecognitionBlock.CreateTextBlock(0, 4, 5, 6)); ocrEngine.Config.AddRecognitionBlock(RecognitionBlock.CreateTextBlock(0, 5, 5, 6)); // Ignore everything else on the image other than the user defined recognition blocks ocrEngine.Config.DetectTextRegions = false; // Set Image property by loading an image from file path ocrEngine.Image = ImageStream.FromFile(dataDir + "Sampleocr.bmp"); // Run recognition process if (ocrEngine.Process()) { // Retrieve user defined blocks that determines the paye layout var blocks = ocrEngine.Config.RecognitionBlocks; // Loop over the list of blocks foreach (var block in blocks) { // Display if block is set to be recognized Console.WriteLine(block.ToRecognize); // Check if block has recognition data if (block.RecognitionData == null) { Console.WriteLine("Null{0}", Environment.NewLine); continue; } // Display dimension & size of rectangle that defines the recognition block Console.WriteLine("Block: {0}", block.Rectangle); if (block.RecognitionData is IRecognizedTextPartInfo) { // Display the recognition results IRecognizedTextPartInfo textPartInfo = (IRecognizedTextPartInfo)block.RecognitionData; Console.WriteLine("Text: {0}{1}", textPartInfo.Text, Environment.NewLine); } } } // ExEnd:UserDefinedRecognitionBlocks }
public static void Run() { // ExStart:GetTextPartHierarchy // The path to the documents directory. string dataDir = RunExamples.GetDataDir_OCR(); // Initialize an instance of OcrEngine OcrEngine ocrEngine = new OcrEngine(); // Set the Image property by loading the image from file path location or an instance of MemoryStream ocrEngine.Image = ImageStream.FromFile(dataDir + "Sampleocr.bmp"); // Process the image if (ocrEngine.Process()) { // Retrieve the first block of the recognized text part IRecognizedTextPartInfo firstBlock = (ocrEngine.Text.PartsInfo[0] as IRecognizedTextPartInfo); // Get the children of the first block that will be the lines in the block if (firstBlock != null) { IRecognizedPartInfo[] linesOfFirstBlock = firstBlock.Children; // Retrieve the fist line from the collection of lines IRecognizedTextPartInfo firstLine = (linesOfFirstBlock[0] as IRecognizedTextPartInfo); // Display the level of line if (firstLine != null) { Console.WriteLine(firstLine.Level); // Retrieve the fist word from the collection of words IRecognizedTextPartInfo firstWord = (firstLine.Children[0] as IRecognizedTextPartInfo); // Display the level of word Console.WriteLine(firstWord.Level); // Retrieve the fist character from the collection of characters IRecognizedTextPartInfo firstCharacter = (firstWord.Children[0] as IRecognizedTextPartInfo); // Display the level of character Console.WriteLine(firstCharacter.Level); } } } // ExEnd:GetTextPartHierarchy }
public static void Main() { // The path to the documents directory. string dataDir = Aspose.OCR.Examples.Utils.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); //Initialize an instance of OcrEngine OcrEngine ocrEngine = new OcrEngine(); //Set the Image property by loading the image from file path location or an instance of MemoryStream ocrEngine.Image = ImageStream.FromFile(dataDir + "Sampleocr.bmp"); //Process the image if (ocrEngine.Process()) { //Retrieve the first block of the recognized text part IRecognizedTextPartInfo firstBlock = (ocrEngine.Text.PartsInfo[0] as IRecognizedTextPartInfo); //Get the children of the first block that will the the lines in the block IRecognizedPartInfo[] linesOfFirstBlock = firstBlock.Children; //Retrieve the fist line from the collection of lines IRecognizedTextPartInfo firstLine = (linesOfFirstBlock[0] as IRecognizedTextPartInfo); //Display the level of line Console.WriteLine(firstLine.Level); //Retrieve the fist word from the collection of words IRecognizedTextPartInfo firstWord = (firstLine.Children[0] as IRecognizedTextPartInfo); //Display the level of word Console.WriteLine(firstWord.Level); //Retrieve the fist character from the collection of characters IRecognizedTextPartInfo firstCharacter = (firstWord.Children[0] as IRecognizedTextPartInfo); //Display the level of character Console.WriteLine(firstCharacter.Level); } }