-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathuint8-to-binary.rb
42 lines (30 loc) · 978 Bytes
/
uint8-to-binary.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Converts array of image pixels to binary format
# Run separately for Training Set and Test Set
require 'CSV'
class Integer
def to_bin(width)
'%0*b' % [width, self]
end
end
DATA_SET_UINT8 = "/Users/aman/Desktop/Synapse/fer2013/fer2013.csv"
DATA_SET_BIN = "/Users/aman/Desktop/Synapse/fer2013/fer2013.bin"
# DATA_SET_BIN = "/Users/aman/Desktop/Synapse/fer2013/test_batch.bin" # Test data
puts "Reading csv ..."
File.open(DATA_SET_BIN, 'wb') do |output|
ctr = 1
CSV.foreach(DATA_SET_UINT8, headers: true) do |row|
emotion = row["emotion"]
pixels = row["pixels"]
usage = row["Usage"]
if usage == 'Training'
# if usage == 'PublicTest' # Test data
arr = [emotion.to_i.to_bin(8)] + pixels.split(' ').map { |i| i.to_i.to_bin(8) }
puts "#{ctr} : #{emotion} | #{pixels[0..10]} ... | #{usage} | bytes=#{arr.count}"
output.write [arr.join].pack("B*")
ctr += 1
else
break
end
end
end
puts "Done!"