private void RunTesseract(string tag, Mat image) { using (var tesseract = OCRTesseract.Create(@"./tessdata", charWhitelist: @"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.,:/-\*")) { tesseract.Run(image, out var outputText, out var componentRects, out var componentTexts, out var componentConfidences, ComponentLevels.TextLine); switch (tag) { case "0": Results0.Text = outputText; break; case "1": Results1.Text = outputText; break; case "2": Results2.Text = outputText; break; default: break; } } }
private void DoOcr() { if (canvas.Children.Count >= 2 && (canvas.Children[1] as Rectangle) != null) { Rectangle rect = canvas.Children[1] as Rectangle; GetRoi(rect, out int left, out int top, out int right, out int bottom); OpenCvSharp.Mat m = new OpenCvSharp.Mat(); if (top < bottom && left < right && left >= 0 && right < viewModel.SourceMat.Cols && top >= 0 && bottom < viewModel.SourceMat.Rows) { viewModel.SourceMat.SubMat(top, bottom, left, right).CopyTo(m); OpenCvSharp.Cv2.HConcat(new OpenCvSharp.Mat[] { m, m, m, m }, m); viewModel.BitmapRoi = BitmapSourceConverter.ToBitmapSource(m); using (var tesseract = OCRTesseract.Create(MainViewModel.TessData, "eng", "0123456789-", 3, 7)) // 7: Page Segmentation Mode 7:single text line { OpenCvSharp.Cv2.GaussianBlur(m, m, new OpenCvSharp.Size(5, 5), 0); tesseract.Run(m, out var outputText, out var componentRects, out var componentTexts, out var componentConfidences, ComponentLevels.TextLine); string data = "("; data += outputText + ")\n"; for (int i = 0; i < componentRects.Length; i++) { data += $"({componentTexts[i]}) apperred at {componentRects[i]} with confidence {componentConfidences[i]}\n"; } tbxData.Text = outputText; } } } }
public void Create() { using (var tesseract = OCRTesseract.Create(TessData)) { GC.KeepAlive(tesseract); } }
static void Main(string[] args) { var img = new Mat(@"image.png"); var gray = new Mat(); Cv2.CvtColor(img, gray, ColorConversionCodes.BGR2GRAY); var ocr = OCRTesseract.Create("./tessdata", "eng"); string text; Rect[] boxes; string[] words; float[] confidences; ocr.Run(gray, out text, out boxes, out words, out confidences); Console.WriteLine("text: " + text); for (int i = 0; i < boxes.Count(); i++) { Console.WriteLine("boxes: " + boxes[i]); Console.WriteLine("words: " + words[i]); Console.WriteLine("confidences: " + confidences[i]); } Cv2.ImShow("test", gray); Cv2.WaitKey(); }
public void Run() { using (var image = Image("alphabet.png")) using (var tesseract = OCRTesseract.Create(TessData)) { tesseract.Run(image, out var outputText, out var componentRects, out var componentTexts, out var componentConfidences); testOutputHelper.WriteLine(outputText); Assert.NotEmpty(outputText); } }
internal static void Init() { Logger.LogInfo("Loading OCR data..."); var traineddataDir = RatConfig.Paths.Data + "\\"; var traineddataPath = traineddataDir + "bender.traineddata"; if (!System.IO.File.Exists(traineddataPath)) { Logger.LogError("Could not find traineddata at: " + traineddataPath); return; } ocrTesseractInstance = OCRTesseract.Create(traineddataDir, "bender", null, 3, 7); }
public void Run() { Console.WriteLine(Directory.GetCurrentDirectory()); using (var image = Image("alphabet.png")) using (var tesseract = OCRTesseract.Create(TessData)) { tesseract.Run(image, out var outputText, out var componentRects, out var componentTexts, out var componentConfidences); Console.WriteLine(outputText); Assert.NotEmpty(outputText); } }
////public override void Cleanup() ////{ //// // Clean up if needed //// base.Cleanup(); ////} /// private void Run() { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "*.png|*.png|*.jpg|*.jpg"; if (dialog.ShowDialog() == true) { using (var fs = new FileStream(dialog.FileName, FileMode.Open)) using (var tesseract = OCRTesseract.Create(TessData, "eng", "0123456789-")) { Mat mat = Mat.FromStream(fs, ImreadModes.Grayscale); Cv2.GaussianBlur(mat, mat, new Size(5, 5), 0); tesseract.Run(mat, out var outputText, out var componentRects, out var componentTexts, out var componentConfidences, ComponentLevels.TextLine); string data = "("; data += outputText + ")\n"; for (int i = 0; i < componentRects.Length; i++) { data += $"({componentTexts[i]}) apperred at {componentRects[i]} with confidence {componentConfidences[i]}\n"; } Content = data; } } }
static void Main(string[] args) { var imagePath = @"image.jpg"; var tessDataPath = @""; var engine = OCRTesseract.Create(tessDataPath, null, null, 3, 3); var mat = new Mat(imagePath); var gray = mat.CvtColor(ColorConversionCodes.BGR2GRAY); var canny = gray.Canny(10, 200); canny.SaveImage("canny.png"); var weighted = new Mat(gray.Size(), MatType.CV_8UC1); Cv2.AddWeighted(canny, 0.1, gray, 0.9, 0, weighted); weighted.SaveImage("weighted.png"); engine.Run(weighted, out string text, out Rect[] rects, out string[] cText, out float[] conf, ComponentLevels.TextLine); var mask1 = new Mat(mat.Size(), MatType.CV_8UC1); mask1.SetTo(0); var mask2 = new Mat(mat.Size(), MatType.CV_8UC1); mask2.SetTo(0); for (int i = 0; i < conf.Length; i++) { if (conf[i] == 95) { continue; } var temp1 = new Mat(mask1, rects[i]); var temp2 = new Mat(mask2, rects[i]); var roi = new Mat(weighted, rects[i]); var binary = new Mat(roi.Size(), MatType.CV_8UC1); Cv2.Threshold(roi, binary, 0, 255, ThresholdTypes.Otsu); var blured = binary.GaussianBlur(new Size(5, 5), 5); blured.CopyTo(temp1); temp2.SetTo(255); } mask1.SaveImage("mask1.png"); mask2.SaveImage("mask2.png"); var res1 = new Mat(mat.Size(), MatType.CV_8UC3); var res2 = new Mat(mat.Size(), MatType.CV_8UC3); Cv2.Inpaint(mat, mask1, res1, 10, InpaintMethod.NS); Cv2.Inpaint(mat, mask2, res2, 10, InpaintMethod.NS); Console.WriteLine(text); res1.SaveImage("res1.png"); res2.SaveImage("res2.png"); Console.ReadLine(); }