Пример #1
0
 /// <summary>
 ///   Invokes Tesseract OCR Recognize on given image
 ///   Stores the resulting data in the Text,Confidence and ScanTime data members
 /// </summary>
 /// <param name="image"> Source Image to be OCR-ed </param>
 /// <returns> Text from Image </returns>
 public string Scan(Image<Gray, byte> image)
 {
     _tesseract.Recognize(image);
     _text = _tesseract.GetText();
     _chars = _tesseract.GetCharactors();
     return _text;
 }
Пример #2
0
        private static string OcrInitalizer()
        {
            _ocr = new Tesseract("tessdata", "eng", Tesseract.OcrEngineMode.OEM_TESSERACT_CUBE_COMBINED);
            _ocr.Init(@"C:\Users\Nino\Documents\PbpHackthone\dotNet_OCR_server\Emgu\emgucv-windows-x64 2.4.0.1717\bin", "eng", Tesseract.OcrEngineMode.OEM_DEFAULT);

            string result = string.Empty;

            try
            {
                var image = new Image <Bgr, byte>("IMAG0253_cropped.jpg");


                using (Image <Gray, byte> gray = image.Convert <Gray, Byte>())
                {
                    gray.Save(@"C:\Users\Nino\Desktop\IMAG0253_cropped.jpg");

                    Stopwatch watch = new Stopwatch();
                    watch.Start();

                    _ocr.Recognize(gray);
                    Tesseract.Charactor[] charactors = _ocr.GetCharactors();

                    //String text = String.Concat( Array.ConvertAll(charactors, delegate(Tesseract.Charactor t) { return t.Text; }) );
                    result = _ocr.GetText();
                    watch.Stop();

                    var total = watch.ElapsedMilliseconds;
                }
            }
            catch (Exception exception)
            {
            }

            return(result);
        }
Пример #3
0
        private void Form1_Load(object sender, EventArgs e)
        {
            if (openImageFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Bgr drawColor = new Bgr(Color.Blue);
                try
                {
                    Image <Bgr, Byte> image = new Image <Bgr, byte>(openImageFileDialog.FileName);
                    original.Image    = image.ToBitmap();
                    original.SizeMode = PictureBoxSizeMode.Zoom;
                    using (Image <Gray, byte> gray = image.Convert <Gray, Byte>())
                    {
                        _ocr.Recognize(gray);
                        Tesseract.Charactor[] charactors = _ocr.GetCharactors();
                        foreach (Tesseract.Charactor c in charactors)
                        {
                            image.Draw(c.Region, drawColor, 1);
                        }

                        processed.Image    = image.ToBitmap();
                        processed.SizeMode = PictureBoxSizeMode.Zoom;
                        //String text = String.Concat( Array.ConvertAll(charactors, delegate(Tesseract.Charactor t) { return t.Text; }) );
                        String text = _ocr.GetText();
                        ocrTextBox.Text = text;
                    }
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message);
                }
            }
        }
Пример #4
0
        public void TestOCREngBlankPage()
        {
            Version version = Tesseract.Version;
            int     i       = version.Major;

            using (Tesseract ocr = GetTesseract())
                using (Image <Gray, Byte> img = new Image <Gray, byte>(1024, 960))
                {
                    ocr.Recognize(img);
                    Tesseract.Charactor[] results = ocr.GetCharactors();
                    EmguAssert.IsTrue(results.Length == 0);
                }
        }
Пример #5
0
        public override Rectangle[] find(Image <Bgr, Byte> image)
        {
            ocr.Recognize(image.Convert <Gray, Byte>());
            Tesseract.Charactor[] charactors = ocr.GetCharactors();
            result = ocr.GetText();

            List <Rectangle> rects = new List <Rectangle>();

            foreach (Tesseract.Charactor c in charactors)
            {
                rects.Add(c.Region);
            }
            return(rects.ToArray());
        }
Пример #6
0
        /// <summary>
        /// Recognition of MaterialID with tesseract
        ///     - Preprocess image (auto-cropping)
        ///     - Check if count of characters is ok.
        /// </summary>
        /// <param name="labelBox">box where label is found</param>
        /// <param name="img"></param>
        /// <param name="bestPoint"></param>
        /// <returns></returns>
        private string recognizeMaterialID(MCvBox2D labelBox, Image <Bgr, Byte> img, PointW bestPoint)
        {
            Image <Gray, Byte> tmp = img.Copy(labelBox).Convert <Gray, Byte>();

            tmp = tmp.Resize(120, 80, INTER.CV_INTER_LINEAR);

            // Auto cropping (only materialID is left)
            tmp = preprocessMaterialID(tmp);

            // If Tesseract is checked recognize
            if (tmp != null &&
                // If bestPoint matches mostCommong point
                mostCommon.p.X == bestPoint.p.X && mostCommon.p.Y == bestPoint.p.Y)
            {
                _ocr.Recognize(tmp);
                // Check if (size) result of tesseract is allowed.
                if (_ocr.GetCharactors().Length > minimumChars &&
                    _ocr.GetCharactors().Length < maximumChars)
                {
                    return(_ocr.GetText());
                }
            }
            return(null);
        }
Пример #7
0
        public static Tesseract.Charactor[] DoOcr(Image <Gray, byte> img)
        {
            Tesseract.Charactor[] chars;
            string text;

            using (var tesseract = new Tesseract("tessdata", "eng", Tesseract.OcrEngineMode.OEM_TESSERACT_CUBE_COMBINED)
                   )
            {
                tesseract.Recognize(img);
                text  = tesseract.GetText();
                chars = tesseract.GetCharactors();
            }
            Console.WriteLine(text);
            Console.ReadLine();
            return(chars);
        }
Пример #8
0
        public static string ReadOCR(Image <Bgr, Byte> srcImage, Rectangle rect)
        {
            string str_ocr = "";

            _ocr = new Tesseract("", "eng", Tesseract.OcrEngineMode.OEM_TESSERACT_ONLY);
            // _ocr.SetVariable("tessedit_char_whitelist", "0123456789");
            //_ocr.SetVariable("tessedit_char_whitelist", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
            //_ocr.SetVariable("tessedit_char_whitelist", "abcdefghigklmnopqrstuvwxyz");
            _ocr.SetVariable("tessedit_char_whitelist", "0123456789abcdefghigklmnopqrstuvwxyz");
            //_ocr.SetVariable("tessedit_char_whitelist", "0123456789abcdefghigklmnopqrstuvwxyz");

            try
            {
                Image <Gray, Byte> Image_Bin = new Image <Gray, Byte>(new Size(rect.Width, rect.Height));
                // Image<Gray, Byte> Image_Bin1 = new Image<Gray, Byte>(new Size(rect.Width, rect.Height));
                CvInvoke.cvSetImageROI(srcImage, rect);
                using (Image <Gray, byte> gray = srcImage.Convert <Gray, Byte>())
                {
                    CvInvoke.cvThreshold(gray, Image_Bin, 0, 255, THRESH.CV_THRESH_OTSU);

                    // CvInvoke.cvThreshold(Image_Bin, Image_Bin1, 50, 255, THRESH.CV_THRESH_BINARY_INV);

                    _ocr.Recognize(Image_Bin);
                    Tesseract.Charactor[] charactors = _ocr.GetCharactors();

                    ocr_rect = new Rectangle[charactors.Length];

                    for (int i = 0; i < charactors.Length; i++)
                    {
                        ocr_rect[i] = new Rectangle(charactors[i].Region.X + rect.X, charactors[i].Region.Y + rect.Y, charactors[i].Region.Width, charactors[i].Region.Height);
                    }
                    str_ocr = _ocr.GetText();
                }
                Image_Bin.Dispose();
                // Image_Bin1.Dispose();
            }
            catch (Exception exception)
            {
                //MessageBox.Show(exception.Message);
            }
            CvInvoke.cvResetImageROI(srcImage);


            return(str_ocr);
        }
Пример #9
0
        public void TestOCRBgrText()
        {
            using (Tesseract ocr = GetTesseract())
                using (Image <Bgr, Byte> img = new Image <Bgr, byte>(480, 200))
                {
                    ocr.SetVariable("tessedit_char_whitelist", "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,");

                    String message = "Hello, World";
                    CvInvoke.PutText(img, message, new Point(50, 100), CvEnum.FontFace.HersheySimplex, 1.0, new Bgr(Color.Pink).MCvScalar);
                    //ImageViewer.Show(img);
                    ocr.Recognize(img);

                    String messageOcr = ocr.GetText().TrimEnd('\n', '\r'); // remove end of line from ocr-ed text
                    EmguAssert.AreEqual(message, messageOcr, String.Format("'{0}' is not equal to '{1}'", message, messageOcr));

                    Tesseract.Charactor[] results = ocr.GetCharactors();
                }
        }
Пример #10
0
        // read the text from an image
        public static String ReadText(Image <Bgr, byte> img)
        {
            Tesseract _ocr = new Tesseract("", "eng", Tesseract.OcrEngineMode.OEM_TESSERACT_CUBE_COMBINED);

            Image <Gray, byte> gray = img.Convert <Gray, Byte>();

            {
                _ocr.Recognize(gray);
                Tesseract.Charactor[] charactors = _ocr.GetCharactors();

                /*foreach (Tesseract.Charactor c in charactors)
                 * {
                 *  img.Draw(c.Region, new Bgr(0,255,0), 1);
                 * }*/

                //imageBox1.Image = image;

                //String text = String.Concat( Array.ConvertAll(charactors, delegate(Tesseract.Charactor t) { return t.Text; }) );
                //String text = _ocr.GetText();
            }
            return(_ocr.GetText());
        }
Пример #11
0
        private void FindLicensePlate(
            Contour <Point> contours, Image <Gray, Byte> gray, Image <Gray, Byte> canny,
            List <Image <Gray, Byte> > licensePlateImagesList, List <Image <Gray, Byte> > filteredLicensePlateImagesList, List <MCvBox2D> detectedLicensePlateRegionList,
            List <String> licenses)
        {
            for (; contours != null; contours = contours.HNext)
            {
                int numberOfChildren = GetNumberOfChildren(contours);
                //if it does not contains any children (charactor), it is not a license plate region
                if (numberOfChildren == 0)
                {
                    continue;
                }

                if (contours.Area > 400)
                {
                    if (numberOfChildren < 3)
                    {
                        //If the contour has less than 3 children, it is not a license plate (assuming license plate has at least 3 charactor)
                        //However we should search the children of this contour to see if any of them is a license plate
                        FindLicensePlate(contours.VNext, gray, canny, licensePlateImagesList, filteredLicensePlateImagesList, detectedLicensePlateRegionList, licenses);
                        continue;
                    }

                    MCvBox2D box = contours.GetMinAreaRect();
                    if (box.angle < -45.0)
                    {
                        float tmp = box.size.Width;
                        box.size.Width  = box.size.Height;
                        box.size.Height = tmp;
                        box.angle      += 90.0f;
                    }
                    else if (box.angle > 45.0)
                    {
                        float tmp = box.size.Width;
                        box.size.Width  = box.size.Height;
                        box.size.Height = tmp;
                        box.angle      -= 90.0f;
                    }

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

                    using (Image <Gray, Byte> tmp1 = gray.Copy(box))
                        //resize the license plate such that the front is ~ 10-12. This size of front results in better accuracy from tesseract
                        using (Image <Gray, Byte> tmp2 = tmp1.Resize(240, 180, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC, true))
                        {
                            //removes some pixels from the edge
                            int edgePixelSize = 2;
                            tmp2.ROI = new Rectangle(new Point(edgePixelSize, edgePixelSize), tmp2.Size - new Size(2 * edgePixelSize, 2 * edgePixelSize));
                            Image <Gray, Byte> plate = tmp2.Copy();

                            Image <Gray, Byte> filteredPlate = FilterPlate(plate);

                            Tesseract.Charactor[] words;
                            StringBuilder         strBuilder = new StringBuilder();
                            using (Image <Gray, Byte> tmp = filteredPlate.Clone())
                            {
                                _ocr.Recognize(tmp);
                                words = _ocr.GetCharactors();

                                if (words.Length == 0)
                                {
                                    continue;
                                }

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

                            licenses.Add(strBuilder.ToString());
                            licensePlateImagesList.Add(plate);
                            filteredLicensePlateImagesList.Add(filteredPlate);
                            detectedLicensePlateRegionList.Add(box);
                        }
                }
            }
        }
Пример #12
0
        private void ProcessImage(Image <Bgr, byte> image)
        {
            Stopwatch watch = Stopwatch.StartNew();             // time the detection process

            List <Image <Gray, Byte> > licensePlateImagesList         = new List <Image <Gray, byte> >();
            List <Image <Gray, Byte> > filteredLicensePlateImagesList = new List <Image <Gray, byte> >();
            List <MCvBox2D>            licenseBoxList = new List <MCvBox2D>();
            //List<string> words = _licensePlateDetector.DetectLicensePlate(
            //   image,
            //   licensePlateImagesList,
            //   filteredLicensePlateImagesList,
            //   licenseBoxList);

            var _ocr = new Tesseract(@".\tessdata\", "eng",
                                     Tesseract.OcrEngineMode.OEM_DEFAULT, "1234567890");

            List <String> licenses = new List <String>();

            using (Image <Gray, byte> gray = image.Convert <Gray, Byte>())
                //using (Image<Gray, byte> gray = GetWhitePixelMask(img))
                using (Image <Gray, Byte> canny = new Image <Gray, byte>(gray.Size))
                    using (MemStorage stor = new MemStorage())
                    {
                        //CvInvoke.cvCanny(gray, canny, 100, 100, 3);
                        CvInvoke.cvThreshold(gray, canny, 75, 255, Emgu.CV.CvEnum.THRESH.CV_THRESH_BINARY);

                        var bibs    = new List <Image <Gray, Byte> >();
                        var bibsreg = new List <Rectangle>();
                        //imageBox1.Image = gray;
                        var faces = DF(image);

                        Point startPoint = new Point(10, 10);
                        this.Text = "";
                        foreach (Rectangle face in faces)
                        {
                            canny.Draw(face, new Gray(1), 2);


                            var r = new Rectangle(face.X - face.Width / 2, face.Y + face.Height * 2,
                                                  face.Width * 2, face.Height * 3);


                            bibsreg.Add(r);

                            CvInvoke.cvSetImageROI(canny, r);

                            var ni = canny.Copy();

                            _ocr.Recognize(ni);

                            var words = _ocr.GetCharactors();

                            CvInvoke.cvResetImageROI(canny);
                            this.Text += " bib:" + _ocr.GetText().Replace(" ", string.Empty);
                            //AddLabelAndImage(ref startPoint, _ocr.GetText(), ni);
                        }

                        foreach (Rectangle r in bibsreg)
                        {
                            canny.Draw(r, new Gray(1), 10);
                        }

                        imageBox1.Image = canny;
                        //_ocr.Recognize(canny);
                        //var words = _ocr.GetCharactors();
                        //this.Text = _ocr.GetText();

                        //Contour<Point> contours = canny.FindContours(
                        //	 Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE,
                        //	 Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_TREE,
                        //	 stor);
                        //FindLicensePlate(contours, gray, canny, licensePlateImagesList, filteredLicensePlateImagesList, detectedLicensePlateRegionList, licenses);
                    }


            watch.Stop();             //stop the timer
            processTimeLabel.Text = String.Format("License Plate Recognition time: {0} milli-seconds", watch.Elapsed.TotalMilliseconds);

            //panel1.Controls.Clear();
            //Point startPoint = new Point(10, 10);
            //for (int i = 0; i < words.Count; i++)
            //{
            //	AddLabelAndImage(
            //	   ref startPoint,
            //	   String.Format("License: {0}", words[i]),
            //	   licensePlateImagesList[i].ConcateVertical(filteredLicensePlateImagesList[i]));
            //	image.Draw(licenseBoxList[i], new Bgr(Color.Red), 2);
            //}

            //imageBox1.Image = image;
        }
Пример #13
0
        /// <summary>
        /// Processed Image is the one with some rectangles
        /// </summary>
        /// <param name="inputImage"></param>
        /// <param name="ProcessedImage"></param>
        /// <returns></returns>
        public static string OCRDetect(Image <Bgr, byte> inputImage, out Image <Bgr, byte> ProcessedImage)
        {
            ProcessedImage = inputImage;
            Bgr drawColor = new Bgr(Color.Blue);

            Image <Bgr, byte> localImage = inputImage.Copy();

            try
            {
                using (Image <Gray, byte> gray = localImage.Convert <Gray, Byte>())
                {
                    _ocr.Recognize(gray);

                    Tesseract.Charactor[] charactors = _ocr.GetCharactors();

                    /*
                     * // This will filter the charactors that are too small. But noise is going to affect the accuracy already
                     * List<Tesseract.Charactor> charactorsList = new List<Tesseract.Charactor>();
                     * for (int i = 0; i < charactors.Length; i++)
                     * {
                     *  if (charactors[i].Region.Width * charactors[i].Region.Height < 100)   //Too Small
                     *  {
                     *      localImage.Draw(charactors[i].Region, new Bgr(Color.Red), 1);
                     *  }
                     *  else
                     *  {
                     *      localImage.Draw(charactors[i].Region, drawColor, 1);
                     *      charactorsList.Add(charactors[i]);
                     *  }
                     * }
                     *
                     * Tesseract.Charactor[] newCharactors = new Tesseract.Charactor[charactorsList.Count];
                     * for (int i = 0; i < newCharactors.Length; i++)
                     * {
                     *  newCharactors[i] = charactorsList[i];
                     * }
                     *
                     */
                    foreach (Tesseract.Charactor c in charactors)
                    {
                        if (c.Region.Width * c.Region.Height < 200)
                        {
                            localImage.Draw(c.Region, new Bgr(Color.Red), 1);
                            //ShapeNDraw.drawString(c.Cost.ToString(), cleanedInputImage, new Point(c.Region.X, c.Region.Y), 0.3, Color.Red);
                        }
                        else
                        {
                            localImage.Draw(c.Region, drawColor, 1);
                            //ShapeNDraw.drawString(c.Cost.ToString(), cleanedInputImage, new Point(c.Region.X, c.Region.Y), 0.3, Color.Red);
                        }
                    }

                    ProcessedImage = localImage;

                    //string text = String.Concat( Array.ConvertAll(newCharactors, delegate(Tesseract.Charactor t) { return t.Text; }) );
                    string text = _ocr.GetText();
                    return(text);
                }
            }
            catch (Exception exception)
            {
                return("NULL");
            }
        }