예제 #1
0
        protected override RunInfo TesseractRunInfo(OcrParams ocrParams)
        {
            OcrMode mode   = ocrParams.Mode;
            string  folder = mode == OcrMode.Fast || mode == OcrMode.Default ? "fast" : "best";

            if (ocrParams.LanguageCode.Split('+').All(code => !File.Exists(Path.Combine(TesseractBasePath, folder, $"{code.ToLowerInvariant()}.traineddata"))))
            {
                // Use the other source if the selected one doesn't exist
                folder = folder == "fast" ? "best" : "fast";
                mode   = folder == "fast" ? OcrMode.Fast : OcrMode.Best;
            }

            return(new RunInfo
            {
                Arguments = mode == OcrMode.Best ? "--oem 1" : mode == OcrMode.Legacy ? "--oem 0" : "",
                DataPath = folder,
                PrefixPath = folder
            });
        }
예제 #2
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));
        }
예제 #3
0
 public OcrParams(string langCode, OcrMode mode)
 {
     LanguageCode = langCode;
     Mode         = mode;
 }