Ejemplo n.º 1
0
        private void cutZones()
        {
            CutImagePath = string.Empty;
            var newImagePath = ImagePath.Replace(".jpg", "__.jpg");
            var drawing      = new Leptonica.Drawing.PixDrawing();

            using (var pix = new Pix(ImagePath))
            {
                using (Boxa boxa = new Boxa(Zones.Count))
                {
                    foreach (var zone in Zones)
                    {
                        boxa.AddBox(new Box((int)zone.X, (int)zone.Y, (int)zone.ActualWidth, (int)zone.ActualHeight));
                    }

                    int height = (int)(Zones.Max(z => z.Y + z.Height) - Zones.Min(z => z.Y));
                    int width  = (int)(Zones.Max(z => z.X + z.Width) - Zones.Min(z => z.X));

                    using (var newPix = new Pix(pix.Width, pix.Height, pix.Depth))
                    {
                        using (var mask = new Pix(pix.Width, pix.Height, 1))
                        {
                            drawing.MaskBoxa(mask, mask, boxa, GraphicPixelSetting.SET_PIXELS);
                            if (!drawing.CombineMaskedGeneral(newPix, pix, mask, 0, 0))
                            {
                                throw new Exception("Could not mask");
                            }
                        }

                        newPix.Save(newImagePath, ImageFileFormat.JFIF_JPEG);
                        CutImagePath = newImagePath;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public List <string> ocrDetectString(string filename)
        {
            List <string> result = new List <string>();

            if (ocr == null)
            {
                return(result);
            }
            Pix pix = Pix.LoadFromFile(filename);

            if (pix == null)
            {
                return(result);
            }
            Page page      = ocr.Process(pix);
            Pix  processed = page.GetThresholdedImage();

            processed.Save("./adtemp/process.png");
            string text = page.GetText();

            using (var iter = page.GetIterator())
            {
                iter.Begin();
                do
                {
                    do
                    {
                        do
                        {
                            do
                            {
                                if (iter.IsAtBeginningOf(PageIteratorLevel.Block))
                                {
                                    Console.WriteLine("<BLOCK>");
                                }

                                Console.Write(iter.GetText(PageIteratorLevel.Word));
                                Console.Write(" ");

                                if (iter.IsAtFinalOf(PageIteratorLevel.TextLine, PageIteratorLevel.Word))
                                {
                                    Console.WriteLine();
                                }
                            } while (iter.Next(PageIteratorLevel.TextLine, PageIteratorLevel.Word));

                            if (iter.IsAtFinalOf(PageIteratorLevel.Para, PageIteratorLevel.TextLine))
                            {
                                Console.WriteLine();
                            }
                        } while (iter.Next(PageIteratorLevel.Para, PageIteratorLevel.TextLine));
                    } while (iter.Next(PageIteratorLevel.Block, PageIteratorLevel.Para));
                } while (iter.Next(PageIteratorLevel.Block));
            }
            Console.WriteLine(text);
            result.Add(text);
            page.Dispose();
            pix.Dispose();
            return(result);
        }
Ejemplo n.º 3
0
        private void DEBUG_PrintImage(Pix image, string name, Stopwatch watch)
        {
            watch.Stop();

            image.Save(@"../../PerformanceTestData/" + name + ".bmp");

            watch.Start();
        }
        private void SaveResult(Pix result, string filename)
        {
            if (!Directory.Exists(ResultsDirectory))
            {
                Directory.CreateDirectory(ResultsDirectory);
            }

            result.Save(Path.Combine(ResultsDirectory, filename));
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Saves passed<see cref="Tesseract.Pix"/> to given path
 /// </summary>
 /// <param name="tmpFileName">
 /// provided file path to save the <see cref="Tesseract.Pix"/>
 /// </param>
 /// <param name="pix">
 /// provided <see cref="Tesseract.Pix"/> to be saved
 /// </param>
 internal static void SavePixToTempPngFile(string tmpFileName, Pix pix)
 {
     if (pix != null)
     {
         try
         {
             pix.Save(tmpFileName, Tesseract.ImageFormat.Png);
         }
         catch (Exception e)
         {
             LogManager.GetLogger(typeof(TesseractOcrUtil)).Info(MessageFormatUtil.Format(
                                                                     Tesseract4LogMessageConstant.CANNOT_PROCESS_IMAGE,
                                                                     e.Message));
         }
     }
 }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            new DirectoryInfo(@"C:\Users\ACER\Desktop\Tool")
            .GetFiles("*.jpg", SearchOption.AllDirectories)
            .ToList()
            .ForEach(x => {
                Pix pixs = Pix.LoadFromFile(x.FullName);
                pixs     = pixs.PixBackgroundNormSimple();
                pixs     = pixs.PixConvertRGBToGray();
                pixs     = pixs.PixFindSkewAndDeskew();
                pixs     = pixs.PixTophat();
                pixs     = pixs.PixInvert();
                pixs     = pixs.PixGammaRTC();
                pixs     = pixs.PixThresholdToBinary();

                /*
                 * pixs = Pix.Create(LeptonicaNativeApi.Native.pixBackgroundNormSimple(pixs.Reference, IntPtr.Zero, IntPtr.Zero));
                 * pixs = Pix.Create(LeptonicaNativeApi.Native.pixConvertRGBToGray(pixs.Reference, 0.5f, 0.3f, 0.2f));
                 *
                 * float angle, confidence;
                 * pixs = Pix.Create(LeptonicaNativeApi.Native.pixFindSkewAndDeskew(pixs.Reference, 2, out angle, out confidence));
                 *
                 * if(confidence > 2 && confidence < 3) {
                 *  angle *= (float) Math.PI / 180.0f;
                 *  pixs = Pix.Create(LeptonicaNativeApi.Native.pixRotate(pixs.Reference, angle, RotationMethod.Shear, RotationFill.White, pixs.Width, pixs.Height));
                 * }
                 *
                 * pixs = Pix.Create(LeptonicaNativeApi.Native.pixTophat(pixs.Reference, 17, 17, L_TOPHAT.BLACK));
                 * LeptonicaNativeApi.Native.pixInvert(pixs.Reference, pixs.Reference);
                 * LeptonicaNativeApi.Native.pixGammaTRC(pixs.Reference, pixs.Reference, 1.0f, 170, 245);
                 *
                 * pixs = Pix.Create(LeptonicaNativeApi.Native.pixThresholdToBinary(pixs.Reference, 35));*/
                pixs.Save(x.ToNewExtension(".pdf").FullName, ImageSaveFormat.Lpdf);
                pixs.Dispose();
            });

            Console.WriteLine("Entre key to exit...");
            Console.ReadLine();
        }
Ejemplo n.º 7
0
        private void SaveResult(Pix result, string filename)
        {
            var runFilename = TestResultRunFile(Path.Combine(ResultsDirectory, filename));

            result.Save(runFilename);
        }
Ejemplo n.º 8
0
        public static void Capture(IntPtr handle, Point pos, Size size, ref OCRCapture ocrCapture, bool debug = false)
        {
            if (handle == IntPtr.Zero)
            {
                return;
            }
            Bitmap b = CaptureWindowArea(handle, pos, size);

            if (b == null)
            {
                return;
            }
            if (debug)
            {
                Directory.CreateDirectory(Application.StartupPath + @"\debug");
            }
            if (debug)
            {
                b.Save(Application.StartupPath + @"\debug\00_input.png");
            }
            using (Pix pixc = PixConverter.ToPix(b)) {
                b.Dispose();
                using (Pix pixg = pixc.ConvertRGBToGray(0, 0, 0)) {
                    if (debug)
                    {
                        pixg.Save(Application.StartupPath + @"\debug\01_grayscale.png");
                    }
                    using (Pix pixs = pixg.ScaleGrayLI(Config.OCRScaleFactor, Config.OCRScaleFactor)) {
                        if (debug)
                        {
                            pixs.Save(Application.StartupPath + @"\debug\02_scale.png");
                        }
                        //pix = pix.UnsharpMaskingGray(5, 2.5f); //issues with light text on light bg
                        using (Pix pixb = pixs.BinarizeOtsuAdaptiveThreshold(2000, 2000, 0, 0, 0.0f)) {
                            if (debug)
                            {
                                pixb.Save(Application.StartupPath + @"\debug\03_binarize.png");
                            }
                            float pixAvg = pixb.AverageOnLine(0, 0, pixb.Width - 1, 0, 1);
                            pixAvg += pixb.AverageOnLine(0, pixb.Height - 1, pixb.Width - 1, pixb.Height - 1, 1);
                            pixAvg += pixb.AverageOnLine(0, 0, 0, pixb.Height - 1, 1);
                            pixAvg += pixb.AverageOnLine(pixb.Width - 1, 0, pixb.Width - 1, pixb.Height - 1, 1);
                            pixAvg /= 4.0f;
                            using (Pix pixi = pixAvg > 0.5f ? pixb.Invert() : pixb) {
                                if (debug)
                                {
                                    pixi.Save(Application.StartupPath + $@"\debug\04_invert_{pixAvg > 0.5f}.png");
                                }
                                using (Pix pixn = pixi.SelectBySize(Config.OCRNoiseSize, Config.OCRNoiseSize, Config.OCRNoiseConnectivity, Config.OCRNoiseType, Config.OCRNoiseRelation)) {
                                    if (debug)
                                    {
                                        pixn.Save(Application.StartupPath + @"\debug\05_removenoise.png");
                                    }
                                    //pixn.ClipToForeground(IntPtr.Zero);
                                    using (Pix pix = pixn.AddBorder(Config.OCRPadding, 0)) {
                                        if (debug)
                                        {
                                            pix.Save(Application.StartupPath + @"\debug\06_border.png");
                                        }
                                        pix.XRes = 300;
                                        pix.YRes = 300;

                                        using (Page page = Engine.Process(pix, PageSegMode.SingleLine)) {
                                            ocrCapture.Text       = page.GetText().Trim();
                                            ocrCapture.Confidence = page.GetMeanConfidence();
                                            ocrCapture.Iterations++;
                                            if (ocrCapture.Confidence >= Config.OCRMinimumConfidence)
                                            {
                                                if (decimal.TryParse(ocrCapture.Text, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out decimal cooldown))
                                                {
                                                    if (cooldown < Config.CooldownMaxPossible)
                                                    {
                                                        ocrCapture.Cooldown = cooldown;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 9
-1
        private void SaveResult(Pix result, string filename)
        {
            if (!Directory.Exists(ResultsDirectory)) Directory.CreateDirectory(ResultsDirectory);

            result.Save(Path.Combine(ResultsDirectory, filename));
        }