AI_ 視頻監控-人體移動捕捉監測


總目錄地址:AI 系列 總目錄 

需要最新源碼,或技術提問,請加QQ群:538327407

我的各種github 開源項目和代碼:https://github.com/linbin524

 

需求

為了實現特定場景中人員監控、人臉識別的需求,針對相關技術做研究。近場的動態人臉識別已經實現;現在需要針對人距離的移動人物進行捕捉截取,確定當前場所行走的人員做收集。

實現效果:

 

技術方案

 

1、采用Emgu CV 開源框架,對人體進行動態捕捉

2、介紹攝像頭采集 識別移動人體模式

 

技術實現

 

 

 

動態截取人物

 

 

代碼:

    void ProcessFrame(object sender, EventArgs e)
        {
            Mat frame = _cameraCapture.QueryFrame();
            Mat smoothedFrame = new Mat();
            CvInvoke.GaussianBlur(frame, smoothedFrame, new Size(3, 3), 1); //filter out noises
                                                                            //frame._SmoothGaussian(3); 
           
            #region use the BG/FG detector to find the forground mask

            Mat forgroundMask = new Mat();
            _fgDetector.Apply(smoothedFrame, forgroundMask);
            #endregion

            CvBlobs blobs = new CvBlobs();
            _blobDetector.Detect(forgroundMask.ToImage<Gray, byte>(), blobs);
            blobs.FilterByArea(100, int.MaxValue);

            float scale = (frame.Width + frame.Width) / 2.0f;
            _tracker.Update(blobs, 0.01 * scale, 5, 5);

            long detectionTime;

            List<Rectangle> faces = new List<Rectangle>();
            List<Rectangle> eyes = new List<Rectangle>();

            IImage image = (IImage)frame;//這一步是重點
            faceImage = frame.Bitmap;
           

            #region 人物識別
            long processingTime;
            Rectangle[] results;

            if (CudaInvoke.HasCuda)
            {
                using (GpuMat gpuMat = new GpuMat(frame))
                    results = FindPedestrian.Find(gpuMat, out processingTime);
            }
            else
            {
                using (UMat uImage = frame.GetUMat(AccessType.ReadWrite))
                    results = FindPedestrian.Find(uImage, out processingTime);
            }

            foreach (Rectangle rect in results)
            {
                CvInvoke.Rectangle(frame, rect, new Bgr(Color.Red).MCvScalar);
            }
           
            #endregion

            imageBox1.Image = frame;
            imageBox2.Image = forgroundMask;
        }

 

 人物識別動態捕捉核心代碼:

using System;
using System.Collections.Generic;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using System.Drawing;
using System.Diagnostics;
using Emgu.CV.Util;
#if !(__IOS__ || NETFX_CORE)
using Emgu.CV.Cuda;
#endif

namespace PedestrianDetection
{
   public static class FindPedestrian
   {
      /// <summary>
      /// Find the pedestrian in the image
      /// </summary>
      /// <param name="image">The image</param>
      /// <param name="processingTime">The processing time in milliseconds</param>
      /// <returns>The region where pedestrians are detected</returns>
      public static Rectangle[] Find(IInputArray image, out long processingTime)
      {
         Stopwatch watch;
         Rectangle[] regions;

         using (InputArray iaImage = image.GetInputArray())
         {
#if !(__IOS__ || NETFX_CORE)
            //if the input array is a GpuMat
            //check if there is a compatible Cuda device to run pedestrian detection
            if (iaImage.Kind == InputArray.Type.CudaGpuMat)
            {
               //this is the Cuda version
               using (CudaHOG des = new CudaHOG(new Size(64, 128), new Size(16, 16), new Size(8, 8), new Size(8, 8)))
               {
                  des.SetSVMDetector(des.GetDefaultPeopleDetector());

                  watch = Stopwatch.StartNew();
                  using (GpuMat cudaBgra = new GpuMat())
                  using (VectorOfRect vr = new VectorOfRect())
                  {
                     CudaInvoke.CvtColor(image, cudaBgra, ColorConversion.Bgr2Bgra);
                     des.DetectMultiScale(cudaBgra, vr);
                     regions = vr.ToArray();
                  }
               }
            }
            else
#endif
            {
               //this is the CPU/OpenCL version
               using (HOGDescriptor des = new HOGDescriptor())
               {
                  des.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector());
                  watch = Stopwatch.StartNew();

                  MCvObjectDetection[] results = des.DetectMultiScale(image);
                  regions = new Rectangle[results.Length];
                  for (int i = 0; i < results.Length; i++)
                     regions[i] = results[i].Rect;
                  watch.Stop();
               }
            }

            processingTime = watch.ElapsedMilliseconds;

            return regions;
         }
      }
   }
}

 

讀后感覺不錯,有收獲可以微信請作者喝杯咖啡,讀后有疑問請加微信,拉群研討,注明來意

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM