Esempio n. 1
0
        static void Main(string[] args)
        {
            String fileToConvert = @"FILE PATH HERE";

            RasterSupport.SetLicense(@"C:\LEADTOOLS 20\Common\License\LEADTOOLS.LIC", System.IO.File.ReadAllText(@"C:\LEADTOOLS 20\Common\License\LEADTOOLS.LIC.KEY"));

            using (RasterCodecs codecs = new RasterCodecs())
            {
                using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD, false))
                {
                    ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS 20\Bin\Common\OcrLEADRuntime");

                    using (IOcrPage ocrPage = ocrEngine.CreatePage(ocrEngine.RasterCodecsInstance.Load(fileToConvert, 1), OcrImageSharingMode.AutoDispose))
                    {
                        ocrPage.AutoZone(null);
                        ocrPage.Recognize(null);
                        string recognizedCharacters = ocrPage.GetText(-1);

                        BarcodeEngine engine     = new BarcodeEngine();
                        int           resolution = 300;
                        using (RasterImage image = RasterImage.Create((int)(8.5 * resolution), (int)(11.0 * resolution), 1, resolution, RasterColor.FromKnownColor(RasterKnownColor.White)))
                        {
                            BarcodeWriter writer = engine.Writer;

                            QRBarcodeData data = BarcodeData.CreateDefaultBarcodeData(BarcodeSymbology.QR) as QRBarcodeData;

                            data.Bounds = new LeadRect(0, 0, image.ImageWidth, image.ImageHeight);
                            QRBarcodeWriteOptions writeOptions = writer.GetDefaultOptions(data.Symbology) as QRBarcodeWriteOptions;
                            writeOptions.XModule             = 30;
                            writeOptions.HorizontalAlignment = BarcodeAlignment.Near;
                            writeOptions.VerticalAlignment   = BarcodeAlignment.Near;
                            data.Value = recognizedCharacters;

                            writer.CalculateBarcodeDataBounds(new LeadRect(0, 0, image.ImageWidth, image.ImageHeight), image.XResolution, image.YResolution, data, writeOptions);
                            Console.WriteLine("{0} by {1} pixels", data.Bounds.Width, data.Bounds.Height);

                            writer.WriteBarcode(image, data, writeOptions);

                            CropCommand cmd = new CropCommand(new LeadRect(0, 0, data.Bounds.Width, data.Bounds.Height));
                            cmd.Run(image);

                            codecs.Save(image, "QR.tif", RasterImageFormat.CcittGroup4, 1);

                            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                            {
                                var process = new Process();
                                process.StartInfo = new ProcessStartInfo("QR.tif")
                                {
                                    UseShellExecute = true
                                };
                                process.Start();
                            }

                            Console.WriteLine();
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        private void _btnRecognize_Click(object sender, EventArgs e)
        {
            try
            {
                using (WaitCursor wait = new WaitCursor())
                {
                    DocumentFormat format           = _documentFormatSelector.SelectedFormat;
                    String         documentFileName = _lblImageFileName.Text;

                    documentFileName = string.Concat(documentFileName, ".", DocumentWriter.GetFormatFileExtension(format));

                    _ocrPage.Recognize(null);
                    using (IOcrDocument _document = _ocrEngine.DocumentManager.CreateDocument(null, OcrCreateDocumentOptions.AutoDeleteFile))
                    {
                        _document.Pages.Add(_ocrPage);
                        _document.Save(documentFileName, format, null);
                    }

                    // Engine will correct the page deskew before AutoZone or Recognize, so we need to load the page again form the engine.
                    _viewer.Image = _ocrPage.GetRasterImage();
                    _viewer.Refresh();
                    UpdateMyControls();

                    // if the "View Final Document" option is checked then no need to show this message since
                    // it will load the saved document file.
                    if (!_cbViewFinalDocument.Checked)
                    {
                        Messager.ShowInformation(this, String.Format("The output document file was saved at ({0})", documentFileName));
                    }
                    else
                    {
                        if (File.Exists(documentFileName))
                        {
                            try
                            {
                                Process.Start(documentFileName);
                            }
                            catch
                            {
                                Messager.ShowError(this, "Unable to open generated results file with external viewing application");
                            }
                        }
                        else
                        {
                            Messager.ShowError(this, "Unable to open generated results file with external viewing application.\nThe system cannot find the file specified");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Messager.ShowError(this, ex.Message);
            }
        }
Esempio n. 3
0
        private IOcrPage DoublePass(RasterImage image)
        {
            //first pass with default settings
            IOcrPage page = _ocrEngine.CreatePage(image.Clone(), OcrImageSharingMode.AutoDispose);

            page.Recognize(null);

            //second pass with mobile image processing set to true
            _ocrEngine.SettingManager.SetBooleanValue("Recognition.Preprocess.MobileImagePreprocess", true);
            IOcrPage mobilePage = _ocrEngine.CreatePage(image.Clone(), OcrImageSharingMode.AutoDispose);

            mobilePage.Recognize(null);

            //get the confidence of both pages
            PageResults firstPassResults  = GetPageConfidence(page);
            PageResults secondPassResults = GetPageConfidence(mobilePage);

            double confidenceDif = firstPassResults.Confidence - secondPassResults.Confidence;

            IOcrPage    highestConfidence;
            PageResults pageResultsHighest;

            if (confidenceDif > 2)
            {
                highestConfidence  = page;
                pageResultsHighest = firstPassResults;
            }
            else
            {
                highestConfidence  = mobilePage;
                pageResultsHighest = secondPassResults;
            }

            if (pageResultsHighest.TotalWords < 20)
            {
                IOcrPage thirdPass = highestConfidence.Copy();
                thirdPass.Unrecognize();
                OcrZone singleZone = new OcrZone()
                {
                    Bounds = new LeadRect(0, 0, image.Width, image.Height)
                };
                thirdPass.Zones.Add(singleZone);
                thirdPass.Recognize(null);
                PageResults thirdResults        = GetPageConfidence(thirdPass);
                double      confidencetDifThird = thirdResults.Confidence - pageResultsHighest.Confidence;
                if (confidenceDif > 5)
                {
                    highestConfidence  = thirdPass;
                    pageResultsHighest = thirdResults;
                }
            }

            return(highestConfidence);
        }
Esempio n. 4
0
        void _rubberBand_RubberBandCompleted(object sender, ImageViewerRubberBandEventArgs e)
        {
            if (_ocrPage == null)
            {
                return;
            }

            try
            {
                _tsMainZoomComboBox.Enabled = false;

                using (WaitCursor cursor = new WaitCursor())
                {
                    if (_viewer.Image != null)
                    {
                        _currentHighlightRect = _viewer.ConvertRect(
                            null,
                            ImageViewerCoordinateType.Control,
                            ImageViewerCoordinateType.Image,
                            LeadRect.FromLTRB(e.Points[0].X, e.Points[0].Y, e.Points[1].X, e.Points[1].Y));

                        if (_currentHighlightRect.Width > 2 && _currentHighlightRect.Height > 2)
                        {
                            OcrZone zone = new OcrZone();
                            zone.Bounds           = _currentHighlightRect;
                            zone.ZoneType         = OcrZoneType.Text;
                            zone.CharacterFilters = OcrZoneCharacterFilters.None;

                            _ocrPage.Zones.Clear();
                            _ocrPage.Zones.Add(zone);

                            _ocrPage.Recognize(null);
                            _recognitionResults.Text = _ocrPage.GetText(0);

                            if (_recognitionResults.Text == "\n" || _recognitionResults.Text == "")
                            {
                                Messager.ShowInformation(this, "No text was recognized.");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Messager.ShowError(this, ex);
            }
            finally
            {
                _viewer.Invalidate();
                _tsMainZoomComboBox.Enabled = true;
            }
        }
Esempio n. 5
0
    /// <summary>
    /// Uses an instance of the LEADTools OCR Engine to "read" the text
    /// in the pre-defined OCR region representing the mailing address
    /// area.  Is thread safe.
    /// </summary>
    /// <param name="ocrEngine"></param>
    /// <param name="document"></param>
    /// <returns></returns>
    private static string GetAddressBlockText(IOcrEngine ocrEngine, PDFDocument document)
    {
        string returnedText = null;

        using (var codecs = new RasterCodecs())
            using (var image = new RasterImage(document.GetPageImage(codecs, 1)))
                using (IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument())
                    using (IOcrPage ocrPage = ocrDocument.Pages.AddPage(image, null))
                    {
                        var myZone = new OcrZone();
                        myZone.Bounds = new LogicalRectangle(0, 2, 4, 1.4, LogicalUnit.Inch);
                        ocrPage.Zones.Add(myZone);
                        ocrPage.Recognize(null);
                        returnedText = ocrPage.GetText(0).ToUpper();
                    }
        return(returnedText);
    }
Esempio n. 6
0
        public static void exe_ocr(string filename, IOcrDocument ocrDocument)
        {
            Console.WriteLine(filename);
            RasterCodecs rasterCodecs = null;
            RasterImage  rasterImage  = null;

            try
            {
                rasterCodecs = new RasterCodecs();
                rasterCodecs.ThrowExceptionsOnInvalidImages = false;
                rasterImage = rasterCodecs.Load(filename);
                AutoBinarizeCommand command = new AutoBinarizeCommand();
                command.Run(rasterImage);

                //AutoLineRemoveCommand commandLine = new AutoLineRemoveCommand();
                //commandLine.Run(rasterImage);

                IOcrPage page = ocrDocument.Pages.AddPage(rasterImage, null);
                if (page != null)
                {
                    page.UpdateNativeFillMethod();
                    page.Recognize(null);
                }

                // ocrDocument.Pages.zon(NativeOcrZoneType.AutoGraphic);
            }catch (Exception e)
            {
                //    Console.WriteLine("add image faild: " + e);
            }
            finally
            {
                if (rasterCodecs != null)
                {
                    rasterCodecs.Dispose();
                }
                if (rasterImage != null)
                {
                    rasterImage.Dispose();
                }
            }
        }
        private void GetOmrReading(IOcrPage ocrPage, FormField field, ImageField imageField, int retry = 1)
        {
            IOcrPageCharacters pageCharacters = ocrPage.GetRecognizedCharacters();

            if (pageCharacters == null)
            {
                logger.Warn($"could not read OMR for ${field} ");
                imageField.FieldResult.Confidence = 0;
                imageField.FieldResult.Text       = "";
            }
            else
            {
                IOcrZoneCharacters zoneCharacters = pageCharacters[0];
                if (zoneCharacters.Count > 0)
                {
                    OcrCharacter omrCharacter = zoneCharacters[0];
                    imageField.FieldResult.Text       = omrCharacter.Code.ToString();
                    imageField.FieldResult.IsFilled   = omrCharacter.Code == FilledChar;
                    imageField.FieldResult.Confidence = omrCharacter.Confidence;
                    // often on a fill we get the line from the box, so we retry more narrowly
                    if (imageField.FieldResult.IsFilled)
                    {
                        if (retry > 0)
                        {
                            var orgZone = ocrPage.Zones[0];
                            orgZone.Bounds = ChangeBoundsRatio(orgZone.Bounds, 0.66);
                            ocrPage.Recognize(null);
                            GetOmrReading(ocrPage, field, imageField, 0);
                            logger.Info($"FILLED {field.Name}");
                        }
                    }
                }
                else
                {
                    imageField.FieldResult.Text = "";
                }
            }
        }
        public List <FilledForm> ProcessOcr(ResultsForPrettyJson formResults,
                                            List <ImageInfo> fileInfos)
        {
            try
            {
                var       outDir      = formResults.OriginalDirectoryName;
                var       retForms    = new List <FilledForm>();
                var       usedMasters = new HashSet <MasterForm>();
                Stopwatch stopWatch   = new Stopwatch();
                stopWatch.Start();
                formResults.PagesInPdf = fileInfos.Count;
                foreach (var ofi in fileInfos)
                {
                    FilledForm newForm = new FilledForm();
                    retForms.Add(newForm);
                    newForm.ImageInfoMaster.InitialImage = ofi;
                    newForm.Name = Path.GetFileNameWithoutExtension(ofi.ImageFileInfo.Name);
                    if (ofi.Image == null)
                    {
                        ofi.Image = LoadImageFile(ofi.ImageFileInfo.FullName, 1, -1);
                    }

                    //CleanupImage(ofi.Image);
                    var par = new FormThreadCallParams()
                    {
                        ImageInfo = ofi, StopWatch = stopWatch, Form = newForm
                    };
                    if (PageTimeoutInSeconds < 50)
                    {
                        Thread t = new Thread(this.PrepareNewFormThreader);
                        t.Start(par);
                        if (!t.Join(TimeSpan.FromSeconds(PageTimeoutInSeconds)))
                        {
                            t.Abort();
                            formResults.TimedOutPages.Add(newForm.Name);
                            formResults.BestFormConfidence.Add(-1);
                            if (formResults.TimedOutPages.Count > 2 && formResults.PagesMappedToForm == 0)
                            {
                                formResults.Status =
                                    $"Form abandoned for timeout after {formResults.BestFormConfidence.Count} pages";
                                logger.Error(formResults.Status);
                                return(retForms);
                            }

                            continue;
                        }
                    }
                    else
                    {
                        PrepareNewFormThreader(par);
                    }

                    Debug.Assert(par.Attributes != null);
                    var filledFormAttributes = par.Attributes;
                    //List<FormRecognitionResult> results = new List<FormRecognitionResult>();
                    MasterForm currentMasterBlockForm = null;
                    int        bestConfidence         = -1;
                    int        currentConfidence      = 85;
                    foreach (var master in BlockMasterForms)
                    {
                        if (usedMasters.Contains(master))
                        {
                            continue;
                        }
                        var result = RecognitionEngine.CompareForm(master.Attributes, filledFormAttributes, null, null);
                        //logger.Debug($"Check {master} for {newForm} {stopWatch.ElapsedMilliseconds} {result.Confidence}");
                        if (result.Confidence > currentConfidence)
                        {
                            currentMasterBlockForm = master;
                            bestConfidence         = currentConfidence = result.Confidence;
                        }
                        else if (result.Confidence > bestConfidence)
                        {
                            bestConfidence = result.Confidence;
                        }
                    }

                    formResults.BestFormConfidence.Add(bestConfidence);
                    if (currentMasterBlockForm != null)
                    {
                        formResults.MasterFormPages.Add(currentMasterBlockForm.Properties.Name);
                        formResults.PagesMappedToForm++;
                        logger.Info($"FilledForm matched {newForm.Name} {newForm.Status} {stopWatch.ElapsedMilliseconds} ");
                        newForm.ImageInfoMaster.InitialImage = ofi;
                        var centeredImage = ofi.Image.CloneAll();

                        CleanupImage(centeredImage);
                        newForm.ImageInfoMaster.CenteredImage = new ImageInfo()
                        {
                            Image = centeredImage
                        };
                        var omrImage = centeredImage.CloneAll();
                        PrepareOmrImage(omrImage);
                        newForm.ImageInfoMaster.OmrImage = new ImageInfo()
                        {
                            Image = omrImage
                        };
                        newForm.Status = "Matched";
                        newForm.Master = currentMasterBlockForm;
                        var alignment =
                            RecognitionEngine.GetFormAlignment(newForm.Master.Attributes, newForm.Attributes, null);
                        var fields          = currentMasterBlockForm.ProcessingPages[0];
                        var scaler          = currentMasterBlockForm.Resolution;
                        var fieldsOnlyImage = RasterImage.Create(centeredImage.Width, centeredImage.Height,
                                                                 centeredImage.BitsPerPixel, 300, RasterColor.White);
                        //fieldsOnlyImage  = new RasterImage(RasterMemoryFlags.Conventional, centeredImage.Width, centeredImage.Height, centeredInage.BitsPerPixel, RasterByteOrder.Rgb, RasterViewPerspective.TopLeft, null, null, 0);

                        var subDirField       = Path.Combine(outDir, "fields");
                        var fileNameFieldOnly = Path.Combine(subDirField, newForm.Name + "_fields.jpg");
                        var googleResultsFile = Path.Combine(subDirField, newForm.Name + "_google.json");
                        var combined          = false;
                        foreach (var field in fields)
                        {
                            var isBlock = field.Name.Contains("block");
                            var rect200 = alignment[0].AlignRectangle(field.Bounds);
                            scaler = 300;
                            int fudge   = isBlock ? 30 : 1;
                            var rect300 = new LeadRect(rect200.Left * 300 / scaler - fudge, rect200.Top * 300 / scaler - fudge,
                                                       rect200.Width * 300 / scaler + fudge,
                                                       rect200.Height * 300 / scaler + fudge);
                            try
                            {
                                var imageInfoToUse = newForm.ImageInfoMaster.CenteredImage;
                                var zoneType       = OcrZoneType.Text;
                                if (field.GetType() == typeof(OmrFormField))
                                {
                                    imageInfoToUse = newForm.ImageInfoMaster.OmrImage;
                                    zoneType       = OcrZoneType.Omr;
                                }
                                else if (field.GetType() == typeof(ImageFormField))
                                {
                                    zoneType = OcrZoneType.Graphic;
                                }

                                var image      = imageInfoToUse.Image.CloneAll();
                                var subDir     = Path.Combine(outDir, isBlock ? "blocks" : "fields");
                                var fileName   = Path.Combine(subDir, newForm.Name + "_" + field.Name + ".jpg");
                                var imageField = new ImageField
                                {
                                    Field       = field,
                                    FieldResult =
                                    {
                                        FieldName = field.Name,
                                        IsBlock   = isBlock,
                                        ImageFile = fileName,
                                        Bounds    = rect300.ToString(),
                                        FieldType = zoneType.ToString(),

                                        Error     = "None"
                                    }
                                };
                                imageField.Rectangle = new Rectangle(rect300.X, rect300.Y, rect300.Width, rect300.Height);

                                try
                                {
                                    EnsurePathExists(subDir);
                                    CropCommand command = new CropCommand
                                    {
                                        Rectangle = rect300
                                    };
                                    command.Run(image);
                                    RasterCodecs.Save(image, fileName, RasterImageFormat.Jpeg, bitsPerPixel: 8);
                                    if (!isBlock && zoneType == OcrZoneType.Text && !combined)
                                    {
                                        try
                                        {
                                            ;
                                            var combiner = new CombineCommand();
                                            //combiner.DestinationImage = fieldsOnlyImage;
                                            combiner.SourceImage          = image.Clone();
                                            combiner.DestinationRectangle = rect300;
                                            var regionBounds = image.GetRegionBounds(null);
                                            combiner.SourcePoint = new LeadPoint(regionBounds.X, regionBounds.Y);
                                            //combiner.Flags = CombineCommandFlags.OperationAdd | CombineCommandFlags.Destination0 | CombineCommandFlags.Source1 | CombineCommandFlags.Destination0 ;

                                            combiner.Flags = CombineCommandFlags.OperationOr | CombineCommandFlags.Destination0;; // |CombineFastCommandFlags.OperationAverage;
                                            combiner.Run(fieldsOnlyImage);
                                            //combined = true;
                                        }
                                        catch (Exception exCombine)
                                        {
                                            logger.Error(exCombine, $"error combining field {field.Name} {rect300}");
                                        }
                                    }

                                    var imageInfo = new ImageInfo()
                                    {
                                        Image = image, ImageFileInfo = new FileInfo(fileName)
                                    };
                                    imageField.ImageInfo = imageInfo;

                                    if (!isBlock && zoneType != OcrZoneType.Graphic)
                                    {
                                        using (IOcrPage ocrPage = OcrEngine.CreatePage(image, OcrImageSharingMode.AutoDispose))
                                        {
                                            OcrZone ocrZone = new OcrZone
                                            {
                                                ZoneType = zoneType,
                                                Bounds   = new LeadRect(fudge, fudge, image.ImageSize.Width - fudge,
                                                                        image.ImageSize.Height - fudge)
                                            };
                                            ocrPage.Zones.Add(ocrZone);

                                            ocrPage.Recognize(null);
                                            if (zoneType == OcrZoneType.Omr)
                                            {
                                                if (field.Name.Contains("C2NGVD1929"))
                                                {
                                                    logger.Info(ocrZone.Bounds);
                                                }
                                                GetOmrReading(ocrPage, field, imageField);
                                            }
                                            else if (zoneType == OcrZoneType.Text)
                                            {
                                                var resultsPage = GetPageConfidence(ocrPage);
                                                imageField.FieldResult.Confidence = resultsPage.Confidence;
                                                char[] crlf = { '\r', '\n' };
                                                imageField.FieldResult.Text = ocrPage.GetText(0).TrimEnd(crlf);
                                            }
                                        }
                                    }

                                    logger.Info(
                                        $"field {field.Name} {rect300} [{imageField.FieldResult.Text}] confidence: {imageField.FieldResult.Confidence}");
                                }
                                catch (Exception exField)
                                {
                                    logger.Error(exField, $"Error processing {field.Name}");
                                    formResults.FieldsWithError++;
                                    imageField.FieldResult.Error = exField.Message;
                                }

                                newForm.ImageFields.Add(imageField);
                                formResults.OcrFields.Add(imageField.FieldResult);
                                formResults.Status = "FormMatched";
                            }
                            catch (Exception ex)
                            {
                                logger.Error(ex, $"Error on field {field.Name} {rect300}");
                                newForm.Status = $"Error|Field {field.Name} {rect300}: [{ex.Message}]";
                            }
                        }
                        RasterCodecs.Save(PrepareOmrImage(fieldsOnlyImage), fileNameFieldOnly, RasterImageFormat.Jpeg, bitsPerPixel: 8);
                        var googleResults = GoogleOcr(fileNameFieldOnly);
                        if (googleResults.Count > 0)
                        {
                            var json = JsonConvert.SerializeObject(googleResults, Formatting.Indented);
                            File.WriteAllText(googleResultsFile, json);

                            MergeGoogleOcr(newForm, googleResults);
                        }

                        usedMasters.Add(currentMasterBlockForm);
                    }
                    else
                    {
                        newForm.Status = "Unmatched|No MasterForm match";
                    }

                    logger.Info($"FilledForm processed {newForm.Name} {newForm.Status} {stopWatch.ElapsedMilliseconds} ");
                    if (usedMasters.Count == BlockMasterForms.Count)
                    {
                        logger.Info("found all master forms");
                        break;
                    }
                }

                stopWatch.Stop();

                return(retForms);
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Untrapped error found");
                return(null);
            }
        }
Esempio n. 9
0
        private void DoLoadAndRecognizeDocument(OcrProgressDialog dlg, Dictionary <string, object> args)
        {
            // Perform load and recognize here

            OcrProgressCallback callback    = dlg.OcrProgressCallback;
            IOcrDocument        ocrDocument = null;

            try
            {
                string documentFileName = args["documentFileName"] as string;

                ocrDocument = _ocrEngine.DocumentManager.CreateDocument("", OcrCreateDocumentOptions.InMemory);

                IOcrPage ocrPage = null;

                if (!dlg.IsCanceled)
                {
                    // If we are not using a progress bar, update the description text
                    if (callback == null)
                    {
                        dlg.UpdateDescription("Loading the document (first page only)...");
                    }

                    ocrPage = ocrDocument.Pages.AddPage(documentFileName, callback);
                }

                if (!dlg.IsCanceled)
                {
                    // If we are not using a progress bar, update the description text
                    if (callback == null)
                    {
                        dlg.UpdateDescription("Recognizing the page(s) of the document...");
                    }

                    ocrPage.Recognize(callback);
                }

                if (!dlg.IsCanceled)
                {
                    // We did not cancel, use this document
                    SetDocument(ocrDocument, documentFileName);
                    ocrDocument = null;
                }
            }
            catch (Exception ex)
            {
                ShowError(ex);
            }
            finally
            {
                if (callback == null)
                {
                    dlg.EndOperation();
                }

                // Clean up
                if (ocrDocument != null)
                {
                    ocrDocument.Dispose();
                }
            }
        }