Sunday, December 2, 2012

OpenCV Motion Detection Based Action Trigger - Part 1

I created a simple motion detector using OpenCV. My purpose was to use this program to trigger my USB connected digital camera to capture closeup images of birds, insects and rodents. But I think it is generic enough to be used for other purposes. The program monitors the camera feed for movement. Whenever it detects some movement, it executes a pre-configured command. In my case, the action takes a picture by using gPhoto. It could as well be made to execute any command like sending a mail, sounding an alarm or whatever. In addition, it demonstrates the steps involved in motion detection, which may be interesting to some. So here it is...

In my next post, I'll explain the steps behind the motion detection. But for the impatient, right below are the command line arguments and the source code gist.

Source code: https://gist.github.com/4189355


Build instructions:

  • Compile with OpenCV 2.4.3 or above.
  • Also needs libconfig for config file storage.


Executing the application:

  • configuration/demo mode
    • to use video from an attached camera
      motion_detect cam camera_number
    • to use a pre-recorded video file
      motion_detect file file_path
    • to save a configuration file with the adjusted parameters type 's' while on the application window. A file with name "motion_detect.cfg" would be saved.
    • configure the action to be executed by adding
      command_on_motion = "the command"
      to the configuration file.
  • action mode
    • append 'act' to the commands above.
      e.g. motion_detect cam act

Below is a screen shot of the application with motion being sensed from a surveillance camera. (The GUI is not great as it uses only the basic OpenCV provided high_gui module.)



Do download the source code and try out this application and do share with us if you come up with any improvements. In my next post I'll go through the image processing steps used for motion detection.

Continuation: OpenCV Motion Detection Based Action Trigger - Part 2

2 comments:

Unknown said...

Nice work...
I did this..
I suggest you checkout.. :)
http://hardikmadhu.blogspot.com/2013/09/a-simple-and-minimal-aproach-to-human.html

Tanmay said...

Had a look at your post. That's an interesting and unique approach Hardik! Will have a deeper look.