示例#1
0
        ///Improve the quality of the image.
        ///Process the image.

        public async Task <string> GetTextFromImage(Bitmap image)
        {
            var ocr           = new TesseractOCR();
            var improvedImage = ImproveImageQuality(image);
            var result        = await ocr.GetText(improvedImage);

            return(result);
        }
示例#2
0
        private void btProcessImage_Click(object sender, RoutedEventArgs e)
        {
            TesseractOCR tess = new TesseractOCR(Properties.Settings.Default.TesseractPath);

            string result = "";

            try
            {
                result = tess.OCRFromBitmap(this.currentBitmap);
                result = result.Replace(",", string.Empty);
                result = result.Replace(".", string.Empty);
                result = result.Replace("-", "0");
                result = result.Replace("'", "0");

                string[] lines   = result.Split('\n');
                string   resultf = "";

                int cn = 0;
                foreach (string line in lines)
                {
                    string[] words = line.Split(' ');
                    string   nline = "";

                    if (cn++ < 3)
                    {
                        resultf += line + "\n";
                        continue;
                    }
                    if (words.Length < 3)
                    {
                        continue;
                    }

                    int  c = -1;
                    bool r = int.TryParse(words[1], out c);
                    bool v = int.TryParse(words[2], out c);

                    for (int i = words.Length - 1; i >= 0; i--)
                    {
                        string word   = words[i].Trim();
                        bool   addtab = (i != 0) && (i != 1 || r) && (i != 2 || v);

                        nline = nline.Insert(0, (addtab ? ";" : i != 0 ? " " : "") + word);
                    }
                    resultf += nline + "\n";
                }
                result = resultf;
            }
            catch { }//MessageBox.Show("Something went wrong, sorry.", "Error during OCR", MessageBoxButton.OK, MessageBoxImage.Error); }

            this.tbResult.Text = result.ToUpper();
        }
示例#3
0
 private void Button_Click(object sender, RoutedEventArgs e)
 {
     if (!NativeMethods.GetCursorPos(out POINT point))
     {
         return;
     }
     using (Bitmap bmp = ImageHelpers.CaptureBitmap(point, new Size(300, 50)))
     {
         ImageSource img = ImageHelpers.GetImageSource(bmp);
         imgControl.Width      = img.Width;
         imgControl.Height     = img.Height;
         imgControl.Source     = img;
         recognizedText.Text   = TesseractOCR.RecognizeText(ImageHelpers.ImageToByte(bmp));
         recognizedBlocks.Text = TesseractOCR.RecognizeBlocks(ImageHelpers.ImageToByte(bmp));
     }
 }
示例#4
0
        public string Evaluate()
        {
            var folder           = new DirectoryInfo(GlobalSettings.AppSettings.StorageFolderDownloadedFiles);
            var files            = folder.GetFiles("*.png");
            var alreadyProcessed = folder.GetFiles("*.txt").Select(o => Path.GetFileNameWithoutExtension(o.Name)).ToList();
            var filesToProcess   = files.Where(o => !alreadyProcessed.Contains(Path.GetFileNameWithoutExtension(o.Name)));

            var processed = new List <string>();
            var index     = 0;

            Console.WriteLine($"Found {filesToProcess.Count()} unprocessed files");
            foreach (var file in filesToProcess)
            {
                var ocrFile = file.FullName.Remove(file.FullName.Length - file.Extension.Length) + ".txt";
                var started = DateTime.Now;
                var text    = new TesseractOCR(GlobalSettings.AppSettings.PathToTesseract).Run(file.FullName, new string[] { "swe", "eng" });
                var elapsed = DateTime.Now - started;
                File.WriteAllText(ocrFile, text);
                Console.WriteLine($"{index++}/{filesToProcess.Count()} {file.Name}");
                processed.Add(ocrFile);
            }
            return(string.Join("\n", processed));
        }
示例#5
0
        private void dataInit()
        {
            OCREngine ocr;
            string    res = null;

            if (Common.appSettings.OCRsource == "TesseractOCR")
            {
                ocr = new TesseractOCR();
                if (ocr.OCR_Init("", "") != false)
                {
                    ocr.SetOCRSourceLang(Common.appSettings.GlobalOCRLang);
                    res = ocr.OCRProcess(new System.Drawing.Bitmap(img));

                    if (res != null)
                    {
                        sourceText.Text = res;
                    }
                    else
                    {
                        HandyControl.Controls.Growl.ErrorGlobal($"TesseractOCR {Application.Current.Resources["APITest_Error_Hint"]}\n{ocr.GetLastError()}");
                    }
                }
                else
                {
                    HandyControl.Controls.Growl.ErrorGlobal($"TesseractOCR {Application.Current.Resources["APITest_Error_Hint"]}\n{ocr.GetLastError()}");
                }
            }
            else if (Common.appSettings.OCRsource == "TesseractOCR5")
            {
                ocr = new Tesseract5OCR();
                if (ocr.OCR_Init(Common.appSettings.Tesseract5OCR_Path, Common.appSettings.Tesseract5OCR_Args))
                {
                    ocr.SetOCRSourceLang(Common.appSettings.GlobalOCRLang);
                    res = ocr.OCRProcess(new System.Drawing.Bitmap(img));

                    if (res != null)
                    {
                        sourceText.Text = res;
                    }
                    else
                    {
                        HandyControl.Controls.Growl.ErrorGlobal($"TesseractOCR5 {Application.Current.Resources["APITest_Error_Hint"]}\n{ocr.GetLastError()}");
                    }
                }
                else
                {
                    HandyControl.Controls.Growl.ErrorGlobal($"TesseractOCR5 {Application.Current.Resources["APITest_Error_Hint"]}\n{ocr.GetLastError()}");
                }
            }
            else if (Common.appSettings.OCRsource == "BaiduOCR")
            {
                ocr = new BaiduGeneralOCR();
                if (ocr.OCR_Init(Common.appSettings.BDOCR_APIKEY, Common.appSettings.BDOCR_SecretKey))
                {
                    ocr.SetOCRSourceLang(Common.appSettings.GlobalOCRLang);
                    res = ocr.OCRProcess(new System.Drawing.Bitmap(img));

                    if (res != null)
                    {
                        sourceText.Text = res;
                    }
                    else
                    {
                        HandyControl.Controls.Growl.ErrorGlobal($"百度OCR {Application.Current.Resources["APITest_Error_Hint"]}\n{ocr.GetLastError()}");
                    }
                }
                else
                {
                    HandyControl.Controls.Growl.ErrorGlobal($"百度OCR {Application.Current.Resources["APITest_Error_Hint"]}\n{ocr.GetLastError()}");
                }
            }

            if (res == null)
            {
                FirstTransText.Text = "OCR ERROR";
            }
            else
            {
                ITranslator translator1 = TranslateWindow.TranslatorAuto(Common.appSettings.FirstTranslator);
                ITranslator translator2 = TranslateWindow.TranslatorAuto(Common.appSettings.SecondTranslator);
                //5.提交翻译
                string transRes1 = "";
                string transRes2 = "";
                if (translator1 != null)
                {
                    transRes1 = translator1.Translate(res, Common.UsingDstLang, Common.UsingSrcLang);
                }
                if (translator2 != null)
                {
                    transRes2 = translator2.Translate(res, Common.UsingDstLang, Common.UsingSrcLang);
                }

                FirstTransText.Text  = transRes1;
                SecondTransText.Text = transRes2;
            }
        }
示例#6
0
 public Tesseract(string tesseractlanguage)
 {
     // _TessOCR = new TesseractOCR("eng", TesseractOCR.Quality.High); 英文
     _TessOCR = new TesseractOCR(tesseractlanguage, TesseractOCR.Quality.High);
 }
示例#7
0
        private void FindLicensePlate(
            VectorOfVectorOfPoint contours, int[,] hierachy, int idx, IInputArray gray, IInputArray canny,
            List <IInputOutputArray> licensePlateImagesList, List <IInputOutputArray> filteredLicensePlateImagesList, List <RotatedRect> detectedLicensePlateRegionList,
            List <String> licenses, int ocr_mode, int threshold_parameter)
        {
            for (; idx >= 0; idx = hierachy[idx, 0])
            {
                int numberOfChildren = GetNumberOfChildren(hierachy, idx);
                //if it does not contains any children (charactor), it is not a license plate region
                if (numberOfChildren == 0)
                {
                    continue;
                }

                using (VectorOfPoint contour = contours[idx])
                {
                    if (CvInvoke.ContourArea(contour) > 400)
                    {
                        if (numberOfChildren < 6)
                        {
                            //If the contour has less than 6 children, it is not a license plate (assuming license plate has at least 3 charactor)
                            //However we should search the children of this contour to see if any of them is a license plate
                            FindLicensePlate(contours, hierachy, hierachy[idx, 2], gray, canny, licensePlateImagesList,
                                             filteredLicensePlateImagesList, detectedLicensePlateRegionList, licenses, ocr_mode, threshold_parameter);
                            continue;
                        }

                        RotatedRect box = CvInvoke.MinAreaRect(contour);
                        if (box.Angle < -45.0)
                        {
                            float tmp = box.Size.Width;
                            box.Size.Width  = box.Size.Height;
                            box.Size.Height = tmp;
                            box.Angle      += 90.0f;
                        }
                        else if (box.Angle > 45.0)
                        {
                            float tmp = box.Size.Width;
                            box.Size.Width  = box.Size.Height;
                            box.Size.Height = tmp;
                            box.Angle      -= 90.0f;
                        }

                        double whRatio = (double)box.Size.Width / box.Size.Height;
                        if (!(3.0 < whRatio && whRatio < 10.0))
                        //if (!(1.0 < whRatio && whRatio < 2.0))
                        {
                            //if the width height ratio is not in the specific range,it is not a license plate
                            //However we should search the children of this contour to see if any of them is a license plate
                            //Contour<Point> child = contours.VNext;
                            if (hierachy[idx, 2] > 0)
                            {
                                FindLicensePlate(contours, hierachy, hierachy[idx, 2], gray, canny, licensePlateImagesList,
                                                 filteredLicensePlateImagesList, detectedLicensePlateRegionList, licenses, ocr_mode, threshold_parameter);
                            }
                            continue;
                        }

                        using (UMat tmp1 = new UMat())
                            using (UMat tmp2 = new UMat())
                            {
                                PointF[] srcCorners = box.GetVertices();

                                PointF[] destCorners = new PointF[] {
                                    new PointF(0, box.Size.Height - 1),
                                    new PointF(0, 0),
                                    new PointF(box.Size.Width - 1, 0),
                                    new PointF(box.Size.Width - 1, box.Size.Height - 1)
                                };

                                using (Mat rot = CvInvoke.GetAffineTransform(srcCorners, destCorners))
                                {
                                    //box.Center.X -= 20;
                                    //box.Size.Height += 20;
                                    //box.Size.Width += 20;
                                    //box.Center.Y -= 20;

                                    CvInvoke.WarpAffine(gray, tmp1, rot, Size.Round(box.Size));
                                }
                                SaveImageClass.SaveImage(gray, "gray2.jpg");
                                SaveImageClass.SaveImage(tmp1, "tmp1.jpg");
                                //resize the license plate such that the front is ~ 10-12. This size of front results in better accuracy from tesseract
                                Size   approxSize = new Size(240, 180);
                                double scale      = Math.Min(approxSize.Width / box.Size.Width, approxSize.Height / box.Size.Height);
                                Size   newSize    = new Size((int)Math.Round(box.Size.Width * scale), (int)Math.Round(box.Size.Height * scale));
                                CvInvoke.Resize(tmp1, tmp2, newSize, 0, 0, Inter.Cubic);

                                SaveImageClass.SaveImage(tmp1, "tmp1after.jpg");
                                SaveImageClass.SaveImage(tmp2, "tmp2.jpg");

                                //removes some pixels from the edge
                                int       edgePixelSize = 3;
                                Rectangle newRoi        = new Rectangle(new Point(edgePixelSize, edgePixelSize),
                                                                        tmp2.Size - new Size(2 * edgePixelSize, 2 * edgePixelSize));

                                UMat plate = new UMat(tmp2, newRoi);

                                SaveImageClass.SaveImage(plate, "plate.jpg");
                                UMat filteredPlate = new UMat();

                                filteredPlate = FilterPlate(plate, threshold_parameter);

                                SaveImageClass.SaveImage(filteredPlate, "filtered.jpg");
                                StringBuilder strBuilder = new StringBuilder();
                                switch (ocr_mode)
                                {
                                case 1:
                                {
                                    strBuilder = TesseractOCR.GetText(filteredPlate, _ocr);
                                    break;
                                }

                                case 2:
                                {
                                    strBuilder = GoogleApiOCR.GetText(filteredPlate);
                                    break;
                                }

                                case 3:
                                {
                                    strBuilder = ComputerVisionOCR.GetText(filteredPlate);
                                    break;
                                }

                                default:
                                    break;
                                }
                                if (strBuilder != null)
                                {
                                    licenses.Add(strBuilder.ToString());
                                    licensePlateImagesList.Add(plate);
                                    filteredLicensePlateImagesList.Add(filteredPlate);
                                    detectedLicensePlateRegionList.Add(box);
                                }
                            }
                    }
                }
            }
        }
示例#8
0
        private void dataInit()
        {
            IOptChaRec ocr;
            string     res = null;

            if (Common.appSettings.OCRsource == "TesseractOCR")
            {
                ocr = new TesseractOCR();
                if (ocr.OCR_Init("", "") != false)
                {
                    ocr.SetOCRSourceLang(Common.appSettings.GlobalOCRLang);
                    res = ocr.OCRProcess(new System.Drawing.Bitmap(img));

                    if (res != null)
                    {
                        sourceText.Text = res;
                    }
                    else
                    {
                        HandyControl.Controls.Growl.ErrorGlobal("TesseractOCR 工作异常\n" + ocr.GetLastError());
                    }
                }
                else
                {
                    HandyControl.Controls.Growl.ErrorGlobal("TesseractOCR 工作异常\n" + ocr.GetLastError());
                }
            }
            else if (Common.appSettings.OCRsource == "BaiduOCR")
            {
                ocr = new BaiduGeneralOCR();
                if (ocr.OCR_Init(Common.appSettings.BDOCR_APIKEY, Common.appSettings.BDOCR_SecretKey) != false)
                {
                    ocr.SetOCRSourceLang(Common.appSettings.GlobalOCRLang);
                    res = ocr.OCRProcess(new System.Drawing.Bitmap(img));

                    if (res != null)
                    {
                        sourceText.Text = res;
                    }
                    else
                    {
                        HandyControl.Controls.Growl.ErrorGlobal("百度OCR API工作异常\n" + ocr.GetLastError());
                    }
                }
                else
                {
                    HandyControl.Controls.Growl.ErrorGlobal("百度OCR API工作异常\n" + ocr.GetLastError());
                }
            }

            if (res == null)
            {
                FirstTransText.Text = "OCR ERROR";
            }
            else
            {
                ITranslator translator1 = TranslateWindow.TranslatorAuto(Common.appSettings.FirstTranslator);
                ITranslator translator2 = TranslateWindow.TranslatorAuto(Common.appSettings.SecondTranslator);
                //5.提交翻译
                string transRes1 = "";
                string transRes2 = "";
                if (translator1 != null)
                {
                    transRes1 = translator1.Translate(res, Common.UsingDstLang, Common.UsingSrcLang);
                }
                if (translator2 != null)
                {
                    transRes2 = translator2.Translate(res, Common.UsingDstLang, Common.UsingSrcLang);
                }

                FirstTransText.Text  = transRes1;
                SecondTransText.Text = transRes2;
            }
        }
        private async void dataInit()
        {
            OCREngine ocr;
            string    res = null;

            if (Common.appSettings.OCRsource == "TesseractOCR")
            {
                ocr = new TesseractOCR();
                if (ocr.OCR_Init("", "") != false)
                {
                    ocr.SetOCRSourceLang(Common.appSettings.GlobalOCRLang);
                    res = await ocr.OCRProcessAsync(img);

                    if (res != null)
                    {
                        sourceText.Text = res;
                    }
                    else
                    {
                        HandyControl.Controls.Growl.WarningGlobal($"TesseractOCR {Application.Current.Resources["APITest_Error_Hint"]}\n{ocr.GetLastError()}");
                    }
                }
                else
                {
                    HandyControl.Controls.Growl.ErrorGlobal($"TesseractOCR {Application.Current.Resources["APITest_Error_Hint"]}\n{ocr.GetLastError()}");
                }
            }
            else if (Common.appSettings.OCRsource == "Tesseract5")
            {
                ocr = new Tesseract5OCR();
                if (ocr.OCR_Init(Common.appSettings.Tesseract5OCR_Path, Common.appSettings.Tesseract5OCR_Args))
                {
                    ocr.SetOCRSourceLang(Common.appSettings.GlobalOCRLang);
                    res = await ocr.OCRProcessAsync(img);

                    if (res != null)
                    {
                        sourceText.Text = res;
                    }
                    else
                    {
                        HandyControl.Controls.Growl.WarningGlobal($"Tesseract5 {Application.Current.Resources["APITest_Error_Hint"]}\n{ocr.GetLastError()}");
                    }
                }
                else
                {
                    HandyControl.Controls.Growl.ErrorGlobal($"Tesseract5 {Application.Current.Resources["APITest_Error_Hint"]}\n{ocr.GetLastError()}");
                }
            }
            else if (Common.appSettings.OCRsource == "BaiduOCR")
            {
                ocr = new BaiduGeneralOCR();
                if (ocr.OCR_Init(Common.appSettings.BDOCR_APIKEY, Common.appSettings.BDOCR_SecretKey))
                {
                    ocr.SetOCRSourceLang(Common.appSettings.GlobalOCRLang);
                    res = await ocr.OCRProcessAsync(img);

                    if (res != null)
                    {
                        sourceText.Text = res;
                    }
                    else
                    {
                        HandyControl.Controls.Growl.WarningGlobal($"百度智能云OCR {Application.Current.Resources["APITest_Error_Hint"]}\n{ocr.GetLastError()}");
                    }
                }
                else
                {
                    HandyControl.Controls.Growl.ErrorGlobal($"百度智能云OCR {Application.Current.Resources["APITest_Error_Hint"]}\n{ocr.GetLastError()}");
                }
            }
            else if (Common.appSettings.OCRsource == "BaiduFanyiOCR")
            {
                ocr = new BaiduFanyiOCR();
                if (ocr.OCR_Init(Common.appSettings.BDappID, Common.appSettings.BDsecretKey))
                {
                    ocr.SetOCRSourceLang(Common.appSettings.GlobalOCRLang);
                    res = await ocr.OCRProcessAsync(img);

                    if (res != null)
                    {
                        FirstTransText.Text = res;
                    }
                    else
                    {
                        HandyControl.Controls.Growl.WarningGlobal($"百度翻译OCR {Application.Current.Resources["APITest_Error_Hint"]}\n{ocr.GetLastError()}");
                    }
                }
                else
                {
                    HandyControl.Controls.Growl.ErrorGlobal($"百度翻译OCR {Application.Current.Resources["APITest_Error_Hint"]}\n{ocr.GetLastError()}");
                }
            }

            if (res == null)
            {
                FirstTransText.Text = "OCR ERROR";
            }
            else if (Common.appSettings.OCRsource != "BaiduFanyiOCR")
            {
                // 因为历史原因,OCR的源语言用的是三个字母的,如eng和jpn。而翻译的API即Common.UsingSrcLang用的两个字母,如en和jp
                string srclang;
                switch (Common.appSettings.GlobalOCRLang)
                {
                case "eng":
                    srclang = "en";
                    break;

                case "jpn":
                    srclang = "jp";
                    break;

                default:
                    srclang = Common.appSettings.GlobalOCRLang;
                    break;
                }

                if (!Common.appSettings.EachRowTrans)
                {
                    if (srclang == "en")
                    {
                        res = res.Replace("\n", " ").Replace("\r", " ");
                    }
                    else
                    {
                        res = res.Replace("\n", "").Replace("\r", "");
                    }
                }

                ITranslator translator1 = TranslateWindow.TranslatorAuto(Common.appSettings.FirstTranslator);
                ITranslator translator2 = TranslateWindow.TranslatorAuto(Common.appSettings.SecondTranslator);
                //5.提交翻译
                string transRes1 = "";
                string transRes2 = "";
                if (translator1 != null)
                {
                    transRes1 = await translator1.TranslateAsync(res, Common.UsingDstLang, srclang);
                }
                if (translator2 != null)
                {
                    transRes2 = await translator2.TranslateAsync(res, Common.UsingDstLang, srclang);
                }

                FirstTransText.Text  = transRes1;
                SecondTransText.Text = transRes2;
            }
        }