public void MergeTest()
        {
            //arrange
            CombineCommand cc          = new CombineCommand();
            var            exportInfos = new List <GameInfo>()
            {
                new GameInfo()
                {
                    Name = "GameA", CardsRemaining = 3
                },
                new GameInfo()
                {
                    Name = "GameB", CardsRemaining = 1
                },
                new GameInfo()
                {
                    Name = "GameC", CardsRemaining = 4
                }
            };
            var stcInfos = new List <CombineCommand.StcGameInfo> {
                new CombineCommand.StcGameInfo()
                {
                    Name = "GameA", AverageCardValue = 0.11m
                },
                new CombineCommand.StcGameInfo()
                {
                    Name = "GameB", AverageCardValue = 0.05m
                },
                new CombineCommand.StcGameInfo()
                {
                    Name = "GameC", AverageCardValue = 0.22m
                },
                new CombineCommand.StcGameInfo()
                {
                    Name = "GameD", AverageCardValue = 0.17m
                }
            };
            //act
            var result = cc.Merge(exportInfos, stcInfos);
            //assert
            var asArray = result.ToArray();

            Assert.AreEqual(3, asArray.Count());
            Assert.AreEqual("GameA", asArray[0].Name);
            Assert.AreEqual(3, asArray[0].CardsRemaining);
            Assert.AreEqual(0.33m, asArray[0].TotalPrice);
            Assert.AreEqual("GameB", asArray[1].Name);
            Assert.AreEqual(1, asArray[1].CardsRemaining);
            Assert.AreEqual(0.05m, asArray[1].TotalPrice);
            Assert.AreEqual("GameC", asArray[2].Name);
            Assert.AreEqual(4, asArray[2].CardsRemaining);
            Assert.AreEqual(0.88m, asArray[2].TotalPrice);
        }
Beispiel #2
0
        public CombineDialog(RasterImage TempImage)
        {
            InitializeComponent();
            _CombineCommand = new CombineCommand();
            _X      = TempImage.Width / 8;
            _Y      = TempImage.Height / 8;
            _Width  = TempImage.Width;
            _Height = TempImage.Height;

            //Set command default values
            InitializeUI();
        }
Beispiel #3
0
        public void FillImages(List <CompositeJpxImages> compositeImage)
        {
            for (int index = 0; index < compositeImage.Count; index++)
            {
                ColorList.Add(compositeImage[index].ColorImage);

                if (compositeImage[index].OpacityImage != null)
                {
                    OpacityList.Add(compositeImage[index].OpacityImage);
                }
                else
                {
                    OpacityList.Add(((MainForm)MdiParent).NoOpacityBitmap);
                }

                if (compositeImage[index].PreOpacityImage != null)
                {
                    PreOpacityList.Add(compositeImage[index].PreOpacityImage);
                }
                else
                {
                    PreOpacityList.Add(((MainForm)MdiParent).NoPreOpacityBitmap);
                }

                if (compositeImage[index].OpacityImage != null)
                {
                    RasterImage combineImage = new RasterImage(compositeImage[index].ColorImage);

                    CombineCommand command = new CombineCommand(compositeImage[index].OpacityImage,
                                                                new LeadRect(0, 0, compositeImage[index].OpacityImage.Width, compositeImage[index].OpacityImage.Height),
                                                                new LeadPoint(0, 0),
                                                                CombineCommandFlags.OperationMultiply);
                    command.Run(combineImage);

                    ImagesList.Add(combineImage);
                }
                else
                {
                    ImagesList.Add(compositeImage[index].ColorImage);
                }
            }
        }
        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);
            }
        }
Beispiel #5
0
        private void _miFileSaveAllPagesAsTIFF_Click(object sender, System.EventArgs e)
        {
            SaveFileDialog saveDlg = new SaveFileDialog();

            saveDlg.FileName        = _fileName.Remove(_fileName.Length - 4, 4);
            saveDlg.OverwritePrompt = true;

            string formatString = _quality;

            formatString = (formatString.Trim()).Substring(1, formatString.Length - 3);

            saveDlg.FileName += "_M_" + formatString + ".tif";
            saveDlg.Title     = "Save All Pages (TIFF LEAD ABC Format)";
            saveDlg.Filter    = "TIFF LEAD ABC Format (*.tif)|*.tif";

            using (WaitCursor wait = new WaitCursor())
            {
                try
                {
                    if (saveDlg.ShowDialog() == DialogResult.OK)
                    {
                        if (File.Exists(saveDlg.FileName))
                        {
                            File.Delete(saveDlg.FileName);
                        }

                        int            index;
                        CombineCommand cmd      = new CombineCommand();
                        int            loopStep = (_miQualityCombineTwoImages.Checked) ? 2 : 1;

                        RasterImage fullImage = _codecs.Load(_fileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, -1);
                        if (fullImage.HasRegion)
                        {
                            fullImage.MakeRegionEmpty();
                        }

                        StopSaving = false;

                        for (index = 1; index <= fullImage.PageCount && !StopSaving; index += loopStep)
                        {
                            _sbMainStatus.Text = string.Format("Saving page {0}/{1}. Press Esc to cancel.", index, fullImage.PageCount);

                            fullImage.Page = index;
                            int newWidth  = fullImage.Width;
                            int newHeight = fullImage.Height;

                            if (index + 1 > fullImage.PageCount || !_miQualityCombineTwoImages.Checked)
                            {
                                RasterImage newImage = fullImage.Clone();
                                _codecs.Save(newImage, saveDlg.FileName, RasterImageFormat.TifAbc, 1, 1, 1, 1, CodecsSavePageMode.Append);
                            }
                            else
                            {
                                fullImage.Page = index + 1;
                                newWidth       = Math.Max(newWidth, fullImage.Width);
                                newHeight     += fullImage.Height;
                                RasterImage newImage = new RasterImage(RasterMemoryFlags.Conventional, newWidth, newHeight, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, null, IntPtr.Zero, 0);

                                fullImage.Page = index;

                                cmd.SourceImage          = fullImage;
                                cmd.DestinationRectangle = new LeadRect(0, 0, fullImage.Width, fullImage.Height);
                                cmd.Run(newImage);

                                fullImage.Page = index + 1;

                                cmd.SourceImage          = fullImage;
                                cmd.DestinationRectangle = new LeadRect(0, newHeight - fullImage.Height, fullImage.Width, fullImage.Height);
                                cmd.Run(newImage);

                                _codecs.Save(newImage, saveDlg.FileName, RasterImageFormat.TifAbc, 1, 1, 1, 1, CodecsSavePageMode.Append);
                            }
                            Application.DoEvents();
                        }
                        fullImage.Page = 1;
                    }
                }
                catch (Exception ex)
                {
                    Messager.ShowFileSaveError(this, saveDlg.FileName, ex);
                }
                _sbMainStatus.Text = "";
            }
        }