public void CreateHog() { if (GpuInvoke.HasCuda && AllowGpu) { try { //gpuhog = new GpuHOGDescriptor(); gpuhog = new GpuHOGDescriptor(this.winSize, this.blockSize, this.blockStride, this.cellSize, this.nbins, this.winSigma, this.L2HysThreshold, this.gammaCorrection, this.nLevels); gpuhog.SetSVMDetector(GpuHOGDescriptor.GetDefaultPeopleDetector()); //gpuhog.SetSVMDetector(GpuHOGDescriptor.GetPeopleDetector64x128()); // there are 3 different detectors built-in. maybe others work better? } catch (Exception e) { Status = e.ToString(); } } else { try { hog = new HOGDescriptor(this.winSize, this.blockSize, this.blockStride, this.cellSize, this.nbins, 1, this.winSigma, this.L2HysThreshold, this.gammaCorrection); hog.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); } catch (Exception e) { Status = e.ToString(); } } }
/// <summary> /// Find the pedestrian in the image /// </summary> /// <param name="image">The image</param> /// <param name="processingTime">The pedestrian detection time in milliseconds</param> /// <returns>The image with pedestrian highlighted.</returns> public static Image <Bgr, Byte> Find(Image <Bgr, Byte> image) { Rectangle[] regions = new Rectangle[5]; //this is the CPU version using (HOGDescriptor des = new HOGDescriptor()) { des.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); Emgu.CV.Structure.MCvObjectDetection[] objects = des.DetectMultiScale(image); for (int i = 0; i < objects.Length; i++) { regions[i] = objects[i].Rect; if (objects[i].Score > 0.50) { FormVideo.Counter++; } } } foreach (Rectangle pedestrain in regions) { image.Draw(pedestrain, new Bgr(Color.Red), 1); } return(image); }
//=============================Feature Descriptor (HOG) Data Training Tanaman============================= public static Rectangle[] findObjects(Image <Bgr, Byte> image, out long processingTime, Size winSize, string dataFile) { Stopwatch watch; Rectangle[] regions; if (GpuInvoke.HasCuda) { using (GpuHOGDescriptor des = new GpuHOGDescriptor()) { des.SetSVMDetector(GpuHOGDescriptor.GetDefaultPeopleDetector()); watch = Stopwatch.StartNew(); using (GpuImage <Bgr, Byte> gpuImg = new GpuImage <Bgr, byte>(image)) using (GpuImage <Bgra, Byte> gpuBgra = gpuImg.Convert <Bgra, Byte>()) { regions = des.DetectMultiScale(gpuBgra); } } } else { using (HOGDescriptor des = new HOGDescriptor(winSize, blockSize, blockStride, cellSize, nbins, 1, -1, 0.2, true)) { des.SetSVMDetector(GetDataObjects(dataFile)); watch = Stopwatch.StartNew(); regions = des.DetectMultiScale(image); } } watch.Stop(); processingTime = watch.ElapsedMilliseconds; return(regions); }
public Rectangle[] FindBodyHOG_WithoutGpu(Mat image) { Rectangle[] regions = null; //this is the CPU version using (HOGDescriptor des = new HOGDescriptor()) { try { //Mat newImage = new Mat(); //BackgroundSubtractor bs = new BackgroundSubtractorMOG2(500, 16, false); //bs.Apply(image, newImage); des.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); MCvObjectDetection[] allBodies = des.DetectMultiScale(image); regions = new Rectangle[allBodies.Length]; for (int i = 0; i < allBodies.Length; i++) { regions[i] = allBodies[i].Rect; //if (body.Score > threshold) //regions.Add(body.Rect); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } return(regions); }
private static Rectangle[] FindPedestrian(IInputArray image, out long processTime) { Stopwatch watch; Rectangle[] regions; using (InputArray iaImage = image.GetInputArray()) { 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(); } } processTime = watch.ElapsedMilliseconds; return(regions); }
public override Rectangle[] find(Image <Bgr, Byte> image) { HOGDescriptor descriptor = new HOGDescriptor(); descriptor.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); return(descriptor.DetectMultiScale(image)); }
private void videoSourcePlayerCamera_NewFrame(object sender, ref Bitmap image) { // get new frame if (needUpdateCamera) { needUpdateCamera = false; Image <Bgr, Byte> img = null; if (image != null) { img = new Image <Bgr, Byte>(image); //Image<Bgr, Byte> imgBlank = null; HOGDescriptor hogd = new HOGDescriptor(); MCvObjectDetection[] mObj; //ibVideoPieton.Image = imgBlank; Application.DoEvents(); hogd.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); mObj = hogd.DetectMultiScale(img, 0, new Size(4, 4), new Size(8, 8), 1.05); //mObj = hogd.DetectMultiScale(img); foreach (MCvObjectDetection obj in mObj) { img.Draw(obj.Rect, new Bgr(System.Drawing.Color.Yellow), 2); } ibCameraDetection.Image = img; } } //motiondetector.ProcessFrame(image); }
/// <summary> /// Find the pedestrian in the image /// </summary> /// <param name="image">The image</param> /// <param name="processingTime">The pedestrian detection time in milliseconds</param> /// <returns>The region where pedestrians are detected</returns> public static Rectangle[] Find(Mat image, bool tryUseCuda, bool tryUseOpenCL, out long processingTime) { Stopwatch watch; Rectangle[] regions; #if !(IOS || NETFX_CORE) //check if there is a compatible Cuda device to run pedestrian detection if (tryUseCuda && CudaInvoke.HasCuda) { //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 cudaBgr = new GpuMat(image)) using (GpuMat cudaBgra = new GpuMat()) using (VectorOfRect vr = new VectorOfRect()) { CudaInvoke.CvtColor(cudaBgr, cudaBgra, ColorConversion.Bgr2Bgra); des.DetectMultiScale(cudaBgra, vr); regions = vr.ToArray(); } } } else #endif { //Many opencl functions require opencl compatible gpu devices. //As of opencv 3.0-alpha, opencv will crash if opencl is enable and only opencv compatible cpu device is presented //So we need to call CvInvoke.HaveOpenCLCompatibleGpuDevice instead of CvInvoke.HaveOpenCL (which also returns true on a system that only have cpu opencl devices). CvInvoke.UseOpenCL = tryUseOpenCL && CvInvoke.HaveOpenCLCompatibleGpuDevice; //this is the CPU/OpenCL version using (HOGDescriptor des = new HOGDescriptor()) { des.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); //load the image to umat so it will automatically use opencl is available UMat umat = image.ToUMat(AccessType.Read); watch = Stopwatch.StartNew(); MCvObjectDetection[] results = des.DetectMultiScale(umat); 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); }
/// <summary> /// Find the pedestrian in the image /// </summary> /// <param name="image">The image</param> /// <param name="processingTime">The pedestrian detection time in milliseconds</param> /// <returns>The region where pedestrians are detected</returns> public static Rectangle[] Find(Mat image, bool tryUseCuda, out long processingTime) { Stopwatch watch; Rectangle[] regions; #if !(__IOS__ || NETFX_CORE) //check if there is a compatible Cuda device to run pedestrian detection if (tryUseCuda && CudaInvoke.HasCuda) { //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 cudaBgr = new GpuMat(image)) using (GpuMat cudaBgra = new GpuMat()) using (VectorOfRect vr = new VectorOfRect()) { CudaInvoke.CvtColor(cudaBgr, 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()); //load the image to umat so it will automatically use opencl is available UMat umat = image.ToUMat(AccessType.Read); watch = Stopwatch.StartNew(); MCvObjectDetection[] results = des.DetectMultiScale(umat); 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); }
/// <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); } }
public List <IImage> ProcessFrame(IImage original) { Rectangle[] peopleRegion; using (InputArray iaImage = original.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()); using (GpuMat cudaBgra = new GpuMat()) using (VectorOfRect vr = new VectorOfRect()) { CudaInvoke.CvtColor(original, cudaBgra, ColorConversion.Bgr2Bgra); des.DetectMultiScale(cudaBgra, vr); peopleRegion = vr.ToArray(); } } } else #endif { //this is the CPU/OpenCL version using (HOGDescriptor peopleDescriptor = new HOGDescriptor()) { peopleDescriptor.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); MCvObjectDetection[] peopleFound = peopleDescriptor .DetectMultiScale(original, 0, default(Size), default(Size), AdjustableParameters["Scale"].CurrentValue, AdjustableParameters["SimilarityThreshold"].CurrentValue, AdjustableParameters["MeanShiftGrouping"].CurrentValue == 1); peopleRegion = new Rectangle[peopleFound.Length]; for (int i = 0; i < peopleFound.Length; i++) { peopleRegion[i] = peopleFound[i].Rect; } } } IImage copy = CopyAndDraw(original, peopleRegion); return(new List <IImage> { copy }); } }
/// <summary> /// Initialize the pedestrian detection model /// </summary> /// <param name="onDownloadProgressChanged">Call back method during download</param> /// <param name="initOptions">Initialization options. None supported at the moment, any value passed will be ignored.</param> /// <returns>Asyn task</returns> public async Task Init(DownloadProgressChangedEventHandler onDownloadProgressChanged = null, Object initOptions = null) { _hog = new HOGDescriptor(); _hog.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); if (CudaInvoke.HasCuda) { _hogCuda = new CudaHOG( new Size(64, 128), new Size(16, 16), new Size(8, 8), new Size(8, 8)); _hogCuda.SetSVMDetector(_hogCuda.GetDefaultPeopleDetector()); } }
static void Run() { Image <Bgr, Byte> image = new Image <Bgr, byte>("pedestrian.png"); Stopwatch watch; Rectangle[] regions; //check if there is a compatible GPU to run pedestrian detection if (GpuInvoke.HasCuda) { //this is the GPU version using (GpuHOGDescriptor des = new GpuHOGDescriptor()) { des.SetSVMDetector(GpuHOGDescriptor.GetDefaultPeopleDetector()); watch = Stopwatch.StartNew(); using (GpuImage <Bgr, Byte> gpuImg = new GpuImage <Bgr, byte>(image)) using (GpuImage <Bgra, Byte> gpuBgra = gpuImg.Convert <Bgra, Byte>()) { regions = des.DetectMultiScale(gpuBgra); } } } else { //this is the CPU version using (HOGDescriptor des = new HOGDescriptor()) { des.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); watch = Stopwatch.StartNew(); regions = des.DetectMultiScale(image); } } watch.Stop(); foreach (Rectangle pedestrain in regions) { image.Draw(pedestrain, new Bgr(Color.Red), 1); } ImageViewer.Show( image, String.Format("Pedestrain detection using {0} in {1} milliseconds.", GpuInvoke.HasCuda ? "GPU" : "CPU", watch.ElapsedMilliseconds)); }
public void Do(PictureBox pictureBox, Action callback) { using (var imageFrame = _capture.QueryFrame().ToImage <Bgr, Byte>()) { if (imageFrame != null) { var grayframe = imageFrame.Convert <Gray, byte>(); using (HOGDescriptor des = new HOGDescriptor()) { des.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); MCvObjectDetection[] results = des.DetectMultiScale(grayframe); var faces = new Rectangle[results.Length]; for (int i = 0; i < results.Length; i++) { faces[i] = results[i].Rect; } foreach (var face in faces) { var c = CenterRect(face); if (Helper.IsPointInPolygon(c, _points)) { imageFrame.Draw(face, new Bgr(Color.Chartreuse), 1); //the detected face(s) is highlighted here using a box that is drawn around it/them if (callback != null) { callback(); } } } imageFrame.DrawPolyline(_points, true, new Bgr(Color.Crimson), 1); var bmp = EmguHelper.ResizeImage(imageFrame.ToBitmap(), new Size(pictureBox.Width, pictureBox.Height)); pictureBox.Image = bmp; } } } }
public Rectangle[] Detect(Image <Gray, byte> grayframe) { using (HOGDescriptor des = new HOGDescriptor()) { des.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); MCvObjectDetection[] results = des.DetectMultiScale(grayframe); var rects = new Rectangle[results.Length]; if (results.Length > 0) { for (int i = 0; i < results.Length; i++) { rects[i] = results[i].Rect; } } return(rects); } }
/// <summary> /// for estimation /// </summary> /// <param name="svm_path"></param> public VehicleSpeedEstimator(System.Drawing.Size hog_training_img_size, string svm_path) { try { anomaly = new AnomalySpeedDetector(); AllowErrorRate = 1.5f; SurgingSpeedMin = 0.45f; SurgingCountLimit = 7; classifier = OpenCvSharp.ML.SVM.Load(svm_path); hog = new HOGDescriptor(new OpenCvSharp.Size(hog_training_img_size.Width, hog_training_img_size.Height), new OpenCvSharp.Size(16, 16), new OpenCvSharp.Size(8, 8), new OpenCvSharp.Size(8, 8)); var support_vectors = classifier.GetSupportVectors(); var vectors = new float[support_vectors.Width * support_vectors.Height]; Marshal.Copy(support_vectors.Data, vectors, 0, support_vectors.Width * support_vectors.Height); hog.SetSVMDetector(vectors); } catch (Exception) { classifier = OpenCvSharp.ML.SVM.Create(); } }
public static Image <Bgr, Byte> Find(Image <Bgr, Byte> image, out long processingTime) { Stopwatch watch; Rectangle[] regions; if (GpuInvoke.HasCuda) { using (GpuHOGDescriptor des = new GpuHOGDescriptor()) { des.SetSVMDetector(GpuHOGDescriptor.GetDefaultPeopleDetector()); watch = Stopwatch.StartNew(); using (GpuImage <Bgr, Byte> gpuImage = new GpuImage <Bgr, byte>(image)) { using (GpuImage <Bgra, Byte> gpuGpraImage = gpuImage.Convert <Bgra, Byte>()) { regions = des.DetectMultiScale(gpuGpraImage); } } } } else { using (HOGDescriptor des = new HOGDescriptor()) { des.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); watch = Stopwatch.StartNew(); regions = des.DetectMultiScale(image); } } watch.Stop(); processingTime = watch.ElapsedMilliseconds; foreach (Rectangle rect in regions) { image.Draw(rect, new Bgr(Color.Red), 1); } return(image); }
public void Run() { var img = Cv2.ImRead(FilePath.Asahiyama, LoadMode.Color); var hog = new HOGDescriptor(); hog.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); bool b = hog.CheckDetectorSize(); Console.WriteLine("CheckDetectorSize: {0}", b); var watch = Stopwatch.StartNew(); // run the detector with default parameters. to get a higher hit-rate // (and more false alarms, respectively), decrease the hitThreshold and // groupThreshold (set groupThreshold to 0 to turn off the grouping completely). Rect[] found = hog.DetectMultiScale(img, 0, new Size(8, 8), new Size(24, 16), 1.05, 2); watch.Stop(); Console.WriteLine("Detection time = {0}ms", watch.ElapsedMilliseconds); Console.WriteLine("{0} region(s) found", found.Length); foreach (Rect rect in found) { // the HOG detector returns slightly larger rectangles than the real objects. // so we slightly shrink the rectangles to get a nicer output. var r = new Rect { X = rect.X + (int)Math.Round(rect.Width * 0.1), Y = rect.Y + (int)Math.Round(rect.Height * 0.1), Width = (int)Math.Round(rect.Width * 0.8), Height = (int)Math.Round(rect.Height * 0.8) }; img.Rectangle(r.TopLeft, r.BottomRight, Scalar.Red, 3, LineType.Link8, 0); } using (var window = new Window("people detector", WindowMode.None, img)) { window.SetProperty(WindowProperty.Fullscreen, 1); Cv.WaitKey(0); } }
/// <summary> /// Find the pedestrian in the image /// </summary> /// <param name="imageFileName">The name of the image</param> /// <param name="processingTime">The pedestrian detection time in milliseconds</param> /// <returns>The image with pedestrian highlighted.</returns> public static Image <Bgr, Byte> Find(Image <Bgr, byte> source, out long processingTime) { Image <Bgr, Byte> image = source.Copy(); Stopwatch watch; Rectangle[] regions; //check if there is a compatible GPU to run pedestrian detection if (GpuInvoke.HasCuda) { //this is the GPU version using (GpuHOGDescriptor des = new GpuHOGDescriptor()) { des.SetSVMDetector(GpuHOGDescriptor.GetDefaultPeopleDetector()); watch = Stopwatch.StartNew(); using (GpuImage <Bgr, Byte> gpuImg = new GpuImage <Bgr, byte>(image)) using (GpuImage <Bgra, Byte> gpuBgra = gpuImg.Convert <Bgra, Byte>()) { regions = des.DetectMultiScale(gpuBgra); } } } else { //this is the CPU version using (HOGDescriptor des = new HOGDescriptor()) { des.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); watch = Stopwatch.StartNew(); regions = des.DetectMultiScale(image); } } watch.Stop(); processingTime = watch.ElapsedMilliseconds; foreach (Rectangle pedestrain in regions) { image.Draw(pedestrain, new Bgr(Color.Red), 1); } return(image); }
private void ProcessImageAndUpdateGUI() { Image <Bgr, Byte> imgImage = new Image <Bgr, Byte>(lblChosenFile.Text); //Image<Bgr, Byte> imgBlank = null; HOGDescriptor hogd = new HOGDescriptor(); MCvObjectDetection[] mObj; //ibDetectionPietonImage.Image = imgBlank; Application.DoEvents(); hogd.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); mObj = hogd.DetectMultiScale(imgImage); foreach (MCvObjectDetection obj in mObj) { imgImage.Draw(obj.Rect, new Bgr(System.Drawing.Color.Yellow), 2); } ibDetectionPietonImage.Image = imgImage; }
public async Task Init(DownloadProgressChangedEventHandler onDownloadProgressChanged = null, Object initOptions = null) #endif { _hog = new HOGDescriptor(); _hog.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); #if (UNITY_EDITOR || UNITY_IOS || UNITY_ANDROID || UNITY_STANDALONE) yield return(null); #else if (CudaInvoke.HasCuda) { _hogCuda = new CudaHOG( new Size(64, 128), new Size(16, 16), new Size(8, 8), new Size(8, 8)); _hogCuda.SetSVMDetector(_hogCuda.GetDefaultPeopleDetector()); } #endif }
public cameraSetup() { DataTable dt = null; motionDetector = new MotionDetector(new TwoFramesDifferenceDetector(), new MotionAreaHighlighting()); if (isSIDECAM) { dt = taCAMERA.getByName("SIDE"); } else { dt = taCAMERA.getByName("ATM"); } if (dt.Rows[0][2].ToString() != "False") { EqualizeHist = true; } if (dt.Rows[0][3].ToString() != "False") { GammaCorrect = true; } if (dt.Rows[0][4].ToString() != "False") { bodyDetection = true; } if (dt.Rows[0][5].ToString() != "False") { detectMotionAnoramlities = true; } if (dt.Rows[0][6].ToString() != "False") { isSIDECAM = true; } int.TryParse(dt.Rows[0][7].ToString(), out freezDelay); int tmp = 0; int.TryParse(dt.Rows[0][8].ToString(), out tmp); motionRatio = tmp / 100f; fgDetector = new BackgroundSubtractorMOG2(); descriptor.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); }
// New frame event handler, which is invoked on each new available video frame private void video_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs) { // get new frame bitmapPlayed = eventArgs.Frame; if (needUpdate) { needUpdate = false; Image <Bgr, Byte> img = null; if (bitmapPlayed != null) { img = new Image <Bgr, Byte>(bitmapPlayed); Image <Bgr, Byte> imgBlank = null; HOGDescriptor hogd = new HOGDescriptor(); MCvObjectDetection[] mObj; ibVideoPieton.Image = imgBlank; Application.DoEvents(); hogd.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); mObj = hogd.DetectMultiScale(img); foreach (MCvObjectDetection obj in mObj) { img.Draw(obj.Rect, new Bgr(System.Drawing.Color.Yellow), 2); } ibVideoPieton.Image = img; } } //motiondetector.ProcessFrame(eventArgs.Frame); // process the frame }
void RunPedestrianDetection() { using (Mat image = new Mat("pedestrian.png")) { Rectangle[] results; var stopwatch = System.Diagnostics.Stopwatch.StartNew(); using (HOGDescriptor hog = new HOGDescriptor()) using (UMat uImage = image.GetUMat(AccessType.ReadWrite)) { hog.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); results = PedestrianDetection.FindPedestrian.Find(uImage, hog); } stopwatch.Stop(); foreach (Rectangle rect in results) { CvInvoke.Rectangle(image, rect, new Bgr(Color.Red).MCvScalar); } mainImageView.Image = image.ToNSImage(); } }
/// <summary> /// Find the pedestrian in the image /// </summary> /// <param name="image">The image</param> /// <param name="processingTime">The pedestrian detection time in milliseconds</param> /// <returns>The region where pedestrians are detected</returns> public static Rectangle[] Find(Image <Bgr, Byte> image, out long processingTime) { Stopwatch watch; Rectangle[] regions; #if !IOS //check if there is a compatible GPU to run pedestrian detection if (GpuInvoke.HasCuda) { //this is the GPU version using (GpuHOGDescriptor des = new GpuHOGDescriptor()) { des.SetSVMDetector(GpuHOGDescriptor.GetDefaultPeopleDetector()); watch = Stopwatch.StartNew(); using (GpuImage <Bgr, Byte> gpuImg = new GpuImage <Bgr, byte>(image)) using (GpuImage <Bgra, Byte> gpuBgra = gpuImg.Convert <Bgra, Byte>()) { regions = des.DetectMultiScale(gpuBgra); } } } else #endif { //this is the CPU version using (HOGDescriptor des = new HOGDescriptor()) { des.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); watch = Stopwatch.StartNew(); regions = des.DetectMultiScale(image); } } watch.Stop(); processingTime = watch.ElapsedMilliseconds; return(regions); }
public override void ViewDidLoad() { base.ViewDidLoad(); ButtonText = "Detect Pedestrian"; OnButtonClick += delegate { //long processingTime; using (Mat image = CvInvoke.Imread("pedestrian.png", ImreadModes.Color)) using (HOGDescriptor hog = new HOGDescriptor()) { hog.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); Stopwatch watch = Stopwatch.StartNew(); Rectangle[] pedestrians = FindPedestrian.Find(image, hog); watch.Stop(); foreach (Rectangle rect in pedestrians) { CvInvoke.Rectangle( image, rect, new MCvScalar(0, 0, 255), 1); } Size frameSize = FrameSize; using (Mat resized = new Mat()) { CvInvoke.ResizeForFrame(image, resized, frameSize); MessageText = String.Format( "Detection Time: {0} milliseconds.", watch.ElapsedMilliseconds ); SetImage(resized); } } }; }
//public static Image<Bgr, Byte> DetectFeatures() //{ // Stopwatch watch = Stopwatch.StartNew(); // Rectangle[] regions = GetBodies("Untitled7.jpg"); // MCvAvgComp[][] facesDetected = GetFaces("Untitled7.jpg"); // Image<Bgr, Byte> image = new Image<Bgr, byte>("Untitled7.jpg"); // foreach (Rectangle pedestrain in regions) // { // image.Draw(pedestrain, new Bgr(Color.Red), 1); // } // foreach (MCvAvgComp f in facesDetected[0]) // { // //draw the face detected in the 0th (gray) channel with blue color // image.Draw(f.rect, new Bgr(Color.Blue), 2); // } // return image; //} // Body Function public static Rectangle[] GetBodies(string fileName) { Image <Bgr, Byte> image = new Image <Bgr, byte>(fileName); Stopwatch watch; Rectangle[] regions; //check if there is a compatible GPU to run pedestrian detection if (GpuInvoke.HasCuda) { //this is the GPU version using (GpuHOGDescriptor des = new GpuHOGDescriptor()) { des.SetSVMDetector(GpuHOGDescriptor.GetDefaultPeopleDetector()); watch = Stopwatch.StartNew(); using (GpuImage <Bgr, Byte> gpuImg = new GpuImage <Bgr, byte>(image)) using (GpuImage <Bgra, Byte> gpuBgra = gpuImg.Convert <Bgra, Byte>()) { regions = des.DetectMultiScale(gpuBgra); } } } else { //this is the CPU version using (HOGDescriptor des = new HOGDescriptor()) { des.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); watch = Stopwatch.StartNew(); regions = des.DetectMultiScale(image); } } watch.Stop(); return(regions); }
private void btnTest_Click(object sender, EventArgs e) { MyRect[] regions = null; Emgu.CV.ML.SVM vSVM = new Emgu.CV.ML.SVM(); String cExportFileName = txtFileName.Text; FileStorage fileStorage = new FileStorage(cExportFileName, FileStorage.Mode.Read); vSVM.Read(fileStorage.GetFirstTopLevelNode()); int iHeight = vSVM.GetSupportVectors().Height; int iWidth = vSVM.GetSupportVectors().Width; var svmMat = new Matrix <float>(iWidth, iHeight); Matrix <float> resultMat = new Matrix <float>(1, iWidth); Matrix <float> alphaMat = new Matrix <float>(1, iHeight); float[] mydetector = new float[iWidth + 1]; for (int i = 0; i < iWidth; i++) { mydetector[i] = resultMat[0, i]; } //mydetector[iWidth] = rhoValue; Mat vImage = new Mat(); HOGDescriptor hog = new HOGDescriptor(new Size(36, 36), new Size(36, 36), new Size(6, 6), new Size(6, 6)); hog.SetSVMDetector(mydetector); MCvObjectDetection[] results = hog.DetectMultiScale(vImage); regions = new MyRect[results.Length]; for (int i = 0; i < results.Length; i++) { regions[i] = new MyRect(); regions[i].Rect = results[i].Rect; regions[i].Score = results[i].Score; } }
public ActionResult DetectHuman(HttpPostedFileBase imageData) { try { if (imageData == null) { throw new ArgumentException("File is not exist."); } using (var img = Mat.FromStream(imageData.InputStream, LoadMode.Color)) { var ExecutingAssemblyPath = new Uri(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase.ToString())).LocalPath; double scale = 2.0; using (var gray = new Mat()) using (var smallImg = new Mat((int)(img.Rows / scale), (int)(img.Cols / scale), MatType.CV_8UC1)) { byte[] imgBytes = img.ToBytes(".png"); string base64Img = Convert.ToBase64String(imgBytes); ViewBag.Base64Img = base64Img; var hog = new HOGDescriptor(); hog.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); var rects = hog.DetectMultiScale(img); foreach (var rect in rects) { var r = rect; r.X += Cv.Round(rect.Width * 0.1); r.Width = Cv.Round(rect.Width * 0.8); r.Y += Cv.Round(rect.Height * 0.1); r.Height = Cv.Round(rect.Height * 0.8); Cv2.Rectangle(img, r, new Scalar(0, 255, 0), 3); } byte[] resultBytes = img.ToBytes(".png"); string base64Result = Convert.ToBase64String(resultBytes); ViewBag.Base64OrgResult = base64Result; } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } return View(); }
async void PushImageStream() { if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { Console.WriteLine("run bash shell: 'sudo modprobe bcm2835-v4l2'"); "sudo modprobe bcm2835-v4l2".Bash(); } CascadeClassifier cascadeClassifier = new CascadeClassifier("haarcascade_frontalface_default.xml"); // Opens MP4 file (ffmpeg is probably needed) var capture = new VideoCapture(0); int sleepTime = (int)Math.Round(1000 / ((capture.Fps == 0) ? 60 : capture.Fps)); var hog = new HOGDescriptor(); hog.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); var recognizer = OpenCvSharp.Face.EigenFaceRecognizer.Create(2); var img1 = Cv2.ImRead(SamplesCore.FilePath.Image.Girl, ImreadModes.GrayScale); var img2 = Cv2.ImRead(SamplesCore.FilePath.Image.Lenna, ImreadModes.GrayScale); System.Console.WriteLine("img1 width: " + img1.Size().Width + " height:" + img1.Size().Height); System.Console.WriteLine("img2 width: " + img2.Size().Width + " height:" + img2.Size().Height); recognizer.Train(new List <Mat>() { img1, img2 }, new List <int>() { 1, 2 }); recognizer.Write("trainfile1.dat"); recognizer.Read("trainfile1.dat"); System.Console.WriteLine("before predict"); System.Console.WriteLine("img1 width: " + img1.Size().Width + " height:" + img1.Size().Height); System.Console.WriteLine("img2 width: " + img2.Size().Width + " height:" + img2.Size().Height); recognizer.Predict(InputArray.Create(img1), out int label, out double confidence); System.Console.WriteLine("label: " + label + " confidence: " + confidence); const string protoTxt = @"Data/Text/bvlc_googlenet.prototxt"; const string caffeModel = "bvlc_googlenet.caffemodel"; const string synsetWords = @"Data/Text/synset_words.txt"; var classNames = File.ReadAllLines(synsetWords) .Select(line => line.Split(' ').Last()) .ToArray(); using (var net = CvDnn.ReadNetFromCaffe(protoTxt, caffeModel)) //using (var window = new Window("capture")) { // Frame image buffer Mat image = new Mat(); Task.Run(() => { while (true) { FaceProcessing(image, cascadeClassifier); } }); Task.Run(() => { while (true) { HogProcessing(image, hog); } }); Task.Run(() => { while (true) { CaffeProcessing(image, net, classNames); } }); // When the movie playback reaches end, Mat.data becomes NULL. while (true) { capture.Read(image); // same as cvQueryFrame if (image.Empty()) { break; } currentMat = image.Clone(); foreach (var rect in currentFaces) { Cv2.Rectangle(image, rect, Scalar.Red, 2); } foreach (var rect in currentBodies) { Cv2.Rectangle(image, rect, Scalar.Green, 2); } image.PutText(classNames[this.classId] + String.Format(" Probability: {0:P2}", this.classProb), new Point(0 + 10, image.Size().Height - 10), HersheyFonts.HersheyDuplex, 1, Scalar.White); var bytes = image.ToMemoryStream().ToArray(); await this.socket.SendAsync(new ArraySegment <byte>(bytes, 0, bytes.Length), WebSocketMessageType.Binary, true, CancellationToken.None); //if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) //{ // window.ShowImage(image); // Cv2.WaitKey(sleepTime); //} } } }
public PeopleDetector() { descriptor = new HOGDescriptor(); descriptor.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); }