-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathasmimage.py
69 lines (61 loc) · 2 KB
/
asmimage.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
import numpy
from collections import *
import pandas as pd
import binascii
def getMatrixfrom_bin(filename, width = 512, oneRow = False):
with open(filename, 'rb') as f:
content = f.read()
hexst = binascii.hexlify(content)
fh = numpy.array([int(hexst[i:i+2],16) for i in range(0, len(hexst), 2)])
if oneRow is False:
rn = len(fh)/width
fh = numpy.reshape(fh[:rn*width],(-1,width))
fh = numpy.uint8(fh)
return fh
def getMatrixfrom_asm(filename, startindex = 0, pixnum = 5000):
with open(filename, 'rb') as f:
f.seek(startindex, 0)
content = f.read(pixnum)
hexst = binascii.hexlify(content)
fh = numpy.array([int(hexst[i:i+2],16) for i in range(0, len(hexst), 2)])
fh = numpy.uint8(fh)
return fh
def getMatrixfrom_hex(filename, width):
hexar = []
with open(filename,'rb') as f:
for line in f:
hexar.extend(int(el,16) for el in line.split()[1:] if el != "??")
rn = len(hexar)/width
fh = numpy.reshape(hexar[:rn*width],(-1,width))
fh = numpy.uint8(fh)
return fh
def read_hexbytes(filename):
hexar = []
with open(filename,'rb') as f:
for line in f:
hexar.extend(int(el,16) for el in line.split()[1:] if el != "??")
rn = len(hexar)/256
fh = numpy.reshape(hexar[:rn*256],(-1,256))
fh = numpy.uint8(fh)
return fh
basepath = "/home/moon/subtrain/"
mapimg = defaultdict(list)
subtrain = pd.read_csv('subtrainLabels.csv')
i = 0
for sid in subtrain.Id:
i += 1
print "dealing with {0}th file...".format(str(i))
filename = basepath + sid + ".asm"
im = getMatrixfrom_asm(filename, startindex = 0, pixnum = 1500)
mapimg[sid] = im
dataframelist = []
for sid,imf in mapimg.iteritems():
standard = {}
standard["Id"] = sid
for index,value in enumerate(imf):
colName = "pix{0}".format(str(index))
standard[colName] = value
dataframelist.append(standard)
df = pd.DataFrame(dataframelist)
df.to_csv("imgfeature.csv",index=False)