Creating ruby gui applications – an introduction to ruby shoes

As part of my effort to detect objects using opencv I have found myself performing a mind numbing task and as every lazy programmer knows this is the time to build a tool.

After a bit of investigation into the various gui libraries for ruby I have found one that seems pretty good called Ruby Shoes. There seem to be a few variants of this particular library. Strangely the classic Red Ruby Shoes version does not act like a regular ruby library. It seems to be a runtime that uses ruby. The Green Shoes variant however is a straightforward ruby library. You can gem install green_shoes and get up and running straight away.

A gui application in Ruby Shoes is called a shoe app and is started by calling:

Shoes.app do
  # application code goes here
  title "Hello World!"
end

As I want to create an image selection tool I need to be able to track mouse clicks, movements and releases and then draw a rectangle as this is going on.

After playing around for a while I came up with this:

require 'green_shoes'
Shoes.app  do
  title "Selection tool"
  image("your_image.jpg").click do |button, click_left, click_top|
    mouse_down = true
    release do
      mouse_down = false
    end

    nofill
    @selection = rect :left => mouse[1], :top => mouse[2], :width => 1, :height => 1

    motion do |move_left, move_top|
      if mouse_down
        width =  move_left - click_left
        height =  move_top - click_top

        width = 1 if width < 1
        height = 1 if height < 1

        @selection.style(:width => width, :height => height)
      end
    end
  end
end

There are probably more efficient ways to handle the release but this was the best I could come up with in half an hour.

I found the manual quite helpful to figure all this out.

User authorisation with Sinatra

Today I needed to add authorisation of specific routes to my sinatra application. After having a look around I couldn’t find anything I really liked. So I sat down and did the same thing any self respecting developer would – and wrote one myself.

I was actually surprised how easy it was and the result is simple-authorisation (gem install simple-authorisation).

To include this in your app simply:

require 'simple-authorisation'

And then in your sinatra config:

configure do
Simple::Authorisation.route '/', :deny => ['?'], :allow => ['*']
Simple::Authorisation.route '/login', :allow => ['?']
Simple::Authorisation.route '/logout', :allow => ['?']
Simple::Authorisation.route '/admin', :allow => ['*'], :deny => ['?']
end

Adding configuration for a route / will actually apply it to all its child routes. Adding additional routes will overwrite that setting. ‘?’ means anonymous users and ‘*’ means any logged in user.

The last thing you need to do is add a current_user method to your application:

class Application < Sinatra::Application
def current_user
session[:user]
end
end

Now simple-authorisation knows how to get the current user it can do its thing!

I know this is very basic but so far it is all I need. The next thing to add will be specific user groups.

If you like the look of this and have something to add then please fork this on github.

Detecting objects in a photograph with opencv part 2

Eyes being detected using opencv

As I found out last time it is quite easy to detect objects in a photgraph. The difficult part is to create a classifier that will do this for the particular object you are looking for.

I am still working on building a simple object classifier and I will share how I have done this soon. In the mean time you may like to whet your appettite with some of the classifiers that ship as part of opencv.

You can find classifiers that can detect faces, eyes and whole bodies in opencvs source control. For the purpose of demonstration I will use the eye detector. I am also using ruby-opencv from this repository.

In your ruby file (eyes.rb for example) include opencv:

require "opencv"

Load the classifier into opencv:

classifier = 'haarcascade_eye.xml'
detector = OpenCV::CvHaarClassifierCascade::load(classifier)

Load the image you want to run the classifier against:

image = OpenCV::IplImage.load('sample_image.jpg')

We can now detect objects by:

detector.detect_objects(image) do |region|
color = OpenCV::CvColor::Blue
image.rectangle! region.top_left, region.bottom_right, :color => color
end

This will draw blue rectangles over around the eyes. We can then save that image:

image.save_image('eyes_detected.jpg')

The result? Yours truly with square blue glasses!

Robotium 2.4 Released (automated android testing)

As someone who currently spends a lot of time doing testing I always appreciate the effort people put into building automated testing frameworks. There is nothing as boring as clicking on things and then checking the results…

Robotium is an automated test framework for Android. It allows you to write JUnit tests that start your application click on items on screen and from the menu and verify results. I have used it to test an application we are developing and it works quite well.

This release is mainly a maintenance release fixing bugs and so forth:

Hello Robotium Developers,

Robotium 2.4 is now released! Most of the work in this release has
gone into correcting defects and improving Robotium. The assert
methods have been re-designed in order to improve stability. I want to
thank everyone that have reported issues. They have all been corrected
and Robotium is now better then ever!

The only new functionality in this release is: waitForActivity(String
name) which includes a default timeout of 20 seconds (similar to the
other waitFor methods).

I hope you will enjoy this release!

http://code.google.com/p/robotium/downloads/list

Sincerely,
Renas

(exert from the Robotium mailing list)

The one big gap in Robotium is remote control support. Being able to drive Robotium remotely would work quite nicely from situations where you want to write your tests using another language (like ruby). I have had a go at bringing remote control support to robotium by taking a similar approach as the webdriver implementation for android. However there are many security concerns that prevent this being done in a generic way. If someone can find a way around these problems then Robotium would become an even more exciting proposition.

You can download Robotium from here

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.

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)

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.