Class: Treyja::Command::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/treyja/command/image.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reader, output_dir, options = {}) ⇒ Image

Returns a new instance of Image.



8
9
10
11
12
# File 'lib/treyja/command/image.rb', line 8

def initialize reader, output_dir, options = {}
  @reader = reader
  @output_dir = output_dir
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/treyja/command/image.rb', line 6

def options
  @options
end

#output_dirObject (readonly)

Returns the value of attribute output_dir.



6
7
8
# File 'lib/treyja/command/image.rb', line 6

def output_dir
  @output_dir
end

#readerObject (readonly)

Returns the value of attribute reader.



6
7
8
# File 'lib/treyja/command/image.rb', line 6

def reader
  @reader
end

Instance Method Details

#chunk(type, data) ⇒ Object



89
90
91
# File 'lib/treyja/command/image.rb', line 89

def chunk(type, data)
  [data.bytesize, type, data, Zlib.crc32(type + data)].pack("NA4A*N")
end

#runObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/treyja/command/image.rb', line 14

def run
  FileUtils.mkdir_p output_dir

  reader.each_with_index do |tensors, i|
    tensors.tensors.each_with_index do |tensor, j|
      n = tensor.dims.drop(3).inject(1, :*) # Drop width, height and channel and fold the rest
      n.times.each do |k|
        postfix = ([i, j] + (n > 1 ? [k] : [])).map(&:to_s).join('-')
        dst = File.join(output_dir, "image-#{postfix}.png")
        write_image tensor, k, dst
      end
    end
  end
  reader.each do |ts|
    p ts
    STDOUT.flush
  end
end