Skip to content

Instantly share code, notes, and snippets.

@blackwatertepes
Created March 27, 2014 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blackwatertepes/9818837 to your computer and use it in GitHub Desktop.
Save blackwatertepes/9818837 to your computer and use it in GitHub Desktop.
Seeing Machines CSV script
# This converts the CSV generated from job #1 into an input CSV for job #2.
# usage: ruby csv_to_csv.rb <csv input filename> <csv output filename>
# if there is no output file given, one will automatically be generated, with the filename of 'converted_[input filename]'
require 'rubygems'
require 'csv'
require 'json'
raise Exception, 'you must provide a csv' unless ARGV[0]
file_in = ARGV[0]
file_out = ARGV[1] || "converted_#{file_in}"
headers = []
csv_arr = []
CSV.open(file_in, "r") do |lines|
lines.each do |line|
if headers.empty?
headers = ["face_x", "face_y", "image_url"]
else
face_1_x = line[6]
face_1_y = line[8]
face_2_x = line[10]
face_2_y = line[12]
face_count = line[14].to_i
image_url = line[18]
if face_count == 2
csv_arr << [face_1_x, face_1_y, image_url]
puts csv_arr.last
csv_arr << [face_2_x, face_2_y, image_url]
puts csv_arr.last
end
end
end
end
# write out a csv file with a header that is the union of all the json keys
CSV.open(file_out, "w", :headers => headers, :write_headers => true) do |csv|
csv_arr.each do |line|
csv << line
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment