Created
November 10, 2012 19:52
-
-
Save cheald/4052272 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
require 'mongo' | |
require 'progress_bar' | |
db = Mongo::Connection.new["colors"] | |
identify = "identify %s" | |
convert = "convert %s -format %%c -colors 5 -colorspace LAB histogram:info:-" | |
def run(cmd, *args) | |
str = cmd % args | |
`#{str}` | |
end | |
db["pictures"].ensure_index("path", :unique => true) | |
db["pictures"].ensure_index([["matches.c1", Mongo::GEO2D]], :min => 0, :max => 255) | |
db["pictures"].ensure_index([["matches.c2", Mongo::GEO2D]], :min => 0, :max => 255) | |
db["pictures"].ensure_index([["matches.c3", Mongo::GEO2D]], :min => 0, :max => 255) | |
db["pictures"].ensure_index([["matches.c4", Mongo::GEO2D]], :min => 0, :max => 255) | |
db["pictures"].ensure_index([["matches.c5", Mongo::GEO2D]], :min => 0, :max => 255) | |
files = Dir.glob("in/**/*.jpg") | |
bar = ProgressBar.new files.length | |
files.each do |f| | |
_, x, y = run(identify, f).match(/(?<x>\d+)x(?<y>\d+)/).to_a | |
area = x.to_i * y.to_i | |
lines = run convert, f | |
matches = { | |
c1: [], | |
c2: [], | |
c3: [], | |
c4: [], | |
c5: [] | |
} | |
lines.split(/\n/).each do |line| | |
_, c, l, a, b = line.match(/(\d+):\s*\(\s*(\d+),\s*(\d+),\s*(\d+)/).to_a.map(&:to_i) | |
percent = c / area.to_f | |
5.times do |i| | |
cutoff = 0.6 - (i * 0.12) | |
if percent >= cutoff | |
matches[:"c#{i+1}"] << [a, b] | |
break | |
end | |
end | |
end | |
db["pictures"].insert(path: f, matches: matches) | |
bar.increment! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment