C# motion detection source code: How to achieve tripwire with a USB webcam


/ Published in: C#
Save to your folder(s)

Tripwire is a special part of motion detection that can be used to monitor and alert on specific changes. More specifically: tripwire means the detection of intrusion.

This code snippet presents how to create a C# software by using prewritten computer vision components (www.camera-sdk.com) allowing you to get notified when your USB webcam triggers an intrusion. For instance, by using this application, you can use your camera to alarm when a people enters into the shop/office, or even to count how many people entered, etc.

After the necessary using lines and objects you need to implement the Main method and the necessary functions for connecting to a USB webcamera. The startBt_Click method is used to start the tripwire functionality. Thereafter you can see how to handle the enter and exit events.

Nothing could be more simple! :)


Copy this code and paste it in your HTML
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using Ozeki.Media.MediaHandlers;
  5. using Ozeki.Media.MediaHandlers.Video;
  6.  
  7. namespace Tripwire_WF
  8. {
  9. public partial class MainForm : Form
  10. {
  11. private WebCamera _camera;
  12. private DrawingImageProvider _provider;
  13. private MediaConnector _connector;
  14.  
  15. private Tripwire tripwire;
  16.  
  17. private Point _p1, _p2;
  18.  
  19. public MainForm()
  20. {
  21. InitializeComponent();
  22.  
  23. tripwire = new Tripwire();
  24.  
  25. _provider = new DrawingImageProvider();
  26. _connector = new MediaConnector();
  27. }
  28.  
  29. private void connectBt_Click(object sender, EventArgs e)
  30. {
  31. _camera = WebCamera.GetDefaultDevice();
  32. if (_camera == null) return;
  33.  
  34. videoViewerWF1.SetImageProvider(_provider);
  35.  
  36. _connector.Connect(_camera, tripwire);
  37. _connector.Connect(tripwire, _provider);
  38.  
  39. _camera.Start();
  40.  
  41. videoViewerWF1.Start();
  42. }
  43.  
  44. private void startBt_Click(object sender, EventArgs e)
  45. {
  46. tripwire.Line.LineWidth = 3;
  47. tripwire.LineColor = Color.Red;
  48.  
  49. tripwire.SetPoints(new Point(300, 100), new Point(150, 300));
  50. tripwire.HighlightMotion = HighlightMotion.Highlight;
  51.  
  52. tripwire.MotionColor = Color.Blue;
  53. tripwire.TripwireMotionEnteredToLine += TripwireTripwireMotionEnteredToLine;
  54. tripwire.TripwireMotionLeaveFromLine += TripwireTripwireMotionLeaveFromLine;
  55.  
  56. tripwire.Start();
  57. }
  58.  
  59. private void stopBt_Click(object sender, EventArgs e)
  60. {
  61. tripwire.Stop();
  62. }
  63.  
  64. void InvokeThread(Action action)
  65. {
  66. Invoke(action);
  67. }
  68.  
  69. void TripwireTripwireMotionLeaveFromLine(object sender, TripwireMotionCrossedArgs e)
  70. {
  71. InvokeThread(() => { crossedText.Text = @"EXIT!!!"; });
  72. }
  73.  
  74. void TripwireTripwireMotionEnteredToLine(object sender, TripwireMotionCrossedArgs e)
  75. {
  76. InvokeThread(() => { crossedText.Text = @"ENTER!!!"; });
  77. }
  78.  
  79. private void videoViewerWF1_MouseDown(object sender, MouseEventArgs e)
  80. {
  81. if (e.Button != MouseButtons.Left) return;
  82. _p1 = e.Location;
  83. videoViewerWF1.MouseMove += videoViewerWF1_MouseMove;
  84. }
  85.  
  86. private void videoViewerWF1_MouseUp(object sender, MouseEventArgs e)
  87. {
  88. if (e.Button != MouseButtons.Left) return;
  89. _p2 = e.Location;
  90. tripwire.SetPoints(_p1, _p2);
  91. videoViewerWF1.MouseMove -= videoViewerWF1_MouseMove;
  92. }
  93.  
  94. private void videoViewerWF1_MouseMove(object sender, MouseEventArgs e)
  95. {
  96. _p2 = e.Location;
  97. tripwire.SetPoints(_p1, _p2);
  98. }
  99. }
  100. }

URL: http://www.camera-sdk.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.