private void source_NewFrame(object sender, NewFrameEventArgs eventArgs) { if (!IsRunning || (!IsTracking && !IsDetecting)) { return; } lock (syncObject) { Bitmap frame = eventArgs.Frame; int width = frame.Width; int height = frame.Height; BitmapData data = frame.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, frame.PixelFormat); UnmanagedImage head = new UnmanagedImage(data); if (IsDetecting) { IsDetecting = false; // Process the nose detector in the head image Rectangle[] regions = detector.ProcessFrame(head); if (regions.Length >= 1) { // Re-initialize tracker tracker.Reset(); tracker.SearchWindow = regions[0]; tracker.ProcessFrame(head); // Update initial position computeCurrentPosition(width, height); OnFaceEnter(new FaceEventArgs(currentX, currentY)); } else { IsDetecting = true; } } else if (IsTracking) { // Track the object tracker.ProcessFrame(head); // Get the object position TrackingObject obj = tracker.TrackingObject; // Update current position computeCurrentPosition(width, height); if (obj.IsEmpty) { OnFaceLeave(EventArgs.Empty); } else { OnFaceMove(new FaceEventArgs(currentX, currentY)); } } frame.UnlockBits(data); } }
public void ProcessFrame() { string basePath = Path.Combine(NUnit.Framework.TestContext.CurrentContext.TestDirectory, "matching-tracker"); #region doc_track // Let's test the tracker using a sample video from // the collection of test videos in the framework: TestVideos ds = new TestVideos(basePath); string fileName = ds["walking.mp4"]; // Now, let's open the video using FFMPEG: var video = new VideoFileReader(); video.Open(fileName); // And then check the contents of one of the frames: Bitmap frame = video.ReadVideoFrame(frameIndex: 150); frame.Save(Path.Combine(basePath, "walking_frame.png")); // Let's register a template for the bike rider in gray shirt Rectangle roi = new Rectangle(x: 70, y: 105, width: 28, height: 54); // initialization var tracker = new MatchingTracker() { SearchWindow = roi, Threshold = 0.0, // never reset the tracker in case it gets lost RegistrationThreshold = 0.95 // re-register the template if we are 95% // confident that the tracked object is indeed the object we want to follow }; // Creating bitmaps and locking them is an expensive // operation. Instead, let's allocate once and reuse BitmapData bitmapData = frame.LockBits(ImageLockMode.ReadWrite); UnmanagedImage unmanagedImage = new UnmanagedImage(bitmapData); // We will create two color markers: one to show the location of the // tracked object (red) and another one to show the regions of the image // that the tracker is looking at (white). RectanglesMarker objectMarker = new RectanglesMarker(Color.Red); RectanglesMarker windowMarker = new RectanglesMarker(Color.White); // Now, for each frame of the video for (int frameIndex = 0; frameIndex < video.FrameCount; frameIndex++) { // Read the current frame into the bitmap data video.ReadVideoFrame(frameIndex, bitmapData); if (frameIndex > 150) // wait until the bike rider enters the scene { // Feed the frame to the tracker tracker.ProcessFrame(unmanagedImage); // Mark the location of the tracker object in red color objectMarker.SingleRectangle = tracker.TrackingObject.Rectangle; objectMarker.ApplyInPlace(unmanagedImage); // overwrite the frame windowMarker.SingleRectangle = tracker.SearchWindow; windowMarker.ApplyInPlace(unmanagedImage); // overwrite the frame } // Save it to disk frame.Save(Path.Combine(basePath, "frame_{0}.png".Format(frameIndex))); } frame.UnlockBits(bitmapData); #endregion }