Created
March 27, 2014 20:01
-
-
Save blackwatertepes/9817004 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This converts a csv file to a slightly different csv. | |
#usage: ruby csv_to_csv.rb <csv input filename> <csv output filename> | |
# leaving out the output filename will result in a csv file with the same name as the input, and will have 'converted' in front of it. | |
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| | |
print "face_count: ", line[6], "\tface_count:confidence: ", line[7], "\timage_url: ", line[13], "\n" | |
if headers.empty? | |
headers = ["face_count", "face_count_confidence", "face_coordinates", "face_confidence", "image_url"] | |
else | |
face_count = line[6].to_i | |
if face_count > 0 | |
image_annotation = JSON.parse(line[8]) | |
coor_confs = image_annotation["features"].map{|feature| {coordinates: feature["geometry"]["coordinates"], confidence: feature["properties"]["confidence"]}}.sort{|a,b| a[:confidence] <=> b[:confidence]}.reverse.take(face_count) | |
coor_confs.each do |coor_conf| | |
csv_arr << [face_count, line[7], coor_conf[:coordinates], coor_conf[:confidence], line[13]] | |
end | |
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