示例#1
0
        //IntPtr pd = IntPtr.Zero;

        private void ProcessFrame(VideoFrame frame)
        {
            //if (pd == IntPtr.Zero)
            //{
            //    pd = CV.PedestrianDetectorInit();
            //    CV.PedestrianDetectorSetEngineSettings(pd, 0.5, 5, true, Color.YellowGreen);
            //}

            //long time;
            //CVPedestrians items = new CVPedestrians();
            //int count = CV.PedestrianDetectorProcess(pd, frame, ref items, out time);
            //Trace.WriteLine($"Count: {count}, time: {time}");

            var image = frame.ToRAWImage();
            var faces = faceDetector?.Process(image);

            carCounter?.Process(image);
            pedestrianDetector?.Process(image);

            if (cbFDMosaic.Checked)
            {
                if (faces != null)
                {
                    foreach (var face in faces)
                    {
                        var rect = face.Position;
                        rect.Top -= 10;
                        if (rect.Top < 0)
                        {
                            rect.Top = 0;
                        }

                        rect.Left -= 10;
                        if (rect.Left < 0)
                        {
                            rect.Left = 0;
                        }

                        rect.Bottom += 10;
                        if (rect.Bottom > image.Height)
                        {
                            rect.Bottom = image.Height;
                        }

                        rect.Right += 10;
                        if (rect.Right > image.Width)
                        {
                            rect.Right = image.Width;
                        }

                        MFP.EffectMosaicROI(frame.Data, image.Width, image.Height, 45, rect);
                    }
                }
            }
        }
        private void VideoCapture1_OnVideoFrameBuffer(object sender, VideoFrameBufferEventArgs e)
        {
            try
            {
                if (_tempBuffer == IntPtr.Zero)
                {
                    _tempBuffer = Marshal.AllocCoTaskMem(e.Frame.Stride * e.Frame.Height);
                }

                // live
                if (_searchLiveData == null)
                {
                    _searchLiveData = new FingerprintLiveData((int)(_fragmentDuration / 1000), DateTime.Now);
                    _fragmentCount++;
                }

                if (e.StartTime < _fragmentDuration * _fragmentCount)
                {
                    ImageHelper.CopyMemory(_tempBuffer, e.Frame.Data, e.Frame.DataSize);

                    // process frame to remove ignored areas
                    if (_ignoredAreas.Count > 0)
                    {
                        foreach (var area in _ignoredAreas)
                        {
                            if (area.Right > e.Frame.Width || area.Bottom > e.Frame.Height)
                            {
                                continue;
                            }

                            MFP.FillColor(_tempBuffer, e.Frame.Width, e.Frame.Height, area, 0);
                        }
                    }

                    VFPSearch.Process(_tempBuffer, e.Frame.Width, e.Frame.Height, e.Frame.Stride, e.StartTime, ref _searchLiveData.Data);
                }
                else
                {
                    _fingerprintQueue.Enqueue(_searchLiveData);

                    _searchLiveData = null;

                    Dispatcher.BeginInvoke(new ProcessVideoDelegate(ProcessVideoDelegateMethod));
                }

                // overlap
                if (e.StartTime < _fragmentDuration / 2)
                {
                    return;
                }

                if (_searchLiveOverlapData == null)
                {
                    _searchLiveOverlapData = new FingerprintLiveData((int)(_fragmentDuration / 1000), DateTime.Now);
                    _overlapFragmentCount++;
                }

                if (e.StartTime < _fragmentDuration * _overlapFragmentCount + _fragmentDuration / 2)
                {
                    ImageHelper.CopyMemory(_tempBuffer, e.Frame.Data, e.Frame.DataSize);
                    VFPSearch.Process(_tempBuffer, e.Frame.Width, e.Frame.Height, e.Frame.Stride, e.StartTime, ref _searchLiveOverlapData.Data);
                }
                else
                {
                    _fingerprintQueue.Enqueue(_searchLiveOverlapData);

                    _searchLiveOverlapData = null;

                    Dispatcher.BeginInvoke(new ProcessVideoDelegate(ProcessVideoDelegateMethod));
                }
            }
            catch
            {
            }
        }
        private void VideoCapture1_OnVideoFrameBuffer(object sender, SampleGrabberBufferCBEventArgs e)
        {
            try
            {
                if (_stopFlag)
                {
                    return;
                }

                Dispatcher.BeginInvoke(new NewFrameDelegate(NewFrameDelegateMethod), e);

                if (_tempBuffer == IntPtr.Zero)
                {
                    _tempBuffer = Marshal.AllocCoTaskMem(ImageHelper.GetStrideRGB24(e.Width) * e.Height);
                }

                // live
                if (_searchLiveData == null)
                {
                    _searchLiveData = new FingerprintLiveData(TimeSpan.FromMilliseconds(_fragmentDuration), DateTime.Now);
                    _fragmentCount++;
                }

                long timestamp = (long)(e.SampleTime * 1000);
                if (timestamp < _fragmentDuration * _fragmentCount)
                {
                    ImageHelper.CopyMemory(_tempBuffer, e.Buffer, e.BufferLen);

                    // process frame to remove ignored areas
                    if (_ignoredAreas.Count > 0)
                    {
                        foreach (var area in _ignoredAreas)
                        {
                            if (area.Right > e.Width || area.Bottom > e.Height)
                            {
                                continue;
                            }

                            MFP.FillColor(_tempBuffer, e.Width, e.Height, area, 0);
                        }
                    }

                    VFPSearch.Process(_tempBuffer, e.Width, e.Height, ImageHelper.GetStrideRGB24(e.Width), TimeSpan.FromMilliseconds(timestamp), ref _searchLiveData.Data);
                }
                else
                {
                    _fingerprintQueue.Enqueue(_searchLiveData);

                    _searchLiveData = null;

                    Dispatcher.BeginInvoke(new ProcessVideoDelegate(ProcessVideoDelegateMethod));
                }

                // overlap
                if (timestamp < _fragmentDuration / 2)
                {
                    return;
                }

                if (_searchLiveOverlapData == null)
                {
                    _searchLiveOverlapData = new FingerprintLiveData(TimeSpan.FromSeconds(_fragmentDuration), DateTime.Now);
                    _overlapFragmentCount++;
                }

                if (timestamp < _fragmentDuration * _overlapFragmentCount + _fragmentDuration / 2)
                {
                    ImageHelper.CopyMemory(_tempBuffer, e.Buffer, e.BufferLen);
                    VFPSearch.Process(_tempBuffer, e.Width, e.Height, ImageHelper.GetStrideRGB24(e.Width), TimeSpan.FromMilliseconds(timestamp), ref _searchLiveOverlapData.Data);
                }
                else
                {
                    _fingerprintQueue.Enqueue(_searchLiveOverlapData);

                    _searchLiveOverlapData = null;

                    Dispatcher.BeginInvoke(new ProcessVideoDelegate(ProcessVideoDelegateMethod));
                }
            }
            catch
            {
            }
        }