Esempio n. 1
0
    private void Awake()
    {
        if (_instance != null)
        {
            Destroy(_instance.gameObject);
        }

        _instance = this;

        _player = FindObjectOfType <PlayerBehavior>();
        if (_player == null)
        {
            Debug.LogError("No Player Found In The Scene.");
            return;
        }

        _timeMeter = FindObjectOfType <TimeMeter>();
        if (_timeMeter == null)
        {
            Debug.LogError("No Time Meter Found In The Scene.");
            return;
        }

        _levelManager = FindObjectOfType <LevelManager>();
        if (_levelManager == null)
        {
            Debug.LogError("No Level Manager Found In The Scene.");
        }

        #if !UNITY_EDITOR
        infiniteXp = false;
        #endif
    }
Esempio n. 2
0
        public IEnumerator CancelTaskRoutineShouldCancelTaskYetTest()
        {
            bool?cancellationExit = null;

            Routine.Task(token =>
            {
                var timeMeter = TimeMeter.New();
                while (timeMeter.ToSeconds() < 1)
                {
                    Thread.Sleep(15);
                    if (token.IsCancellationRequested)
                    {
                        cancellationExit = true;
                        return;
                    }
                }

                cancellationExit = false;
            }).Self(out var taskRoutine).InBackground();

            yield return(Routine.Delay(0.1f));

            taskRoutine.Cancel();

            // Отклик на отмену задачи ~15 мс ( Thread.Sleep(15) ), поэтому нужно немного подождать
            yield return(Routine.Delay(0.03f));

            Assert.NotNull(cancellationExit);
            Assert.True(cancellationExit);
        }
Esempio n. 3
0
        public bool IsInk(TimeMeter meter)
        {
            meter.Start("Image converting");
            bool[,] bwPixels = PrepareBwPixels();

            meter.Start("Image analysis");
            return FindInk(bwPixels);
        }
Esempio n. 4
0
        public ArtCamBitmap GetImageAnalyzer(TimeMeter meter, bool shrink)
        {
            meter.Start("Image resizing");

            int ratioX = (bitmap.Rect.Width > 400) ? (bitmap.Rect.Width + 200) / 400 : 1;
            int ratioY = (bitmap.Rect.Height > 400) ? (bitmap.Rect.Height + 200) / 400 : 1;
            int width = bitmap.Rect.Width / ratioX;
            int height = bitmap.Rect.Height / ratioY;

            ArtCamBitmap convertBmp;

            if (((ratioX > 1) || (ratioY > 1)) && shrink)
            {
                convertBmp = new ArtCamBitmap(new Rectangle(0, 0, width, height), bitmap.Bitmap.PixelFormat, null);

                int bytesPerPixel = 1;
                switch (bitmap.Bitmap.PixelFormat)
                {
                    case PixelFormat.Format24bppRgb:
                        bytesPerPixel = 3;
                        break;
                    case PixelFormat.Format32bppArgb:
                        bytesPerPixel = 4;
                        break;
                }

                int[] sum = new int[bytesPerPixel];

                for (int i = 0; i < width; i++)
                    for (int j = 0; j < height; j++)
                    {
                        for (int b = 0; b < bytesPerPixel; b++)
                            sum[b] = 0;

                        int idx = 0;
                        int cnt = 0;
                        for (int x = i * ratioX; x < Math.Min(bitmap.Rect.Width, (i + 1) * ratioX); x++)
                            for (int y = j * ratioY; y < Math.Min(bitmap.Rect.Height, (j + 1) * ratioY); y++)
                            {
                                idx = y * bitmap.Rect.Width * bytesPerPixel + x * bytesPerPixel;
                                for (int b = 0; b < bytesPerPixel; b++)
                                    sum[b] += bitmap.Pixels[idx + b];
                                cnt++;
                            }
                        idx = j * width * bytesPerPixel + i * bytesPerPixel;
                        for (int b = 0; b < bytesPerPixel; b++)
                            convertBmp.Pixels[idx + b] = (byte)(sum[b] / cnt);
                    }
            }
            else
                convertBmp = bitmap;

            return convertBmp;
        }
Esempio n. 5
0
        public IEnumerator CancelDelayRoutineShouldBeCancelTest()
        {
            var timeMeter = TimeMeter.New();
            var routine   = Routine.Delay(1);

            routine.Cancel();
            yield return(routine);

            var elapsed = timeMeter.ToSeconds();

            Assert.Less(elapsed, 1f);
        }
Esempio n. 6
0
        public void Snapshot(TimeMeter meter)
        {
            if (helper == null)
                throw new NullReferenceException("helper can't be null.");

            if ((helper.Width != rect.Width) || (helper.Height != rect.Height))
            {
                meter.Start("Library init");

                helper.Close();

                helper.SetPreviewWindow(System.IntPtr.Zero, 0, 0, 0, 0);

                helper.SetCaptureWindow(3488, rect.Left, rect.Width, 2616, rect.Top, rect.Height);
            }

            meter.Start("Snapshot");
            helper.Snapshot(pixels);
        }
Esempio n. 7
0
        public void UpdateBitmap(TimeMeter meter)
        {
            if ((bitmap == null) || (pixels == null))
                return;
            if (meter != null)
            meter.Start("Bitmap update");

            BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, rect.Width, rect.Height), ImageLockMode.ReadWrite, pixelFormat);

            if (stride != bmpData.Stride)
            {
                bitmap.UnlockBits(bmpData);
                throw new ApplicationException("Unsupported format, different stride value...");
            }

            IntPtr pdest = bmpData.Scan0;
            int bytes = bmpData.Stride * bitmap.Height;
            byte[] values = new byte[bytes];

            System.Runtime.InteropServices.Marshal.Copy(pdest, values, 0, bytes);

            int size = Math.Min(bytes, pixels.Length);
            for (int i = 0; i < size; i++)
                values[i] = pixels[i];

            System.Runtime.InteropServices.Marshal.Copy(values, 0, pdest, bytes);

            bitmap.UnlockBits(bmpData);

            if (BitmapUpdated != null)
            {
                EventHandler bmp = BitmapUpdated;
                bmp(this, EventArgs.Empty);
            }
        }
Esempio n. 8
0
        public string Recognize(CarSnapshot carSnapshot)
        {
            TimeMeter timeMeter          = new TimeMeter();
            int       syntaxAnalysisMode = Configurator.GetIntProperty("intelligence_syntaxanalysis");
            int       skewDetectionMode  = Configurator.GetIntProperty("intelligence_skewdetection");

            if (EnableReportGeneration)
            {
                Program.ReportGenerator.InsertText("<h1>Automatic Number Plate Recognition Report</h1>");
                Program.ReportGenerator.InsertText("<span>Image width: " + carSnapshot.GetWidth() + " px</span>");
                Program.ReportGenerator.InsertText("<span>Image height: " + carSnapshot.GetHeight() + " px</span>");

                Program.ReportGenerator.InsertText("<h2>Vertical and Horizontal plate projection</h2>");

                Program.ReportGenerator.InsertImage(carSnapshot.RenderGraph(), "snapshotgraph", 0, 0);
                Program.ReportGenerator.InsertImage(carSnapshot.GetBitmapWithAxes(), "snapshot", 0, 0);
            }

            foreach (Band band in carSnapshot.GetBands())
            {
                if (EnableReportGeneration)
                {
                    Program.ReportGenerator.InsertText("<div class='bandtxt'><h4>Band<br></h4>");
                    Program.ReportGenerator.InsertImage(band.GetBitmap(), "bandsmall", 250, 30);
                    Program.ReportGenerator.InsertText("<span>Band width : " + band.GetWidth() + " px</span>");
                    Program.ReportGenerator.InsertText("<span>Band height : " + band.GetHeight() + " px</span>");
                    Program.ReportGenerator.InsertText("</div>");
                }

                foreach (Plate plate in band.GetPlates())
                {
                    var _plate = plate;
                    if (EnableReportGeneration)
                    {
                        Program.ReportGenerator.InsertText("<div class='_platetxt'><h4>Plate<br></h4>");
                        Program.ReportGenerator.InsertImage(_plate.GetBitmap(), "_platesmall", 120, 30);
                        Program.ReportGenerator.InsertText("<span>Plate width : " + _plate.GetWidth() + " px</span>");
                        Program.ReportGenerator.InsertText("<span>Plate height : " + _plate.GetHeight() + " px</span>");
                        Program.ReportGenerator.InsertText("</div>");
                    }


                    Plate  notNormalizedCopy      = null;
                    Bitmap renderedHoughTransform = null;
                    HoughTransformation hough     = null;
                    if (EnableReportGeneration || skewDetectionMode != 0)
                    {
                        notNormalizedCopy = _plate.Clone();
                        notNormalizedCopy.HorizontalEdgeDetector(notNormalizedCopy.GetBitmap());
                        hough = notNormalizedCopy.GetHoughTransformation();
                        renderedHoughTransform =
                            hough.Render(HoughTransformation.RenderAll, HoughTransformation.ColorBw);
                    }
                    if (skewDetectionMode != 0)
                    {
                        Matrix matrix = new Matrix();
                        matrix.Shear(0f, -hough.Dy / hough.Dx);
                        Bitmap   core     = _plate.CreateBlankBitmap(_plate.GetBitmap());
                        Graphics graphics = Graphics.FromImage(core);
                        graphics.Transform = matrix;
                        graphics.DrawImage(_plate.GetBitmap(), core.Height, core.Width);
                        _plate = new Plate(core);
                    }

                    _plate.Normalize();

                    float plateWHratio = _plate.GetWidth() / (float)_plate.GetHeight();
                    if (plateWHratio < Configurator.GetDoubleProperty("intelligence_minPlateWidthHeightRatio") ||
                        plateWHratio > Configurator.GetDoubleProperty("intelligence_maxPlateWidthHeightRatio")
                        )
                    {
                        continue;
                    }

                    List <Character> chars = _plate.GetChars();

                    if (chars.Count < Configurator.GetIntProperty("intelligence_minimumChars") ||
                        chars.Count > Configurator.GetIntProperty("intelligence_maximumChars")
                        )
                    {
                        continue;
                    }

                    if (_plate.GetCharsWidthDispersion(chars) >
                        Configurator.GetDoubleProperty("intelligence_maxCharWidthDispersion")
                        )
                    {
                        continue;
                    }


                    if (EnableReportGeneration)
                    {
                        Program.ReportGenerator.InsertText("<h2>Detected band</h2>");
                        Program.ReportGenerator.InsertImage(band.GetBitmapWithAxes(), "band", 0, 0);
                        Program.ReportGenerator.InsertImage(band.RenderGraph(), "bandgraph", 0, 0);
                        Program.ReportGenerator.InsertText("<h2>Detected _plate</h2>");
                        Plate plateCopy = _plate.Clone();
                        plateCopy.LinearResize(450, 90);
                        Program.ReportGenerator.InsertImage(plateCopy.GetBitmapWithAxes(), "_plate", 0, 0);
                        Program.ReportGenerator.InsertImage(plateCopy.RenderGraph(), "_plategraph", 0, 0);
                    }

                    if (EnableReportGeneration)
                    {
                        Program.ReportGenerator.InsertText("<h2>Skew detection</h2>");
                        Program.ReportGenerator.InsertImage(notNormalizedCopy.GetBitmap(), "skewimage", 0, 0);
                        Program.ReportGenerator.InsertImage(renderedHoughTransform, "skewtransform", 0, 0);
                        Program.ReportGenerator.InsertText("Detected skew angle : <b>" + hough.Angle + "</b>");
                    }


                    RecognizedPlate recognizedPlate = new RecognizedPlate();

                    if (EnableReportGeneration)
                    {
                        Program.ReportGenerator.InsertText("<h2>Character segmentation</h2>");
                        Program.ReportGenerator.InsertText("<div class='charsegment'>");
                        foreach (Character chr in chars)
                        {
                            Program.ReportGenerator.InsertImage(Photo.LinearResizeBitmap(chr.GetBitmap(), 70, 100), "",
                                                                0, 0);
                        }
                        Program.ReportGenerator.InsertText("</div>");
                    }

                    foreach (Character chr in chars)
                    {
                        chr.Normalize();
                    }

                    float averageHeight     = _plate.GetAveragePieceHeight(chars);
                    float averageContrast   = _plate.GetAveragePieceContrast(chars);
                    float averageBrightness = _plate.GetAveragePieceBrightness(chars);
                    float averageHue        = _plate.GetAveragePieceHue(chars);
                    float averageSaturation = _plate.GetAveragePieceSaturation(chars);

                    foreach (Character chr in chars)
                    {
                        bool   ok         = true;
                        string errorFlags = "";

                        float widthHeightRatio = chr.PieceWidth;
                        widthHeightRatio /= chr.PieceHeight;

                        if (widthHeightRatio < Configurator.GetDoubleProperty("intelligence_minCharWidthHeightRatio") ||
                            widthHeightRatio > Configurator.GetDoubleProperty("intelligence_maxCharWidthHeightRatio")
                            )
                        {
                            errorFlags += "WHR ";
                            ok          = false;
                            if (!EnableReportGeneration)
                            {
                                continue;
                            }
                        }


                        if ((chr.PositionInPlate.X1 < 2 ||
                             chr.PositionInPlate.X2 > _plate.GetWidth() - 1) &&
                            widthHeightRatio < 0.12
                            )
                        {
                            errorFlags += "POS ";
                            ok          = false;
                            if (!EnableReportGeneration)
                            {
                                continue;
                            }
                        }


                        float contrastCost   = Math.Abs(chr.StatisticContrast - averageContrast);
                        float brightnessCost = Math.Abs(chr.StatisticAverageBrightness - averageBrightness);
                        float hueCost        = Math.Abs(chr.StatisticAverageHue - averageHue);
                        float saturationCost = Math.Abs(chr.StatisticAverageSaturation - averageSaturation);
                        float heightCost     = (chr.PieceHeight - averageHeight) / averageHeight;

                        if (brightnessCost > Configurator.GetDoubleProperty("intelligence_maxBrightnessCostDispersion"))
                        {
                            errorFlags += "BRI ";
                            ok          = false;
                            if (!EnableReportGeneration)
                            {
                                continue;
                            }
                        }
                        if (contrastCost > Configurator.GetDoubleProperty("intelligence_maxContrastCostDispersion"))
                        {
                            errorFlags += "CON ";
                            ok          = false;
                            if (!EnableReportGeneration)
                            {
                                continue;
                            }
                        }
                        if (hueCost > Configurator.GetDoubleProperty("intelligence_maxHueCostDispersion"))
                        {
                            errorFlags += "HUE ";
                            ok          = false;
                            if (!EnableReportGeneration)
                            {
                                continue;
                            }
                        }
                        if (saturationCost > Configurator.GetDoubleProperty("intelligence_maxSaturationCostDispersion"))
                        {
                            errorFlags += "SAT ";
                            ok          = false;
                            if (!EnableReportGeneration)
                            {
                                continue;
                            }
                        }
                        if (heightCost < -Configurator.GetDoubleProperty("intelligence_maxHeightCostDispersion"))
                        {
                            errorFlags += "HEI ";
                            ok          = false;
                            if (!EnableReportGeneration)
                            {
                                continue;
                            }
                        }

                        float similarityCost = 0;
                        CharacterRecognizer.RecognizedChar rc = null;
                        if (ok)
                        {
                            rc             = ChrRecog.Recognize(chr);
                            similarityCost = rc.GetPatterns()[0].Cost;
                            if (similarityCost >
                                Configurator.GetDoubleProperty("intelligence_maxSimilarityCostDispersion"))
                            {
                                errorFlags += "NEU ";
                                ok          = false;
                                if (!EnableReportGeneration)
                                {
                                    continue;
                                }
                            }
                        }

                        if (ok)
                        {
                            recognizedPlate.AddChar(rc);
                        }

                        if (EnableReportGeneration)
                        {
                            Program.ReportGenerator.InsertText("<div class='heuristictable'>");
                            Program.ReportGenerator.InsertImage(
                                Photo.LinearResizeBitmap(chr.GetBitmap(), chr.GetWidth() * 2, chr.GetHeight() * 2),
                                "skeleton", 0, 0);
                            Program.ReportGenerator.InsertText(
                                "<span class='name'>WHR</span><span class='value'>" + widthHeightRatio + "</span>");
                            Program.ReportGenerator.InsertText(
                                "<span class='name'>HEI</span><span class='value'>" + heightCost + "</span>");
                            Program.ReportGenerator.InsertText(
                                "<span class='name'>NEU</span><span class='value'>" + similarityCost + "</span>");
                            Program.ReportGenerator.InsertText(
                                "<span class='name'>CON</span><span class='value'>" + contrastCost + "</span>");
                            Program.ReportGenerator.InsertText(
                                "<span class='name'>BRI</span><span class='value'>" + brightnessCost + "</span>");
                            Program.ReportGenerator.InsertText(
                                "<span class='name'>HUE</span><span class='value'>" + hueCost + "</span>");
                            Program.ReportGenerator.InsertText(
                                "<span class='name'>SAT</span><span class='value'>" + saturationCost + "</span>");
                            Program.ReportGenerator.InsertText("</table>");
                            if (errorFlags.Length != 0)
                            {
                                Program.ReportGenerator.InsertText("<span class='errflags'>" + errorFlags + "</span>");
                            }
                            Program.ReportGenerator.InsertText("</div>");
                        }
                    }

                    if (recognizedPlate.Chars.Count <
                        Configurator.GetIntProperty("intelligence_minimumChars"))
                    {
                        continue;
                    }

                    _lastProcessDuration = timeMeter.GetTime();
                    string parsedOutput = Parser.Parse(recognizedPlate, syntaxAnalysisMode);

                    if (EnableReportGeneration)
                    {
                        Program.ReportGenerator.InsertText("<span class='recognized'>");
                        Program.ReportGenerator.InsertText("Recognized _plate : " + parsedOutput);
                        Program.ReportGenerator.InsertText("</span>");
                    }

                    return(parsedOutput);
                }
            }

            _lastProcessDuration = timeMeter.GetTime();
            return(null);
        }
Esempio n. 9
0
 private static Common.Node CreateNodeRecord(string resourceName, TimeMeter.Integrator.NodeInfo info)
 {
     var res = new Common.Node();
     res.ResourceName = resourceName;
     res.DNSName = info.DNSName;
     res.CoresAvailable = info.NumberOfCores;
     res.CoresTotal = info.NumberOfCores;
     res.Parameters[SummaryNodePerformance.CoresPerformance] = JsonConvert.SerializeObject(info.FrequencyOfCores.ToArray());
     return res;
 }