Setup a webcam security system with Ubuntu Linux and Motion

May 17
2009
Snap from Office Security Cam

Snap from Office Security Cam

So, now that I’m in Morgantown – my home is too small to comfortably work on side gigs and personal projects – especially now that my family is getting bigger with the baby!  I’ve been using the office space I leased out more and more.  While exploring video conferencing with Matt last week, I had the thought “wouldn’t it be cool to have a security camera in the office?”.  So I did just that, and it’s actually quite easy for Ubuntu linux users.

What you need:
  • Ubuntu Linux ( I was using 8.04.1 at the time of installation )
  • one or more USB web cameras
What you can do:
  • Motion detection – record video/and or frames if there is motion.
  • Snapshot intervals – take time interval snapshots regardless of motion detection.
  • Live video IP stream in mjpeg format.
  • Specify recorded video to be saved in your choice mpeg, avi, flv, swf format.
  • When motion exists, have frames and videos draw a box around the specific motion for more obvious recognition of subtle movements ( this actually shows the shadow of the janitor near the door around 6 a.m. every morning – I wouldn’t have noticed otherwise! )
  • Easily send all data to a backup server in a variety of ways – I keep it simple by saving data to my Dropbox directory, a wonderful cross-platform data syncronization and sharing utility.
Steps:

1.  Plugin your webcam.
For me, the Logitech QuickCam® Pro 9000 worked right out of the box, and was only 105$.

2.  Install Motion – software motion detector, and turn it on.

sudo apt-get install motion
sudo motion

3. Configure Motion

Everything really works out of the box with this – but isn’t quite organized to my liking, and probably not yours either. Global configuration is located inside /etc/motion.conf ( You’ll notice there are multiple threadN.conf files in this directory – which can be used for custom configured individual cameras if you are setting up more than one ).

Note: Be sure to restart the Motion server everytime you make a configuration change.

sudo /etc/init.d/motion restart

Take a look at the files, they are well documented. Below are a few helpful configurations to get your data organized quicker:

#/etc/motion/motion.conf

# Locate and draw a box around the moving object.
locate on

# Draws the timestamp using same options as C function strftime(3)
text_right %Y-%m-%dn%T-%q

# Text is placed in lower left corner
text_left SECURITY CAMERA %t - Office

Organize the filesytem to save data by date, instead of all in one directory.

# File path for snapshots (jpeg or ppm) relative to target_dir
snapshot_filename %Y%m%d/camera-%t/snapshots/hour-%H/camera-%t-%v-%Y%m%d%H%M%S-snapshot

# File path for motion triggered images (jpeg or ppm) relative to target_dir
jpeg_filename %Y%m%d/camera-%t/motions/hour-%H/camera-%t-%v-%Y%m%d%H%M%S-%q-motion

# File path for motion triggered ffmpeg films (mpeg) relative to target_dir
movie_filename %Y%m%d/camera-%t/movies/hour-%H/camera-%t-%v-%Y%m%d%H%M%S-movie

# File path for timelapse mpegs relative to target_dir
timelapse_filename %Y%m%d/camera-%t/timelapses/hour-%H/camera-%t-%Y%m%d-timelapse

4.  (Optional)  Setup a backup solution

a. Easy solution, get and install Dropbox — instructions on the Dropbox site.  Then update your motion.conf to save to your Dropbox directory:

#/etc/motion/motion.conf
...
target_dir /path/to/dropbox/security_camera
...

b. A more granular solution is to take advantage of hooks configurable in motion.conf. Using these, you can create bash scripts to do anything your heart desires ( like trigger a silent alarm on motion detection outside business hours ). Available hooks: on_event_start, on_event_end, on_picture_save, on_motion_detected, on_movie_start, on_movie_end.

If you have wput installed, you can easily upload files to a remote backup server with these hooks:

#motion.conf
...
on_picture_save wput ftp://user@pass@server %f
...

However, this solution is somewhat less secure, as it uses FTP. In a future post I will detail how to secure this up using encrypted transfer and phrase free keys. ( Stay tuned! )

5. Live feed

This comes working out of the box with Motion. Check out your live stream in your web browser by navigating to: http://localhost:8081

That’s it! Webcam security made easy :-)

Choosing the best platform for the job: CMS Solution, PHP MVC, Django, or Ruby on Rails

May 14
2009

I see this all the time:  “I’m building a website, but I don’t know what the best technology to use is?  This guy says PHP,  that guy says Ruby.  What’s better?”.  While there are definitely a handful of developers that will quickly jump to share my point, there are far too many that have biases for certain technologies and will quickly defend them with a bullet list.  Avoid those guys with biases – their tool belts are smaller.

Some things to Consider

Choosing the best technology really depends on what it has to do.  You wouldn’t hit a screw with a hammer?  Well, you can… but you shouldn’t.  Choose your website technologies based on what it has to do and what it takes to create and support it.  Some things to consider:

  • Budget and Market Cost
  • Continued Support and Development
  • Scalability – meaning, when new problems arise, will the solution be able to meet and defeat those problems?
  • Demographics – how well does the workforce powering your technology cater to your product’s needs?

Simple Sites

Say, perhaps, you want to build a small website to advertise a product or yourself.  It needs a few dynamic options, like a contact form, and maybe a shopping cart. I highly recommend exhausting service oriented solutions before going custom.

You could quickly find a web developer, blow a few hundred bucks, spend several weeks customizing and tweaking, and there you have it – no real support, you may or may not have a scalable back-end to manage it, and future expansions become expensive.  Or…

You could throw up a wordpress.com site for your landing page, link it to an inexpensive shopping cart service like e-junkie.com, and have significantly more bang for FREE by gaining access to thousands of plug-n-play plug-ins to extend your site with gallery applications, feeds, sharing tools, SEO management tools, as well as all the support of a service like e-junkie on call for your every e-commerce need.

See where I’m going with this?

More Complex Project

Lets envision you need something more complex than a personal page.  How about, a niche social network?  You want your own version of Myspace, and white label solutions just don’t have what it takes.  So you’ve raised thousands of dollars in angel investment, got your business plan, and are perplexed by the question of this post, “What technology?!?!”.  In this case, I would suggest considering the labor force and scalability behind each technology in correlation to what that is going to mean for your budget.

PHP Frameworks

While PHP has a huge work force, and many success stories, the language itself is rather controversial, as are philosophical differences in it’s “thousands” of frameworks.   A handful of frameworks are well-known, such as Symfony, CakePHP, Zend, CodeIgniter, KohanaPHP; though being an expert level PHP developer, I can tell you that many of the PHP frameworks, well, quite frankly, suck.  It’s new and easy to learn, and bloated with newbies making decent money, but not deploying consistent theory or solid code.  This is not always their fault, PHP lacks as a new language.

PHP also brings a lack of legacy support with each new version.  Consider the lifespan and size of your project.  Does this site need to run code for several years?  How difficult might it be to migrate old code to new versions of the language as they are released?  Just an example:  When  PHP5 released, it left thousands of web hosts outdated and incapable of updating without major financial and time expense due to the fact that PHP5 had changed syntax to introduce OOP, which broke PHP4.  To simply upgrade to PHP5 would break millions of websites without those webmasters first learning the changes in the language and making updates to their scripts.  Such scenario meant serious problems for web hosts that wanted to support latest technologies for new clients while maintaining legacy technology for existing clients.  PHP6 bodes better, but will still admittedly face similar problems.

As for the workforce, it’s just too easy to get into PHP.  The skill level required to create a PHP site is significantly less than that of other languages.  It’s the nature of PHP – Hypertext Pre-Processor, which, to many, is code (pun intended) for HTML on steroids.  Your average MySpace fanatic knows a bit of HTML these days, PHP is just the next informal step for many.  If you can find a skilled Object Oriented Programmer (OOP) – you’re well off, but the work force powering PHP is predominately less read into computer science theory and less likely to deploy best practices.  Why should they? – that’s all part of the beauty of the language.

Not to say a quality website can’t be developed in PHP, but I haven’t worked with any particular PHP Framework that really nailed it for me as a perfectionist.  You’ll find many like me in the PHP work force:  “just another php framework author”.  There seems to be an abundant number of us that find it difficult to produce quality code in popular frameworks, causing us to resort to building our own.  Mine is coined Typhoon.

Though, don’t get me wrong, I LOVE PHP and have developed numerous websites in the language.  I also look forward to the great new benefits in Version 6.  When you come down to picking PHP for your choice as a platform tool, just be sure the scope of your project is small to medium sized, speed of development being important, and ongoing support of the project being minimal.  Past that, the coding standards recognized by the PHP community are just too flexible and inconsistent for me to feel comfortable  spending significant time and expense on a bigger PHP site.

Django

“Developed four years ago by a fast-moving online-news operation, Django was designed to handle two challenges: the intensive deadlines of a newsroom and the stringent requirements of the experienced Web developers who wrote it. It lets you build high-performing, elegant Web applications quickly.” – from the creators of the Django Project

Now Django is a cool framework built in the Python language, and perhaps the premier web framework of Python. Unlike PHP, Python is a general utility language. It’s usage can be found in many aspects of computer science from desktop applications, to video games, to web development – and it has been around awhile.

Lets keep in mind the quote above though.  It was developed by-and-for the newsroom.  As such, it caters VERY well to companies that have a similar infrastructure.  While you can develop many applications regardless, that fundamental purpose gives the framework an overall flavor that may not be quite as flexible in every problem domain.

The built in content management system (CMS) feature of Django is really quite a gem.  It’s effective at allowing developers to rapidly generate applications for non-technical people to manage.  Would I use it for a social network?  Probably not as my first pick.  Would I use it for news syndication site?  Hell yes.

Python has been around since the early 90′s and has had sufficient time to incorporate advanced computer science theory and stabilize.   The workforce powering Django applications is also significantly more well-read than the PHP workforce ( I say this loosely, of course ), as the Pythoon workforce has been around longer in general.

Django can sleep safe at night knowing it was built on a stable language that doesn’t anticipate significant change year after year, making it a great platform for websites requiring long term support and development.

Ruby on Rails ( Soon to merge with MERB in Rails 3 )

Talk about some BUZZ!  I’ll admit I did not want to learn Ruby on Rails at first.  I love learning technology, but when Rails was just gaining it’s hype, I was getting deep into PHP and tended to stay in the PHP world.  Mistake at the time, on my part.

Rails identifies itself as a “web framework”.  Unlike Django, Ruby on Rails was developed to tackle many more of the standard problems developers face on a much broader sense of web development.  This by itself should not be translated as “Rails is the creme de la creme” for your web project, but it does make it a strong candidate to initially consider.

When to Consider Rails

  • You have a project with many developers, possibly several contractors that will come and go as needed.
  • You need fast scalability and flexibility due to a multi-faceted problem domain, ie:  niche social networks tend to start simple and then tack on lots of complex features.

Some Pros

In terms of computer science, 9.9999 times out of 10, problems we face in development are problems we’ve answered time and time again, either in theory or practice.  Thankfully, the Rails community innately conforms to the DRY Principle ( don’t-repeat-yourself ).  The community also drastically alleviates extra invention faced by developers, simply by embracing, building upon, and reinforcing community accepted solutions.  The now famous Rails motto:  “Convention over Configuration” sets a tone where the community expects eachother to follow a standard set of approaches versus relying on the individual’s limited resources to reach the same conclusions others have already reached.

Not only is it a motto, it’s an obstacle for “do-it-my-way” programmers, as it’s rather difficult to make a Rails application without doing it the “Rails way” – things will just not go smoothly if you attempt to break standards.  As developers tend to think differently about the same problem domain, Rails lends itself as a great framework to conform the result of their different approaches to the same represented solution.  In identifying conventions and standards, developers of the rails community spend significantly less time “re-inventing the wheel”, and more time knocking out features and drinking beer.

Some Cons

It’s memory intensive/innefficient.  MERB is merging with Rails 3 – which promises to improve performance, though in the meantime, I have several Rails applications running for small sites where I should have deployed PHP, I’m noticing I’m low on memory due to such, which ultimately costs me more money to keep my servers up to par with my Rails demands.

Windows users tend to have a more difficult time with Rails whereas the PHP community has gone through a few more lengths to make development in windows fairly painless.  Rails is significantly is better to develop on unix-based platforms, like Linux and Mac.  Windows users will need to go through extra hoops to keep up with dependencies largely created and maintained by the unix-based users – though, if you’re a serious web programmer using windows as your primary platform, you should consider weening off sooner than later anyways.

Rails isn’t THAT light weight.  It incorporates a lot of functionality and sub framework patterns that you just may never use on some projects.  Again, Rails 3 promises to improve in this area as well.

[Conclusion Pending]

Visit Other Sites!

Find me on other sites...

Archives

All entries, chronologically...

Pages List

General info about this site...