Detecting objects in a photograph with opencv

Two faces being detected using OpenCV

For a project I am working I need to detect simple rectangular objects in a photograph. After some investigation I have settled on using the Open Computer Vision (opencv) libraries to do this. I have played around with opencv a little in the past but still consider myself a newbie. Having said that it has not been too hard to get up and running.

My programming language of choice is ruby so I am making use of the ruby-opencv gem. Unfortunately this gem doesn’t seem to be very well supported and didn’t install using good old gem install opencv. What I found was that this repository seemed to be the most up to date fork of code. So I cloned it and went about getting it installed.

ruby-opencv is a native gem that wraps the opencv libraries so there is not much ruby going on in there and plenty of c++. After muddling my way through a few different messages about dependencies I did not have and some confusing error messages – which again where simply unmet dependencies I finally managed to get the gem built.

The repository has a sample that shows how to do face detection. It is one of those pieces of code that make you go what the heck! it can’t be that simple! And while you can do face detection in about 8 lines of code it is not really the code that is the magic part here.

The face detection algorithm is actually a set of Haar-like features. This is basically a description of what sort of object we are looking for – but go read the wikipedia article for a more comprehensive description. In order to detect a specific type of object we are going to have to write our own classifier that describes what we are looking for.

The next step is to write a classifier for our objects which we will do in the next post.

 

ReSharper 6 Out now

It seems like just yesterday I was lobbying management to upgrade to ReSharper 5. It was in fact over a year ago and as usual JetBrains have not been resting on their laurels. After numerous releases they have gifted .NET developers everywhere with another feature packed release.

ReSharper has always been a must have tool since the 2.x days but I am contionusly surprised by all the extra useful features they manage to come up with. Although it wouldn’t be one of the features I would use every day the JavaScript and CSS support looks particularly nice. The decompiler on the other hand.. well lets just say I could never live without Lutz. I don’t know what it is about seeing how other people have implemented a solution, maybe I love to learn.. or maybe I am just nosy!

You can get ReSharper 6 from Jetbrains.

BoxJs – A simple package management service that compiles CoffeeScript

There are lots of script loaders out there in the wild; require.js, ControlJS, $scriptJs to name a few. So why do people still create them and why would we care if someone created another one? Well, there are a few problems with script loaders.

  • They load scripts asynchronously, which means your larger scripts will load slower then small scripts and there is no guarantee if a piece of code will be ready and loaded. If you are dealing with lots of files, ie 20 (like most large sites) this is simply a nightmare.
  • Script Order. As mentioned before, the asynchronous way the scripts are loaded cannot guarantee load order. There are plugins that helps with this, but only to some extent.
  • Lots of HTTP requests. When you inspect the network traffic on your page loads, you will see as many HTTP requests as you have js files. Like I mentioned before, for most large sites this will end up being close to 30 or 40 requests!
  • You have to jump through hoops to get minification and most have no Coffeescript support.

So what makes BoxJs different? And why would I even need script loaders in the first place?

To answer the latter question, with libraries such as Coffeescript and Class extensions, developers are now ending up with lots of .js files. Rather than working on one .js file with 100s of methods, we want to follow a more structured and Object-Oriented approach. The problem is lots of files = lots of HTTP request = making site painfully slow. No amount of minification is going to solve this issue. Your files could be 2 bytes, but it will still need a HTTP GET request to fetch. To make matters worse, if you host your .js files in the same web application (which is about 99% of the cases) then the synchronous nature of how web servers work (other than Node.js) means all the files will wait in a queue and get processed one by one. That means your site is waiting even if you are loading in the files with an asynchronous client library such as require.js.

BoxJs, a script loader with a difference!

  • BoxJS has a client and a server side to it. The client gathers all the information it needs and sends 1 request to the server. The server will fetch all the JS files from your site, package them and minify the whole lot. So even if you have 50 JS files, it will end up with 1 request and 1 minified file.
  • Scripts are combined in the order you give them. Period. The synchronous way we fetch the files means guaranteed load order.
  • Isn’t it slow to fetch the scripts asynchronously and minify them on the fly? If you would need to do this every time a request is made, then absolutely. It is considerably slower, but that’s where the caching comes in. BoxJS will only ever fetch these files once. All subsequent requests will return the final boxed version which is lightning fast. But what if I make lots of changes to my JS files and don’t want to use the cached version? Simply ad a version=1.0 or version=1.1 in the BoxJs call and it will produce a new version. It will also cache the older versions till the next refresh, would is usually a few days.
  • Minification out of the box! BoxJS supports CoffeeScript too. It will automatically compile any .coffee files into .js files before it passes it to the packager to be minified and cached too!

This sounds cool, but how can I rely on the CDN?
BoxJS uses Amazon and Heroku which are very reliable. It scales depending on the amount of traffic. For some, this may still not be enough. They suggest grabbing the output of the BoxJs HTTP request and save that locally as one file for your production server. This would get rid of any worries.

There is no way this will stay free!
If usage of BoxJS becomes more popular, it will be difficult to stay free. But its almost certain that there will be a free version for sites with lower traffic. For now we will have to wait and see!

Selenium 2.0 released

Anyone who has seriously looked at automated browser testing will have come across selenium. For the last year or so it has been undergoing a pretty hefty transformation after merging with the webdriver project.

The result is a very fast (for browser based testing) reliable browser testing framework. Merging with webdriver brought native browser plugins which tremendously speeds up execution time. The plugin implements a REST API that allows the browser to be controlled from any of the client libraries or from Selenium Server – the remote control product.

One of the beauties of the Selenium project is that you can use it from pretty much any programming language. That is thanks to the REST API, as it is called over http pretty much any language can communicate with it so creating a library is not technically challenging. My language of choice is Ruby and the client library for it is excellent.

Today sees the release of version 2.0 which you can get from here. Ruby users can of course simply: gem install selenium-webdriver

 

Speed up typing on android with Swype

I have been using the Swype keyboard on my android for over a year now and almost immediately it became one of my must have apps.

It works by simply dragging your finger over the letters that make up a word and then it figures out what word you meant. It always shows you a list of the possible words, much like any other android keyboard does, so you can select an alternate if it was not what you meant.The swype keyboard being used

Recent updates have fixed a number of little niggles I have had with it – the word list is now unobtrusive (it used to display over the text). It is more accurate and it is faster – for those of us on older devices the speed boast is a great update.

Swype recently opened up their beta program to anyone so if you haven’t got it yet I would suggest you head over there and give it a go.

Image copyright of swype (http://swypeinc.com)

Using Dalli client with Heroku Memcache plugin in Sinatra

Today we are going to learn how to use the Memcache plugin on Heroku in a Sinatra app. We will be using a gem called ‘Dalli’. Memcache means ‘a general-purpose distributed memory caching system’ or in other words, something cool, fast and easy to use for speeding up your website. The way to do this is to cache certain parts of your application for a specific time period (ttl) so that users on your site would share the same data output and not need to trigger the data collection mechanism for each request.

Lets get to it! First we have to enable the Memcache plugin on heroku, so go ahead and find the plugin (see screenshot) and add it to your app.

The 5MB version is free and should be adequate enough for your average blog, brochure website. In a commercial website, don’t be surprised if the number is closer to 20GB.

Adding the Memcache plugin would add the necessary environment variables that ‘Dalli’ needs to use. So, lets go ahead and add ‘Dalli’ to our Gemfile….

gem 'dalli'

and install it…

bundle install

Next we need to set up ‘Dalli’ in our rack_app.rb

require 'sinatra'
require 'erb'
require 'dalli'

set :cache, Dalli::Client.new
set :enable_cache, true
set :short_ttl, 400
set :long_ttl, 4600

get '/' do
  erb :index
end

You can play around with the :short_ttl and :long_ttl times to find the best setting for your content, but it wouldn’t be frowned upon to cache certain things for hours or even days (ie. ‘Post of the week’ promotion on your homepage).

Next we simply need to store our data in ‘Dalli’ whenever we want to cache it, and remember to look for it before using our time consuming fetching mechanism. We will do this with a CacheKey.

get '/' do

  @todays_posts = get(:todays_posts)
  erb :index

end

def get(key, time_to_live=settings.long_ttl)

  if(!settings.enable_cache)
    return some_really_complicated_dataretrieval(key);
  end
  if(settings.cache.get(key) == nil)
    settings.cache.set(key, some_really_complicated_dataretrieval(key), ttl=time_to_live+rand(100))
  end

  return settings.cache.get(key)

end

Note that I added a rand(100) to the TTL. This is so that when the cache refreshes, it doesn’t refresh all the items with the same TTL at once. This would cause a small hiccup in performance at that time. Thats it! That should be all you need to use Memcache on Heroku using Sinatra.

Gnome 3 wallpaper extension

The gnome wallpaper selector in action

Javier Goday has just released a great new extension for Gnome 3.0. While many people don’t like Gnome 3.0 and think it is a step backwards, extensions like this are just one of the things that make it a really interesting proposition.

The extension allows you to very quickly change wallpapers sourced from either files stored on your computer or the vast collection of deviantart. Javier has augmented the overview with a “Wallpaper” tab which allows you to navigate and select the image you desire.

You can get the extension from here.