Example #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);
   }
Example #2
0
        /// <summary>
        /// Tesseract OCR
        /// </summary>
        /// <param name="filteredPlate"></param>
        /// <returns></returns>
        private StringBuilder TesseractOCR(UMat filteredPlate)
        {
            Emgu.CV.OCR.Tesseract.Character[] words;
            StringBuilder strBuilder = new StringBuilder();

            using (UMat tmp = filteredPlate.Clone())
            {
                _ocr.SetImage(tmp);
                _ocr.Recognize();

                strBuilder.Append(_ocr.GetUTF8Text());

                words = _ocr.GetCharacters();

                //if (words.Length == 0) continue;

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

            return(strBuilder);
        }
Example #3
0
        static void Main(string[] args)
        {
            var files = Directory.GetFiles(@"d:\raw\classified\");

            //var img = new Image<Rgb, byte>(files[0]);
            var img = new Image<Emgu.CV.Structure.Gray, byte>(@"d:\raw\classified\test.jpg");

            var _ocr = new Tesseract(@"D:\", "eng", OcrEngineMode.TesseractCubeCombined);
            _ocr.SetVariable("tessedit_char_whitelist", "1234567890");

            _ocr.Recognize(img);

            var result = _ocr.GetCharacters();
        }
Example #4
0
        private void button5_Click(object sender, EventArgs e)
        {
            var _ocr = new Tesseract(@"D:\", "eng", OcrEngineMode.TesseractCubeCombined);
            _ocr.SetVariable("tessedit_char_whitelist", "1234567890");

            _ocr.Recognize(_inputArray);

            var result = _ocr.GetCharacters().ToList();

            var sb = new StringBuilder();

            result.ForEach(c => sb.Append(c.Text));

            label1.Text = sb.ToString();
        }
Example #5
0
        private void FindLicensePlate(
            VectorOfVectorOfPoint contours,
            int[,] hierachy,
            int idx,
            IInputArray gray,
            IInputArray canny,
            List <IInputOutputArray> licensePlateImagesList,
            List <IInputOutputArray> filteredLicensePlateImagesList,
            List <RotatedRect> detectedLicensePlateRegionList,
            List <String> licenses)
        {
            if (hierachy.Length != 0)
            {
                for (; idx >= 0; idx = hierachy[idx, 0])
                {
                    int numberOfChildren = GetNumberOfChildren(hierachy, idx);
                    //if it does not contains any children (charactor), it is not a license plate region
                    if (numberOfChildren == 0)
                    {
                        continue;
                    }

                    using (VectorOfPoint contour = contours[idx])
                    {
                        if (CvInvoke.ContourArea(contour) > 400)
                        {
                            if (numberOfChildren < 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, hierachy, hierachy[idx, 2], gray, canny, licensePlateImagesList,
                                                 filteredLicensePlateImagesList, detectedLicensePlateRegionList, licenses);
                                continue;
                            }

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

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

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

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

                                    using (Mat rot = CvInvoke.GetAffineTransform(srcCorners, destCorners))
                                    {
                                        CvInvoke.WarpAffine(gray, tmp1, rot, Size.Round(box.Size));
                                    }

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

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

                                    UMat filteredPlate = FilterPlate(plate);

                                    //Tesseract.Character[] words;
                                    StringBuilder strBuilder = new StringBuilder();
                                    using (UMat tmp = filteredPlate.Clone())
                                    {
                                        Emgu.CV.OCR.Tesseract.Character[] words;

                                        _ocr.Recognize(tmp);
                                        strBuilder.Append(_ocr.GetText());

                                        words = _ocr.GetCharacters();

                                        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);
                                }
                        }
                    }
                }
            }
        }