Beispiel #1
2
   // Use this for initialization
   void Start()
   {
      String[] names = new string[] {"eng.cube.bigrams", "eng.cube.fold", "eng.cube.lm", "eng.cube.nn", "eng.cube.params", "eng.cube.size", "eng.cube.word-freq", "eng.tesseract_cube.nn", "eng.traineddata"};
      String outputPath = Path.Combine(Application.persistentDataPath, "tessdata");
      if (!Directory.Exists(outputPath))
         Directory.CreateDirectory(outputPath);
      
      foreach (String n in names)
      {
         TextAsset textAsset = Resources.Load<TextAsset>(Path.Combine("tessdata", n));  
         String filePath = Path.Combine(outputPath, n);
#if UNITY_METRO
         UnityEngine.Windows.File.WriteAllBytes(filePath, textAsset.bytes);
#else
         if (!File.Exists(filePath))
            File.WriteAllBytes(filePath, textAsset.bytes);
#endif
      }

      _ocr = new Tesseract(outputPath, "eng", OcrEngineMode.TesseractCubeCombined);

      Debug.Log("OCR engine loaded.");

      Image<Bgr, Byte> img = new Image<Bgr, byte>(480, 200);
      
      String message = "Hello, World";
      CvInvoke.PutText(img, message, new Point(50, 100), Emgu.CV.CvEnum.FontFace.HersheySimplex, 1.0, new MCvScalar(255, 255, 255));

      _ocr.Recognize(img);


      Tesseract.Character[] characters = _ocr.GetCharacters();
      foreach (Tesseract.Character c in characters)
      {
         CvInvoke.Rectangle(img, c.Region, new MCvScalar(255, 0, 0));
      }

      String messageOcr = _ocr.GetText().TrimEnd('\n', '\r'); // remove end of line from ocr-ed text   
      Debug.Log("Detected text: "+ message);

      Texture2D texture = TextureConvert.InputArrayToTexture2D(img, FlipType.Vertical);

      this.GetComponent<GUITexture>().texture = texture;
      this.GetComponent<GUITexture>().pixelInset = new Rect(-img.Width / 2, -img.Height / 2, img.Width, img.Height);
   }
        public List<Word> GetTextFromImage( Bitmap image)
        {
            List<Word> tempResult;
            List<Word> result = new List<Word>();
            try
            {
                var ocr = new Tesseract();
                //ocr.SetVariable("tessedit_char_whitelist", "0123456789");
                ocr.SetVariable("tessedit_char_whitelist", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:<>"); 
                ocr.Init("tessdata", "eng", false);
                tempResult = ocr.DoOCR(image, Rectangle.Empty);
                
                // Weed out the bad match results.
                foreach (Word word in tempResult)
                {
                    if (word.Confidence < 160) {
                        result.Add(word);
                    }
                }
            }
            catch (Exception e)
            {
                throw new ClosedEyedVisuals_Exception("OCR error:" + e.ToString());
            }


            return result;
        }
Beispiel #3
0
      private void PerformOCR(string fileName)
      {
         //Read the image from file
         Image<Gray, Byte> image = new Image<Gray, byte>(fileName);

         fileNameTextBox.Text = fileName;

         //Resize the image if it is too big, display it on the image box
         int width = Math.Min(image.Width, imageBox1.Width);
         int height = Math.Min(image.Height, imageBox1.Height);
         imageBox1.Image = image.Resize(width, height, true);

         //Perform OCR
         Tesseract ocr = new Tesseract();
         //You can download more language definition data from
         //http://code.google.com/p/tesseract-ocr/downloads/list
         //Languages supported includes:
         //Dutch, Spanish, German, Italian, French and English
         ocr.Init("eng", numericalOnlyCheckBox.Checked); 
         List<tessnet2.Word> result = ocr.DoOCR(image.Bitmap, Rectangle.Empty);

         //Obtain the texts from OCR result
         String[] texts = result.ConvertAll<String>(delegate(Word w) { return w.Text; }).ToArray();

         //Display the text in the text box
         textBox1.Text = String.Join(" ", texts);
      }
Beispiel #4
0
 /// <summary>Initializes a new instance of the <see cref="ImageOCR"/> class.</summary>
 public ImageOCR()
 {
     this.recognizer = new Tesseract();
     var folder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
     var path = Path.Combine(folder, "tessdata");
     path = path.Substring(6);
     this.recognizer.Init(path, "eng", false);
 }
Beispiel #5
0
        public MainForm()
        {
            InitializeComponent();

            tesseract = new Tesseract();
            tesseract.SetVariable("tessedit_char_whitelist", "0123456789.");
            tesseract.Init("OcrData", "eng", false);

            uiContext = TaskScheduler.FromCurrentSynchronizationContext();
        }
      /// <summary>
      /// Create a license plate detector
      /// </summary>
      public LicensePlateDetector()
      {
         //create OCR engine
         _ocr = new Tesseract();

         //You can download more language definition data from
         //http://code.google.com/p/tesseract-ocr/downloads/list
         //Languages supported includes:
         //Dutch, Spanish, German, Italian, French and English
         _ocr.Init(null, "eng", false);
      }
Beispiel #7
0
 /// <summary>
 /// Create a license plate detector
 /// </summary>
 public LicensePlates()
 {
     //create OCR engine
     _ocr = new Tesseract();
     //InitDtkEngine();
     try
     {
         _ocr.Init("tessdata", "eng", false);
     }
     catch (Exception ex)
     {
     }
 }
Beispiel #8
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="gameEngine">An instance of the <see cref="Tesseract"/> game engine.</param>
        public override void Render(Tesseract gameEngine)
        {
            var context = gameEngine.DeviceManager.Context3D;

            context.PixelShader.SetShaderResource(0, TextureView);
            context.PixelShader.SetSampler(0, TextureSamplerState);

            context.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;
            context.InputAssembler.SetIndexBuffer(IndexBuffer, Format.R16_UInt, 0);
            context.InputAssembler.SetVertexBuffers(0, VertexBufferBinding);

            context.DrawIndexed(IndexArray.Length, 0, 0);
        }
        public void Return(string path)
        {
            Tesseract tesseract = new Tesseract();
            Bitmap bitmap = new Bitmap(path);
            Point start = new Point(0,0);
            Rectangle rect = new Rectangle(start,bitmap.Size);
            var output = tesseract.DoOCR(bitmap, rect);
            string text = string.Empty;
            foreach (Word a in output)
            {
               text += a.Text;

            }
            int b = 0;
        }
Beispiel #10
0
        private static string DoTesseract(Image input)
        {
            var bmp = new Bitmap(input, new Size(100, 44));
            var ocr = new Tesseract();
            //ocr.SetVariable("tessedit_char_blacklist", "0123456789+-");
            ocr.Init(null, "eng", false);

            var result = ocr.DoOCR(bmp, Rectangle.Empty);
            string ret = string.Empty;

            foreach (var item in result)
            {
                ret += item.Text + " ";
            }

            return ret.Trim();
        }
Beispiel #11
0
        public MainForm()
        {
            InitializeComponent();

            if (!DesignMode)
            {
                var block = (LayoutBlock)LayoutBlockBindingSource.AddNew();
                SetDefaultBlockValues(block);
                LayoutBlockBindingSource.EndEdit();

                Debug.Assert(block != null, "block != null");
                CurrentDisplayBlock = new DisplayBlock(Rectangle.Empty);
                CurrentDisplayBlock.SetPictureBox(PictureBox);
                CurrentDisplayBlock.SizeChanged += CurrentDisplayBlock_SizeChanged;

                tesseract = new Tesseract();
                tesseract.SetVariable("tessedit_char_whitelist", "0123456789.");
                tesseract.Init("OcrData", "eng", false);
            }
        }
Beispiel #12
0
        //Fonction qui permet de detecter le nombre de caractères
        public int getText(Image imageToSplit)
        {
            string s     = "";
            var    image = new Bitmap(imageToSplit);
            var    ocr   = new Tesseract();

            ocr.SetVariable("load_system_dawg", false);
            ocr.SetVariable("load_freq_dawg", false);
            ocr.Init(Server.MapPath(@"\tessdata\"), "eng", false);
            var result   = ocr.DoOCR(image, Rectangle.Empty);
            int nbLettre = 0;

            foreach (tessnet2.Word word in result)
            {
                s += Text.Text;

                Text.Text = let;
            }

            mot = s;
            return(nbC = nbLettre);
        }
Beispiel #13
0
        private void convertToTextToolStripMenuItem_Click(object sender, EventArgs e)
        {
            /*   ImageFormat imageFormat = null;
             * imageFormat = ImageFormat.Jpeg;
             * pictureBox1.Image.Save(@"C:/Users/yashharkhani/Pictures/100000test.jpg", imageFormat);
             * ocr.Image = ImageStream.FromFile("C:/Users/yashharkhani/Pictures/100000test.jpg");
             * if (ocr.Process())
             * {
             *     textBox1.Text = " " + ocr.Text;
             * }
             */
            var image  = ImageFormat.Jpeg;
            var varocr = new Tesseract();

            ocr.Init(@ "C:/Program Files (x86)/Tesseract-OCR/tessdata", "eng", false);
            var result = ocr.DoOCR(image, Rectangle.Empty);

            foreach (tessnet2.Word word in result)
            {
                Console.writeline(textBox1.Text);
            }
        }
Beispiel #14
0
        public void ProcessImg()
        {
            var imgDic            = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images");
            var curImgPath        = Path.Combine(imgDic, "20180109093048226.png");
            var tesssdataPath     = Path.Combine(AppDomain.CurrentDomain.BaseDirectory);
            Image <Bgr, Byte> img = new Image <Bgr, Byte>(curImgPath);

            //原图宽的1/2
            var imgWidthSplit = (int)(img.Width / 3.0);
            //原图高的1/3
            var imgHeightSplit = (int)(img.Height / 3.0);

            var grayImg = img.Convert <Gray, Byte>();

            mainImg.Image = grayImg;

            Rectangle rect = new Rectangle(0, 0, imgWidthSplit * 2, imgHeightSplit);

            CvInvoke.cvSetImageROI(grayImg, rect);
            var newImg = new Image <Gray, Byte>(imgWidthSplit * 2, imgHeightSplit);

            CvInvoke.cvCopy(grayImg, newImg, IntPtr.Zero);

            imgBox1.Image = newImg;

            var thresImg = newImg.ThresholdBinary(new Gray(78), new Gray(255));

            imgBox2.Image = thresImg;

            Tesseract ocr = new Tesseract("", "num", OcrEngineMode.TesseractOnly);

            ocr.SetImage(thresImg);
            ocr.SetVariable("tessedit_char_whitelist", "0123456789");
            var rr  = ocr.Recognize();
            var val = ocr.GetUTF8Text();

            MessageBox.Show(val);
        }
 static void Main(string[] args)
 {
     try
     {
         var image = new Bitmap(@"C:\OCRTest\saimon.jpg");
         var ocr   = new Tesseract();
         ocr.SetVariable("tessedit_char_whitelist", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.,$-/#&=()\"':?");             // If digit only
         //@"C:\OCRTest\tessdata" contains the language package, without this the method crash and app breaks
         ocr.Init(@"C:\OCRTest\tessdata", "eng", true);
         var result = ocr.DoOCR(image, Rectangle.Empty);
         foreach (Word word in result)
         {
             if (word.contains("aimon"))
             {
                 Console.WriteLine("" + word.Confidence + " " + word.Text + " " + word.Top + " " + word.Bottom + " " + word.Left + " " + word.Right);
             }
         }
         Console.ReadLine();
     }
     catch (Exception exception)
     {
     }
 }
Beispiel #16
0
        private async Task InitTesseract(String lang, OcrEngineMode mode, System.Net.DownloadProgressChangedEventHandler onDownloadProgressChanged = null)
        {
            if (_ocr == null)
            {
                FileDownloadManager manager = new FileDownloadManager();
                manager.AddFile(Emgu.CV.OCR.Tesseract.GetLangFileUrl(lang), _modelFolderName);
                manager.AddFile(Emgu.CV.OCR.Tesseract.GetLangFileUrl("osd"), _modelFolderName); //script orientation detection

                if (onDownloadProgressChanged != null)
                {
                    manager.OnDownloadProgressChanged += onDownloadProgressChanged;
                }
                await manager.Download();

                if (manager.AllFilesDownloaded)
                {
                    _lang = lang;
                    _mode = mode;
                    FileInfo fi = new FileInfo(manager.Files[0].LocalFile);
                    _ocr = new Tesseract(fi.DirectoryName, _lang, _mode);
                }
            }
        }
Beispiel #17
0
        private void EmguVC()
        {
            Tesseract tesseract = new Tesseract(@"D:\Programme\Emgu\emgucv-windesktop 3.4.1.2976\Emgu.CV.OCR\tessdata", "owz", OcrEngineMode.Default);
            //var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
            //var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
            //gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
            //Image<Bgr, byte> image = LiftImage(bmpScreenshot.Clone(new Rectangle(345, 1365, 140, 55), System.Drawing.Imaging.PixelFormat.Format4bppIndexed));


            Rectangle rect = new Rectangle(orginalRect.Location, orginalRect.Size);

            rect.X      += rnd.Next(7) - 3;
            rect.Y      += rnd.Next(7) - 3;
            rect.Width  += rnd.Next(7) - 3;
            rect.Height += rnd.Next(7) - 3;

            Bitmap tmpBitmap     = new Bitmap(rect.Width, rect.Height, PixelFormat.Format24bppRgb);
            var    gfxScreenshot = Graphics.FromImage(tmpBitmap);

            gfxScreenshot.CopyFromScreen(rect.X, rect.Y, 0, 0, new Size(rect.Width, rect.Height), CopyPixelOperation.SourceCopy);

            Image <Bgr, byte> image = LiftImage(tmpBitmap.Clone(new Rectangle(0, 0, rect.Width, rect.Height), PixelFormat.Format4bppIndexed));

            actImage = image.ToBitmap();



            //tesseract.SetVariable("editor_image_text_color", "2");
            tesseract.SetImage(image);
            int[] health = ValidatHealth(tesseract.GetUTF8Text());
            if (health != null && health.Length == 2)
            {
                textBox1.Text = HealthToString(health);
                DisplayHealthColor(health);
                pictureBox1.Image = image.ToBitmap();
            }
        }
Beispiel #18
0
        public string SearchText(int iterations = 251, int thresholdValue = 80, string language = "eng")
        {
            VectorOfVectorOfPoint contours = GetContours(SearchArea(new Gray(255), 5, thresholdValue).Dilate(iterations));

            Image <Bgr, byte>         copy      = SourceImage.Copy();
            List <Image <Bgr, byte> > roiImages = new List <Image <Bgr, byte> >();

            for (int i = 0; i < contours.Size; i++)
            {
                if (CvInvoke.ContourArea(contours[i], false) > 50)
                {
                    Rectangle rect = CvInvoke.BoundingRectangle(contours[i]);

                    copy.Draw(rect, new Bgr(Color.Blue), 1);

                    SourceImage.ROI = rect;

                    roiImages.Add(SourceImage.Copy());

                    SourceImage.ROI = Rectangle.Empty;
                }
            }

            SourceImage = copy;

            Tesseract ocr = new Tesseract(LANGUAGES_PATH, language, OcrEngineMode.Default);

            string text = "";

            roiImages.ForEach((Image <Bgr, byte> roiImage) => {
                ocr.SetImage(roiImage);
                ocr.Recognize();
                text += ocr.GetUTF8Text();
            });

            return(text);
        }
        public Window1()
        {
            InitializeComponent();
            m_CoreForImageViewer = new ImageCore();
            m_CoreForImageThum   = new ImageCore();
            m_TwainManager       = new TwainManager(m_StrProductKey);
            dsViewer.Bind(m_CoreForImageViewer);
            dsViewerThum.Bind(m_CoreForImageThum);
            m_PDFRasterizer = new PDFRasterizer(m_StrProductKey);
            m_PDFCreator    = new PDFCreator(m_StrProductKey);
            m_Tesseract     = new Tesseract(m_StrProductKey);

            try
            {
                dpTitle.Background = new ImageBrush(new BitmapImage(new Uri(imageDirectory + "title.png", UriKind.RelativeOrAbsolute)));
                IconBitmapDecoder ibd = new IconBitmapDecoder(
                    new Uri(imageDirectory + "dnt_demo_icon.ico", UriKind.RelativeOrAbsolute),
                    BitmapCreateOptions.None,
                    BitmapCacheOption.Default);
                this.Icon = ibd.Frames[0];
            }
            catch { }
            dpTitle.MouseLeftButtonDown += new MouseButtonEventHandler(MoveWindow);

            dsViewerThum.MouseShape = true;
            dsViewerThum.SetViewMode(1, 4);
            dsViewerThum.AllowMultiSelect          = true;
            dsViewerThum.OnMouseClick             += new OnMouseClickHandler(dsViewerThum_OnMouseClick);
            dsViewerThum.EnableKeyboardInteractive = false;
            dsViewerThum.OnViewerKeyDown          += new KeyEventHandler(dsViewerThum_OnViewerKeyDown);
            string dynamicDotNetTwainDirectory = strCurrentDirectory;
            int    index = System.Reflection.Assembly.GetExecutingAssembly().Location.IndexOf("Samples");

            dsViewer.MouseShape = false;
            dsViewer.SetViewMode(-1, -1);
            m_CoreForImageViewer.ImageBuffer.MaxImagesInBuffer = 1;
        }
Beispiel #20
0
    private static IEnumerator TesseractDownloadLangFile(String folder, String lang)
    {
        String subfolderName = "tessdata";
        String folderName    = System.IO.Path.Combine(folder, subfolderName);

        if (!System.IO.Directory.Exists(folderName))
        {
            System.IO.Directory.CreateDirectory(folderName);
        }
        String dest = System.IO.Path.Combine(folderName, String.Format("{0}.traineddata", lang));


        if (!System.IO.File.Exists(dest) || !(new FileInfo(dest).Length > 0))
        {
            String source = Tesseract.GetLangFileUrl(lang);
            using (UnityEngine.Networking.UnityWebRequest webclient = new UnityEngine.Networking.UnityWebRequest(source))
            {
                Debug.Log(String.Format("Downloading file from '{0}' to '{1}'", source, dest));

                webclient.downloadHandler = new UnityEngine.Networking.DownloadHandlerFile(dest);
                yield return(webclient.SendWebRequest());

                if (webclient.isNetworkError || webclient.isHttpError)
                {
                    Debug.LogError(webclient.error);
                }

                if (!System.IO.File.Exists(dest) || !(new FileInfo(dest).Length > 0))
                {
                    Debug.LogError(String.Format("File {0} is empty, failed to download file.", dest));
                }

                Debug.Log("File successfully downloaded and saved to " + dest);
                //Debug.Log(String.Format("Download completed"));
            }
        }
    }
Beispiel #21
0
        /// <summary>
        /// Recognize the license plate number from a given image
        /// </summary>
        /// <remarks>
        /// Uses a Tesseract OCR library to recognize the set of characters.
        /// When you would like to distinguish single character PageSegMode.SingleChar should be used,
        /// otherwise PageSegMode.SingleBlock will be applied.
        /// Tesseract uses already prepared training data which consists of built-in data set and
        /// special training set for polish license plates.
        /// </remarks>
        /// <param name="imgWithNumber">Mat containing the image of possible license plate area</param>
        /// <param name="pageMode">PageSegMode which should be used when recognizing the character </param>
        /// <returns>Recognized plate number</returns>
        private string RecognizeNumber(Mat imgWithNumber, PageSegMode pageMode = PageSegMode.SingleChar)
        {
            Tesseract.Character[] characters;

            StringBuilder licensePlateNumber = new StringBuilder();

            using (var ocr = new Tesseract())
            {
                ocr.Init(
                    _ocrParams["TEST_DATA_PATH"],
                    _ocrParams["TEST_DATA_LANG"],
                    OcrEngineMode.LstmOnly);

                ocr.SetVariable(
                    "tessedit_char_whitelist",
                    _ocrParams["WHITE_LIST"]);

                ocr.SetVariable
                    ("user_defined_dpi", "70");

                ocr.PageSegMode = pageMode;

                using (Mat tmp = imgWithNumber.Clone())
                {
                    ocr.SetImage(tmp);
                    ocr.Recognize();
                    characters = ocr.GetCharacters();

                    for (int i = 0; i < characters.Length; i++)
                    {
                        licensePlateNumber.Append(characters[i].Text);
                    }
                }

                return(licensePlateNumber.ToString());
            }
        }
Beispiel #22
0
        private NumberRecognition()
        {
            try
            {
                if (_ocr != null)
                {
                    _ocr.Dispose();
                    _ocr = null;
                }

                _ocr = new Tesseract(".\\", "eng", OcrEngineMode.TesseractLstmCombined);
                _ocr.SetVariable("tessedit_char_whitelist", "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
            }
            catch (System.Net.WebException e)
            {
                _ocr = null;
                throw new Exception("Unable to download tesseract lang file. Please check internet connection.", e);
            }
            catch (Exception e)
            {
                _ocr = null;
                throw e;
            }
        }
        private static void CreateJson()
        {
            var files = new DirectoryInfo(@"C:\WindowsServiceInput\").GetFiles();

            foreach (var file in files)
            {
                if (!File.Exists(@"C:\WindowsServiceOutput\" + file.Name))
                {
                    var ocr   = new Tesseract();
                    var image = Image.FromFile(file.FullName);
                    ocr.Init(@"..\..\Content\tessdata", "eng", false);
                    var           result = ocr.DoOCR((Bitmap)image, Rectangle.Empty);
                    List <string> data   = new List <string>();
                    foreach (Word word in result)
                    {
                        data.Add(word.Text);
                    }

                    string json = JsonConvert.SerializeObject(data.ToArray());
                    System.IO.File.WriteAllText(
                        @"C:\WindowsServiceOutput\" + Path.GetFileNameWithoutExtension(file.Name) + ".json", json);
                }
            }
        }
Beispiel #24
0
        private void getOcr()
        {
            _ocr = new Tesseract("", "eng", OcrEngineMode.TesseractCubeCombined);
            _ocr.SetVariable("tessedit_char_blacklist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ - 1234567890");
            Bgr drawColor           = new Bgr(Color.Blue);
            Image <Gray, byte> gray = SoureceImage.Convert <Gray, byte>();

            _ocr.Recognize(gray);
            Tesseract.Character[] characters = _ocr.GetCharacters();
            foreach (Tesseract.Character c in characters)
            {
                gray.Draw(c.Region, new Gray(0), 1);
            }

            DataSource = new DataTable();
            DataSource.Columns.Add("字符串", typeof(System.String));
            DataRow newLine = DataSource.NewRow();

            newLine["字符串"] = _ocr.GetText();
            DataSource.Rows.Add(newLine);

            SoureceImage.Bitmap = gray.Bitmap;
            ShowFormImage();
        }
Beispiel #25
0
        private void BtnOCR_Click(object sender, EventArgs args)
        {
            var ocr = new Tesseract();

            ocr.Init(@".\tessdata", "eng", false);

            using (OpenFileDialog fileDialog = new OpenFileDialog()
            {
                Filter = COMPATIBLE_FILETYPES
            })
            {
                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    Bitmap img = (Bitmap)Image.FromFile(fileDialog.FileName);
                    ImgBox.Image = img;
                    var result = ocr.DoOCR(img, Rectangle.Empty);

                    foreach (var word in result)
                    {
                        MessageBox.Show(word.Text);
                    }
                }
            }
        }
Beispiel #26
0
        public ActionResult Index()
        {
            // now add the following C# line in the code page
            var image = new Bitmap("\\work\\Tesseract\\OCR-APP\\uber\\scan_1.tif");

            varocr = new Tesseract();
            ocr.Init("\\work\\Tesseract\\OCR-APP\\uber\\", "eng", false);
            var result = ocr.DoOCR(image, Rectangle.Empty);

            List <string> datamine = null;

            datamine = new List <string> {
                "asd", "zxc"
            };
            //foreach (tessnet2.Word word in result)
            //{

            //    datamine.Add(word.text);

            //}


            return(View(datamine));
        }
Beispiel #27
0
        public static string genCaptcha()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.sunat.gob.pe/cl-ti-itmrconsruc/captcha?accion=image");

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
            request.CookieContainer = cokkie;
            HttpWebResponse response       = (HttpWebResponse)request.GetResponse();
            Stream          responseStream = response.GetResponseStream();

            var image = new Bitmap(responseStream);
            var ocr   = new Tesseract();
            //ocr.Init(@"C:\Users\Miguel\Desktop\SUNAT\PRUEBA\Content\tessdata", "eng", false);
            var path = HostingEnvironment.MapPath(@"~/Componentes/tessdata");

            ocr.Init(path, "eng", false);

            var result = ocr.DoOCR(image, Rectangle.Empty);

            foreach (Word word in result)
            {
                captcha = word.Text;
            }
            return(captcha);
        }
Beispiel #28
0
 public static String getImageText(Image<Gray, Byte> vBmp)
 {
     String cStr = null;
     try
     {
         vBmp.Save("PTX.jpg");
         string path = Application.StartupPath + "\\tessdata";  //下载识别文件夹
         string language = "eng";//识别语言
         Tesseract ocr = new Tesseract(path, language, OcrEngineMode.Default);//生成OCR对象。
         ocr.SetVariable("tessedit_char_whitelist", "0123456789XPT.");//此方法表示只识别1234567890与x字母
         ocr.SetImage(vBmp);
         cStr = ocr.GetUTF8Text();
     }
     catch (Exception ex)
     {
         log4net.WriteLogFile(ex.Message);
         cStr = null;
     }
     if (!String.IsNullOrEmpty(cStr))
     {
         cStr = cStr.Trim().Replace("\n", "").Replace("\r", "");
     }
     return cStr;
 }
Beispiel #29
0
        /* CONSTRUCTOR METHOD */
        /// <summary>
        /// Construct an object to manage mouse and keyboard control of the game.
        /// </summary>
        /// <param name="gameEngine">An instance of the <see cref="Tesseract"/> game engine.</param>
        public ControlManager(Tesseract gameEngine)
        {
            // Register handlers to engine events
            gameEngine.OnInitialized += PostInitialize;
            gameEngine.OnRendered += PostRender;

            // Initialize a set of keys capable of being sticky
            StickyKeys.Add(Keys.A, false);
            StickyKeys.Add(Keys.ControlKey, false);
            StickyKeys.Add(Keys.D, false);
            StickyKeys.Add(Keys.S, false);
            StickyKeys.Add(Keys.ShiftKey, false);
            StickyKeys.Add(Keys.Space, false);
            StickyKeys.Add(Keys.W, false);
        }
Beispiel #30
0
 public override void Apply(Tesseract.Backends.IGraphics g)
 {
     font.Apply(g);
     g.Text(text, 0, 0);
 }
Beispiel #31
0
        /* CONSTRUCTOR METHOD */
        /// <summary>
        /// Initializes variables needed for terrain generation.
        /// </summary>
        /// <param name="gameEngine">An instance of the <see cref="Tesseract"/> game engine.</param>
        /// <param name="startingHeight">The height value at which to start the chunk.</param>
        public ProceduralMesh(Tesseract gameEngine, Biome biome)
            : base(gameEngine)
        {
            // Initialize the vertex array & height map with the proper number of elements
            VertexArray = new Vertex[NumHorizontal * NumVertical * 6];
            QuadArray = new Vertex[NumVertical, NumHorizontal, 6];

            // Initialize the random number generator
            Seed = RandomNumber.Next((int)1e9);
            RandomNumber = new Random(Seed);

            // Initialize the array of quads that will draw the map chunk
            Biome = biome;
            for (int a = 0; a < NumVertical; a++)
            {
                for (int b = 0; b < NumHorizontal; b++)
                {
                    // Create a quad at the current map grid location
                    Vertex[] currentQuad = Vertex.UnitQuadXZ(b, 0f, -a, Color.White);
                    Vertex.Scale(ref currentQuad, (1f / TessellationFactor));

                    // Set each vertex's texture coordinates
                    currentQuad[0].TexCoords = new Vector2(0f, 1f);
                    currentQuad[1].TexCoords = new Vector2(0f, 0f);
                    currentQuad[2].TexCoords = new Vector2(1f, 0f);
                    currentQuad[3].TexCoords = new Vector2(0f, 1f);
                    currentQuad[4].TexCoords = new Vector2(1f, 0f);
                    currentQuad[5].TexCoords = new Vector2(1f, 1f);

                    // Store each set of quads in the quad array
                    for (int c = 0; c < currentQuad.Length; c++) { QuadArray[a, b, c] = currentQuad[c]; }
                }
            }

            // Broadcast that this chunk has been initialized
            IsInitialized = true;
        }
Beispiel #32
0
 /* CONSTRUCTOR METHOD */
 /// <summary>
 /// Construct a shader manager to hold pixel, vertex, and tessellation shader settings.
 /// </summary>
 /// <param name="gameEngine">An instance of the <see cref="Tesseract"/> game engine.</param>
 public ShaderManager(Tesseract gameEngine)
 {
     gameEngine.OnInitialize += Initialize;
 }
Beispiel #33
0
 /// <summary>
 /// Creates and binds device-dependent resources independent of the game engine initialization procedure.
 /// </summary>
 /// <param name="gameEngine">An instance of the <see cref="Tesseract"/> game engine.</param>
 public void CreateRuntimeResources(Tesseract gameEngine)
 {
     CreateDeviceResources(gameEngine);
     BindDeviceResources(gameEngine);
 }
Beispiel #34
0
 public Test()
 {
     ocr = new Tesseract();
     ocr.SetVariable("tessedit_char_whitelist", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.,$-/#&=()\"':?"); // If digit only
     ocr.Init(@"..\..\..\tessdata", "eng", false); ; // To use correct tessdata
 }
Beispiel #35
0
 //tśś
 public PlatesRecognizionHelper()
 {
     _ocr = new Tesseract();
     _ocr.SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ-1234567890");
 }
Beispiel #36
0
        private void ProcessImageOCR(Bitmap b, Rectangle rect, int retry)
        {
            Log("Start processing password from image. (OCR)");
            var ocr = new Tesseract();
            ocr.SetVariable("tessedit_char_whitelist", "0123456789");

            ocr.Init(@tessResFolder, "eng", true);
            var result = ocr.DoOCR(b, Rectangle.Empty);
            foreach (Word word in result)
            {
                Log("OCR returned: " + word.Text);
                if(word.Text.Length >= 5)
                {
                    Clipboard.SetText(word.Text);
                    Log("Password copied to clipboard.");
                } else
                {
                    Thread.Sleep(1000);
                    Log("OCR not accepted. Retrying... " + retry);
                    CaptureScreen(rect, retry - 1);
                }

            }

            if(isFastRun)
            {
                // Close the application as we saved results to clipboard.
                Application.Exit();
            }
        }
Beispiel #37
0
        /* SHADER COMPILATION PROCEDURE */
        /// <summary>
        /// Compile the shader byte code from separate files.
        /// </summary>
        /// <param name="gameEngine">A reference to the Direct3D game engine.</param>
        private void CompileShaders(Tesseract gameEngine)
        {
            // Get a reference to the device & context
            var device = gameEngine.DeviceManager.Device3D;
            var context = gameEngine.DeviceManager.Context3D;

            // Compile the vertex shader & create its input layout
            using (var vsCode = HLSL.Compile(@"Shaders/Simple.vs", "Main", "vs_5_0"))
            {
                VertexShader = ToDispose(new VertexShader(device, vsCode));
                VertexLayout = ToDispose(
                    new InputLayout(
                        device,
                        vsCode.GetPart(ShaderBytecodePart.InputSignatureBlob),
                        new[]
                        {
                            new InputElement("SV_Position", 0, Format.R32G32B32_Float, 0, 0),
                            new InputElement("NORMAL", 0, Format.R32G32B32_Float, 12, 0),
                            new InputElement("COLOR", 0, Format.R8G8B8A8_UNorm, 24, 0),
                            new InputElement("TEXCOORD", 0, Format.R32G32_Float, 28, 0),
                            new InputElement("BLENDINDICES", 0, Format.R32G32B32A32_UInt, 36, 0),
                            new InputElement("BLENDWEIGHT", 0, Format.R32G32B32A32_Float, 52, 0),
                        }));
            }

            // Compile all available pixel shaders
            string[] psNames = Enum.GetNames(typeof(PixelShaders));
            for (int a = 0; a < psNames.Length; a++)
            {
                string currentPS = "Shaders\\" + psNames[a] + ".ps";
                using (var code = HLSL.Compile(currentPS, "Main", "ps_5_0"))
                {
                    AllPixelShaders.Add((PixelShaders)Enum.Parse(typeof(PixelShaders), psNames[a]), ToDispose(new PixelShader(device, code)));
                }
            }

            // Select a pixel shader to use by default
            PixelShader = AllPixelShaders[SettingsManager.PixelShader];

            // Bind new resources to the rendering pipeline
            context.InputAssembler.InputLayout = VertexLayout;
            context.VertexShader.Set(VertexShader);
            context.PixelShader.Set(PixelShader);
        }
Beispiel #38
0
        /* RENDERING METHODS */
        /// <summary>
        /// Binds the vertex array to its buffer and creates a texture resource view.
        /// </summary>
        /// <param name="gameEngine"></param>
        protected override void BindDeviceResources(Tesseract gameEngine)
        {
            //base.BindDeviceResources(gameEngine);

            // Store references to the now-initialized Direct3D device & device context
            Context3D = gameEngine.DeviceManager.Context3D;
            Device3D = gameEngine.DeviceManager.Device3D;

            // Bind the texture sampler state description
            TextureSamplerState = ToDispose(new SamplerState(Device3D, TextureSamplerStateDescription));

            // Create and bind the vertex buffer
            VertexBuffer = ToDispose(Buffer.Create(Device3D, BindFlags.VertexBuffer, VertexArray, 0, ResourceUsage.Default, CpuAccessFlags.None));
            VertexBufferBinding = new VertexBufferBinding(VertexBuffer, Utilities.SizeOf<Vertex>(), 0);

            //TextureView = ToDispose(ShaderResourceView.FromFile(Device3D, "Textures/stone.png"));
            TextureView = ToDispose(ShaderResourceView.FromFile(Device3D, "Textures/dirt01.dds"));

            // Broadcast that this chunk has been finalized & its vertices bound to the vertex buffer
            if (IsFilled) { IsFinalized = true; }
        }
Beispiel #39
0
        /* SHADER MANAGER INITIALIZATION PROCEDURE */
        /// <summary>
        /// Release any existing resources and compile shader byte code.
        /// </summary>
        /// <param name="gameEngine">The Direct3D application.</param>
        public void Initialize(Tesseract gameEngine)
        {
            // Release pre-existing resources
            RemoveAndDispose(ref PixelShader);
            RemoveAndDispose(ref VertexLayout);
            RemoveAndDispose(ref VertexShader);

            // Compile the shader code & store within this object
            CompileShaders(gameEngine);
        }
Beispiel #40
0
        private void btnConvert_Click(object sender, EventArgs e)
        {
            string            filename = @"maze2.txt";
            Image <Bgr, Byte> img      = new Image <Bgr, byte>(filename).Resize(625, 625, Emgu.CV.CvEnum.Inter.Linear, true); //fileNameTextBox.Text
            UMat uimage = new UMat();

            CvInvoke.CvtColor(img, uimage, ColorConversion.Bgr2Gray);

            double cannyThreshold = 120.0;


            double cannyThresholdLinking = 120.0;
            UMat   cannyEdges            = new UMat();

            CvInvoke.Canny(uimage, cannyEdges, cannyThreshold, cannyThresholdLinking);
            //***Lines algorithm, play with the values to get the correct lines***
            LineSegment2D[] lines = CvInvoke.HoughLinesP(
                cannyEdges,
                1,              //Distance resolution in pixel-related units
                Math.PI / 45.0, //Angle resolution measured in radians.
                20,             //threshold
                30,             //min Line width
                15);            //gap between lines

            Tile[,] tiles = myMaze.getTiles();
            int    d = 0;
            string info;
            string vertOrHori = "none"; //none, vert, hori

            foreach (LineSegment2D l in lines)
            {
                // l.P1.X +l.P2.x
                //detect if vertical or horizontal line
                if (Math.Abs(l.P1.X - l.P2.X) < 20)
                {
                    vertOrHori = "vert";
                }
                else if (Math.Abs(l.P1.Y - l.P2.Y) < 20)
                {
                    vertOrHori = "hori";
                }
                else
                {
                    vertOrHori = "none";
                }
                Console.WriteLine(l.P1.X.ToString() + " " + l.P1.Y.ToString() + " " + l.P2.X.ToString() + " " + l.P2.Y.ToString() + vertOrHori);

                //***Cycle through the
                if (vertOrHori == "vert")
                {
                    int jStart = l.P1.Y / 25;
                    int jEnd   = l.P2.Y / 25;
                    int iSame  = l.P1.X / 25; //x vals stay the same, find the items of the array to cycle through
                    if (jStart > jEnd)
                    {
                        int temp = jStart;
                        jStart = jEnd;
                        jEnd   = temp;
                    }
                    for (int j = jStart; j < jEnd; j++)
                    {
                        tiles[iSame, j].SetWall(true);
                    }
                }


                if (vertOrHori == "hori")
                {
                    int iStart = l.P1.X / 25;
                    int iEnd   = l.P2.X / 25;
                    int jSame  = l.P1.Y / 25; //x vals stay the same, find the items of the array to cycle through
                    if (iStart > iEnd)
                    {
                        int temp = iStart;
                        iStart = iEnd;
                        iEnd   = temp;
                    }
                    for (int i = iStart; i < iEnd; i++)
                    {
                        tiles[i, jSame].SetWall(true);
                    }
                }
                img.Draw(l, new Bgr(Color.Orange), 2);
                d++;
            }

            Tesseract ocr = new Tesseract();

            //   Tesseract.Character[] characters;

            //  string data = @"C:\C sharp 2020\Project Idea 2 Visual Trees CVIS\tessdata";

            /*   string data = @"G:\tessdata\";
             *    ocr.SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ-1234567890");
             *    ocr.Init(data, "eng", OcrEngineMode.TesseractLstmCombined); */
            /*  characters = ocr.GetCharacters();
             *
             *
             * foreach (Tesseract.Character c in characters)
             * {
             *    CvInvoke.Rectangle(img, c.Region, new MCvScalar(255, 0, 0));
             * }*/
            //imgOCR = new Image<Bgr, byte>(panelStream.Image.Bitmap).Copy();

            /*   Image<Bgr, Byte> imgOCR;
             *       using (imgOCR = img.Copy())
             *       {
             *           using (ocr)
             *           {
             *               var ocre = ocr.Recognize();
             *               var characters = ocr.GetCharacters();
             *
             *               foreach (Tesseract.Character c in characters)
             *               {
             *                   CvInvoke.Rectangle(imgOCR, c.Region, new MCvScalar(255, 0, 0));
             *               }
             *
             *               //String messageOcr = _ocr.GetText().TrimEnd('\n', '\r'); // remove end of line from ocr-ed text
             *           }
             *       }*/

            imgBoxTree.Image  = img;
            imgBoxTree.Width  = img.Size.Width;
            imgBoxTree.Height = img.Size.Height;
            Draw();
            imgBoxTree.Location = new Point(900, 400);
        }
Beispiel #41
0
 /* QUAD RENDERER PROPERTIES */
 /// <summary>
 /// 
 /// </summary>
 /// <param name="gameEngine">An instance of the <see cref="Tesseract"/> game engine.</param>
 public Quad(Tesseract gameEngine)
     : base(gameEngine)
 {
 }
Beispiel #42
0
 public List<Word> GetWordFromImage(Bitmap bitmap)
 {
     Tesseract ocr = new Tesseract();
         ocr.SetVariable("tessedit_char_whitelist", alphabetnum);
         ocr.Init(null, "eng", false);
         List<tessnet2.Word> result = ocr.DoOCR(bitmap, Rectangle.Empty);
         return result;
 }
Beispiel #43
0
        /// <summary>
        /// Renders the map chunk.
        /// </summary>
        /// <param name="gameEngine">An instance of the <see cref="Tesseract"/> game engine.</param>
        public override void Render(Tesseract gameEngine)
        {
            // Update vertex array & buffer if needed
            if (NeedsRefresh) { RefreshResources(); }

            // If the chunk is not meant to be visible, leave this function without rendering
            if (!IsVisible) { return; }

            // Update the per-object constant buffer
            gameEngine.BufferManager.PerObjectData = new ConstantBuffer.PerObject(World, gameEngine.Camera.ViewProjection);
            gameEngine.BufferManager.UpdateContextResources();

            // Load texture resources & setup the input assembler
            Context3D.PixelShader.SetShaderResource(0, TextureView);
            Context3D.PixelShader.SetSampler(0, TextureSamplerState);
            Context3D.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;
            Context3D.InputAssembler.SetVertexBuffers(0, VertexBufferBinding);

            // Draw the mesh
            Context3D.Draw(VertexArray.Length, 0);
        }
Beispiel #44
0
        /* ENGINE EVENT HANDLERS */
        /// <summary>
        /// Stores reference to the camera, game window, and adds callback functions to keyboard/mouse events in the game window.
        /// <para>This function is called once the game's initialization procedure is complete to ensure that the needed resources exist.</para>
        /// </summary>
        /// <param name="gameEngine">An instance of the <see cref="Tesseract"/> game engine.</param>
        private void PostInitialize(Tesseract gameEngine)
        {
            // Store references to other engine objects
            Camera = gameEngine.Camera;
            MapManager = gameEngine.MapManager;
            Window = gameEngine.WindowManager.Window;

            // Register handlers for window events
            Window.KeyDown += KeyDownHandler;
            Window.KeyUp += KeyUpHandler;
            Window.MouseClick += MouseClickRouter;
            Window.MouseMove += MouseMoveHandler;

            // Initialize the mouse click handler
            OnLeftClick += DestroyTerrain;
        }
Beispiel #45
0
        static void Main(string[] args)
        {
            Ds = new Tesseract();
            OutLosning = new Timer();
            OutLosning.Tick += new EventHandler(OutLosning_Tick);
            OutLosning.Interval = 500;
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Checkup.Show();
            Ds.Init(@"tessdata\\", "eng", false);

            InitiateExtensions();
             Checkup = new frmCheckup(views);
            ST.Tick += new EventHandler(ST_Tick);
            ST.Interval = 10;
            ST.Start();
            DSM();
            Application.Run();
        }
Beispiel #46
0
 /// <summary>
 /// Create a license plate detector
 /// </summary>
 public LicensePlateDetector()
 {
     //create OCR engine
     _ocr = new Tesseract("tessdata", "eng", Tesseract.OcrEngineMode.OEM_TESSERACT_CUBE_COMBINED);
     _ocr.SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ-1234567890");
 }
 public void SetRectangle (Tesseract.Rectangle rect)
 {
     _api.SetRectangle ((int)rect.Left, (int)rect.Top, (int)rect.Width, (int)rect.Height);
 }
Beispiel #48
0
        static void Main(string[] args)
        {
            Ds = new Tesseract();
            OutLosning = new System.Windows.Forms.Timer();
            OutLosning.Tick += new EventHandler(OutLosning_Tick);
            OutLosning.Interval = 500;
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(true);

            d = new Form1();
            URI = args[0];
            InitiateExtensions();
               ST.Tick += new EventHandler(ST_Tick);
               ST.Interval = 1;
               ST.Start();
            Application.Run(new Form());
        }
 /// <summary>
 /// Create a license plate detector
 /// </summary>
 /// <param name="dataPath">
 /// The datapath must be the name of the parent directory of tessdata and
 /// must end in / . Any name after the last / will be stripped.
 /// </param>
 public LicensePlateDetector(String dataPath)
 {
     //create OCR engine
     _ocr = new Tesseract(dataPath, "eng", OcrEngineMode.TesseractCubeCombined);
     _ocr.SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ-1234567890");
 }
Beispiel #50
0
        static void Main(string[] args)
        {
            var ActionSvc = IocContainer.Resolve <IActionService>();
            var deviceID  = ActionSvc.GetDeciveID();

            if (String.IsNullOrEmpty(deviceID))
            {
                Console.WriteLine("当前没有检测到手机");
                Console.WriteLine("请在手机 设置->开发者选项->打开开发都选项和USB调试 后继续运行此程序");
                Console.ReadKey();
                return;
            }
            var targetScore = 100;

            Console.WriteLine("如果累积误差比较大,请停止此程序。可以运行半自动版本消除误差后再重新运行此版本");
            Console.WriteLine("请输入您希望跳的目标分数(当达到此分数后,程序自动停止)");
            var inputScore = Console.ReadLine();

            targetScore = int.Parse(inputScore);

            var Model = new AutoCacheModel();
            var rand  = new Random();

            while (true)
            {
                var bitImg = ActionSvc.GetScreenshots();

                Model.Image = new WidthHeight()
                {
                    Width = bitImg.Width, Height = bitImg.Height
                };

                Image <Rgb, Byte> img       = new Image <Rgb, Byte>(bitImg);
                Image <Rgb, Byte> sourceImg = new Image <Rgb, Byte>(bitImg);

                //原图宽的1/2
                var imgWidthCenter = (int)(img.Width / 2.0);
                //原图宽的1/3
                var imgWidthSplit = (int)(img.Width / 3.0);
                //原图高的1/3
                var imgHeightSplit = (int)(img.Height / 3.0);

                //成绩识别
                var       sourceGrayImg = sourceImg.Convert <Gray, Byte>();
                Rectangle rectScore     = new Rectangle(0, 0, imgWidthSplit * 2, imgHeightSplit);
                CvInvoke.cvSetImageROI(sourceGrayImg, rectScore);
                var scoreImg = new Image <Gray, Byte>(imgWidthSplit * 2, imgHeightSplit);
                CvInvoke.cvCopy(sourceGrayImg, scoreImg, IntPtr.Zero);
                var thresImg = scoreImg.ThresholdBinary(new Gray(78), new Gray(255));
                //thresImg.ToBitmap().Save(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DownLoad", DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".png"));
                Tesseract ocr = new Tesseract("", "num", OcrEngineMode.TesseractLstmCombined, "0123456789");
                ocr.SetImage(thresImg);
                ocr.SetVariable("tessedit_char_whitelist", "0123456789");
                if (ocr.Recognize() == 0)
                {
                    var val = ocr.GetUTF8Text();
                    val = val.Trim().Replace(" ", "");
                    if (!String.IsNullOrEmpty(val))
                    {
                        int score    = Model.Score;
                        var canParse = int.TryParse(val, out score);
                        if (canParse && score > Model.Score)
                        {
                            Model.Score = score;
                        }
                    }
                }

                if (Model.Score >= targetScore)
                {
                    break;
                }

                var tempGrayPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Template", "Current.png");
                var tempGrayImg  = new Image <Rgb, byte>(tempGrayPath);
                var match        = img.MatchTemplate(tempGrayImg, TemplateMatchingType.CcorrNormed);

                double min = 0, max = 0;
                Point  maxp = new Point(0, 0);//最好匹配的点
                Point  minp = new Point(0, 0);
                CvInvoke.MinMaxLoc(match, ref min, ref max, ref minp, ref maxp);

                var startPoint = new Point();
                startPoint.X = maxp.X + (int)(tempGrayImg.Width / 2.0);
                startPoint.Y = maxp.Y + tempGrayImg.Height - 20;

                //裁剪查找区域
                //原图片1/3以下,小黑人以上
                var       newImgStart  = imgHeightSplit;
                var       newImgEnd    = maxp.Y + tempGrayImg.Height;
                var       newImgHeight = newImgEnd - newImgStart;
                Rectangle rect         = new Rectangle(0, newImgStart, img.Width, newImgHeight);

                CvInvoke.cvSetImageROI(sourceImg, rect);
                var newImg = new Image <Rgb, byte>(sourceImg.Width, newImgHeight);
                CvInvoke.cvCopy(sourceImg, newImg, IntPtr.Zero);

                //看小黑人在程序的左边还是右边
                //如果在左边,那目标点就在图片的右边
                bool targetInLeft = true;
                if (maxp.X < imgWidthCenter)
                {
                    targetInLeft = false;
                }

                Rectangle halfRect;
                if (targetInLeft)
                {
                    halfRect = new Rectangle(0, 0, imgWidthCenter, newImgHeight);
                }
                else
                {
                    halfRect = new Rectangle(imgWidthCenter, 0, imgWidthCenter, newImgHeight);
                }

                CvInvoke.cvSetImageROI(newImg, halfRect);
                var halfImg = new Image <Rgb, byte>(imgWidthCenter, newImgHeight);
                CvInvoke.cvCopy(newImg, halfImg, IntPtr.Zero);

                Point topPoint = new Point();
                for (int i = 0; i < halfImg.Rows; i++)
                {
                    for (int j = 0; j < halfImg.Cols - 1; j++)
                    {
                        var cur  = halfImg[i, j];
                        var next = halfImg[i, j + 1];
                        if (Math.Abs(RgbHelp.GetDiff(cur, next)) > 2)
                        {
                            var x = 2;
                            next = halfImg[i, j + x];
                            while (Math.Abs(RgbHelp.GetDiff(cur, next)) > 2)
                            {
                                x++;
                                next = halfImg[i, j + x];
                            }
                            topPoint.Y = i;
                            topPoint.X = j + (int)(x / 2.0);
                            break;
                        }
                    }
                    if (!topPoint.IsEmpty)
                    {
                        break;
                    }
                }

                //这个顶点在原图中的位置
                var oldTopX = topPoint.X;
                if (!targetInLeft)
                {
                    oldTopX += imgWidthCenter;
                }
                var oldTopY     = topPoint.Y + imgHeightSplit;
                var oldTopPoint = new Point(oldTopX, oldTopY);

                Model.Top   = oldTopPoint;
                Model.Start = startPoint;
                Console.WriteLine(JsonConvert.SerializeObject(Model));
                ActionSvc.Action(Model.Image, Model.Time);

                bitImg.Dispose();
                img.Dispose();
                sourceImg.Dispose();
                sourceGrayImg.Dispose();
                scoreImg.Dispose();
                thresImg.Dispose();
                tempGrayImg.Dispose();
                match.Dispose();
                newImg.Dispose();
                halfImg.Dispose();

                Thread.Sleep(Model.Time + rand.Next(600, 2000));
            }
        }
        public IActionResult Steps(StepData uploadFile)
        {
            StepData           stepData        = new StepData();
            ProcessCaptured    processCaptured = new ProcessCaptured();
            Image <Bgra, Byte> imgg            = null;
            Mat finalImg = new Mat();

            using (MemoryStream ms = new MemoryStream())
            {
                if (ms != null)
                {
                    CvInvoke.UseOpenCL = true;
                    uploadFile.FormFile.CopyTo(ms);
                    byte[] fileBytes = ms.ToArray();
                    imgg     = processCaptured.GetImageFromStream(ms);
                    finalImg = imgg.Mat.Clone();
                    stepData = AddData(stepData, finalImg, "Orginal");
                    using (Mat im = imgg.Mat)
                    {
                        using (Mat threshold = new Mat())
                        {
                            using (Mat gray = new Mat())
                            {
                                using (Mat canny = new Mat())
                                {
                                    using (Mat rectImg = new Mat())
                                    {
                                        CvInvoke.Threshold(im, threshold, 100, 255, ThresholdType.BinaryInv);
                                        stepData = AddData(stepData, threshold, "Threshold");

                                        CvInvoke.CvtColor(threshold, gray, ColorConversion.Bgr2Gray);
                                        stepData = AddGrayData(stepData, gray, "Gray");

                                        CvInvoke.Canny(gray, canny, 100, 50, 7);
                                        stepData = AddData(stepData, canny, "Canny");

                                        List <RotatedRect> rect = new List <RotatedRect>();
                                        rect = Contours(canny);

                                        if (rect != null && rect.Count > 0)
                                        {
                                            foreach (RotatedRect boxr in rect)
                                            {
                                                CvInvoke.Polylines(finalImg, Array.ConvertAll(boxr.GetVertices(), Point.Round), true,
                                                                   new Bgr(Color.DarkOrange).MCvScalar, 2);
                                            }
                                        }
                                        stepData = AddData(stepData, finalImg, "With Rectangle");

                                        List <Mat> mat = RoI(rect, gray);
                                        int        i   = 0;
                                        //OCR
                                        string    path = AppContext.BaseDirectory;
                                        Tesseract _ocr = new Tesseract(path, "eng", OcrEngineMode.TesseractLstmCombined);
                                        _ocr.SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ-1234567890");
                                        foreach (Mat m in mat)
                                        {
                                            i += 1;
                                            _ocr.SetImage(m);
                                            _ocr.Recognize();
                                            Tesseract.Character[] words = _ocr.GetCharacters();
                                            //string wor=words.
                                            StringBuilder sb = new StringBuilder();
                                            foreach (var c in words)
                                            {
                                                sb.Append(c.Text);
                                            }
                                            stepData = AddData(stepData, m, i.ToString(), sb.ToString());
                                        }
                                    }
                                }
                            }
                        }
                    }

                    //Mat blur = new Mat();
                    //blur = Blur(im);
                    //stepData = AddData(stepData, blur, "Blur");

                    //Mat gray = new Mat();
                    //gray = Gray(blur);
                    //stepData = AddData(stepData, gray, "Gray");

                    //Mat threshold = new Mat();
                    //threshold = Threshold(im);
                    //stepData = AddData(stepData, threshold, "Threshold");

                    //Mat gray = new Mat();
                    //gray = Gray(threshold);
                    //stepData = AddGrayData(stepData, gray, "Gray");

                    //Mat canny = new Mat();
                    //canny = Canny(gray);
                    //stepData = AddData(stepData, canny, "Canny");


                    //List<RotatedRect> rect = new List<RotatedRect>();
                    //rect = Contours(canny);

                    //Mat rectImg = new Mat();

                    //rectImg = DrawRect(imgg.Mat, rect);
                    //stepData = AddData(stepData, rectImg, "With Rectangle");

                    //List<Mat> mat = RoI(rect, threshold.Clone());

                    //int i = 0;
                    //foreach (Mat m in mat)
                    //{
                    //    i += 1;
                    //    stepData = AddData(stepData, m, i.ToString());
                    //}
                }
            }
            #region Old
            //Image<Bgr, Byte> imgg = null;
            //Image<Bgr, byte> tempImg = null;
            //Image<Gray, Byte> GrayImg = null;
            //Image<Gray, Byte> CannyImgTemp = null;
            //Image<Bgr, Byte> CannyImg = null;
            //Image<Bgr, byte> temp = null;
            //int[,] hierachy = null;
            //List<VectorOfPoint> box = new List<VectorOfPoint>();
            //List<RotatedRect> boxList = new List<RotatedRect>();
            //CvInvoke.UseOpenCL = true;
            //using (MemoryStream ms = new MemoryStream())
            //{
            //    if (ms != null)
            //    {
            //        uploadFile.FormFile.CopyTo(ms);
            //        byte[] fileBytes = ms.ToArray();
            //        imgg = processCaptured.GetImageFromStream(ms);
            //        Mat im = imgg.Mat;
            //        stepData = AddData(stepData, im, "Orginal");
            //        tempImg = imgg.Copy();
            //    }
            //}
            //using (Mat blur = new Mat())
            //{
            //    using (Mat gray = new Mat())
            //    {
            //        using (Mat canny = new Mat())
            //        {
            //            Size kSize = new Size(3, 3);
            //           // CvInvoke.GaussianBlur(imgg, blur, kSize, 0);
            //            CvInvoke.Threshold(imgg, blur, 50, 255, ThresholdType.Binary);
            //            //CvInvoke.BilateralFilter(imgg, blur, -1, -1, 100);
            //            stepData = AddData(stepData, blur, "Gray");

            //            CvInvoke.Threshold(imgg, gray,50, 255, ThresholdType.Binary);
            //            //CvInvoke.CvtColor(blur, gray, ColorConversion.Bgr2Gray);

            //            stepData = AddData(stepData, gray, "Threshold");



            //            CvInvoke.Canny(imgg, canny, 100, 50, 3, false);
            //            CannyImgTemp = canny.ToImage<Gray, byte>();
            //            stepData = AddData(stepData, gray, "canny");

            //            //Find the Rectangle

            //            using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())
            //            {
            //                using (VectorOfPoint approxContour = new VectorOfPoint())
            //                {
            //                    //hierachy = CvInvoke.FindContourTree(canny, contours, ChainApproxMethod.ChainApproxSimple);
            //                    CvInvoke.FindContours(canny, contours, null, RetrType.List, ChainApproxMethod.ChainApproxSimple);
            //                    //for (int i = 0; i < hierachy.GetLength(0); i++)
            //                    int count = contours.Size;
            //                    for (int i = 0; i < count; i++)
            //                    {
            //                        CvInvoke.ApproxPolyDP(contours[i], approxContour, CvInvoke.ArcLength(contours[i], true) * 0.05, true);
            //                        if (CvInvoke.ContourArea(approxContour, false) > 250)
            //                        {
            //                        //    if (approxContour.Size <=6) //The contour has 4 vertices.
            //                        //    {
            //                                //#region determine if all the angles in the contour are within [80, 100] degree
            //                                //bool isRectangle = true;
            //                                //Point[] pts = approxContour.ToArray();
            //                                //LineSegment2D[] edges = PointCollection.PolyLine(pts, true);

            //                                //for (int j = 0; j < edges.Length; j++)
            //                                //{
            //                                //    double angle = Math.Abs(
            //                                //        edges[(j + 1) % edges.Length].GetExteriorAngleDegree(edges[j]));
            //                                //    if (angle < 80 || angle > 100)
            //                                //    {
            //                                //        isRectangle = false;
            //                                //        break;
            //                                //    }
            //                                //}

            //                                //#endregion
            //                                //if (isRectangle)
            //                                boxList.Add(CvInvoke.MinAreaRect(approxContour));
            //                           // }
            //                        }
            //                    }

            //                }
            //            }
            //        }
            //    }
            //}
            //foreach (RotatedRect boxr in boxList)
            //{
            //    CvInvoke.Polylines(tempImg, Array.ConvertAll(boxr.GetVertices(), Point.Round), true,
            //        new Bgr(Color.DarkOrange).MCvScalar, 2);
            //}
            //Mat pI = tempImg.Mat;
            //stepData = AddData(stepData, pI, "Poly");
            #endregion
            return(View("Index", stepData));
        }
        public LicenesePlateDetector(String dataPath)
        {
            ocr = new Tesseract("", "eng", OcrEngineMode.TesseractLstmCombined);

            ocr.SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ-1234567890");
        }
Beispiel #53
0
        private void HandleHotkey(object sender, KeyPressedEventArgs e)
        {
            // Capture the primary screen
            Bitmap   bitmap   = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Graphics graphics = Graphics.FromImage(bitmap);

            graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
            // Save the image as jpg for possible upload later
            bitmap.Save(@"img\screenshot.jpg", ImageFormat.Jpeg);
            Image <Bgr, byte> image = new Image <Bgr, byte>(bitmap);

            // Apply an adaptive threshold to improve OCR recognition
            image = image
                    .Convert <Gray, byte>()
                    .ThresholdAdaptive(
                new Gray(255),
                AdaptiveThresholdType.GaussianC,
                Emgu.CV.CvEnum.ThresholdType.Binary,
                51,
                new Gray(-10)
                ).Convert <Bgr, byte>();
            bool errors     = false;
            var  platvalues = new int[4] {
                -1, -1, -1, -1
            };
            var subimages = new Image <Bgr, byte> [8];

            for (int i = 0; i < 8; i++)
            {
                // Extract the subimage containing the item name
                // this has to be done sequentially and not in parallel
                // TODO: multiline names
                subimages[i] = image.Copy(rectangles[i]);
            }
            // returns a funtion that tries to recognize item i
            // if big is true, considers a 2-line item name
            Action <int> recognize(bool big)
            {
                return((i) => {
                    var subimage = subimages[big ? 4 + i : i];
                    // Save the image as jpg for possible upload later
                    subimage.ToBitmap().Save($@"img\{i}.jpg", ImageFormat.Jpeg);
                    // Only one OCR instance per thread or runtime errors happen
                    var ocr = new Tesseract(
                        @".\tessdata",
                        "weng",
                        OcrEngineMode.TesseractOnly,
                        // Fun fact: no Q or J appears on any name
                        "ABCDEFGHIJKLMNOPRSTUVWXYZ&"
                        );
                    ocr.SetImage(subimage);
                    ocr.Recognize();
                    var str = ocr.GetUTF8Text().Trim();
                    // Save the originally recognized text
                    reportStrings[i, 0] = str;
                    var item = EditDistance.BestMatch(str);
                    // Save the fixed name
                    reportStrings[i, 1] = item;
                    if (item == "NOT RECOGNIZED")
                    {
                        if (big)
                        {
                            errors = true;
                        }
                        else
                        {
                            recognize(true)(i);
                        }
                    }
                    else
                    {
                        platvalues[i] = GetWarframeMarketPrice(item);
                    }
                });
            }

            // We run both the OCR and the warframe.market query in parallel
            Parallel.For(0, 4, recognize(false));
            // Labels can only be edited in the main thread
            for (int i = 0; i < 4; i++)
            {
                var name = reportStrings[i, 1];
                labels[i, 0].Text = Items.Normalize(name);
                if (platvalues[i] >= 0)
                {
                    labels[i, 1].Text = Items.Ducats[name] + "";
                    labels[i, 2].Text = platvalues[i] + "";
                }
                else
                {
                    labels[i, 1].Text = labels[i, 2].Text = "???";
                }
            }
            // Minimize and restore window to bring it to the front
            WindowState = FormWindowState.Minimized;
            WindowState = FormWindowState.Normal;
            if (errors && Properties.Settings.Default.submit_errors)
            {
                SubmitErrorAsync();
            }
            else
            {
                submitErrorButton.Enabled = true;
            }
        }
        public StepData StepsVideo(string uploadFile)
        {
            StepData           stepData        = new StepData();
            ProcessCaptured    processCaptured = new ProcessCaptured();
            Image <Bgra, Byte> imgg            = null;
            Mat finalImg = new Mat();

            if (!string.IsNullOrWhiteSpace(uploadFile))
            {
                byte[] fileBytes = Convert.FromBase64String(uploadFile);
                using (MemoryStream ms = new MemoryStream(fileBytes))
                {
                    imgg     = processCaptured.GetImageFromStream(ms);
                    finalImg = imgg.Mat.Clone();
                    //stepData = AddData(stepData, finalImg.Clone(), "Orginal");
                    using (Mat im = imgg.Mat.Clone())
                    {
                        using (Mat threshold = new Mat())
                        {
                            using (Mat gray = new Mat())
                            {
                                using (Mat canny = new Mat())
                                {
                                    using (Mat rectImg = new Mat())
                                    {
                                        CvInvoke.Threshold(im, threshold, 100, 255, ThresholdType.BinaryInv);
                                        //stepData = AddData(stepData, threshold, "Threshold");

                                        CvInvoke.CvtColor(threshold, gray, ColorConversion.Bgr2Gray);
                                        //stepData = AddGrayData(stepData, gray, "Gray");

                                        CvInvoke.Canny(gray, canny, 100, 50, 7);
                                        //stepData = AddData(stepData, canny, "Canny");

                                        List <RotatedRect> rect = new List <RotatedRect>();
                                        rect = Contours(canny);

                                        if (rect != null && rect.Count > 0)
                                        {
                                            foreach (RotatedRect boxr in rect)
                                            {
                                                CvInvoke.Polylines(finalImg, Array.ConvertAll(boxr.GetVertices(), Point.Round), true,
                                                                   new Bgr(Color.DarkOrange).MCvScalar, 2);
                                            }
                                        }
                                        //stepData = AddData(stepData, finalImg, "With Rectangle");

                                        List <Mat> mat = RoI(rect, gray);
                                        int        i   = 0;
                                        //OCR
                                        string    path = AppContext.BaseDirectory;
                                        Tesseract _ocr = new Tesseract(path, "eng", OcrEngineMode.TesseractLstmCombined);
                                        _ocr.SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
                                        foreach (Mat m in mat)
                                        {
                                            i += 1;
                                            _ocr.SetImage(m);
                                            _ocr.Recognize();
                                            Tesseract.Character[] words = _ocr.GetCharacters();
                                            //string wor=words.
                                            StringBuilder sb = new StringBuilder();
                                            foreach (var c in words)
                                            {
                                                sb.Append(c.Text);
                                            }
                                            if (sb.Length > 3 && sb.Length <= 10)
                                            {
                                                stepData = AddData(stepData, m, i.ToString(), sb.ToString());
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            //return View("Index", stepData);
            finalImg = null;
            return(stepData);
        }
Beispiel #55
0
        public static WordList DoOCR(string pdf_filename, int page_number)
        {
            Logging.Info("+Rendering page {1} for PDF file {0}", pdf_filename, page_number);
            using (MemoryStream ms = new MemoryStream(SoraxPDFRenderer.GetPageByDPIAsImage(pdf_filename, pdf_user_password, page_number, 200)))
            {
                Bitmap bitmap = (Bitmap)Image.FromStream(ms);

                Logging.Info("-Rendering page #{0}", page_number);

                Logging.Info("Startup directory is {0}", Environment.CurrentDirectory);
                Logging.Info("Language is '{0}'", language);

                using (Tesseract ocr = new Tesseract())
                {
                    ocr.Init(null, language, false);

                    Logging.Info("+Doing OCR");

                    const int MIN_WIDTH = 0;

                    // Build a list of all the rectangles to process
                    PDFRegionLocator        pdf_region_locator = new PDFRegionLocator(bitmap);
                    PDFRegionLocator.Region last_region        = pdf_region_locator.regions[0];
                    List <Rectangle>        rectangles         = new List <Rectangle>();
                    Rectangle last_rectangle = new Rectangle();
                    foreach (PDFRegionLocator.Region region in pdf_region_locator.regions)
                    {
                        int  rect_height     = region.y - last_region.y;
                        bool alarming_height = (rect_height <= 0);

                        Rectangle rectangle = new Rectangle();

                        if (last_region.state == PDFRegionLocator.SegmentState.BLANKS)
                        {
                            // LHS
                            {
                                rectangle = new Rectangle(0, last_region.y, bitmap.Width / 2, Math.Max(MIN_WIDTH, rect_height));
                            }
                            // RHS
                            {
                                rectangle = new Rectangle(bitmap.Width / 2, last_region.y, bitmap.Width / 2, Math.Max(MIN_WIDTH, rect_height));
                            }
                        }
                        else if (last_region.state == PDFRegionLocator.SegmentState.PIXELS)
                        {
                            // Full column
                            {
                                rectangle = new Rectangle(0, last_region.y, bitmap.Width, Math.Max(MIN_WIDTH, rect_height));
                            }
                        }

                        if (alarming_height || rectangle.Height <= 0)
                        {
                            Logging.Warn("Calculated region height is negative or zero: {0} :: Calculated region {1} <-- CURRENT:{2} - LAST:{3}", rect_height, rectangle, region, last_region);

                            // skip rectangle
                        }
                        else if (last_rectangle.X == rectangle.X && last_rectangle.Y == rectangle.Y)
                        {
                            Logging.Warn("Overlapping subsequent rectangles will be merged :: CURRENT:{0} - LAST:{1}", rectangle, last_rectangle);
                            last_rectangle.Width  = Math.Max(last_rectangle.Width, rectangle.Width);
                            last_rectangle.Height = Math.Max(last_rectangle.Height, rectangle.Height);
                            Logging.Warn("--> Updated 'last' rectangle:{0}", last_rectangle);
                        }
                        else
                        {
                            rectangles.Add(rectangle);
                            last_rectangle = rectangle;
                        }

                        last_region = region;
                    }

                    // DEBUG CODE: Draw in the region rectangles
                    //
                    // When we run in NOKILL mode, we "know" we're running in a debugger or stand-alone environment
                    // intended for testing this code. Hence we should dump the regions image as part of the process.
                    if (no_kill)
                    {
                        string bitmap_diag_path = pdf_filename + @"." + page_number + @"-ocr.png";

                        Logging.Info("Dumping regions-augmented page {0} PNG image to file {1}", page_number, bitmap_diag_path);
                        Graphics g = Graphics.FromImage(bitmap);
                        foreach (Rectangle rectangle in rectangles)
                        {
                            if (rectangle.Width <= MIN_WIDTH && rectangle.Height > MIN_WIDTH)
                            {
                                DrawRectangleOutline(g, Pens.Purple, rectangle);
                            }
                            else if (rectangle.Width > MIN_WIDTH && rectangle.Height <= MIN_WIDTH)
                            {
                                DrawRectangleOutline(g, Pens.PowderBlue, rectangle);
                            }
                            else if (rectangle.Width <= MIN_WIDTH && rectangle.Height <= MIN_WIDTH)
                            {
                                DrawRectangleOutline(g, Pens.Red, rectangle);
                            }
                            else
                            {
                                DrawRectangleOutline(g, Pens.LawnGreen, rectangle);
                            }
                        }

                        bitmap.Save(bitmap_diag_path, ImageFormat.Png);
                    }

                    // Do the OCR on each of the rectangles
                    WordList word_list = new WordList();
                    foreach (Rectangle rectangle in rectangles)
                    {
                        if (0 == rectangle.Width || 0 == rectangle.Height)
                        {
                            Logging.Info("Skipping zero extent rectangle {0}", rectangle.ToString());
                            continue;
                        }

                        Logging.Info("Doing OCR for region {0} on bitmap WxH: {1}x{2}", rectangle.ToString(), bitmap.Width, bitmap.Height);
                        List <Word> result = ocr.DoOCR(bitmap, rectangle);
                        Logging.Info("Got {0} words", result.Count);
                        word_list.AddRange(ConvertToWordList(result, rectangle, bitmap));
                    }

                    Logging.Info("-Doing OCR");


                    Logging.Info("Found {0} words ({1} @ #{2})", word_list.Count, pdf_filename, page_number);

#if false
                    Logging.Info("+Reordering words for columns");
                    WordList word_list_ordered = ColumnWordOrderer.ReorderWords(word_list);
                    Logging.Info("-Reordering words for columns");
                    word_list_ordered.WriteToFile(ocr_output_filename);
#endif

                    return(word_list);
                }
            }
        }
Beispiel #56
0
        public OcrPage()
            : base()
        {
            var button = this.GetButton();

            button.Text     = "Perform Text Detection";
            button.Clicked += OnButtonClicked;

            OnImagesLoaded += async(sender, image) =>
            {
                if (image == null || image[0] == null)
                {
                    return;
                }
                SetMessage("Please wait...");
                SetImage(null);

                Task <Tuple <Mat, String, long> > t = new Task <Tuple <Mat, String, long> >(
                    () =>
                {
                    String lang = "eng";
#if NETFX_CORE
                    String path = System.IO.Path.Combine(ApplicationData.Current.LocalFolder.Path, "tessdata");
#elif __ANDROID__
                    String path = System.IO.Path.Combine(
                        Android.OS.Environment.ExternalStorageDirectory.AbsolutePath,
                        Android.OS.Environment.DirectoryDownloads,
                        "tessdata");
#elif __IOS__
                    String path = System.IO.Path.Combine(
                        Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                        "tessdata");
#else
                    String path = "./tessdata/";
#endif

                    TesseractDownloadLangFile(path, lang);
                    TesseractDownloadLangFile(path, "osd");   //script orientation detection

                    if (_ocr == null)
                    {
                        _ocr = new Tesseract(path, lang, OcrEngineMode.TesseractOnly);
                    }

                    _ocr.SetImage(image[0]);
                    if (_ocr.Recognize() != 0)
                    {
                        throw new Exception("Failed to recognize image");
                    }
                    String text = _ocr.GetUTF8Text();
                    long time   = 0;

                    return(new Tuple <Mat, String, long>(image[0], text, time));
                });
                t.Start();

                var result = await t;
                SetImage(t.Result.Item1);
                String computeDevice = CvInvoke.UseOpenCL ? "OpenCL: " + Ocl.Device.Default.Name : "CPU";
                string ocrResult     = t.Result.Item2;
                if (Device.RuntimePlatform.Equals("WPF"))
                {
                    ocrResult = ocrResult.Replace(System.Environment.NewLine, " ");
                }

                SetMessage(ocrResult);
            };
        }
Beispiel #57
0
 public OcrWithMutex(Tesseract ocr, object locker)
 {
     Ocr    = ocr;
     Locker = locker;
 }
Beispiel #58
0
 /// <summary>
 /// Create a license plate detector
 /// </summary>
 /// <param name="dataPath">
 /// The datapath must be the name of the parent directory of tessdata and
 /// must end in / . Any name after the last / will be stripped.
 /// </param>
 public LicensePlateDetector(string dataPath)
 {
     _ocr = new Tesseract(dataPath, "eng", OcrEngineMode.TesseractOnly);
     _ocr.SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ-1234567890");
 }
Beispiel #59
-1
        /// <summary>
        /// 
        /// </summary>
        /// <param name="gameEngine">An instance of the <see cref="Tesseract"/> game engine.</param>
        protected override void CreateDeviceResources(Tesseract gameEngine)
        {
            // Create a texture sampler state (in the base class)
            base.CreateDeviceResources(gameEngine);

            // Initialize variables needed to create the vertex & index buffers
            var device = gameEngine.DeviceManager.Device3D;
            Vector3 quadNormal = new Vector3(0, 1, 0);
            Vector2 texCoords = new Vector2(0, 0);
            VertexArray = new Vertex[]
            {
                new Vertex(new Vector3(1, 0,  0), quadNormal, Color.Lime,	texCoords),
                new Vertex(new Vector3(0, 0,  0), quadNormal, Color.Blue,	texCoords),
                new Vertex(new Vector3(0, 0, -1), quadNormal, Color.Red,	texCoords),
                new Vertex(new Vector3(1, 0, -1), quadNormal, Color.Yellow, texCoords),
            };
            IndexArray = new ushort[]
            {
                0, 1, 2, // Front A
                0, 2, 3, // Front B
            };

            // Create the buffers
            VertexBuffer = ToDispose(Buffer.Create(device, BindFlags.VertexBuffer, VertexArray));
            VertexBufferBinding = new VertexBufferBinding(VertexBuffer, Utilities.SizeOf<Vertex>(), 0);
            IndexBuffer = ToDispose(Buffer.Create(device, BindFlags.IndexBuffer, IndexArray));
        }
Beispiel #60
-1
        public static TextAnalysisResult ProcessText(Bitmap bmp, List<TextTrigger> filters)
        {
            TextAnalysisResult analysisResult = new TextAnalysisResult();

            //if there aren't any triggers, don't do anything, this stuff is busy
            if (filters.Count == 0)
            {
                Debug.WriteLine("TextAnalysis.ProcessText no text triggers");
                return analysisResult;
            }

            double confidenceFilter = double.Parse(ConfigurationManager.AppSettings["textFilterConfidence"]);

            List<Word> results;

            //perform OCR thread safe :/
            Stopwatch watch = new Stopwatch();
            watch.Start();
            lock (lockObj)
            {
                using (Tesseract tessocr = new Tesseract())
                {
                    tessocr.Init(configPath, "eng", false);
                    results = tessocr.DoOCR(bmp, Rectangle.Empty);
                }
            }
            watch.Stop();
            Debug.WriteLine("TextAnalysis.ProcessText OCR took {0}ms", watch.ElapsedMilliseconds);

            //lower number for confidence is greater certainty, don't ask, i don't know why.
            foreach (var resultWord in results.Where(word => word.Confidence < confidenceFilter))
            {
                string word = resultWord.Text;
                foreach (var trigger in filters)
                {
                    if (Regex.IsMatch(word.ToUpperInvariant(), trigger.triggerString.ToUpperInvariant()))
                    {
                        analysisResult.AddFault(trigger.userEmail, trigger.triggerString, word, (int)resultWord.Confidence);
                    }
                }
            }
            return analysisResult;
        }