0 people like it.

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

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 (http://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! :)

  1: 
  2: 
  3: 
  4: 
  5: 
  6: 
  7: 
  8: 
  9: 
 10: 
 11: 
 12: 
 13: 
 14: 
 15: 
 16: 
 17: 
 18: 
 19: 
 20: 
 21: 
 22: 
 23: 
 24: 
 25: 
 26: 
 27: 
 28: 
 29: 
 30: 
 31: 
 32: 
 33: 
 34: 
 35: 
 36: 
 37: 
 38: 
 39: 
 40: 
 41: 
 42: 
 43: 
 44: 
 45: 
 46: 
 47: 
 48: 
 49: 
 50: 
 51: 
 52: 
 53: 
 54: 
 55: 
 56: 
 57: 
 58: 
 59: 
 60: 
 61: 
 62: 
 63: 
 64: 
 65: 
 66: 
 67: 
 68: 
 69: 
 70: 
 71: 
 72: 
 73: 
 74: 
 75: 
 76: 
 77: 
 78: 
 79: 
 80: 
 81: 
 82: 
 83: 
 84: 
 85: 
 86: 
 87: 
 88: 
 89: 
 90: 
 91: 
 92: 
 93: 
 94: 
 95: 
 96: 
 97: 
 98: 
 99: 
100: 
101: 
102: 
103: 
104: 
105: 
106: 
107: 
108: 
/*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 (http://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! :) */

using System;
using System.Drawing;
using System.Windows.Forms;
using Ozeki.Media.MediaHandlers;
using Ozeki.Media.MediaHandlers.Video;
 
namespace Tripwire_WF
{
    public partial class MainForm : Form
    {
        private WebCamera _camera;
        private DrawingImageProvider _provider;
        private MediaConnector _connector;
 
        private Tripwire tripwire;
 
        private Point _p1, _p2;
 
        public MainForm()
        {
            InitializeComponent();
 
            tripwire = new Tripwire();
 
            _provider = new DrawingImageProvider();
            _connector = new MediaConnector();
        }
 
        private void connectBt_Click(object sender, EventArgs e)
        {
            _camera = WebCamera.GetDefaultDevice();
            if (_camera == null) return;
 
            videoViewerWF1.SetImageProvider(_provider);
 
            _connector.Connect(_camera, tripwire);
            _connector.Connect(tripwire, _provider);
 
            _camera.Start();
 
            videoViewerWF1.Start();
        }
 
        private void startBt_Click(object sender, EventArgs e)
        {
            tripwire.Line.LineWidth = 3;
            tripwire.LineColor = Color.Red;
 
            tripwire.SetPoints(new Point(300, 100), new Point(150, 300));
            tripwire.HighlightMotion = HighlightMotion.Highlight;
 
            tripwire.MotionColor = Color.Blue;
            tripwire.TripwireMotionEnteredToLine += TripwireTripwireMotionEnteredToLine;
            tripwire.TripwireMotionLeaveFromLine += TripwireTripwireMotionLeaveFromLine;
 
            tripwire.Start();
        }
 
        private void stopBt_Click(object sender, EventArgs e)
        {
            tripwire.Stop();
        }
 
        void InvokeThread(Action action)
        {
            Invoke(action);
        }
 
        void TripwireTripwireMotionLeaveFromLine(object sender, TripwireMotionCrossedArgs e)
        {
            InvokeThread(() => { crossedText.Text = @"EXIT!!!"; });
        }
 
        void TripwireTripwireMotionEnteredToLine(object sender, TripwireMotionCrossedArgs e)
        {
            InvokeThread(() => { crossedText.Text = @"ENTER!!!"; });
        }
 
        private void videoViewerWF1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left) return;
            _p1 = e.Location;
            videoViewerWF1.MouseMove += videoViewerWF1_MouseMove;
        }
 
        private void videoViewerWF1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left) return;
            _p2 = e.Location;
            tripwire.SetPoints(_p1, _p2);
            videoViewerWF1.MouseMove -= videoViewerWF1_MouseMove;
        }
 
        private void videoViewerWF1_MouseMove(object sender, MouseEventArgs e)
        {
            _p2 = e.Location;
            tripwire.SetPoints(_p1, _p2);
        }
    }
}
val using : resource:'T -> action:('T -> 'U) -> 'U (requires 'T :> System.IDisposable)

Full name: Microsoft.FSharp.Core.Operators.using
val exit : exitcode:int -> 'T

Full name: Microsoft.FSharp.Core.Operators.exit
namespace System
namespace System.Drawing
namespace System.Windows
namespace System.Windows.Forms

More information

Link:http://fssnip.net/qK
Posted:5 years ago
Author:chewie-wookiee
Tags: c# , motion , detection , camera , tripwire , webcam , ip camera , usb , cam , intrusion , recognition , source , code , example , trigger , notify , camera sdk