Ejemplo n.º 1
0
        private void InitOcr(String path, String lang, OcrEngineMode mode)
        {
            try
            {
                if (_ocr != null)
                {
                    _ocr.Dispose();
                    _ocr = null;
                }

                if (String.IsNullOrEmpty(path))
                {
                    path = ".";
                }

                TesseractDownloadLangFile(path, lang);
                TesseractDownloadLangFile(path, "osd"); //script orientation detection
                String pathFinal = path.Length == 0 ||
                                   path.Substring(path.Length - 1, 1).Equals(Path.DirectorySeparatorChar.ToString())
                    ? path
                    : String.Format("{0}{1}", path, System.IO.Path.DirectorySeparatorChar);

                _ocr = new Emgu.CV.OCR.Tesseract(pathFinal, lang, mode);
            }
            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;
            }
        }
Ejemplo n.º 2
0
        private void TextRecogniser(Image image)
        {
            Emgu.CV.OCR.Tesseract tesseract = new Emgu.CV.OCR.Tesseract
                                                  (@"F:\Working Pack\c#\Scripter\Scripter\Templates\eng.traineddata", "eng",
                                                  OcrEngineMode.TesseractLstmCombined);
            var tempImg = new Bitmap(image).ToImage <Gray, byte>();

            tesseract.SetImage(tempImg);
            tesseract.Recognize();
            string response = tesseract.GetUTF8Text();

            tesseract.Dispose();
        }
Ejemplo n.º 3
0
        //główne operacje na obrazie
        public void ocr()
        {
            //otworzenie pliku
            FileStream srcstream = new FileStream(pic_file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            //stworzenie bitmapy
            Bitmap source = new Bitmap(srcstream);
            //zmiana ustawień webform

            Panel1.Visible = false;
            Image1.Dispose();

            Label2.Text = "Processing...";
            Panel3.Visible = true;

            //Preperation code
            Bitmap ext = source;

            //AForge.Imaging.Filters.
            //Przekształcenie obrazu na skalę odcieni szarości - testować dla obrazów o różnej kolorystyce(opracować system wyznaczania parametrów filtru na podstawie RGB zdjęcia)
            AForge.Imaging.Filters.Grayscale grScl = new AForge.Imaging.Filters.Grayscale(0.2125, 0.0154, 0.0721 );
            source = grScl.Apply(source);
            //Zwiększenie kontrastu
            AForge.Imaging.Filters.ContrastStretch conCor = new AForge.Imaging.Filters.ContrastStretch();

            source = conCor.Apply(source);
            //Wyostrzenie
            AForge.Imaging.Filters.Sharpen shp = new AForge.Imaging.Filters.Sharpen();
            source = shp.Apply(source);

            //Segmentation code
            bool procesed = false;
               // Image2.Width = 350;
               // Image2.Height = (int)((source.Height * 200) / source.Width);

            try
            {
                Emgu.CV.Image<Bgr, Byte> to_rec = new Emgu.CV.Image<Bgr, byte>(source);
                Do_ocr = new Tesseract("tessdata", "eng", Tesseract.OcrEngineMode.OEM_DEFAULT);
                try
                {
                    Do_ocr.Recognize<Bgr>(to_rec);
                    //recognizedText.Text = ocr.GetText();
                    PastOCRBox.Text = Do_ocr.GetText();
                   // StatusBox.Text = "Finished! Ready for next one...";
                    Do_ocr.Dispose();
                    to_rec.Dispose();
                }
                catch (Exception exp)
                {
                    Label2.Text = "Recognition error! " + exp.Message;
                    Do_ocr.Dispose();
                    return;
                }
            }
            catch (Exception exp)
            {
                Label2.Text = "OCR engine failed! " + exp.Message;
                return;
            }

            //czyszczenie z plików tymczasowych

              //  source.Save("D:\\test.bmp");
              //  ext.Save("D:\\testcor.bmp");
            source.Dispose();
            srcstream.Close();
            srcstream.Dispose();
            //System.IO.File.Delete(pic_file);
            System.IO.File.Delete(Server.MapPath("~/img/prev.bmp"));
            System.IO.File.Delete(Server.MapPath("~/img/tmp.bmp"));
            //przygotować wygląd strony po rozpoznawaniu
            Panel3.Visible = false;
            Label1.Visible = false;
            Panel0.Visible = false;
            Panel5.Visible = false;

            Panel4.Visible = true;
        }
Ejemplo n.º 4
0
        private void server()
        {
            int port = 8001;

            IPAddress ipAd = IPAddress.Parse(ip);

            // use local m/c IP address, and
            // use the same in the client
            ASCIIEncoding asen = new ASCIIEncoding();
            /* Initializes the Listener */
            TcpListener myList = new TcpListener(IPAddress.Any, port);

            Console.WriteLine("The server is running at port 8001...");
            Console.WriteLine("The local End point is  :" +
                              myList.LocalEndpoint);
            /* Start Listeneting at the specified port */

            byte[] b = new byte[2000000];
            myList.Start();

            while (!stoped)
            {
                    char[] recived = new char[2000000];
                    Console.WriteLine("Waiting for a connection.....");

                    TcpClient s = myList.AcceptTcpClient();
                    Console.WriteLine("Connection accepted " +s.Client.RemoteEndPoint);
                    NetworkStream stream = s.GetStream();

                    var reader = new StreamReader(stream);

                    var k = stream.Read(b, 0, 2000000);

                    Console.WriteLine("Recieved...");

                    ImageConverter conv = new ImageConverter();
                    Bitmap rec = new Bitmap((Bitmap)conv.ConvertFrom(b));

                    Console.WriteLine("Recognicion...");
                    Tesseract ocr = new Tesseract("tessdata", "pol+eng", Tesseract.OcrEngineMode.OEM_DEFAULT);

                    ocr.Recognize<Bgr>(new Emgu.CV.Image<Bgr, Byte>(rec));

                    string to_send_str = ocr.GetText();
                    ocr.Dispose();
                    Console.Write(to_send_str + "\n");

                    stream.Write(asen.GetBytes(to_send_str),0,to_send_str.Length);

                    Console.WriteLine("\nSent Acknowledgement");
                    /* clean up */
                    s.Close();

               }
            myList.Stop();
            Console.WriteLine("Server stopped");
        }