Commit 7e755616f4ae8dd3176c3dd1263ddd7757b23261

Authored by Chunk
2 parents 51708346 95f76ce8
Exists in master

staged.

mdata/ANALYSIS.py
... ... @@ -1,127 +0,0 @@
1   -__author__ = 'chunk'
2   -
3   -import os
4   -import numpy as np
5   -from numpy.random import randn
6   -import pandas as pd
7   -from scipy import stats
8   -import matplotlib as mpl
9   -import matplotlib.pyplot as plt
10   -import seaborn as sns
11   -
12   -np.random.seed(sum(map(ord, "whoami")))
13   -plt.ticklabel_format(style='sci', axis='both', scilimits=(1, 4))
14   -
15   -package_dir = os.path.dirname(os.path.abspath(__file__))
16   -
17   -
18   -def anal_ILSVRC():
19   - df_ILS = pd.read_csv('../res/file-tag.tsv', names=['hash', 'width', 'height', 'size', 'quality'], sep='\t')
20   - print df_ILS[df_ILS.size < 2000000]
21   - print df_ILS.describe()
22   - # df_ILS.boxplot(column='size')
23   - # plt.show()
24   -
25   - length = df_ILS.shape[0]
26   -
27   - # print type(df_ILS.size.order()) # <class 'pandas.core.series.Series'>
28   - print df_ILS.size.order().iloc[map(lambda x: x * length, [1.0 / 3, 2.0 / 3, 0.9999])]
29   - """
30   - 7082 108514
31   - 3826 150389
32   - 8761 4814541
33   - """
34   -
35   - print df_ILS.size[df_ILS.size <= 102400].count()
36   - print df_ILS.size[(df_ILS['size'] > 102400) & (df_ILS['size'] <= 153600)].count()
37   - print df_ILS.size[df_ILS.size > 153600].count()
38   -
39   - """
40   - (-,100K,150K,+):
41   - 4519
42   - 6163
43   - 4831
44   - (-,100K,500K,+):
45   - 4519
46   - 10932
47   - 62
48   - """
49   -
50   - ## Quality
51   - print df_ILS.quality.order().iloc[map(lambda x: x * length, [1.0 / 3, 2.0 / 3, 0.9999])]
52   - """
53   - 13507 96
54   - 831 96
55   - 6529 100
56   - """
57   - df_new = df_ILS.sort(['size', 'quality'], ascending=True)
58   - print df_new
59   -
60   - rand_class = stats.bernoulli.rvs(0.3, size=length)
61   - # df_new['class'] = pd.Series(rand_class, index=df_new.index)
62   - df_new['class'] = rand_class
63   -
64   - print rand_class[:100]
65   - print df_new
66   -
67   - df_new.to_csv('../res/test.tsv', header=False, index=False, sep='\t')
68   -
69   -
70   -def anal_ILSVRC_Test():
71   - df_ILS_T = pd.read_csv('../res/file-tag-test.tsv', names=['hash', 'width', 'height', 'size', 'quality','class'], sep='\t')
72   - print df_ILS_T
73   - print df_ILS_T.size.describe()
74   -
75   - print df_ILS_T.size[df_ILS_T.size <= 102400].count()
76   - print df_ILS_T.size[(df_ILS_T['size'] > 102400) & (df_ILS_T['size'] <= 153600)].count()
77   - print df_ILS_T.size[df_ILS_T.size > 153600].count()
78   -
79   - length = df_ILS_T.shape[0]
80   - df_ILS_T['class2'] = np.zeros(length, np.int32)
81   - df_ILS_T.to_csv('../res/file-tag-test.tsv', header=False, index=False, sep='\t')
82   -
83   -def anal_0000():
84   - df_ILS = pd.read_csv('../res/file-tag-test.tsv', names=['hash', 'width', 'height', 'size', 'quality','chosen','class'], sep='\t')
85   - length = df_ILS.shape[0]
86   - print df_ILS.size.describe()
87   - print df_ILS.size.order().iloc[map(lambda x: x * length, [1.0 / 3, 2.0 / 3, 0.9999])]
88   -
89   - print df_ILS.size[df_ILS.size <= 166500].count()
90   - print df_ILS.size[(df_ILS['size'] > 166500) & (df_ILS['size'] <= 187500)].count()
91   - print df_ILS.size[df_ILS.size > 187500].count()
92   -
93   - df_ILS.hist(column='size',bins=100)
94   - plt.show()
95   -
96   -
97   -
98   -def pre_crop():
99   - df_ILS = pd.read_csv('../res/file-tag-test.tsv', names=['hash', 'width', 'height', 'size', 'quality','chosen','class'], sep='\t')
100   - print df_ILS.shape
101   - print df_ILS[(df_ILS['width'] >= 300) & (df_ILS['height'] >= 300)].shape
102   -
103   - # 300x300 4213 0.917 *
104   - # 200x200 4534 0.987
105   - # 400x400 932 0.202
106   -
107   -
108   -
109   -
110   -if __name__ == '__main__':
111   - # anal_ILSVRC()
112   - # anal_ILSVRC_Test()
113   - anal_0000()
114   - # print timeit.timeit("anal_ILSVRC()", setup="from __main__ import anal_ILSVRC", number=1)
115   -
116   -
117   - # pre_crop()
118   - pass
119   -
120   -
121   -
122   -
123   -
124   -
125   -
126   -
127   -
msteg/__init__.py
... ... @@ -7,7 +7,8 @@ from ..common import *
7 7  
8 8 __all__ = ['StegBase', 'sample_key']
9 9  
10   -sample_key = [46812L, 20559L, 31360L, 16681L, 27536L, 39553L, 5427L, 63029L, 56572L, 36476L, 25695L, 61908L, 63014L,
  10 +sample_key = [46812L, 20559L, 31360L, 16681L, 27536L, 39553L, 5427L, 63029L, 56572L, 36476L, 25695L,
  11 + 61908L, 63014L,
11 12 5908L, 59816L, 56765L]
12 13  
13 14  
... ... @@ -38,7 +39,6 @@ class StegBase(object):
38 39 self.capacity = np.sum(self.cov_data != 0) - np.size(self.cov_data) / 64
39 40 return self.cov_data
40 41  
41   -
42 42 def _get_hid_data(self, src_hidden, frommem=False):
43 43 """
44 44 Returnsthe secret data as byte sequence.
... ... @@ -55,19 +55,20 @@ class StegBase(object):
55 55 self.hid_data = np.array(raw, dtype=np.uint8)
56 56  
57 57 if np.size(self.hid_data) * 8 > self.capacity:
58   - raise Exception("Cover image is too small to embed data.Cannot fit %d bits in %d NZ-DCT coefficients" % (
59   - np.size(self.hid_data) * 8, self.capacity))
  58 + raise Exception(
  59 + "Cover image is too small to embed data.Cannot fit %d bits in %d NZ-DCT coefficients" % (
  60 + np.size(self.hid_data) * 8, self.capacity))
60 61 return self.hid_data
61 62  
62   -
63   - def _post_embed_actions(self, src_cover, src_hidden, tgt_stego):
  63 + def _post_embed_actions(self, src_cover, src_hidden, tgt_stego, frommem=False):
64 64 """
65 65 This function isn't named very accurately. It actually calls the
66 66 _raw_embed function in inherited classes.
67 67 """
68 68 try:
69 69 cov_data = self._get_cov_data(src_cover)
70   - hid_data = self._get_hid_data(src_hidden)
  70 + # hid_data = self._get_hid_data(src_hidden)
  71 + hid_data = self._get_hid_data(src_hidden, frommem)
71 72 # print hid_data.dtype,type(hid_data),hid_data.tolist()
72 73 cov_data, bits_cnt = self._raw_embed(cov_data, hid_data)
73 74  
... ... @@ -130,7 +131,6 @@ class StegBase(object):
130 131 # print hid_data.dtype,type(hid_data),hid_data.tolist()
131 132 hid_data[4:].tofile(tgt_hidden)
132 133  
133   -
134 134 self._display_rate('extract', self.capacity, bits_cnt)
135 135  
136 136 # self._display_stats("extracted", emb_size,
... ... @@ -140,7 +140,6 @@ class StegBase(object):
140 140 print "Exception when extracting!"
141 141 raise
142 142  
143   -
144 143 def _looks_like_jpeg(self, path):
145 144 try:
146 145 with open(path, 'r') as f:
... ...
msteg/steganography/F3.py
1 1 __author__ = 'chunk'
2 2  
3   -
4 3 import math
5 4 import numpy as np
6 5 from .. import *
... ... @@ -18,7 +17,7 @@ class F3(StegBase):
18 17 """
19 18 StegBase.__init__(self)
20 19  
21   - def embed_raw_data(self, src_cover, src_hidden, tgt_stego):
  20 + def embed_raw_data(self, src_cover, src_hidden, tgt_stego, frommem=False):
22 21 """ This method embeds arbitrary data into a cover image.
23 22 The cover image must be a JPEG.
24 23  
... ... @@ -33,7 +32,7 @@ class F3(StegBase):
33 32 lossy compression.
34 33 """
35 34 self.t0 = time.time()
36   - self._post_embed_actions(src_cover, src_hidden, tgt_stego)
  35 + self._post_embed_actions(src_cover, src_hidden, tgt_stego, frommem)
37 36  
38 37 def extract_raw_data(self, src_steg, tgt_hidden):
39 38 """ This method extracts secret data from a stego image. It is
... ... @@ -65,7 +64,7 @@ class F3(StegBase):
65 64 i += 1
66 65 if i == hid_data.size: break
67 66  
68   - return cov_data,i
  67 + return cov_data, i
69 68  
70 69 def _raw_extract(self, steg_data, num_bits):
71 70 """
... ... @@ -81,7 +80,7 @@ class F3(StegBase):
81 80 hid_data[j] = x & 1
82 81 j = j + 1
83 82  
84   - return hid_data,j
  83 + return hid_data, j
85 84  
86 85 def __str__(self):
87 86 return 'F3'
... ...
msteg/steganography/F4.py
... ... @@ -13,7 +13,7 @@ class F4(StegBase):
13 13 with the F3 algorithm and <i>extract_raw_data</i> to extract data
14 14 which was embedded previously. """
15 15  
16   - def __init__(self, key=sample_key):
  16 + def __init__(self, key=None):
17 17 """
18 18 Constructor of the F3 class.
19 19 """
... ... @@ -27,15 +27,21 @@ class F4(StegBase):
27 27  
28 28 cov_data = self.cov_jpeg.getsignal(channel='Y')
29 29 self.cov_data = np.array(cov_data, dtype=np.int16)
  30 + self.capacity = np.sum(self.cov_data != 0) - self.cov_data.size / 64
  31 +
  32 + # signals = self.cov_jpeg.getsignal(channel='Y')
  33 + # self.signals = np.array(signals, dtype=np.int16)
  34 + # self.capacity = np.sum(self.signals != 0)
  35 +
30 36 return self.cov_data
31 37  
32   - def embed_raw_data(self, src_cover, src_hidden, tgt_stego):
  38 + def embed_raw_data(self, src_cover, src_hidden, tgt_stego, frommem=False):
33 39  
34 40 self.t0 = time.time()
35 41  
36 42 try:
37 43 cov_data = self._get_cov_data(src_cover)
38   - hid_data = self._get_hid_data(src_hidden)
  44 + hid_data = self._get_hid_data(src_hidden, frommem)
39 45 # print hid_data.dtype,type(hid_data),hid_data.tolist()
40 46  
41 47 cov_data, bits_cnt = self._raw_embed(cov_data, hid_data)
... ... @@ -47,8 +53,8 @@ class F4(StegBase):
47 53 self.cov_jpeg.setsignal(cov_data, channel='Y')
48 54 self.cov_jpeg.Jwrite(tgt_stego)
49 55  
50   - cov_bits = np.sum(cov_data != 0) - cov_data.size / 64
51   - self._display_rate(cov_bits, bits_cnt)
  56 + # cov_bits = np.sum(cov_data != 0) - cov_data.size / 64
  57 + self._display_rate('embed', self.capacity, bits_cnt)
52 58  
53 59 # # size_cov = os.path.getsize(tgt_stego)
54 60 # size_cov = np.size(cov_data) / 8
... ...
msteg/steganography/LSB.py
... ... @@ -12,13 +12,13 @@ class LSB(StegBase):
12 12 with the F3 algorithm and <i>extract_raw_data</i> to extract data
13 13 which was embedded previously. """
14 14  
15   - def __init__(self):
  15 + def __init__(self, key=None):
16 16 """
17 17 Constructor of the F3 class.
18 18 """
19   - StegBase.__init__(self)
  19 + StegBase.__init__(self, key)
20 20  
21   - def embed_raw_data(self, src_cover, src_hidden, tgt_stego):
  21 + def embed_raw_data(self, src_cover, src_hidden, tgt_stego, frommem=False):
22 22 """ This method embeds arbitrary data into a cover image.
23 23 The cover image must be a JPEG.
24 24  
... ... @@ -33,7 +33,7 @@ class LSB(StegBase):
33 33 lossy compression.
34 34 """
35 35 self.t0 = time.time()
36   - StegBase._post_embed_actions(self, src_cover, src_hidden, tgt_stego)
  36 + StegBase._post_embed_actions(self, src_cover, src_hidden, tgt_stego, frommem)
37 37  
38 38 def extract_raw_data(self, src_steg, tgt_hidden):
39 39 """ This method extracts secret data from a stego image. It is
... ... @@ -59,11 +59,12 @@ class LSB(StegBase):
59 59 if x == 0 or x == 1 or cnt % 64 == 0: continue
60 60  
61 61 m = (hid_data[i] & 1)
62   - x[...] = (x & 0xfffffffe) | m # '0xfffe' is enough, for elements are expected to be short integers.
  62 + x[...] = (
  63 + x & 0xfffffffe) | m # '0xfffe' is enough, for elements are expected to be short integers.
63 64 i += 1
64 65 if i == hid_data.size: break
65 66  
66   - return cov_data
  67 + return cov_data, i
67 68  
68 69 def _raw_extract(self, steg_data, num_bits):
69 70 """
... ...
res/high/pic1.jpg 0 โ†’ 100644

797 KB

res/high/pic2.jpg 0 โ†’ 100644

179 KB

res/high/pic3.jpg 0 โ†’ 100644

235 KB

res/high/pic3_dest.jpg 0 โ†’ 100644

232 KB

res/high/pic3_f5_1_0.8.jpg 0 โ†’ 100644

210 KB

res/high/pic3_orig.jpg 0 โ†’ 100644

235 KB

test/test_jpeg.py
1 1 __author__ = 'chunk'
2 2  
3 3 import numpy as np
  4 +import matplotlib.pyplot as plt
  5 +import seaborn as sns
4 6 from .. import mjpeg
5 7 from ..mjpeg import base
  8 +from ..msteg.steganography import LSB, F3, F4, F5
6 9  
7 10 from ..common import *
8 11  
... ... @@ -17,9 +20,14 @@ sample = [[7, 12, 14, -12, 1, 0, -1, 0],
17 20 [0, 0, 0, 0, 0, 0, 0, 0],
18 21 [0, 0, 0, 0, 0, 0, 0, 0]]
19 22  
20   -sample_key = [46812L, 20559L, 31360L, 16681L, 27536L, 39553L, 5427L, 63029L, 56572L, 36476L, 25695L, 61908L, 63014L,
  23 +sample_key = [46812L, 20559L, 31360L, 16681L, 27536L, 39553L, 5427L, 63029L, 56572L, 36476L, 25695L,
  24 + 61908L, 63014L,
21 25 5908L, 59816L, 56765L]
22 26  
  27 +# plt.ticklabel_format(style='sci', axis='both', scilimits=(1, 4))
  28 +
  29 +plt.ticklabel_format(style='sci', axis='both')
  30 +
23 31 package_dir = os.path.dirname(os.path.abspath(__file__))
24 32  
25 33  
... ... @@ -33,7 +41,7 @@ def test_setblocks():
33 41  
34 42 imb.setCoefMatrix(np.array([[0] * 800 for i in range(600)]), channel='Y')
35 43  
36   - block = imb.getCoefBlock(channel='Y', loc=(-1, 2))
  44 + block = imb.getCoefBlock(channe99l='Y', loc=(-1, 2))
37 45 print block
38 46  
39 47 imb.Jwrite("res/test4.jpg")
... ... @@ -165,7 +173,37 @@ def test_jpeg():
165 173 """
166 174  
167 175  
  176 +def test_hist():
  177 + ima = mjpeg.Jpeg(os.path.join(package_dir, "../res/high/pic3_orig.jpg"), key=sample_key)
  178 + print ima.getQuality()
  179 + print ima.getCapacity('All')
  180 +
  181 + capacity = ima.getCapacity()
  182 + print capacity
  183 + rate = 0.65
  184 + hidden = np.random.bytes(int(int(capacity) * rate) / 8)
  185 + steger = F5.F5(sample_key, 1)
  186 + steger2 = F4.F4(key=None)
  187 + steger3 = LSB.LSB(key=None)
  188 + embed_rate = steger3.embed_raw_data(os.path.join(package_dir, "../res/high/pic3_orig.jpg"),
  189 + hidden,
  190 + os.path.join(package_dir, "../res/high/pic3_dest.jpg"),
  191 + frommem=True)
  192 + #
  193 + print embed_rate
  194 +
  195 + imb = mjpeg.Jpeg(os.path.join(package_dir, "../res/high/pic3_dest.jpg"), key=sample_key)
  196 + print imb.getQuality()
  197 + print imb.getCapacity('All')
168 198  
  199 + A = imb.rawsignal().tolist()
  200 + E = [i for i in range(-8, 10)]
  201 + plt.hist(A, E, histtype='bar', rwidth=0.8, align='left')
  202 + plt.xlabel("JPEG coefficients after quantisation")
  203 + plt.ylabel("Frequency")
  204 + plt.xticks([i for i in range(-8, 9)])
  205 + plt.ylim(ymax=300000)
  206 + plt.show()
169 207  
170 208  
171 209 if __name__ == '__main__':
... ... @@ -214,14 +252,3 @@ if __name__ == &#39;__main__&#39;:
214 252 # sample = np.array(sample)[mmask]
215 253 # print np.hstack([[],sample])
216 254 pass
217   -
218   -
219   -
220   -
221   -
222   -
223   -
224   -
225   -
226   -
227   -
... ...