Class: MnistDigit

Inherits:
Object
  • Object
show all
Defined in:
lib/mnist_digit.rb

Instance Method Summary collapse

Constructor Details

#initialize(label, pixels) ⇒ MnistDigit

Returns a new instance of MnistDigit.



4
5
6
7
# File 'lib/mnist_digit.rb', line 4

def initialize(label, pixels)
  @label = label
  @pixels = pixels
end

Instance Method Details

#ascii_imageObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mnist_digit.rb', line 23

def ascii_image
  img = ""
  pixels.each_with_index do |value, index|
    img += "\n" if index > 0 and index % 28 == 0
    img += gray(value)
  end
  img_array = img.split("\n")
  new_img_array = []
  found = false
  img_array.each do |line|
    next if !found and line.strip.empty?
    found = true
    new_img_array.push(line)
  end
  found = false
  new_img_array2 = []
  new_img_array.reverse.each do |line|
    next if !found and line.strip.empty?
    found = true
    new_img_array2.push(line)
  end
  
  img =  " ____________________________ \n"
  img += "|            #{label}               |\n"
  
  img += "|----------------------------|\n"
  img += "|                            |\n"
  
  new_img_array2.reverse.each do |line|
    img += "|#{line}|\n"
  end
  
  img += "|____________________________|\n"
  
end

#labelObject



9
10
11
# File 'lib/mnist_digit.rb', line 9

def label 
  @label
end

#pixel_image(no_zeros = false) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mnist_digit.rb', line 59

def pixel_image(no_zeros = false)
  img =  " _________________________________________________________________________________________________________________ \n"
  img += "|                                                  #{label}                                                              |\n"
  img += "|-----------------------------------------------------------------------------------------------------------------|\n"
  
  pixels.each_slice(28).to_a.each do |array|
    img += "| %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d |\n" % array
  end
  
  img +=  "|_________________________________________________________________________________________________________________|\n"
  
  if not no_zeros
    img
  else
    img.gsub(/ 0 /, "   ")
  end
end

#pixelsObject



13
14
15
# File 'lib/mnist_digit.rb', line 13

def pixels
  @pixels
end

#png_image(white_background = false) ⇒ Object



17
18
19
20
21
# File 'lib/mnist_digit.rb', line 17

def png_image(white_background = false)
  require_relative 'draw_image.rb'
  puts
  DrawImage.draw(pixels, white_background)
end