private void _processTimer_Tick(object sender, EventArgs e)
        {
            if (SourceMediaElement.Position.Seconds < 1)
            {
                return;
            }



            var w = SourceMediaElement.ActualWidth;
            var h = SourceMediaElement.ActualHeight;

            System.Drawing.Size dpi = new System.Drawing.Size(96, 96);

            RenderTargetBitmap bmp = new RenderTargetBitmap((int)w, (int)h, dpi.Width, dpi.Height, PixelFormats.Pbgra32);

            bmp.Render(SourceMediaElement);


            MemoryStream  stream  = new MemoryStream();
            BitmapEncoder encoder = new BmpBitmapEncoder();

            encoder.Frames.Add(BitmapFrame.Create(bmp));
            encoder.Save(stream);

            Bitmap bitmap = new Bitmap(stream);

            // try to recognize image
            AlprResultsNet result = _process.Recognize(bitmap);

            foreach (var plate in result.Plates)
            {
                if (plate.BestPlate.Characters.Length >= 7)
                {
                    if (plate.BestPlate.OverallConfidence > Confidence)
                    {
                        ResultsList.Add(new ResultViewModel(plate.BestPlate.Characters, SourceMediaElement.Position, plate.BestPlate.OverallConfidence, true));
                        LastRecognizedPlate = plate.BestPlate.Characters;
                    }
                    else
                    {
                        ResultsList.Add(new ResultViewModel(plate.BestPlate.Characters, SourceMediaElement.Position, plate.BestPlate.OverallConfidence));
                    }


                    _lineTimer.Start();
                }

                SetFirstLines(plate);
                SetSecondLines(plate);
                SetThirdLines(plate);
                SetFourthLines(plate);
            }
        }
        private void Regconize()
        {
            var alpr = new AlprNet("us", "openalpr.conf.defaults", "runtime_data");

            if (!alpr.IsLoaded())
            {
                Console.WriteLine("OpenAlpr failed to load!");
                return;
            }
            // Optionally apply pattern matching for a particular region
            alpr.DefaultRegion = "vn2";

            AlprResultsNet results = alpr.Recognize(GetBitmap(LicencePlate));
            int            i       = 0;

            foreach (var result in results.Plates)
            {
                Console.WriteLine("Plate {0}: {1} result(s)", i++, result.TopNPlates.Count);
                Console.WriteLine("  Processing Time: {0} msec(s)", result.ProcessingTimeMs);
                foreach (var plate in result.TopNPlates)
                {
                    Console.WriteLine("  - {0}\t Confidence: {1}\tMatches Template: {2}", plate.Characters,
                                      plate.OverallConfidence, plate.MatchesTemplate);
                }
            }


            //var alpr2 = new AlprNet("us", "openalpr.conf", "runtime_data");
            //if (!alpr2.IsLoaded())
            //{
            //    Console.WriteLine("OpenAlpr failed to load!");
            //    return;
            //}
            //// Optionally apply pattern matching for a particular region
            //alpr2.DefaultRegion = "vn";

            ////var results2 = alpr2.Recognize("123.jpg");
            //var results2 = alpr2.Recognize("77.jpg");
            //i = 0;
            //foreach (var result in results2.Plates)
            //{
            //    Console.WriteLine("Plate {0}: {1} result(s)", i++, result.TopNPlates.Count);
            //    Console.WriteLine("  Processing Time: {0} msec(s)", result.ProcessingTimeMs);
            //    foreach (var plate in result.TopNPlates)
            //    {
            //        Console.WriteLine("  - {0}\t Confidence: {1}\tMatches Template: {2}", plate.Characters,
            //                          plate.OverallConfidence, plate.MatchesTemplate);
            //    }
            //}
        }
            private void ExitExaminer()
            {
                AlprNet alpr = new AlprNet("us", "openalpr.conf", "runtime_dir");

                alpr.TopN = 1;
                HashSet <string> recognized = new HashSet <string>();
                Stopwatch        bar        = null;

                while (true)
                {
                    Bitmap bitmap;
                    lock (_lock)
                    {
                        if (_kill)
                        {
                            break;
                        }
                    }
                    if (bar != null && bar.IsRunning)
                    {
                        if (bar.ElapsedMilliseconds < 10000)
                        {
                            continue;
                        }
                        else
                        {
                            bar.Stop();
                            _framesQueue.TryDequeue(out bitmap);
                            _WriteLog("Bar closed", _name);
                        }
                    }
                    if (_framesQueue.TryDequeue(out bitmap))
                    {
                        bool           left    = false;
                        AlprResultsNet results = alpr.Recognize(bitmap);
                        if (results.Plates.Count != 0)
                        {
                            AlprPlateNet plate = results.Plates[0].BestPlate;
                            if (recognized.Add(plate.Characters))
                            {
                                _WriteLog(plate.Characters, _name);
                                try
                                {
                                    Api <HistoryLast> data = ("http://api.ipark.altervista.org/history/" + plate.Characters + "/last").SetQueryParams(new { token = _token }).GetJsonAsync <Api <HistoryLast> >().Result;
                                    if (data.meta.code == 200)
                                    {
                                        if (data.response.payment == null)
                                        {
                                            _WriteLog(plate.Characters + " has not paid yet", _name, LogType.WARNING);
                                            recognized.Remove(recognized.Last());
                                        }
                                        else
                                        {
                                            Api <Registered> internalData = ("http://api.ipark.altervista.org/history/register/exit/" + data.response.plate).SetQueryParams(new { token = _token }).PutJsonAsync(null).ReceiveJson <Api <Registered> >().Result;
                                            if (internalData.meta.code == 200)
                                            {
                                                _UpdatePie(UpdateType.SUB);
                                                _LogResult(internalData.response, _name);
                                                left = true;
                                            }
                                        }
                                    }
                                }
                                catch (Exception exception)
                                {
                                    _WriteLog(exception.ToString(), _name, LogType.ERROR);
                                }
                            }
                        }
                        if (left)
                        {
                            recognized.Clear();
                            bar = new Stopwatch();
                            bar.Start();
                            _WriteLog("Bar raised", _name);
                        }
                    }
                }
            }
示例#4
0
        public Boolean RecognizeNumber(string path)
        {
            try
            {
                double maxVehicle = Convert.ToDouble(ConfigurationManager.AppSettings["MaxVehicle"]);
                int    maxResult  = Convert.ToInt32(ConfigurationManager.AppSettings["MaxResult"]);


                string confPath    = AppDomain.CurrentDomain.BaseDirectory + @"\openalpr.conf";
                string runtimePath = AppDomain.CurrentDomain.BaseDirectory + @"\runtime_date";

                AlprNet alpr = new AlprNet("eu", confPath, runtimePath);

                if (!alpr.IsLoaded())
                {
                    throw new System.Exception("Error loading openalpr library");
                }

                alpr.DetectRegion = true;
                alpr.TopN         = maxResult;

                AlprResultsNet results = alpr.Recognize(path);

                if (images != null)
                {
                    images = null;
                }

                if (vehicleResult != null)
                {
                    vehicleResult = null;
                }

                images        = new List <Image>(results.Plates.Count);
                vehicleResult = new List <VehicleResult>();



                Int16 count     = 1;
                Int16 listIndex = 0;

                if (results.Plates.Count > 0)
                {
                    foreach (AlprPlateResultNet result in results.Plates)
                    {
                        if (count > maxVehicle)
                        {
                            break;
                        }

                        Rectangle rect    = BoundingRectangle(result.PlatePoints);
                        Image     img     = Image.FromFile(path);
                        Image     cropped = CropImage(img, rect);
                        cropped = ResizeImage(cropped);
                        images.Add(cropped);

                        string filePath = ConfigurationManager.AppSettings["CSVPath"];


                        foreach (AlprPlateNet plate in result.TopNPlates)
                        {
                            vehicleResult.Add(new VehicleResult(plate.Characters, listIndex, count--));

                            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(filePath, true))
                            {
                                sw.WriteLine(string.Format(string.Join(",", plate.Characters, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"))));
                            }

                            //Console.WriteLine(plate.Characters);

                            listIndex++;
                        }

                        count++;
                    }

                    return(true);
                }
                else
                {
                    return(false);
                }
                //System.IO.File.Delete(path);
            }
            catch (Exception ex) {
                throw ex;
            }

            return(false);
        }