Example #1
0
        internal ResultIterator(ResultIterator resultIterator)
        {
            var pointer = Native.DllImports.TessResultIteratorCopy(resultIterator.handleRef);

            if (pointer == IntPtr.Zero)
            {
                throw new ArgumentNullException("TessBaseAPIGetIterator returned a null pointer");
            }

            handleRef = new HandleRef(this, pointer);
        }
Example #2
0
        /// <summary>
        /// Get a reading-order iterator to the results of LayoutAnalysis and/or
        /// Recognize.The returned iterator must be deleted after use.
        /// WARNING! This class points to data held within the TessBaseAPI class, and
        /// therefore can only be used while the TessBaseAPI class still exists and
        /// has not been subjected to a call of Init, SetImage, Recognize, Clear, End
        /// DetectOS, or anything else that changes the internal PAGE_RES.
        /// </summary>
        /// <returns></returns>
        public ResultIterator GetIterator()
        {
            ResultIterator iterator = null;

            try
            {
                iterator = new ResultIterator(this);
            }
            catch (ArgumentNullException)
            { }

            return(iterator);
        }
Example #3
0
        public static ScanResults GetResults(this Tesseract.ResultIterator iter)
        {
            var llsr = new List <CharResult[]>();

            iter.Begin();


            if (iter.TryGetBoundingBox(PageIteratorLevel.Symbol, out Rect symbolBounds))
            {
                var lsr = new List <CharResult>();
                using (var choice = iter.GetChoiceIterator())
                {
                    do
                    {
                        lsr.Add(new CharResult()
                        {
                            Character  = choice.GetText().First(),
                            Confidence = choice.GetConfidence()
                        });
                    } while (choice.Next());
                }
                llsr.Add(lsr.ToArray());
            }

            while (!iter.IsAtFinalOf(PageIteratorLevel.TextLine, PageIteratorLevel.Symbol))
            {
                iter.Next(PageIteratorLevel.Symbol);
                if (iter.TryGetBoundingBox(PageIteratorLevel.Symbol, out symbolBounds))
                {
                    var lsr = new List <CharResult>();
                    using (var choice = iter.GetChoiceIterator())
                    {
                        do
                        {
                            lsr.Add(new CharResult()
                            {
                                Character  = choice.GetText().First(),
                                Confidence = choice.GetConfidence()
                            });
                        } while (choice.Next());
                    }
                    llsr.Add(lsr.ToArray());
                }
            }
            ;

            return(new ScanResults()
            {
                Results = llsr.ToArray()
            });
        }
        public void Print(ResultIterator iter)
        {
            logger.Log("Is beginning of block: {0}", iter.IsAtBeginningOf(PageIteratorLevel.Block));
            logger.Log("Is beginning of para: {0}", iter.IsAtBeginningOf(PageIteratorLevel.Para));
            logger.Log("Is beginning of text line: {0}", iter.IsAtBeginningOf(PageIteratorLevel.TextLine));
            logger.Log("Is beginning of word: {0}", iter.IsAtBeginningOf(PageIteratorLevel.Word));
            logger.Log("Is beginning of symbol: {0}", iter.IsAtBeginningOf(PageIteratorLevel.Symbol));

            logger.Log("Block text: \"{0}\"", iter.GetText(PageIteratorLevel.Block));
            logger.Log("Para text: \"{0}\"", iter.GetText(PageIteratorLevel.Para));
            logger.Log("TextLine text: \"{0}\"", iter.GetText(PageIteratorLevel.TextLine));
            logger.Log("Word text: \"{0}\"", iter.GetText(PageIteratorLevel.Word));
            logger.Log("Symbol text: \"{0}\"", iter.GetText(PageIteratorLevel.Symbol));
        }
Example #5
0
        /// <summary>
        /// Processes the specific image.
        /// </summary>
        /// <remarks>
        /// You can only have one result iterator open at any one time.
        /// </remarks>
        /// <param name="image">The image to process.</param>
        /// <param name="region">The image region to process.</param>
        /// <returns>A result iterator</returns>
        public ResultIterator Process(IPix image, Rect region)
        {
            if (image == null) throw new ArgumentNullException("image");
            if (region.X1 < 0 || region.Y1 < 0 || region.X2 > image.Width || region.Y2 > image.Height)
                throw new ArgumentException("The image region to be processed must be within the image bounds.", "region");
            if (processCount > 0) throw new InvalidOperationException("Only one image can be processed at once. Please make sure you dispose of the result iterator once your finished with it.");

            processCount++;

            Interop.TessApi.BaseApiSetImage(handle, image.Handle);
            Interop.TessApi.BaseApiSetRectangle(handle, region.X1, region.Y1, region.Width, region.Height);
            if (Interop.TessApi.BaseApiRecognize(handle, IntPtr.Zero) != 0) {
                throw new InvalidOperationException("failed to process document.");
            }

            var iteratorHandle = Interop.TessApi.BaseApiGetIterator(handle);
            var iterator = new ResultIterator(iteratorHandle);
            iterator.Disposed += OnIteratorDisposed;
            return iterator;
        }
Example #6
0
 internal PageIterator(ResultIterator resultIterator)
 {
     handleRef = new HandleRef(this, Native.DllImports.TessResultIteratorGetPageIterator(resultIterator.handleRef));
 }
Example #7
0
 internal ResultIterator(ResultIterator resultIterator)
     : base(Native.DllImports.TessResultIteratorCopy((HandleRef)resultIterator))
 {
 }
 internal ChoiceIterator(ResultIterator resultIterator)
     : base(Native.DllImports.TessResultIteratorGetChoiceIterator((HandleRef)resultIterator))
 {
 }