Exemplo n.º 1
0
        public unsafe void Convert_BitmapToPix(int depth)
        {
            string pixType;

            if (depth < 16)
            {
                pixType = "palette";
            }
            else if (depth == 16)
            {
                pixType = "grayscale";
            }
            else
            {
                pixType = "rgb";
            }

            var sourceFile      = String.Format("Conversion/photo_{0}_{1}bpp.tif", pixType, depth);
            var sourceFilePath  = TestFilePath(sourceFile);
            var bitmapConverter = new BitmapToPixConverter();

            using (var source = new Bitmap(sourceFilePath)) {
                Assert.That(BitmapHelper.GetBPP(source), Is.EqualTo(depth));
                using (var dest = bitmapConverter.Convert(source)) {
                    var destFilename = String.Format("Conversion/BitmapToPix_{0}_{1}bpp.tif", pixType, depth);
                    dest.Save(TestResultRunFile(destFilename), ImageFormat.Tiff);

                    AssertAreEquivalent(source, dest, true);
                }
            }
        }
Exemplo n.º 2
0
        private string GetTextFromBitmap(Bitmap bitmap)
        {
            Bitmap scaledBitmap = null;

            // If bitmap is too tiny, double size
            if (bitmap.Width < 1000 || bitmap.Height < 1000)
            {
                scaledBitmap = new Bitmap(bitmap, bitmap.Width * 2, bitmap.Height * 2);
                bitmap       = scaledBitmap;
            }

            using (Pix pix = new BitmapToPixConverter().Convert(bitmap))
            {
                if (scaledBitmap != null)
                {
                    scaledBitmap.Dispose();
                }

                // Avoid warning
                pix.XRes = 70;
                pix.YRes = 70;

                using (Page page = tesseractEngine.Process(pix, PageSegMode.Auto))
                {
                    return(page.GetText());
                }
            }
        }
Exemplo n.º 3
0
        private string ToString(Bitmap bmp)
        {
            string res = "";

            try
            {
                using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default))
                {
                    using (var img = new BitmapToPixConverter().Convert(bmp))
                    {
                        using (var page = engine.Process(img))
                        {
                            res = page.GetText();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Trace.TraceError(e.ToString());
                Console.WriteLine("Unexpected Error: " + e.Message);
                Console.WriteLine("Details: ");
                Console.WriteLine(e.ToString());
            }
            return(res.Trim());
        }
Exemplo n.º 4
0
        public string GetText(Bitmap image)
        {
            TesseractEngineInstance engineInstance = null;

            try
            {
                engineInstance = provider.Rent();
            }
            catch (InvalidOperationException)
            {
                if (engineInstance != null)
                {
                    provider.Release(engineInstance);
                }
                throw;
            }
            SetupEngine(engineInstance.Engine);

            using (var img = new BitmapToPixConverter().Convert(image))
            {
                using (var page = engineInstance.Engine.Process(img, PageSegMode.SingleWord))
                {
                    provider.Release(engineInstance);
                    return(page.GetText());
                }
            }
        }
        public unsafe void Convert_BitmapToPix(PixelFormat pixelFormat)
        {
            var    depth = Image.GetPixelFormatSize(pixelFormat);
            string pixType;

            if (depth < 16)
            {
                pixType = "palette";
            }
            else if (depth == 16)
            {
                pixType = "grayscale";
            }
            else
            {
                pixType = Image.IsAlphaPixelFormat(pixelFormat) ? "argb" : "rgb";
            }

            var sourceFile      = string.Format("Conversion/photo_{0}_{1}bpp.tif", pixType, depth);
            var sourceFilePath  = TestFilePath(sourceFile);
            var bitmapConverter = new BitmapToPixConverter();

            using (var source = new Bitmap(sourceFilePath))
            {
                Assert.AreEqual(source.PixelFormat, pixelFormat);
                Assert.AreEqual(source.GetBPP(), depth);
                using (var dest = bitmapConverter.Convert(source))
                {
                    var destFilename = string.Format("Conversion/BitmapToPix_{0}_{1}bpp.tif", pixType, depth);
                    dest.Save(TestResultRunFile(destFilename), ImageFormat.Tiff);

                    AssertAreEquivalent(source, dest, true);
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// https://github.com/tesseract-ocr/tessdata
        /// https://stackoverflow.com/questions/10947399/how-to-implement-and-do-ocr-in-a-c-sharp-project
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static string DoMagic(Bitmap image)
        {
            if (!Directory.Exists(@"./tessdata"))
            {
                Directory.CreateDirectory(@"./tessdata");
            }

            using TesseractEngine engine = new("./tessdata", "eng");
            using Pix pix   = new BitmapToPixConverter().Convert(image);
            using Page page = engine.Process(pix);

            return(page.GetText());
        }
Exemplo n.º 7
0
        private static void InvokeTesseractOnBitmap(Bitmap bitmap, TesseractEngine engine)
        {
            BitmapToPixConverter converter = new BitmapToPixConverter();

            using (var img = converter.Convert(bitmap))
            {
                using (var page = engine.Process(img))
                {
                    var text = page.GetText();
                    Console.WriteLine(text);
                }
            }
        }
Exemplo n.º 8
0
        public unsafe void Convert_BitmapToPix(
            [Values("photo.jpg", "photo.bmp", "photo_8.bmp", "photo_24.bmp", "photo.png", "photo_8.png", "photo_24.png", "photo_32.png", "photo.tif", "photo.gif")]
            string sourceFile)
        {
            var sourceFilePath = Path.Combine(DataDirectory, sourceFile);
            var bitmapConverter = new BitmapToPixConverter();
            using (var source = new Bitmap(sourceFilePath)) {
                using (var dest = bitmapConverter.Convert(source)) {
                    AssertAreEquivalent(source, dest, true);

                    //dest.Save("converted_img.bmp");
                }
            }
        }
Exemplo n.º 9
0
        private string ReadTextFromImage()
        {
            string          result         = "";
            var             imageConverter = new BitmapToPixConverter();
            Pix             image          = imageConverter.Convert(new Bitmap(@"E:\CompanyID.JPG"));
            TesseractEngine ocr            = new TesseractEngine(@"E:\tessdata", "eng");
            var             ocrResult      = ocr.Process(image, region: Rect.Empty);

            result = ocrResult.GetText();
            //MessageBox.Show(ocrResult.GetText());


            return(result);
        }
Exemplo n.º 10
0
        private static string AnalyseFrameUsingTesseract(Bitmap c1, TesseractEngine engine, out float cf1)
        {
            var    conv = new BitmapToPixConverter();
            var    p    = conv.Convert(c1);
            string t1;

            using (var page = engine.Process(p))
            {
                t1  = page.GetText().Replace("\n\n", "").Replace(".", "").Replace(",", "");
                cf1 = page.GetMeanConfidence();
            }

            return(t1);
        }
Exemplo n.º 11
0
 public static void ShowText(Bitmap bitmap, TextBox textBox)
 {
     textBox.Text = "";
     using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default))
     {
         using (var pix = new BitmapToPixConverter().Convert(bitmap))
         {
             using (var page = engine.Process(pix, PageSegMode.SingleLine))
             {
                 textBox.Text = page.GetText().Trim();
             }
         }
     }
 }
Exemplo n.º 12
0
        public unsafe void Convert_BitmapToPix_Format8bppIndexed()
        {
            var sourceFile      = TestFilePath("Conversion/photo_palette_8bpp.png");
            var bitmapConverter = new BitmapToPixConverter();

            using (var source = new Bitmap(sourceFile)) {
                Assert.That(source.GetBPP(), Is.EqualTo(8));
                Assert.That(source.PixelFormat, Is.EqualTo(PixelFormat.Format8bppIndexed));
                using (var dest = bitmapConverter.Convert(source)) {
                    var destFilename = TestResultRunFile("Conversion/BitmapToPix_palette_8bpp.png");
                    dest.Save(destFilename, ImageFormat.Png);

                    AssertAreEquivalent(source, dest, true);
                }
            }
        }
Exemplo n.º 13
0
        public unsafe void Convert_ScaledBitmapToPix()
        {
            var sourceFilePath  = TestFilePath("Conversion/photo_rgb_32bpp.tif");
            var bitmapConverter = new BitmapToPixConverter();

            using (var source = new Bitmap(sourceFilePath)) {
                using (var scaledSource = new Bitmap(source, new Size(source.Width * 2, source.Height * 2))) {
                    Assert.That(scaledSource.GetBPP(), Is.EqualTo(32));
                    using (var dest = bitmapConverter.Convert(scaledSource)) {
                        dest.Save(TestResultRunFile("Conversion/ScaledBitmapToPix_rgb_32bpp.tif"), ImageFormat.Tiff);

                        AssertAreEquivalent(scaledSource, dest, true);
                    }
                }
            }
        }
Exemplo n.º 14
0
 public void ReadScreen()
 {
     try
     {
         log.Info("OCR read screen");
         TesseractEnviornment.CustomSearchPath = "resources/Tesseract";
         var engine = new TesseractEngine(@"./resources/Tesseract/tessdata", "eng", EngineMode.Default);     //var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default);
         var conv   = new BitmapToPixConverter();
         var img    = conv.Convert(ImageUtil.RemoveNoise(ImageUtil.SetGrayscale(adbClass.GetAdbScreen())));
         var page   = engine.Process(img);
         log.Info("OCR : " + page.GetText());
     }
     catch (Exception e)
     {
         log.Debug(e);
     }
 }
Exemplo n.º 15
0
        public unsafe void Convert_BitmapToPix_Format8bppIndexed()
        {
            if (!Directory.Exists(ResultsDirectory)) Directory.CreateDirectory(ResultsDirectory);

            var sourceFile = "photo_palette_8bpp.png";
            var sourceFilePath = Path.Combine(DataDirectory, sourceFile);
            var bitmapConverter = new BitmapToPixConverter();
            using (var source = new Bitmap(sourceFilePath)) {
                Assert.That(BitmapHelper.GetBPP(source), Is.EqualTo(8));
                Assert.That(source.PixelFormat, Is.EqualTo(PixelFormat.Format8bppIndexed));
                using (var dest = bitmapConverter.Convert(source)) {
                    var destFilename = "BitmapToPix_palette_8bpp.png";
                    dest.Save(Path.Combine(ResultsDirectory, destFilename), ImageFormat.Png);

                    AssertAreEquivalent(source, dest, true);
                }
            }
        }
Exemplo n.º 16
0
        public unsafe void Convert_ScaledBitmapToPix()
        {
            if (!Directory.Exists(ResultsDirectory)) Directory.CreateDirectory(ResultsDirectory);

            var sourceFile = "photo_rgb_32bpp.tif";
            var sourceFilePath = Path.Combine(DataDirectory, sourceFile);
            var bitmapConverter = new BitmapToPixConverter();
            using (var source = new Bitmap(sourceFilePath)) {
                using (var scaledSource = new Bitmap(source, new Size(source.Width * 2, source.Height * 2))) {
                    Assert.That(BitmapHelper.GetBPP(scaledSource), Is.EqualTo(32));
                    using (var dest = bitmapConverter.Convert(scaledSource)) {
                        var destFilename = "ScaledBitmapToPix_rgb_32bpp.tif";
                        dest.Save(Path.Combine(ResultsDirectory, destFilename), ImageFormat.Tiff);

                        AssertAreEquivalent(scaledSource, dest, true);
                    }
                }
            }
        }
Exemplo n.º 17
0
        public static List <Pix> GetTextAreas(string filePath)
        {
            using (Mat src = new Mat(filePath, ImreadModes.Color))
                using (Mat gray = new Mat())
                {
                    Cv2.CvtColor(src, gray, ColorConversionCodes.BGR2GRAY);

                    MSER               mser          = MSER.Create();
                    Point[][]          msers         = null;
                    OpenCvSharp.Rect[] boundingBoxes = null;
                    mser.DetectRegions(gray, out msers, out boundingBoxes); // MSER::operator()

                    int meanWidth = (int)(boundingBoxes.Select(x => x.Width).Average());
                    int stdWidth  = (int)(
                        Math.Sqrt(
                            boundingBoxes.Select(
                                x => x.Width
                                ).Average(
                                x => x * x
                                ) - Math.Pow(meanWidth, 2)));

                    int meanHeight = (int)(boundingBoxes.Select(x => x.Height).Average());
                    int stdHeight  = (int)(
                        Math.Sqrt(
                            boundingBoxes.Select(
                                x => x.Height
                                ).Average(
                                x => x * x
                                ) - Math.Pow(meanHeight, 2)));

                    foreach (OpenCvSharp.Rect rect in boundingBoxes)
                    {
                        rect.Inflate(6, 2);
                        if (rect.Width - meanWidth < stdWidth && rect.Height - meanHeight < stdHeight)
                        {
                            gray.Rectangle(rect, Scalar.Black, -1);
                        }
                    }
                    var bitmapToPix = new BitmapToPixConverter();
                    return(ExtractTextAreasFromMask(gray).Select(x => bitmapToPix.Convert(x.ToBitmap())).ToList());
                }
        }
Exemplo n.º 18
0
        public BuildingMatch GetBuilding()
        {
            var bitmap = SnapShotBuildingTitle();

            if (bitmap == null)
            {
                return(null);
            }

            using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default))
            {
                using (var pix = new BitmapToPixConverter().Convert(bitmap))
                {
                    using (var page = engine.Process(pix, PageSegMode.SingleLine))
                    {
                        return(ToBuildingName(page.GetText().Trim().ToLower()));
                    }
                }
            }
        }
Exemplo n.º 19
0
        private void ShowBitmap(Bitmap bitmap, PictureBox pictureBox, TextBox textBox)
        {
            textBox.Text = "";
            pictureBox.BackgroundImage = null;

            if (bitmap != null)
            {
                pictureBox.BackgroundImage = ToImage(bitmap);

                using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default))
                {
                    using (var pix = new BitmapToPixConverter().Convert(bitmap))
                    {
                        using (var page = engine.Process(pix, PageSegMode.SingleLine))
                        {
                            textBox.Text = page.GetText().Trim();
                        }
                    }
                }
            }
        }
Exemplo n.º 20
0
        public unsafe void Convert_BitmapToPix_Format8bppIndexed()
        {
            if (!Directory.Exists(ResultsDirectory))
            {
                Directory.CreateDirectory(ResultsDirectory);
            }

            var sourceFile      = "photo_palette_8bpp.png";
            var sourceFilePath  = Path.Combine(DataDirectory, sourceFile);
            var bitmapConverter = new BitmapToPixConverter();

            using (var source = new Bitmap(sourceFilePath)) {
                Assert.That(BitmapHelper.GetBPP(source), Is.EqualTo(8));
                Assert.That(source.PixelFormat, Is.EqualTo(PixelFormat.Format8bppIndexed));
                using (var dest = bitmapConverter.Convert(source)) {
                    var destFilename = "BitmapToPix_palette_8bpp.png";
                    dest.Save(Path.Combine(ResultsDirectory, destFilename), ImageFormat.Png);

                    AssertAreEquivalent(source, dest, true);
                }
            }
        }
Exemplo n.º 21
0
        public int getAvaiableStorage()
        {
            Bitmap bitmap = Crop(352, 19, 75, 25);
            int    rel    = 0;

            using (var pix = new BitmapToPixConverter().Convert(bitmap))
            {
                using (var page = engine.Process(pix, PageSegMode.SingleLine))
                {
                    try
                    {
                        log.Info(page.GetText().Trim().ToLower());
                        rel = int.Parse(page.GetText().Trim().Split('/')[1]) - int.Parse(page.GetText().Trim().Split('/')[0]);
                    }
                    catch (System.Exception ex)
                    {
                        log.Info(ex);
                    }
                }
            }
            return(rel);
        }
Exemplo n.º 22
0
        public unsafe void Convert_BitmapToPix(int depth)
        {
            if (!Directory.Exists(ResultsDirectory)) Directory.CreateDirectory(ResultsDirectory);

            string pixType;
            if (depth < 16) pixType = "palette";
            else if (depth == 16) pixType = "grayscale";
            else pixType = "rgb";

            var sourceFile = String.Format("photo_{0}_{1}bpp.tif", pixType, depth);
            var sourceFilePath = Path.Combine(DataDirectory, sourceFile);
            var bitmapConverter = new BitmapToPixConverter();
            using (var source = new Bitmap(sourceFilePath)) {
                Assert.That(BitmapHelper.GetBPP(source), Is.EqualTo(depth));
                using (var dest = bitmapConverter.Convert(source)) {
                    var destFilename = String.Format("BitmapToPix_{0}_{1}bpp.tif", pixType, depth);
                    dest.Save(Path.Combine(ResultsDirectory, destFilename), ImageFormat.Tiff);

                    AssertAreEquivalent(source, dest, true);
                }
            }
        }
Exemplo n.º 23
0
        public static TessConvertResult BitmapToString(Bitmap bitmap, OcrMode mode)
        {
            string textResult;
            float  confidence;

            if (bitmap.Size.IsEmpty)
            {
                return(null);
            }

            using (var engine = new TesseractEngine(@"./tessdata", "pol", EngineMode.Default))
            {
                if (mode == OcrMode.numeric)
                {
                    engine.SetVariable("tessedit_char_whitelist", "-0123456789.,");
                }
                Tesseract.BitmapToPixConverter converter = new BitmapToPixConverter();

                var imgPix = converter.Convert(bitmap);
                imgPix = imgPix.ConvertRGBToGray();
                imgPix = imgPix.Scale(7, 7);
                //imgPix.Save("test.tiff");


                using (var page = engine.Process(imgPix))
                {
                    textResult = page.GetText();
                    confidence = page.GetMeanConfidence();
                }

                if (confidence == 0 || textResult == "")
                {
                    return(null);
                }
            }

            return(new TessConvertResult(textResult, confidence));
        }
Exemplo n.º 24
0
        public unsafe void Convert_ScaledBitmapToPix()
        {
            if (!Directory.Exists(ResultsDirectory))
            {
                Directory.CreateDirectory(ResultsDirectory);
            }

            var sourceFile      = "photo_rgb_32bpp.tif";
            var sourceFilePath  = Path.Combine(DataDirectory, sourceFile);
            var bitmapConverter = new BitmapToPixConverter();

            using (var source = new Bitmap(sourceFilePath)) {
                using (var scaledSource = new Bitmap(source, new Size(source.Width * 2, source.Height * 2))) {
                    Assert.That(BitmapHelper.GetBPP(scaledSource), Is.EqualTo(32));
                    using (var dest = bitmapConverter.Convert(scaledSource)) {
                        var destFilename = "ScaledBitmapToPix_rgb_32bpp.tif";
                        dest.Save(Path.Combine(ResultsDirectory, destFilename), ImageFormat.Tiff);

                        AssertAreEquivalent(scaledSource, dest, true);
                    }
                }
            }
        }
Exemplo n.º 25
0
        public unsafe void Convert_BitmapToPix(int depth)
        {
            if (!Directory.Exists(ResultsDirectory))
            {
                Directory.CreateDirectory(ResultsDirectory);
            }

            string pixType;

            if (depth < 16)
            {
                pixType = "palette";
            }
            else if (depth == 16)
            {
                pixType = "grayscale";
            }
            else
            {
                pixType = "rgb";
            }

            var sourceFile      = String.Format("photo_{0}_{1}bpp.tif", pixType, depth);
            var sourceFilePath  = Path.Combine(DataDirectory, sourceFile);
            var bitmapConverter = new BitmapToPixConverter();

            using (var source = new Bitmap(sourceFilePath)) {
                Assert.That(BitmapHelper.GetBPP(source), Is.EqualTo(depth));
                using (var dest = bitmapConverter.Convert(source)) {
                    var destFilename = String.Format("BitmapToPix_{0}_{1}bpp.tif", pixType, depth);
                    dest.Save(Path.Combine(ResultsDirectory, destFilename), ImageFormat.Tiff);

                    AssertAreEquivalent(source, dest, true);
                }
            }
        }
Exemplo n.º 26
0
    static oTesseractRequest __ocrExecute(oTesseractRequest req, Bitmap image)
    {
        PageIteratorLevel level = PageIteratorLevel.Word;

        switch (req.command)
        {
        case TESSERACT_COMMAND.GET_SEGMENTED_REGION_BLOCK:
            level = PageIteratorLevel.Block;
            break;

        case TESSERACT_COMMAND.GET_SEGMENTED_REGION_PARA:
            level = PageIteratorLevel.Para;
            break;

        case TESSERACT_COMMAND.GET_SEGMENTED_REGION_SYMBOL:
            level = PageIteratorLevel.Symbol;
            break;

        case TESSERACT_COMMAND.GET_SEGMENTED_REGION_TEXTLINE:
            level = PageIteratorLevel.TextLine;
            break;

        case TESSERACT_COMMAND.GET_SEGMENTED_REGION_WORD:
            level = PageIteratorLevel.Word;
            break;

        case TESSERACT_COMMAND.GET_TEXT:
            break;
        }

        EngineMode mode = EngineMode.Default;

        switch (req.mode)
        {
        case ENGINE_MODE.LSTM_ONLY:
            mode = EngineMode.LstmOnly;
            break;

        case ENGINE_MODE.TESSERACT_AND_LSTM:
            mode = EngineMode.TesseractAndLstm;
            break;

        case ENGINE_MODE.TESSERACT_ONLY:
            mode = EngineMode.TesseractOnly;
            break;
        }

        using (var engine = new TesseractEngine(req.data_path, req.lang, mode))
            using (var pix = new BitmapToPixConverter().Convert(image))
            {
                using (var tes = engine.Process(pix))
                {
                    switch (req.command)
                    {
                    case TESSERACT_COMMAND.GET_TEXT:
                        string s = tes.GetText().Trim();
                        req.output_text  = s;
                        req.output_count = s.Length;
                        req.ok           = 1;
                        break;

                    default:
                        var boxes = tes.GetSegmentedRegions(level).Select(x =>
                                                                          string.Format("{0}_{1}_{2}_{3}", x.X, x.Y, x.Width, x.Height)).ToArray();
                        req.output_format = "x_y_width_height";
                        req.output_text   = string.Join("|", boxes.Select(x => x.ToString()).ToArray());
                        req.output_count  = boxes.Length;
                        req.ok            = 1;
                        break;
                    }
                }
            }
        return(req);
    }
Exemplo n.º 27
0
        public void PerformOcr(List <Tuple <int, int> > textRowLocations)
        {
            var conv = new BitmapToPixConverter();

            Pix p = conv.Convert(_bTrimmedHeader);

            string headerResult;

            using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default))
            {
                using (var page = engine.Process(p))
                {
                    var text = page.GetText();
                    headerResult = StripPunctuationFromScannedText(text);// (text + " {" + page.GetMeanConfidence() + "}\r\n");
                }
            }

            var matchesInStationReferenceList =
                _callingForm.StationReferenceList.Where(x => x.System == SystemAtTimeOfScreenshot.ToUpper()).OrderBy(x => _levenshtein.LD2(headerResult, x.Name)).ToList();

            var q = _callingForm.StationReferenceList.Where(x => x.Name.Contains("'"));

            if (matchesInStationReferenceList.Count > 0)
            {
                var ld = _levenshtein.LD2(headerResult, matchesInStationReferenceList[0].Name.ToUpper());
                if (ld < 5)
                {
                    headerResult = matchesInStationReferenceList[0].Name.ToUpper();
                }
            }

            _callingForm.DisplayResults(headerResult);

            var commodityColumnText       = new string[textRowLocations.Count(), 8];;
            var originalBitmaps           = new Bitmap[textRowLocations.Count(), 8];
            var originalBitmapConfidences = new float[textRowLocations.Count(), 8];
            var rowIds = new string[textRowLocations.Count()];

            var rowCtr = 0;

            var bTrimmedContrast = Contrast(MakeGrayscale(MakeBrighter((Bitmap)(_bTrimmed.Clone()), .25f)), 60);

            var bitmapCtr = 0;

            foreach (var row in textRowLocations)
            {
                int startRow  = row.Item1 - 3;
                int heightRow = row.Item2 - row.Item1 + 6;

                if (startRow < 0)
                {
                    startRow = 0;
                }
                if (heightRow + startRow > bTrimmedContrast.Height)
                {
                    heightRow = bTrimmedContrast.Height - startRow;
                }

                // We'll use this later to identify the right correction image
                rowIds[rowCtr] = Guid.NewGuid().ToString();
                using (Bitmap b = Crop(bTrimmedContrast, new Rectangle(0, startRow, bTrimmedContrast.Width, heightRow)))
                {
                    b.Save(".//OCR Correction Images//" + rowIds[rowCtr] + ".png");
                }

                int columnCounter = 0;
                while (columnCounter < 8)
                {
                    int left, width;
                    switch (columnCounter)
                    {
                    case 0:
                        left = 0; width = _calibrationPoints[3].X - _calibrationPoints[2].X;
                        break;

                    case 1:
                        left = _calibrationPoints[3].X - _calibrationPoints[2].X; width = _calibrationPoints[4].X - _calibrationPoints[3].X;
                        break;

                    case 2:
                        left = _calibrationPoints[4].X - _calibrationPoints[2].X; width = _calibrationPoints[5].X - _calibrationPoints[4].X;
                        break;

                    case 3:
                        left = _calibrationPoints[5].X - _calibrationPoints[2].X; width = _calibrationPoints[6].X - _calibrationPoints[5].X;
                        break;

                    case 4:
                        left = _calibrationPoints[6].X - _calibrationPoints[2].X; width = _calibrationPoints[7].X - _calibrationPoints[6].X;
                        break;

                    case 5:
                        left = _calibrationPoints[7].X - _calibrationPoints[2].X; width = _calibrationPoints[8].X - _calibrationPoints[7].X;
                        break;

                    case 6:
                        left = _calibrationPoints[8].X - _calibrationPoints[2].X; width = _calibrationPoints[9].X - _calibrationPoints[8].X;
                        break;

                    case 7:
                        left = _calibrationPoints[9].X - _calibrationPoints[2].X; width = _calibrationPoints[10].X - _calibrationPoints[9].X;
                        break;

                    default:
                        left = 0; width = _calibrationPoints[3].X - _calibrationPoints[2].X;
                        break;
                    }
                    var fudgeFactor = 0;// _bOriginal.Height * 6 / 1440;
                    left  = left + fudgeFactor;
                    width = width - fudgeFactor;

                    if (columnCounter != 0 && columnCounter != 5 && columnCounter != 7)
                    {   //If it's a numeric column write it out for Brainerous to process later
                        var brainerousOut = Crop(bTrimmedContrast,
                                                 new Rectangle(left, startRow, width, heightRow));

                        if (!Directory.Exists("./Brainerous/images"))
                        {
                            Directory.CreateDirectory("./Brainerous/images");
                        }

                        brainerousOut.Save("./Brainerous/images/" + bitmapCtr + ".png");
                        bitmapCtr++;
                    }
                    else
                    {   // It's a text column, we'll use Tesseract
                        // Prepare some different versions of the bitmap, we will take the best result
                        var c = new Bitmap[7];
                        c[0] = (Crop(bTrimmedContrast, new Rectangle(left, startRow, width, heightRow)));
                        c[1] = (Crop(bTrimmedContrast, new Rectangle(left + 1, startRow, width, heightRow)));
                        c[2] = (Crop(bTrimmedContrast, new Rectangle(left - 1, startRow, width, heightRow)));
                        c[3] = (Crop(bTrimmedContrast, new Rectangle(left, startRow - 1, width, heightRow)));
                        c[4] = (Crop(bTrimmedContrast, new Rectangle(left + 1, startRow - 1, width, heightRow)));
                        c[5] = (Crop(bTrimmedContrast, new Rectangle(left - 1, startRow - 1, width, heightRow)));
                        c[6] = (Crop(bTrimmedContrast, new Rectangle(left, startRow + 2, width, heightRow - 2)));

                        var t  = new string[c.Length];
                        var cf = new float[c.Length];

                        using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default))
                        {
                            for (int i = 0; i < c.Length; i++)
                            {
                                t[i] = AnalyseFrameUsingTesseract((Bitmap)(c[i].Clone()), engine, out cf[i]);
                            }
                        }

                        int   result     = 0;
                        float confidence = cf[0];

                        for (int i = 1; i < c.Length; i++)
                        {
                            if (confidence < cf[i])
                            {
                                result = i; confidence = cf[i];
                            }
                        }

                        originalBitmaps[rowCtr, columnCounter] = (Bitmap)(c[result].Clone());

                        switch (columnCounter)
                        {
                        //bodges for number columns
                        case 1:
                        case 2:
                        case 3:
                            t[result] = t[result].Replace(" ", "").Replace("O", "0").Replace("I", "1").Replace("'", "");
                            t[result] = System.Text.RegularExpressions.Regex.Replace(t[result], @"[a-zA-Z\s]+", string.Empty);     // remove any alphas that remain
                            break;

                        case 5:
                        case 7:
                            t[result] = t[result].Replace(" ", "").Replace("-", "");
                            if (t[result] == "HIGH" || t[result] == "MED" || t[result] == "LOW")
                            {
                                cf[result] = 1;
                            }
                            break;
                        }
                        if ((columnCounter == 5 && t[result].Contains("ENTER")) ||
                            (columnCounter == 6 && (t[result].Contains("NGAR") || t[result].Contains("SURFACE"))))
                        {
                            t[result]  = "";
                            cf[result] = 1;
                        }
                        commodityColumnText[rowCtr, columnCounter]      += t[result];
                        originalBitmapConfidences[rowCtr, columnCounter] = cf[result];
                    }

                    columnCounter++;
                }
                rowCtr++;
            }

            if (textRowLocations.Count > 0)
            {
                // Call out to Brainerous to process the numeric bitmaps we saved earlier
                var outputFromBrainerous = "";
                var pr = new Process();
                pr.StartInfo.UseShellExecute        = false;
                pr.StartInfo.CreateNoWindow         = true;
                pr.StartInfo.RedirectStandardOutput = true;
                pr.StartInfo.FileName         = "./Brainerous/nn_training.exe";
                pr.StartInfo.WorkingDirectory = "./Brainerous/";
                pr.Start();
                outputFromBrainerous = pr.StandardOutput.ReadToEnd();
                while (outputFromBrainerous.Contains("Failed to pad successfully"))
                {
                    var o2 = outputFromBrainerous.IndexOf("Failed to ");
                    var o3 = outputFromBrainerous.Substring(0, o2);
                    var o4 = outputFromBrainerous.Substring(o2).IndexOf("./images");
                    var o5 = outputFromBrainerous.Substring(o2 + o4);
                    outputFromBrainerous = o3 + "\r\n" + o5;
                }

                pr.WaitForExit();

                var splitOutput = outputFromBrainerous.Replace("\r", "").Split('\n');

                // Load the result from Brainerous into the OCR output
                for (var i = 0; i < textRowLocations.Count; i++)
                {
                    commodityColumnText[i, 1]       = splitOutput[i * 10 + 1];
                    originalBitmaps[i, 1]           = null;
                    originalBitmapConfidences[i, 1] = 1;
                    commodityColumnText[i, 2]       = splitOutput[i * 10 + 3];
                    originalBitmaps[i, 2]           = null;
                    originalBitmapConfidences[i, 2] = 1;
                    commodityColumnText[i, 3]       = splitOutput[i * 10 + 5];
                    originalBitmaps[i, 3]           = null;
                    originalBitmapConfidences[i, 3] = 1;
                    commodityColumnText[i, 4]       = splitOutput[i * 10 + 7];
                    originalBitmaps[i, 4]           = null;
                    originalBitmapConfidences[i, 4] = 1;
                    commodityColumnText[i, 6]       = splitOutput[i * 10 + 9];
                    originalBitmaps[i, 6]           = null;
                    originalBitmapConfidences[i, 6] = 1;
                }
            }
            _bOriginal.Dispose();
            _bOriginalClone.Dispose();
            // Send the results for this screenshot back to the Form
            _callingForm.DisplayCommodityResults(commodityColumnText, originalBitmaps, originalBitmapConfidences, rowIds, CurrentScreenshot);

            // ...and if we've got any buffered screenshots waiting to be processed, process the next one
            if (ScreenshotBuffer.Count > 0)
            {
                var screenshot = ScreenshotBuffer[0];
                ScreenshotBuffer.Remove(screenshot);
                ProcessNewScreenshot(screenshot);
            }

            Working = false;

            Debug.WriteLine("set to " + Working);
        }
        private bool CaptureLoadsXBOX()
        {
            bool isLoad = false;

            if ((timerStart.Ticks - liveSplitState.Run.Offset.Ticks) <= DateTime.Now.Ticks)
            {
                framesSinceLastManualSplit++;
                //Console.WriteLine("TIME NOW: {0}", DateTime.Now - lastTime);
                //Console.WriteLine("TIME DIFF START: {0}", DateTime.Now - lastTime);
                //lastTime = DateTime.Now;

                wasBlackScreen = isBlackScreen;
                //Capture image using the settings defined for the component
                Bitmap capture = settings.CaptureImage();
                isBlackScreen = FeatureDetector.IsBlackScreen(ref capture, settings.blacklevel);
                BitmapToPixConverter btp = new BitmapToPixConverter();
                //if (!testSaved)
                //{
                //    Bitmap jpntestbmp = new Bitmap("cutout.bmp");
                //    FeatureDetector.ClearBackgroundPostLoad(ref jpntestbmp);
                //    jpntestbmp.Save("jpntest.bmp");
                //    Pix jpntest = btp.Convert(jpntestbmp);
                //    using (var page = engine.Process(jpntest, PageSegMode.SingleChar))
                //    {
                //        Console.WriteLine(page.GetText());
                //    }
                //    testSaved = true;
                //}

                if (wasBlackScreen && !isBlackScreen)
                {
                    //This could be a pre-load transition, start timing it
                    transitionStart = DateTime.Now;
                    waitOnLoad      = true;
                }

                if (!isBlackScreen && !waitOnFadeIn && waitOnLoad)
                {
                    specialLoad = FeatureDetector.ClearBackground(ref capture, settings.blacklevel);
                    Pix    img        = btp.Convert(capture);
                    string ResultText = "";
                    using (var page = engine.Process(img, PageSegMode.SingleChar))
                    {
                        ResultText = page.GetText();
                    }
                    int counter = 0;
                    foreach (char c in expectedResultEng)
                    {
                        if (ResultText.Contains(c))
                        {
                            counter++;
                        }
                    }
                    if (counter > 5 && ResultText.Length == 8)
                    {
                        isLoading = true;
                        isLoad    = true;
                    }
                }

                timer.CurrentState.IsGameTimePaused = isLoading || isBlackScreen;

                if (waitOnFadeIn && !specialLoad)
                {
                    capture = settings.CaptureImagePostLoad();
                    int lowestBitLast = lowestBit;
                    lowestBit = FeatureDetector.ClearBackgroundPostLoad(ref capture, settings.blacklevel);
                    if (lowestBit == lowestBitLast && lowestBit != 0)
                    {
                        Pix img = btp.Convert(capture);
                        using (var page = engine.Process(img, PageSegMode.SingleChar))
                        {
                            string result = page.GetText();
                            if (result != "\n")
                            {
                                Console.WriteLine(page.GetText());
                                waitOnFadeIn = false;
                                isLoading    = false;
                                lowestBit    = 0;
                                //the lifecounter coming in from the top takes a quarter of a second to stop
                                TimeSpan quarter = new TimeSpan(2500000);
                                timer.CurrentState.SetGameTime(timer.CurrentState.GameTimePauseTime + quarter);
                            }
                        }
                    }
                }

                if (waitOnFadeIn && isBlackScreen && !specialLoad)
                {
                    waitOnFadeIn = false;
                    isLoading    = false;
                }

                if (waitOnFadeIn && specialLoad && FeatureDetector.IsEndOfSpecialLoad(ref capture, settings.blacklevel))
                {
                    specialLoad  = false;
                    waitOnFadeIn = false;
                    isLoading    = false;
                }

                if (isLoading && waitOnLoad)
                {
                    // This was a pre-load transition, subtract the gametime
                    TimeSpan delta = (DateTime.Now - transitionStart);
                    timer.CurrentState.SetGameTime(timer.CurrentState.GameTimePauseTime - delta);
                    waitOnLoad   = false;
                    waitOnFadeIn = true;
                }
            }
            return(isLoad);
        }
Exemplo n.º 29
0
    public static void convertImage2Text_OneOrAllPage(string requestId, COMMANDS cmd, string input, Dictionary <string, object> data)
    {
        var redis = new RedisBase(new RedisSetting(REDIS_TYPE.ONLY_WRITE, __CONFIG.REDIS_PORT_READ));

        long docId = 0;
        int  page  = -1;

        string[] a = input.Split('.');
        long.TryParse(a[0], out docId);
        if (a.Length > 1)
        {
            int.TryParse(a[1], out page);
        }

        var ocr_lang  = data.Get <string>("ocr_lang", "vie");
        var ocr_mode  = data.Get <EngineMode>("ocr_mode", EngineMode.Default);
        var ocr_level = data.Get <PageIteratorLevel>("ocr_level", PageIteratorLevel.Word);

        try
        {
            if (redis.HEXISTS(docId.ToString(), page.ToString()))
            {
                var bitmap = redis.HGET_BITMAP(docId, page);
                if (bitmap != null)
                {
                    var dic = new Dictionary <string, object>()
                    {
                        { "id", docId },
                        { "page", page },
                    };

                    using (var engine = new TesseractEngine("tessdata", ocr_lang, ocr_mode))
                        using (var pix = new BitmapToPixConverter().Convert(bitmap))
                        {
                            using (var tes = engine.Process(pix))
                            {
                                switch (cmd)
                                {
                                case COMMANDS.OCR_TEXT_PAGE:
                                    string s = tes.GetText().Trim();
                                    dic.Add("ocr_text", s);
                                    break;

                                case COMMANDS.OCR_BOX_PAGE:
                                    var boxes = tes.GetSegmentedRegions(ocr_level).Select(x =>
                                                                                          string.Format("{0}_{1}_{2}_{3}", x.X, x.Y, x.Width, x.Height)).ToArray();
                                    dic.Add("box_format", "x_y_width_height");
                                    dic.Add("box_text", string.Join("|", boxes.Select(x => x.ToString()).ToArray()));
                                    dic.Add("box_count", boxes.Length);
                                    break;
                                }
                            }
                        }

                    App.Reply(cmd, requestId, input, dic);
                }
            }
        }
        catch (Exception exInfo)
        {
            //string errInfo = cmd.ToString() + " -> " + file + Environment.NewLine + exInfo.Message + Environment.NewLine + exInfo.StackTrace;
            //redis.HSET("_ERROR:PDF:" + cmd.ToString(), docId.ToString(), errInfo);
        }
    }
        private bool CaptureLoadsPS2()
        {
            bool isLoad = false;

            framesSinceLastManualSplit++;
            //Console.WriteLine("TIME NOW: {0}", DateTime.Now - lastTime);13
            //Console.WriteLine("TIME DIFF START: {0}", DateTime.Now - lastTime);
            lastTime = DateTime.Now;

            //Capture image using the settings defined for the component
            Bitmap capture = settings.CaptureImage();

            wasBlackScreen = isBlackScreen;
            isBlackScreen  = FeatureDetector.IsBlackScreen(ref capture, settings.blacklevel);
            //BitmapToPixConverter btp = new BitmapToPixConverter();
            //if (!testSaved)
            //{
            //    Bitmap jpntestbmp = new Bitmap("cutout.bmp");
            //    FeatureDetector.GetBlackLevel(ref jpntestbmp);
            //    FeatureDetector.ClearBackground(ref jpntestbmp);
            //    jpntestbmp.Save("jpntest.bmp");
            //    Pix jpntest = btp.Convert(jpntestbmp);
            //    using (var page = engine.Process(jpntest, PageSegMode.SingleChar))
            //    {
            //        Console.WriteLine(page.GetText());
            //    }
            //    testSaved = true;
            //}


            if (!isBlackScreen && waitOnLoad)
            {
                FeatureDetector.ClearBackground(ref capture, settings.blacklevel);

                BitmapToPixConverter btp = new BitmapToPixConverter();
                Pix    img        = btp.Convert(capture);
                string ResultText = "";
                using (var page = engine.Process(img, PageSegMode.SingleChar))
                {
                    ResultText = page.GetText();
                    //Console.WriteLine(ResultText);
                }
                int counter = 0;
                if (settings.platform == "ENG/PS2")
                {
                    foreach (char c in expectedResultEng)
                    {
                        if (ResultText.Contains(c))
                        {
                            counter++;
                        }
                    }
                    if (counter > 5 && ResultText.Length == 8)
                    {
                        isLoading = true;
                        isLoad    = true;
                    }
                    else
                    {
                        waitFrames++;
                        if (waitFrames == WAIT_ON_LOAD_FRAMES)
                        {
                            waitOnLoad = false;
                            waitFrames = 0;
                        }
                    }
                }
                else if (settings.platform == "JPN/PS2")
                {
                    foreach (char c in expectedResultJpn)
                    {
                        if (ResultText.Contains(c))
                        {
                            counter++;
                        }
                    }
                    if (counter > 3)
                    {
                        isLoading = true;
                        isLoad    = true;
                    }
                    else
                    {
                        waitFrames++;
                        if (waitFrames == WAIT_ON_LOAD_FRAMES)
                        {
                            waitOnLoad = false;
                            waitFrames = 0;
                        }
                    }
                }
                //TODO: add korean
                else
                {
                    foreach (char c in expectedResultKrn)
                    {
                        if (ResultText.Contains(c))
                        {
                            counter++;
                        }
                    }
                    if (counter > 1)
                    {
                        isLoading = true;
                        isLoad    = true;
                    }
                    else
                    {
                        waitFrames++;
                        if (waitFrames == WAIT_ON_LOAD_FRAMES)
                        {
                            waitOnLoad = false;
                            waitFrames = 0;
                        }
                    }
                }
            }

            timer.CurrentState.IsGameTimePaused = isLoading || isBlackScreen;

            if (waitOnFadeIn && isBlackScreen)
            {
                waitOnFadeIn = false;
                isLoading    = false;
            }


            if (wasBlackScreen && !isBlackScreen)
            {
                //This could be a pre-load transition, start timing it
                transitionStart = DateTime.Now;
                waitOnLoad      = true;
                waitFrames      = 0;
            }



            if (isLoading && waitOnLoad)
            {
                // This was a pre-load transition, subtract the gametime
                TimeSpan delta = (DateTime.Now - transitionStart);
                timer.CurrentState.SetGameTime(timer.CurrentState.GameTimePauseTime - delta);
                waitOnLoad   = false;
                waitFrames   = 0;
                waitOnFadeIn = true;
            }
            return(isLoad);
        }
        private void CalibrateBlacklevel()
        {
            Bitmap capture        = settings.CaptureImage();
            int    tempBlacklevel = FeatureDetector.GetBlackLevel(ref capture);

            if (tempBlacklevel != -1 && tempBlacklevel < settings.cmpBlackLevel)
            {
                settings.cmpBlackLevel = tempBlacklevel;
            }
            FeatureDetector.ClearBackground(ref capture, settings.cmpBlackLevel);

            BitmapToPixConverter btp = new BitmapToPixConverter();
            Pix    img        = btp.Convert(capture);
            string ResultText = "";

            using (var page = engine.Process(img, PageSegMode.SingleChar))
            {
                ResultText = page.GetText();
            }
            int counter = 0;

            if (settings.platform == "ENG/PS2" || settings.platform == "ENG/XBOX&GC")
            {
                foreach (char c in expectedResultEng)
                {
                    if (ResultText.Contains(c))
                    {
                        counter++;
                    }
                }
                if (counter > 5 && ResultText.Length == 8)
                {
                    isCmpFinished = true;
                }
            }
            else if (settings.platform == "JPN/PS2")
            {
                foreach (char c in expectedResultJpn)
                {
                    if (ResultText.Contains(c))
                    {
                        counter++;
                    }
                }
                if (counter > 3)
                {
                    isCmpFinished = true;
                }
            }
            else
            {
                foreach (char c in expectedResultKrn)
                {
                    if (ResultText.Contains(c))
                    {
                        counter++;
                    }
                }
                if (counter > 2)
                {
                    isCmpFinished = true;
                }
            }
            if (isCmpFinished)
            {
                settings.blacklevel = settings.cmpBlackLevel;
                isCmpFinished       = false;
                settings.isCalibratingBlacklevel = false;
                settings.cmpBlackLevel           = 100;
                Console.WriteLine("BLACKLEVEL: {0}", settings.blacklevel);
                settings.UpdateBlacklevelLabel();
                settings.Refresh();
            }
        }
Exemplo n.º 32
0
        private void Scan()
        {
            var          scanTime     = DateTime.Now;
            var          bounds       = _selectionForegroundPath.Data.Bounds;
            var          targetBounds = new Rectangle((int)bounds.X, (int)bounds.Y, (int)bounds.Width, (int)bounds.Height);
            GraphicsPath graphicsPath;

            if (_lassoSelectionForegroundGeometry != null)
            {
                graphicsPath = new GraphicsPath();
                graphicsPath.AddLines(
                    _lassoSelectionForegroundGeometry.Segments.Cast <LineSegment>()
                    .Select(s => new PointF((float)(s.Point.X - bounds.X), (float)(s.Point.Y - bounds.Y)))
                    .Where(p => p.X > 0 && p.Y > 0 && p.X < bounds.Width && p.Y < bounds.Height)
                    .ToArray());
            }
            else
            {
                graphicsPath = new GraphicsPath();
                graphicsPath.AddRectangle(new RectangleF(0, 0, (float)bounds.Width, (float)bounds.Height));
            }
            var croppedScreenshot = CropBitmap(_fullScreenshotBitmap, targetBounds, graphicsPath);

            // test show cropped bitmap
            //ShowCroppedScreenshot(croppedScreenshot);

            // OCR
            var converter = new BitmapToPixConverter();
            var target    = converter.Convert(croppedScreenshot);

            Task.Run(() =>
            {
                if (_reseted)
                {
                    return;
                }

                // start watch
                var watch = new Stopwatch();
                watch.Start();

                // OCR
                double confidence;
                var result = Ocr(target, out confidence);
                if (_reseted)
                {
                    return;
                }

                // stop watch
                watch.Stop();

                // log and display
                var cleanText = string.Join(Environment.NewLine, result.Replace(Environment.NewLine, "").Replace("\t", "").Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries));
                cleanText    += Environment.NewLine;
                Dispatcher.Invoke(() =>
                {
                    // log
                    _checker.Log(confidence, cleanText, croppedScreenshot, watch.ElapsedMilliseconds, scanTime);

                    // display
                    PrintScanTextOnScreen(cleanText);
                });
            });
        }