Skip to content

Instantly share code, notes, and snippets.

@liuliu
Created August 27, 2013 05:07
Show Gist options
  • Select an option

  • Save liuliu/6349801 to your computer and use it in GitHub Desktop.

Select an option

Save liuliu/6349801 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
exit unless ARGV.length == 1
files = Dir.glob(ARGV[0] + '/*.txt')
files.each do |file|
name = nil;
File.new(file).each_line do |line|
next if line[0] == '#'
name = line[line.rindex('/') + 1, line.rindex('"') - (line.rindex('/') + 1)] if line[0, 14].downcase == "image filename"
if line[0, 16].downcase == "bounding box for"
i = line.scan(/object\s*(\d+)/)[0][0].to_i
coord = line.scan(/\((\d+),\s*(\d+)\)\s*-\s*\((\d+),\s*(\d+)\)/)[0]
x = coord[0].to_i
y = coord[1].to_i
width = coord[2].to_i - coord[0].to_i
height = coord[3].to_i - coord[1].to_i
print name + " " + (x + width * 0.5).to_s + " " + (y + height * 0.5).to_s + " " + (width * 0.5).to_s + " " + (height * 0.5).to_s + " 0 0 0\n"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment