Skip to content

Instantly share code, notes, and snippets.

@liuliu
Created September 22, 2012 06:35
Show Gist options
  • Save liuliu/3765365 to your computer and use it in GitHub Desktop.
Save liuliu/3765365 to your computer and use it in GitHub Desktop.
extract annotations of VOC dataset into ccv understandable format
#!/usr/bin/env ruby
require "rexml/document"
prng = Random.new Random.new_seed
objfiles = Hash.new
objtrainfiles = Hash.new
objvalfiles = Hash.new
Dir.glob "Annotations/*.xml" do |filename|
file = File.new filename
xml = REXML::Document.new file.read
annotation = xml.root
imagefile = (annotation.get_text "filename").to_s
if prng.rand <= 0.8
annotation.each_element "object" do |object|
next if (object.get_text "difficult").to_s == "1" or (object.get_text "truncated").to_s == "1" # ignore difficult or truncated
objname = (object.get_text "name").to_s
objtrainfiles[objname] = File.new objname + ".train.txt", "w+" unless objtrainfiles.has_key? objname
xmin, ymin, xmax, ymax = (object.get_text "bndbox/xmin").to_s.to_i, (object.get_text "bndbox/ymin").to_s.to_i, (object.get_text "bndbox/xmax").to_s.to_i, (object.get_text "bndbox/ymax").to_s.to_i
objtrainfiles[objname] << imagefile + " " + xmin.to_s + " " + ymin.to_s + " " + (xmax - xmin + 1).to_s + " " + (ymax - ymin + 1).to_s + "\n"
end
else
annotation.each_element "object" do |object|
next if (object.get_text "difficult").to_s == "1" or (object.get_text "truncated").to_s == "1" # ignore difficult or truncated
objname = (object.get_text "name").to_s
objvalfiles[objname] = File.new objname + ".val.txt", "w+" unless objvalfiles.has_key? objname
xmin, ymin, xmax, ymax = (object.get_text "bndbox/xmin").to_s.to_i, (object.get_text "bndbox/ymin").to_s.to_i, (object.get_text "bndbox/xmax").to_s.to_i, (object.get_text "bndbox/ymax").to_s.to_i
objvalfiles[objname] << imagefile + " " + xmin.to_s + " " + ymin.to_s + " " + (xmax - xmin + 1).to_s + " " + (ymax - ymin + 1).to_s + "\n"
end
end
annotation.each_element "object" do |object|
next if (object.get_text "difficult").to_s == "1" or (object.get_text "truncated").to_s == "1" # ignore difficult or truncated
objname = (object.get_text "name").to_s
objfiles[objname] = File.new objname + ".txt", "w+" unless objfiles.has_key? objname
xmin, ymin, xmax, ymax = (object.get_text "bndbox/xmin").to_s.to_i, (object.get_text "bndbox/ymin").to_s.to_i, (object.get_text "bndbox/xmax").to_s.to_i, (object.get_text "bndbox/ymax").to_s.to_i
objfiles[objname] << imagefile + " " + xmin.to_s + " " + ymin.to_s + " " + (xmax - xmin + 1).to_s + " " + (ymax - ymin + 1).to_s + "\n"
end
end
objfiles.each do |key, value|
value.close
end
objtrainfiles.each do |key, value|
value.close
end
objvalfiles.each do |key, value|
value.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment