Esempio n. 1
0
        // Detect orientation of all pages in a document
        // Return array with orientations for all pages
        private FREngine.RotationTypeEnum[] detectOrientation(string imagePath, FREngine.PrepareImageMode _prepareImageMode,
                                                              FREngine.PageProcessingParams pageProcessingParams)
        {
            // Correct skew during loading
            FREngine.PrepareImageMode prepareImageMode = engine.CreatePrepareImageMode();
            prepareImageMode.CopyFrom(_prepareImageMode);
            prepareImageMode.CorrectSkew     = true;
            prepareImageMode.CorrectSkewMode = correctSkewFlags;

            FREngine.RotationTypeEnum[] rotations  = null;
            FREngine.FRDocument         frDocument = engine.CreateFRDocument();
            try
            {
                frDocument.AddImageFile(imagePath, prepareImageMode, null);

                // Get orientation for every page
                int pagesCount = frDocument.Pages.Count;
                rotations = new FREngine.RotationTypeEnum[pagesCount];
                for (int pageIndex = 0; pageIndex < pagesCount; pageIndex++)
                {
                    FREngine.IFRPage         frPage          = frDocument.Pages[pageIndex];
                    FREngine.TextOrientation textOrientation = frPage.DetectOrientation(null, pageProcessingParams.ObjectsExtractionParams,
                                                                                        pageProcessingParams.RecognizerParams);
                    rotations[pageIndex] = FREngine.RotationTypeEnum.RT_UnknownRotation;
                    if (textOrientation != null)
                    {
                        rotations[pageIndex] = textOrientation.RotationType;
                    }
                    frPage.Flush(false);
                }
            }
            finally
            {
                frDocument.Close();
            }

            return(rotations);
        }
Esempio n. 2
0
        public RecognitionStatistics Process(string imagePath, ProcessingSettings settings)
        {
            DateTime startTime = System.DateTime.Now;

            setStep("Applying profile...");
            engine.LoadPredefinedProfile("DocumentConversion_Accuracy");

            setStep("Applying settings...");
            FREngine.PrepareImageMode         pim = engine.CreatePrepareImageMode();
            FREngine.DocumentProcessingParams dpp = engine.CreateDocumentProcessingParams();
            FREngine.PageProcessingParams     ppp = dpp.PageProcessingParams;

            ppp.RecognizerParams.SetPredefinedTextLanguage(settings.Language);
            disableAllModifications(pim, ppp);

            pim.AutoOverwriteResolution = false;
            if (settings.CorrectResolution)
            {
                if (settings.NewResolution == 0)
                {
                    pim.AutoOverwriteResolution = true;
                }
                else if (settings.NewResolution > 0)
                {
                    pim.OverwriteResolution    = true;
                    pim.XResolutionToOverwrite = settings.NewResolution;
                    pim.YResolutionToOverwrite = settings.NewResolution;
                }
            }

            if (settings.ConvertToBW)
            {
                pim.DiscardColorImage = true;
            }
            if (settings.DeskewImage)
            {
                pim.CorrectSkew = true;
            }

            // Detect orientation for all pages
            setStep("Detecting orientation...");
            FREngine.RotationTypeEnum[] rotation = null;
            if (settings.CorrectOrientationMode == ProcessingSettings.OrientationCorrectionMode.Automatic)
            {
                rotation = detectOrientation(imagePath, pim, ppp);
            }

            setStep("Loading image...");

            // Create document
            FREngine.FRDocument   frDocument       = engine.CreateFRDocument();
            RecognitionStatistics recognitionStats = new RecognitionStatistics();

            try
            {
                // Add image file to document
                frDocument.AddImageFile(imagePath, pim, null);

                if (frDocument.Pages.Count == 0)
                {
                    throw new Exception("No pages in a file");
                }

                setStep("Performing image modification...");
                for (int pageIndex = 0; pageIndex < frDocument.Pages.Count; pageIndex++)
                {
                    FREngine.IFRPage          frPage       = frDocument.Pages[pageIndex];
                    FREngine.RotationTypeEnum pageRotation = FREngine.RotationTypeEnum.RT_UnknownRotation;
                    if (rotation != null && pageIndex < rotation.Length)
                    {
                        pageRotation = rotation[pageIndex];
                    }

                    applyGeometricalTransformations(frPage, ppp, settings, pageRotation);

                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    frPage.Flush(true);
                }

                int[] sourcePageIndices = splitImage(frDocument, ppp, settings);

                for (int pageIndex = 0; pageIndex < frDocument.Pages.Count; pageIndex++)
                {
                    FREngine.IFRPage frPage = frDocument.Pages[pageIndex];
                    applyImageTransformations(frPage, settings);

                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    frPage.Flush(true);
                }

                if (settings.IsRecognize)
                {
                    setStep("Recognizing image...");
                    frDocument.Process(dpp);
                }

                setStep("Applying visual enhancements...");
                for (int pageIndex = 0; pageIndex < frDocument.Pages.Count; pageIndex++)
                {
                    FREngine.IFRPage frPage = frDocument.Pages[pageIndex];
                    applyVisualEnhancements(frPage, settings);

                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    frPage.Flush(true);
                }

                TimeSpan processingTime = DateTime.Now - startTime;

                setStep("Computing statistics...");

                recognitionStats = computeStatistics(frDocument);
                recognitionStats.TotalProcessingTime = processingTime;
                recognitionStats.SourcePageIndices   = sourcePageIndices;

                setStep("Retrieving images...");
                for (int pageIndex = 0; pageIndex < frDocument.Pages.Count; pageIndex++)
                {
                    if (recognitionStats.PreprocessedImages == null)
                    {
                        recognitionStats.PreprocessedImages = new System.Drawing.Image[frDocument.Pages.Count];
                    }
                    FREngine.IFRPage frPage = frDocument.Pages[pageIndex];
                    recognitionStats.PreprocessedImages[pageIndex] = getImageFromPage(frPage);

                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    frPage.Flush(false);
                }



                //frDocument.Export(AppDomain.CurrentDomain.BaseDirectory + "FileSample2.xml", FREngine.FileExportFormatEnum.FEF_XML, null);

                //frDocument.Export(AppDomain.CurrentDomain.BaseDirectory + "FileSample2.txt", FREngine.FileExportFormatEnum.FEF_TextUnicodeDefaults, null);
            }
            finally
            {
                frDocument.Close();
            }

            return(recognitionStats);
        }