-
Notifications
You must be signed in to change notification settings - Fork 0
/
DetectFake.py
327 lines (255 loc) · 9.24 KB
/
DetectFake.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import os
import multiprocessing
from concurrent.futures import ProcessPoolExecutor
import time
import cv2
import numpy as np
from tqdm import tqdm
from functools import partial
from scipy.stats import chisquare
import numpy as np
from scipy.fftpack import dct
from queue import Queue
import ctypes
import pdb
def timeViewer(func):
def inner(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
end = time.time()
print("Function '{}' took {:.3f} seconds".format(func.__name__, end - start))
return result
return inner
def OpenCVDCT(block, dict, N):
block = cv2.dct(block*100)
for i in range(N):
for j in range(N):
strDij = str(abs(block[i,j]))[0]
if(strDij!='0'):
dict[strDij]+=1
def oldDCT(block, dict, N):
para = 1/(2*np.sqrt(2*N))
OrthogonalValue = 1/np.sqrt(2)
for i in range(N):
for j in range(N):
if(i==0):
Ci = OrthogonalValue
else:
Ci = 1
if(j==0):
Cj = OrthogonalValue
else:
Cj = 1
Dij = 0
for x in range(N):
for y in range(N):
Dij += para * Ci * Cj * (block[x,y]) * np.cos((2*x+1)*i*np.pi / (2*N)) * np.cos((2*y+1)*j*np.pi / (2*N))
strDij = str(abs(Dij))[0]
if(strDij!='0'):
dict[strDij]+=1
def genrateDCTmask(N):
mask = np.zeros((N, N, N, N), dtype=np.float32)
para = 1/(2*np.sqrt(2*N))
OrthogonalValue = 1/np.sqrt(2)
for i in range(N):
for j in range(N):
if(i==0):
Ci = OrthogonalValue
else:
Ci = 1
if(j==0):
Cj = OrthogonalValue
else:
Cj = 1
for x in range(N):
for y in range(N):
mask[i, j, x, y] = (para * Ci * Cj * np.cos((2*x+1)*i*np.pi / (2*N)) * np.cos((2*y+1)*j*np.pi / (2*N)))
# print("Now i j x y:" + str(i) + " " + str(j) + " " + str(x) + " " + str(y) + " ")
minimum = (np.min(abs(mask)))
AMPfactor = 1
while(minimum < 1):
minimum *= 10
AMPfactor *=10
mask *= AMPfactor
return mask
def dotMask(block, mask, dict, N):
DCT = np.zeros((N,N), dtype=np.float32)
for i in range(N):
for j in range(N):
DCT[i, j] = np.sum(block * mask[i, j])
strDij = str(abs(DCT[i,j]))[0]
# print("i: " + str(i) + ", j: " + str(j))
# pdb.set_trace()
if(strDij!='0'):
dict[strDij]+=1
# pdb.set_trace()
def MutiDCT(args):
mask, block, N = args
localArr = np.zeros(9, dtype=np.float32)
DCT = np.zeros((N,N), dtype=np.float32)
for i in range(N):
for j in range(N):
DCT[i, j] = np.sum(block * mask[i, j])
strDij = str(abs(DCT[i,j]))[0]
if strDij != '0':
Dij = int(strDij)
localArr[Dij-1] += 1
return localArr
def dct2(a):
return dct(dct(a.T, norm='ortho').T, norm='ortho')
@timeViewer
def oldSingleTransform(img):
# 初始化圖片的首位字典
manager = multiprocessing.Manager()
dict = manager.dict({str(i): 0 for i in range(1, 10)})
# 初始化 Benford's Law 的標準字典
BenfordsDict = {str(i): np.log10((i+1)/i) for i in range(1, 10)}
# 設定區塊大小
N = 8
# 將圖片分成可以被 8x8 整除的尺寸
h, w = img.shape
hPara = h // N
wPara = w // N
img = cv2.resize(img, (wPara*N, hPara*N))
# 將圖片分成 8x8 的區塊
blocks = [np.float32(img[i:i+N, j:j+N]-128) for i in range(0, img.shape[0], N) for j in range(0, img.shape[1], N)]
# 算 DCT 並紀錄首位數字
times = len(blocks)
progress = tqdm(total=times)
for block in blocks:
oldDCT(block*1000, dict, N)
progress.update(1)
# 計算結果
result = 0
SumValue = img.shape[0] * img.shape[1]
# print("Single Dict:")
# print(dict)
# print("Single SumValue: "+ str(SumValue))
for key in dict:
dict[key] /= SumValue
result += abs(BenfordsDict[key] - dict[key]) * (1 / BenfordsDict[key])
return result
@timeViewer
def OpenCVSingleTransform(img):
# 初始化圖片的首位字典
manager = multiprocessing.Manager()
dict = manager.dict({str(i): 0 for i in range(1, 10)})
# 初始化 Benford's Law 的標準字典
BenfordsDict = {str(i): np.log10((i+1)/i) for i in range(1, 10)}
# 設定區塊大小
N = 8
# 將圖片分成可以被 8x8 整除的尺寸
h, w = img.shape
hPara = h // N
wPara = w // N
img = cv2.resize(img, (wPara*N, hPara*N))
# 將圖片分成 8x8 的區塊
blocks = [np.float32(img[i:i+N, j:j+N]-128) for i in range(0, img.shape[0], N) for j in range(0, img.shape[1], N)]
# 算 DCT 並紀錄首位數字
times = len(blocks)
progress = tqdm(total=times)
for block in blocks:
OpenCVDCT(block*1000, dict, N)
progress.update(1)
# 計算結果
result = 0
SumValue = img.shape[0] * img.shape[1]
# print("Single Dict:")
# print(dict)
# print("Single SumValue: "+ str(SumValue))
for key in dict:
dict[key] /= SumValue
result += abs(BenfordsDict[key] - dict[key]) * (1 / BenfordsDict[key])
# print("BenfordsDict: ")
# print(BenfordsDict)
# print("OurDict: ")
# print(dict)
return result
@timeViewer
def SingleTransform(img):
# 初始化圖片的首位字典
manager = multiprocessing.Manager()
dict = manager.dict({str(i): 0 for i in range(1, 10)})
# 初始化 Benford's Law 的標準字典
BenfordsDict = {str(i): np.log10((i+1)/i) for i in range(1, 10)}
# 設定區塊大小
N = 8
# 初始化 DCTmask
DCTmask = genrateDCTmask(N)
# 將圖片分成可以被 8x8 整除的尺寸
h, w = img.shape
hPara = h // N
wPara = w // N
img = cv2.resize(img, (wPara*N, hPara*N))
# 將圖片分成 8x8 的區塊
blocks = [np.float32(img[i:i+N, j:j+N]-128) for i in range(0, img.shape[0], N) for j in range(0, img.shape[1], N)]
# 算 DCT 並紀錄首位數字
times = len(blocks)
progress = tqdm(total=times)
for block in blocks:
# block = np.dot(block, DCTmask)
# block = dct2(block)
# pdb.set_trace()
dotMask(block, DCTmask, dict, N)
progress.update(1)
# 計算結果
result = 0
SumValue = img.shape[0] * img.shape[1]
# print("Single Dict:")
# print(dict)
# print("Single SumValue: "+ str(SumValue))
# print("Single TotalNum: "+ str(TotalNum))
for key in dict:
dict[key] /= SumValue
result += abs(BenfordsDict[key] - dict[key]) * (1 / BenfordsDict[key])
# print("BenfordsDict: ")
# print(BenfordsDict)
# print("OurDict: ")
# print(dict)
return result
@timeViewer
def MultiTransform(img, cores=None):
if cores is None:
ratio = 0.8
num_cores = int(multiprocessing.cpu_count() * ratio)
N = 8
blocks = [np.float32(img[i:i+N, j:j+N]-128) for i in range(0, img.shape[0], N) for j in range(0, img.shape[1], N)]
BenfordsArray = np.array([np.log10((i+1)/i) for i in range(1, 10)])
DCTmask = genrateDCTmask(N)
progress = tqdm(total=len(blocks))
with ProcessPoolExecutor(max_workers=num_cores) as executor:
results = executor.map(MutiDCT, ((DCTmask, block, N) for block in blocks))
result_data = np.zeros(9)
for localArr in results:
result_data += localArr
progress.update(1)
SumValue = img.shape[0] * img.shape[1]
SumValue_array = np.full(len(result_data), SumValue)
result_data = result_data / SumValue_array
for i in range(len(result_data)):
result_data[i] = abs(result_data[i] - BenfordsArray[i]) * (1 / BenfordsArray[i])
return np.sum(result_data)
@timeViewer
def SingleCTransform(imgPath):
# Load C++ library
lib = ctypes.cdll.LoadLibrary('./Detectlib/DetectFake.so')
lib.DetectC.argtypes = [ctypes.c_char_p]
lib.DetectC.restype = ctypes.c_float
# Call C++ function
c_imgPath = ctypes.c_char_p(imgPath.encode('utf-8'))
result = lib.DetectC(c_imgPath)
return result
if __name__ == '__main__':
path = '/home/franky/Data/Lab/Project/SourceCode/GraphAppBot/Head.jpg'
image = cv2.imread('./Head.jpg', cv2.IMREAD_GRAYSCALE)
image = cv2.resize(image, (512, 512), interpolation=cv2.INTER_NEAREST)
resultO = oldSingleTransform(image)
resultS = OpenCVSingleTransform(image)
resultF = SingleTransform(image)
resultM = MultiTransform(image)
resultC = SingleCTransform(path)
print("這張照片的修圖程度(單執行緒 土法煉鋼 Method): " + str('{:.3f}'.format(resultO)) +"\n")
print("這張照片的修圖程度(單執行緒 OpenCV Method): " + str('{:.3f}'.format(resultS)) +"\n")
print("這張照片的修圖程度(單執行緒 Mask Method): " + str('{:.3f}'.format(resultF)) +"\n")
print("這張照片的修圖程度(多執行緒 Mask Method): " + str('{:.3f}'.format(resultM)) +"\n")
print("這張照片的修圖程度(單執行緒 CPP Method): " + str('{:.3f}'.format(resultC)) +"\n")