Example #1
0
        private void ProcessImage(IInputOutputArray image)
        {
            Stopwatch watch = Stopwatch.StartNew(); // time the detection process

            List <IInputOutputArray> licensePlateImagesList         = new List <IInputOutputArray>();
            List <IInputOutputArray> filteredLicensePlateImagesList = new List <IInputOutputArray>();
            List <RotatedRect>       licenseBoxList = new List <RotatedRect>();
            List <string>            words          = _licensePlateDetector.DetectLicensePlate(
                image,
                licensePlateImagesList,
                filteredLicensePlateImagesList,
                licenseBoxList);

            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++)
            {
                Mat dest = new Mat();
                CvInvoke.VConcat(licensePlateImagesList[i], filteredLicensePlateImagesList[i], dest);
                AddLabelAndImage(
                    ref startPoint,
                    String.Format("License: {0}", words[i]),
                    dest);
                PointF[] verticesF = licenseBoxList[i].GetVertices();
                Point[]  vertices  = Array.ConvertAll(verticesF, Point.Round);
                using (VectorOfPoint pts = new VectorOfPoint(vertices))
                    CvInvoke.Polylines(image, pts, true, new Bgr(Color.Red).MCvScalar, 2);
            }
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="image"></param>
        /// <param name="ocr_mode"></param>
        /// <param name="count"></param>
        /// <param name="canny_thres">Canny threshold will take 3 values 20, 30, 40, 50</param>
        /// <returns></returns>
        private bool ProcessImage(IInputOutputArray image, int ocr_mode)
        {
            List <IInputOutputArray> licensePlateImagesList         = new List <IInputOutputArray>();
            List <IInputOutputArray> filteredLicensePlateImagesList = new List <IInputOutputArray>();
            List <RotatedRect>       licenseBoxList = new List <RotatedRect>();
            List <string>            words          = new List <string>();
            var           result        = false;
            UMat          filteredPlate = new UMat();
            StringBuilder strBuilder    = new StringBuilder();

            CvInvoke.CvtColor(image, filteredPlate, ColorConversion.Bgr2Gray);

            UMat uImg = (UMat)image;

            words = _licensePlateDetector.DetectLicensePlate(
                image,
                licensePlateImagesList,
                filteredLicensePlateImagesList,
                licenseBoxList,
                ocr_mode);


            var validWords         = new List <string>();
            var validLicencePlates = new List <IInputOutputArray>();

            for (int w = 0; w < words.Count; w++)
            {
                string replacement2 = Regex.Replace(words[w], @"\t|\n|\r", "");
                string replacement  = Regex.Replace(replacement2, "[^0-9a-zA-Z]+", "");
                if (replacement.Length >= 6 && replacement != null)
                {
                    var filteredLicence = FilterLicenceSpain(replacement);
                    if (!string.IsNullOrWhiteSpace(filteredLicence))
                    {
                        if (!validWords.Contains(replacement))
                        {
                            validWords.Add(filteredLicence);
                            validLicencePlates.Add(licensePlateImagesList[w]);
                        }
                    }
                }
            }

            ShowResults(image, validLicencePlates, filteredLicensePlateImagesList, licenseBoxList, validWords);

            SetImageBitmap(uImg.Bitmap);
            uImg.Dispose();

            result = true;
            return(result);
        }
        private LicensePlateModel ProcessImage(IInputOutputArray image)
        {
            LicensePlateModel licensePlateModel = new LicensePlateModel();

            Stopwatch watch = Stopwatch.StartNew();

            List <IInputOutputArray> licensePlateImagesList         = new List <IInputOutputArray>();
            List <IInputOutputArray> filteredLicensePlateImagesList = new List <IInputOutputArray>();
            List <RotatedRect>       licenseBoxList = new List <RotatedRect>();
            List <string>            words          = _licensePlateDetector.DetectLicensePlate(
                image,
                licensePlateImagesList,
                filteredLicensePlateImagesList,
                licenseBoxList);

            watch.Stop();

            licensePlateModel.Timer = watch.Elapsed.TotalMilliseconds.ToString();
            licensePlateModel.GuantityPlateResult = words.Count;
            Point  startPoint    = new Point(10, 10);
            string numberLicense = string.Empty;

            for (int i = 0; i < words.Count; i++)
            {
                numberLicense = words[i].Replace(" ", string.Empty);

                numberLicense = new string(numberLicense.Where(c => !char.IsPunctuation(c)).ToArray());

                Mat dest = new Mat();
                CvInvoke.VConcat(licensePlateImagesList[i], filteredLicensePlateImagesList[i], dest);
                BitmapSource bso = ToBitmapSource(dest);
                licensePlateModel.ImageLicensePlate = bso;

                PointF[] verticesF = licenseBoxList[i].GetVertices();
                Point[]  vertices  = Array.ConvertAll(verticesF, Point.Round);
                licensePlateModel.Points = vertices;

                if (numberLicense.Length >= 8)
                {
                    licensePlateModel.LicensePlate = numberLicense;
                    return(licensePlateModel);
                }
            }
            return(licensePlateModel);
        }
Example #4
0
        public UploadFile ProcessImage(IInputOutputArray image)
        {
            string                   path                           = AppContext.BaseDirectory;
            string                   base64string                   = string.Empty;
            UploadFile               uploadFile                     = new UploadFile();
            LicensePlateDetector     _licensePlateDetector          = new LicensePlateDetector(path);
            List <IInputOutputArray> licensePlateImagesList         = new List <IInputOutputArray>();
            List <IInputOutputArray> filteredLicensePlateImagesList = new List <IInputOutputArray>();
            List <RotatedRect>       licenseBoxList                 = new List <RotatedRect>();
            List <string>            words                          = _licensePlateDetector.DetectLicensePlate(
                image,
                licensePlateImagesList,
                filteredLicensePlateImagesList,
                licenseBoxList);
            Point startPoint = new Point(10, 10);

            for (int i = 0; i < words.Count; i++)
            {
                Mat dest = new Mat();
                CvInvoke.VConcat(licensePlateImagesList[i], filteredLicensePlateImagesList[i], dest);
                PointF[] verticesF = licenseBoxList[i].GetVertices();
                Point[]  vertices  = Array.ConvertAll(verticesF, Point.Round);
                using (VectorOfPoint pts = new VectorOfPoint(vertices))
                    CvInvoke.Polylines(image, pts, true, new Bgr(Color.Red).MCvScalar, 2);
                UMat mat = (UMat)filteredLicensePlateImagesList[i];
                Image <Bgr, byte> LpImage = mat.ToImage <Bgr, byte>();
                byte[]            LpBytes = LpImage.ToJpegData();
                uploadFile.RawData.Add(new RawData {
                    RawImage = "data:image/jpg;base64," + Convert.ToBase64String(LpBytes, 0, LpBytes.Length), Number = words[i]
                });
            }
            using (Image <Bgr, byte> img = (Image <Bgr, byte>)image)
            {
                Byte[] bytes = img.ToJpegData();
                base64string          = "data:image/jpg;base64," + Convert.ToBase64String(bytes, 0, bytes.Length);
                uploadFile.OutputFile = base64string;
            }
            return(uploadFile);
        }
Example #5
0
        private void ProcessImage(IInputOutputArray image)
        {
            Stopwatch watch = Stopwatch.StartNew(); // time the detection process

            List <IInputOutputArray> licensePlateImagesList         = new List <IInputOutputArray>();
            List <IInputOutputArray> filteredLicensePlateImagesList = new List <IInputOutputArray>();
            List <RotatedRect>       licenseBoxList = new List <RotatedRect>();

            var found = new List <string>();

            for (double rWidth = 1; rWidth < 12; rWidth += 0.2)
            {
                for (double rHeight = 1; rHeight < 12; rHeight += 0.2)
                {
                    List <string> words1 = _licensePlateDetector.DetectLicensePlate(
                        image,
                        licensePlateImagesList,
                        filteredLicensePlateImagesList,
                        licenseBoxList, rWidth, rHeight);

                    if (words1.Any())
                    {
                        var f = $"FOUND: {rWidth}-{rHeight} = {string.Concat(words1)}";

                        found.Add(f);

                        Console.WriteLine(f);
                    }
                    else
                    {
                        //Console.WriteLine($"FAILED: {rWidth}-{rHeight}");
                    }
                }
            }
            List <string> words = new List <string>();

            //List<string> words = _licensePlateDetector.DetectLicensePlate(
            //   image,
            //   licensePlateImagesList,
            //   filteredLicensePlateImagesList,
            //   licenseBoxList, 6, 12);

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

            //panel1.Controls.Clear();
            System.Drawing.Point startPoint = new System.Drawing.Point(10, 10);
            for (int i = 0; i < words.Count; i++)
            {
                Mat dest = new Mat();
                CvInvoke.VConcat(licensePlateImagesList[i], filteredLicensePlateImagesList[i], dest);
                AddLabelAndImage(
                    ref startPoint,
                    String.Format("License: {0}", words[i]),
                    dest);
                PointF[] verticesF = licenseBoxList[i].GetVertices();
                System.Drawing.Point[] vertices = Array.ConvertAll(verticesF, System.Drawing.Point.Round);
                using (VectorOfPoint pts = new VectorOfPoint(vertices))
                    CvInvoke.Polylines(image, pts, true, new Bgr(System.Drawing.Color.Red).MCvScalar, 2);
            }
        }
        /*
         * private static void 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))
         *      using (System.Net.WebClient webclient = new System.Net.WebClient())
         *      {
         *          String source =
         *              String.Format("https://github.com/tesseract-ocr/tessdata/blob/4592b8d453889181e01982d22328b5846765eaad/{0}.traineddata?raw=true", lang);
         *
         *          Console.WriteLine(String.Format("Downloading file from '{0}' to '{1}'", source, dest));
         *          webclient.DownloadFile(source, dest);
         *          Console.WriteLine(String.Format("Download completed"));
         *      }
         * }*/

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            RootElement root = Root;

            root.UnevenRows = true;
            UIImageView   imageView      = new UIImageView(View.Frame);
            StringElement messageElement = new StringElement("");
            StringElement licenseElement = new StringElement("");

            root.Add(new Section()
            {
                new StyledStringElement("Process", delegate
                {
                    String path = "tessdata";

                    messageElement.Value = String.Format("Checking Tesseract language data...");
                    try
                    {
                        LicensePlateDetector.TesseractDownloadLangFile(path, "eng");
                        LicensePlateDetector.TesseractDownloadLangFile(path, "osd");
                    }
                    catch (System.Net.WebException)
                    {
                        messageElement.Value =
                            String.Format(
                                "Failed to download Tesseract language file, please check internet connection.");
                        return;
                    }
                    catch (Exception e)
                    {
                        messageElement.Value = e.Message;
                        return;
                    }
                    messageElement.Value = String.Format("Checking Tesseract language data...");

                    using (Image <Bgr, Byte> image = new Image <Bgr, byte>("license-plate.jpg"))
                    {
                        LicensePlateDetector detector = new LicensePlateDetector(path);
                        Stopwatch watch = Stopwatch.StartNew(); // time the detection process

                        List <IInputOutputArray> licensePlateImagesList         = new List <IInputOutputArray>();
                        List <IInputOutputArray> filteredLicensePlateImagesList = new List <IInputOutputArray>();
                        List <RotatedRect> licenseBoxList = new List <RotatedRect>();
                        List <string> words = detector.DetectLicensePlate(
                            image,
                            licensePlateImagesList,
                            filteredLicensePlateImagesList,
                            licenseBoxList);

                        watch.Stop(); //stop the timer
                        messageElement.Value = String.Format("{0} milli-seconds", watch.Elapsed.TotalMilliseconds);

                        StringBuilder builder = new StringBuilder();
                        foreach (String w in words)
                        {
                            builder.AppendFormat("{0} ", w);
                        }
                        licenseElement.Value = builder.ToString();

                        messageElement.GetImmediateRootElement().Reload(messageElement, UITableViewRowAnimation.Automatic);
                        licenseElement.GetImmediateRootElement().Reload(licenseElement, UITableViewRowAnimation.Automatic);
                        foreach (RotatedRect box in licenseBoxList)
                        {
                            image.Draw(box, new Bgr(Color.Red), 2);
                        }
                        Size frameSize = FrameSize;
                        using (Mat resized = new Mat())
                        {
                            CvInvoke.ResizeForFrame(image, resized, frameSize);
                            imageView.Image = resized.ToUIImage();
                            imageView.Frame = new RectangleF(PointF.Empty, resized.Size);
                        }
                        imageView.SetNeedsDisplay();
                        ReloadData();
                    }
                }
                                        )
            });
            root.Add(new Section("Recognition Time")
            {
                messageElement
            });
            root.Add(new Section("License Plate")
            {
                licenseElement
            });
            root.Add(new Section()
            {
                imageView
            });
        }
Example #7
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            RootElement root = Root;

            root.UnevenRows = true;
            UIImageView   imageView      = new UIImageView(View.Frame);
            StringElement messageElement = new StringElement("");
            StringElement licenseElement = new StringElement("");

            root.Add(new Section()
            {
                new StyledStringElement("Process", delegate {
                    using (Image <Bgr, Byte> image = new Image <Bgr, Byte>("license-plate.jpg"))
                    {
                        LicensePlateDetector detector = new LicensePlateDetector(".");
                        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 <RotatedRect> licenseBoxList = new List <RotatedRect>();
                        List <string> words = detector.DetectLicensePlate(
                            image,
                            licensePlateImagesList,
                            filteredLicensePlateImagesList,
                            licenseBoxList);

                        watch.Stop(); //stop the timer
                        messageElement.Value = String.Format("{0} milli-seconds", watch.Elapsed.TotalMilliseconds);

                        StringBuilder builder = new StringBuilder();
                        foreach (String w in words)
                        {
                            builder.AppendFormat("{0} ", w);
                        }
                        licenseElement.Value = builder.ToString();

                        messageElement.GetImmediateRootElement().Reload(messageElement, UITableViewRowAnimation.Automatic);
                        licenseElement.GetImmediateRootElement().Reload(licenseElement, UITableViewRowAnimation.Automatic);
                        foreach (RotatedRect box in licenseBoxList)
                        {
                            image.Draw(box, new Bgr(Color.Red), 2);
                        }
                        Size frameSize = FrameSize;
                        using (Image <Bgr, byte> resized = image.Resize(frameSize.Width, frameSize.Height, Emgu.CV.CvEnum.Inter.Cubic, true))
                        {
                            imageView.Image = resized.ToUIImage();
                            imageView.Frame = new RectangleF(PointF.Empty, resized.Size);
                        }
                        imageView.SetNeedsDisplay();
                        ReloadData();
                    }
                }
                                        )
            });
            root.Add(new Section("Recognition Time")
            {
                messageElement
            });
            root.Add(new Section("License Plate")
            {
                licenseElement
            });
            root.Add(new Section()
            {
                imageView
            });
        }
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            OnImagePicked += (sender, image) =>
            {
                if (image == null)
                {
                    return;
                }

                ISharedPreferences preference = PreferenceManager.GetDefaultSharedPreferences(ApplicationContext);
                String             appVersion = PackageManager.GetPackageInfo(PackageName, Android.Content.PM.PackageInfoFlags.Activities).VersionName;
                if (!preference.Contains("tesseract-data-version") || !preference.GetString("tesseract-data-version", null).Equals(appVersion) ||
                    !preference.Contains("tesseract-data-path"))
                {
                    AndroidFileAsset.OverwriteMethod overwriteMethod = AndroidFileAsset.OverwriteMethod.AlwaysOverwrite;

                    FileInfo a8 = AndroidFileAsset.WritePermanantFileAsset(this, "tessdata/eng.traineddata", "tmp", overwriteMethod);
                    FileInfo a0 = AndroidFileAsset.WritePermanantFileAsset(this, "tessdata/eng.cube.bigrams", "tmp", overwriteMethod);
                    FileInfo a1 = AndroidFileAsset.WritePermanantFileAsset(this, "tessdata/eng.cube.fold", "tmp", overwriteMethod);
                    FileInfo a2 = AndroidFileAsset.WritePermanantFileAsset(this, "tessdata/eng.cube.lm", "tmp", overwriteMethod);
                    FileInfo a3 = AndroidFileAsset.WritePermanantFileAsset(this, "tessdata/eng.cube.nn", "tmp", overwriteMethod);
                    FileInfo a4 = AndroidFileAsset.WritePermanantFileAsset(this, "tessdata/eng.cube.params", "tmp", overwriteMethod);
                    FileInfo a5 = AndroidFileAsset.WritePermanantFileAsset(this, "tessdata/eng.cube.size", "tmp", overwriteMethod);
                    FileInfo a6 = AndroidFileAsset.WritePermanantFileAsset(this, "tessdata/eng.cube.word-freq", "tmp", overwriteMethod);
                    FileInfo a7 = AndroidFileAsset.WritePermanantFileAsset(this, "tessdata/eng.tesseract_cube.nn", "tmp", overwriteMethod);

                    //save tesseract data path
                    ISharedPreferencesEditor editor = preference.Edit();
                    editor.PutString("tesseract-data-version", appVersion);
                    editor.PutString("tesseract-data-path", System.IO.Path.Combine(a0.DirectoryName, "..") + System.IO.Path.DirectorySeparatorChar);
                    editor.Commit();
                }

                LicensePlateDetector detector = new LicensePlateDetector(preference.GetString("tesseract-data-path", null));
                Stopwatch            watch    = Stopwatch.StartNew(); // time the detection process

                List <IInputOutputArray> licensePlateImagesList         = new List <IInputOutputArray>();
                List <IInputOutputArray> filteredLicensePlateImagesList = new List <IInputOutputArray>();
                List <RotatedRect>       licenseBoxList = new List <RotatedRect>();
                List <string>            words          = detector.DetectLicensePlate(
                    image,
                    licensePlateImagesList,
                    filteredLicensePlateImagesList,
                    licenseBoxList);

                watch.Stop(); //stop the timer

                StringBuilder builder = new StringBuilder();
                builder.Append(String.Format("{0} milli-seconds. ", watch.Elapsed.TotalMilliseconds));
                foreach (String w in words)
                {
                    builder.AppendFormat("{0} ", w);
                }
                SetMessage(builder.ToString());

                foreach (RotatedRect box in licenseBoxList)
                {
                    Rectangle rect = box.MinAreaRect();
                    CvInvoke.Rectangle(image, rect, new Bgr(System.Drawing.Color.Red).MCvScalar, 2);
                }

                SetImageBitmap(image.ToBitmap());
                image.Dispose();
            };


            OnButtonClick += delegate
            {
                PickImage("license-plate.jpg");
            };
        }
Example #9
0
        /*
         * private static void 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))
         *      using (System.Net.WebClient webclient = new System.Net.WebClient())
         *      {
         *          String source = Emgu.CV.OCR.Tesseract.GetLangFileUrl(lang);
         *
         *          Console.WriteLine(String.Format("Downloading file from '{0}' to '{1}'", source, dest));
         *          webclient.DownloadFile(source, dest);
         *          Console.WriteLine(String.Format("Download completed"));
         *      }
         * }*/

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            OnImagePicked += (sender, image) =>
            {
                if (image == null)
                {
                    return;
                }

                String path = System.IO.Path.Combine(this.FilesDir.AbsolutePath, "tessdata");

                try
                {
                    SetProgressMessage("Checking Tesseract Lang files...");
                    LicensePlateDetector.TesseractDownloadLangFile(path, "eng");
                    LicensePlateDetector.TesseractDownloadLangFile(path, "osd");
                    SetProgressMessage("Please wait ...");
                }
                catch (System.Net.WebException e)
                {
                    SetMessage("Unable to download tesseract language file from Internet, please check your Internet connection.");
                    Console.WriteLine(e);
                    return;
                }
                catch (Exception e)
                {
                    SetMessage(e.ToString());
                    Console.WriteLine(e);
                    return;
                }


                LicensePlateDetector detector = new LicensePlateDetector(path + System.IO.Path.DirectorySeparatorChar);


                Stopwatch watch = Stopwatch.StartNew(); // time the detection process

                List <IInputOutputArray> licensePlateImagesList         = new List <IInputOutputArray>();
                List <IInputOutputArray> filteredLicensePlateImagesList = new List <IInputOutputArray>();
                List <RotatedRect>       licenseBoxList = new List <RotatedRect>();
                List <string>            words          = detector.DetectLicensePlate(
                    image,
                    licensePlateImagesList,
                    filteredLicensePlateImagesList,
                    licenseBoxList);

                watch.Stop(); //stop the timer

                StringBuilder builder = new StringBuilder();
                builder.Append(String.Format("{0} milli-seconds. ", watch.Elapsed.TotalMilliseconds));
                foreach (String w in words)
                {
                    builder.AppendFormat("{0} ", w);
                }
                SetMessage(builder.ToString());

                foreach (RotatedRect box in licenseBoxList)
                {
                    Rectangle rect = box.MinAreaRect();
                    CvInvoke.Rectangle(image, rect, new Bgr(System.Drawing.Color.Red).MCvScalar, 2);
                }

                SetImageBitmap(image.ToBitmap());
                image.Dispose();
            };


            OnButtonClick += delegate
            {
                PickImage("license-plate.jpg");
            };
        }
Example #10
0
        public void LicensePlateDetectorTest()
        {
            var objectTest = new LicensePlateDetector(@"D:\Downloads\LicencePlateRecognition\GUI-Prototype\testData\image002.png");

            objectTest.DetectLicensePlate()
        }