Computer Vision using Ruby and libJIT

The RubyConf 2009 took place near San Francisco. I gave a talk about Computer vision using Ruby and libJIT. Here is the abstract of the talk:

Ruby originated in Japan, the country which is world-leading in robotic research. It suggests itself to put the two together and to start using Ruby as a language to program robots. However at the moment the performance of available Ruby interpreters is not sufficient. It is hard to achieve performance comparable to compiled C++-code since manipulation of Ruby-integers and Ruby-arrays requires frequent bounds-checking. It can be shown that universal bounds-check elimination is actually impossible.

This talk presents HornetsEye which is a Ruby-extension facilitating the development of real-time machine vision algorithms for future robotic applications. HornetsEye offers I/O facilities to capture and display videos. HornetsEye also can be integrated into GUI-applications developed with Qt4-QtRuby. Furthermore there is a set of Ruby classes which provides the means to compose native datatypes and specify operations on them. The libJIT just-in-time compiler is used to achieve real-time performance. The project was inspired by NArray and ruby-libjit.

The software is called HornetsEye and it is available under the terms and conditions of the GPL.

Feel free to leave comments below.

Discussion

Lazy computation

Future work on HornetsEye could be about using lazy computation to avoid the creation of intermediate results when doing array operations. Here’s a small prototype implementation. Let me know if you have any suggestions.

class Promise
  def initialize( size, &action )
    @size, @action = size, action
  end
  def inspect
    "Promise(#{@size})"
  end
  def []( index )
    @action.call index
  end
  def demand
    Seq[ *( 0 ... @size ).collect { |i| @action.call i } ]
  end
  def -@
    Promise.new( @size ) { |i| -@action.call( i ) }
  end
  def fill!( value = 0 )
    @action = proc { |i| value }
    self
  end
end

class Seq
  class << self
    def []( *args )
      retval = new args.size
      retval.data = args
      retval
    end
    def new( size )
      unless Thread.current[ :lazy ]
        super size
      else
        Promise.new( size ) { 0 }
      end
    end
  end
  attr_accessor :data
  def initialize( size )
    @data = [ 0 ] * size
  end
  def inspect
    "Seq#{@data.inspect}"
  end
  def []( index )
    @data[ index ]
  end
  def -@
    Seq[ *@data.collect { |x| -x } ]
  end
  def fill!( value = 0 )
    @data = [ value ] * @data.size
    self
  end
end

def lazy
  previous_state = Thread.current[ :lazy ]
  Thread.current[ :lazy ] = true
  retval = yield
  Thread.current[ :lazy ] = previous_state
  if retval.is_a? Array
    retval.collect { |x| x.demand }
  else
    retval.demand
  end
end
# Ordinary computation
a = -Seq.new( 3 ).fill!( 1 )
# Simple lazy computation
b = lazy { -Seq.new( 3 ).fill!( 1 ) }
# ...

Reflection

Using the following code one can use reflection to interpret Ruby code in order to for example translate it to machine code later.

class Const
  attr_accessor :inspect
  alias_method :to_s, :inspect
  def initialize( s )
    @inspect = s.to_s
  end
  def method_missing( name, *args )
    str = "#{ self }.#{ name }"
    unless args.empty?
      str += "( #{args.join ', '} )"
    end
    Const.new str
  end
  def coerce( y )
    return Const.new( y ), self
  end
end
a = Const.new 'a'
# a
b = Const.new 'b'
# b
-a
# a.-@
a + b
# a.+( b )
a[ 2 ]
# a[]( 2 )
2 * a
# 2.*( a )
2 * a + b
# 2.*( a ).+( b )
2 * ( a + b )
# 2.*( a.+( b ) )

Unfortunately Ruby reflection does not capture creation of variables, assignment of values, and control structures such as while or if-then-else.

GPU

One could use “monkey patching” to add GPU support (using OpenCL) in a transparent way. Furthermore it would be desirable to make use of multi-core computers. Ruby 1.9 supports native threads but there is a global interpreter lock on extension calls. So multi-threading has to be implemented explicitely if it is to be used in such a Ruby extension.

Technical Issues

It was suggested to me to

  • make the array classes available separately
  • to use Rubygems for packaging the software and Gemcutter for publishing the software
  • to use Git for version control and to host the repositories on Github
  • to use ffi so that the extension works with different Ruby VMs (JRuby, Rubinius, …)

Update:

The video of the talk is now available!

Tauss leaves social democratic party over censorship row

Jörg Tauss left the German Social Democratic Party. Tauss criticised the party for voting in favour of a controversial law for fighting child pornography which will give the German criminal investigation department the power to block domains, reroute requests to a web page displaying a stop sign, and log requests to that page. The law will make Germany much more similar to countries like Australia, China, and Iran, were the government spies on its citizens. Tauss also pointed out that the parliament is blatantly ignoring the biggest petition ever being submitted in Germany (more than 130,000 signatures).

Adding to the controversy, Tauss as well as his political adversary Ursula von der Leyen got into trouble because it is illegal to possess or distribute child pornography. The right to obtain material for investigative purposes requires special legal authorisation. Dealers of child pornography usually require potential customers to break the law by providing illegal material themselves before trusting them.

I’ve tried to translate Tauss’ speech to English:

Mrs president! Dear colleagues! I am voting against this law - in the meantime one must say: I have voted against this law - because it does not have anything to do with its caption. The goal, fighting child pornography, was - contrary to all accussations and the inquiries against me - the prime motivation for me to concern myself with the internet.

I voted against this law not because I would not unconditionally support the goal but because that it is in fact - colleague Mrs Krogmann, I didn't understand your objections at this point - a law which would introduce a structure for surveillance in the free part of Germany for the first time since 1949.

I have voted against this law because it does not prevent child pornography. This is a legend which our minister - I think it's a shame that she is not present - has created. She has said to all request which have been made - for example by the FDP party - that she wasn't presented with any insights. I submit: If one does not have any insights, one should participate in the debate; that would be the least one ought to demand.

(applause by deputy Renate Künast [BÜNDNIS 90/DIE GRÜNEN])

The „Stopp!“-sign which is to appear when one visits an indexed site, enables perpetrators to notice wether they have been discovered and have to change their address. The association of German detectives has justifiably ascertained: The search for perpetrators will be made more difficult by this law. That means that exactly what you actually want, colleague Mrs Noll - a goal we all have -, is going to be prevented.

I am very sad about it, that only a single expert was cited. All other experts such as professor Sieber from the renown Max-Planck-Institute for National and International Criminal Law have pointed out the considerable legal and technical problems. It was him, colleague Mrs Krogmann, who stimulated the dialog, which you are demanding, in the first place. Indeed: One should have held a dialog before arriving at such a law.

I have voted against this law because it suspends the separation of powers. For the first time the executive branch has control itself. The position at the commissary for data protection is - I have spoken about it already - not suitable for solving the problems springing up.

Obviously it is only about creating authorities and positions in spite of the German Federal Criminal Office Law. If I consider how many laws of this coalition have failed in the Federal Constitutional Court, I must say: I would have wished that one does not simply say: „Constitutionally everything is fine“, but that one checks that thoroughly.

I have voted against this law because as I said with it surveillance structures are created. The Wall Street Journal has mentioned - without justification in my opinion; but nevertheless; it shows, that it is being debatted internationally - Germany in one breath with China and Iran. I consider this to be extremeley problematical. But anyone who gets upset about it may look into that law. It is so that the providers are forced to negotiate with the police about technical guidelines. If one knows how the treaties where extorted from the providers - in a manner I have talked about earlier as well -, one can make it clear, what the problem is like: Here proliferation-prone technology is made available - proliferation-prone technology which can be gratefully accepted by all dictatorships of this world. That is irresponsible.

I have voted against this law, because the technology does not work in the closed circles of child pornographers, but it is able to filter democracy and freedom in many parts of the world.

I have voted against this law, because now additional treaties which have come into existence, in a manner as I have expressed before, are supposed to be legitimized with a law subsequently.

I have voted against this law because it was brought about against the advice and the warnings of almost all experts - at least the large majority of experts -. There is the petition of the 134,000 which is likely going to be skipped over coldly like a dog's muzzle this evening. The votes of these 134,000 and many oher young people, who are falling behind in resignation facing this project today, I don't want to - apparently as one of the rare members of parliament of the great coalition - trample on with my feet. I don't resign in front of ministerial incompetence. Please continue to fight an infrastructure for censorship. Nie kämpft es sich schlecht für Freiheit und Recht! That's why I voted against it.

Deleting instead of blocking would be the motto. That what is happening today is a undesirable development. I can only agree with the Green Party, which I want to thank for the application for a vote by name. Here it is not about the internet being a law-free zone; here it is only about making the internet, increasingly and with more and more restrictive measures - they were intensified in the last years, and this is expected to continue, as the remarks from Strobl et al. are showing -, a civil-right-free zone.

I have told my party that I am relatively thankful that I ...

(Vice-president Dr. h. c. Susanne Kastner: Colleague Mr Tauss, the five minutes for your personal statement have expired.)

Yes. Time's up. This was my last memo in this parliament. Surely many are happy about it. Conversely I would, Mrs president, like to say at this point: It was fun sometimes. We have accomplished much for education, science, and research. To those I have worked together with in good spirit I can only say: We were successful in these areas.

But this law I regard as a deplorable hour. Considering this it is by all means easy for me to leave the German Parliament. Nonetheless I wish you all the best personally!

Thank you.

Updates

In the meantime Tauss joined the German Pirate Party.

Pirate Party

See also:

Free software robotic arm

Industrial robotics still is dominated by proprietary “off-the-shelf” solutions. It is very common to find that a single robot is controlled by several unrelated software products which do not interoperate with each other. Furthermore the control PCs are running Windows XP which is scheduled using a hardware task switcher and the VxWorks software. Basically the situation is a set of hacks which reflects the political situation of the marketplace rather than the demands of the technical problem at hand.

The situation is very reminiscent of medieval guilds. The medieval guilds were small communities (companies) where an apprentice had to stay with a master for many years to get inaugurated into the secrets (intellectual property) of the trade. The career options were extremely limited since the guilds had the exclusive right of selling products on the market (OEM deals). After many centuries of moderate progress, industrial mass production and global trading finally swept away the rigid guild system. But I am digressing.

[Katana robot arm] There is a real opportunity for a robotic company to make a difference by simply providing documentation and free software along with their product. Neuronics in Switzerland is maybe the only company currently targeting the industrial market with an industrial precision (0.1 mm) robotic arm which comes with free software and documentation. The product is called Katana. The Katana robot arm can carry 400 grams and it can be controlled using TCP/IP (i.e. XML-RPC) and the client library. The robot’s API supports point-to-point operation, customisation of speed and acceleration, as well as spline-motions in configuration space. There also is a native API for programming the control board directly and finally there is the source code of the control software itself.

Neuronics is located in Zürich and there is potential for a collaboration with local universities.

See also:

Obama speech at National Archives

[Obama speaks at National Archives]

Click on the picture to watch the video at The Huffington Post.

The Whitehouse website provides an article as well as the transcript of the speech.

Update:

A video of the speech is now available from the Whitehouse website itself

Public media is deeply concerned that the current US government is lacking commitment to stop the unconstitutional practice of prolonged detention.

Also see PBS-documentary: Bush’s War.

Update:

The video is now only on Youtube.

The Hunt for Gollum

Today the fan film The Hunt for Gollum was released on the web. The film premiere is at Sci-Fi-London. The 40 minutes film was created by volunteer actors and artists in UK and it even includes some computer graphics.

The Hunt for Gollum

The scenery of London, Essex, Wales and Epping Forest served as a replacement for Middle Earth. The scenes with the nice wheather were generated by Stefan Menzel in Sheffield using the Terragen software.

If this is not enough, there is another upcoming fan film called Born of Hope coming out autumn this year.

See also: