Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'pp'
- class Face < Array
- attr_accessor :width, :height
- def initialize(file)
- header = file.read(8)
- self.width, self.height = header.unpack("LL")
- data = file.read(8 * length)
- data.unpack("D" * length).each do |pixel|
- self << pixel
- end
- end
- def length
- width * height
- end
- def to_h
- {
- #width: width,
- #height: height,
- data: self[0..1]
- }
- end
- end
- class Faces < Array
- attr_accessor :count, :width, :height, :average
- def initialize(file)
- header = file.read(12)
- self.count, self.width, self.height = header.unpack('LLL')
- (1..count).each do |i|
- self << Face.new(file)
- end
- self.average = Face.new(file)
- end
- def to_h
- {
- count: count,
- #width: width,
- #height: height,
- average: average.to_h,
- data: self.map(&:to_h)
- }
- end
- end
- def error(faces1, faces2)
- faces1.each_with_index do |face, index|
- otherFace = faces2[index]
- scalar = otherFace[0] / face[0]
- error = 0.0
- face.each_with_index do |v, i|
- error += (v - otherFace[i] / scalar) ** 2
- end
- puts Math.sqrt(error)
- end
- end
- face1 = nil
- face2 = nil
- File.open(ARGV[0], 'rb') do |file|
- face1 = Faces.new(file)
- end
- File.open(ARGV[1], 'rb') do |file|
- face2 = Faces.new(file)
- end
- puts ARGV[0]
- pp face1.to_h
- puts ARGV[1]
- pp face2.to_h
- error(face1, face2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement