-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtest_damage.py
executable file
·145 lines (117 loc) · 4.97 KB
/
test_damage.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# This script works in Linux, not tested in Windows
# First argument is the media file to clone and damage, and the second argument is the working folder to put clones to
# e.g:
# test_damage.py test_folder/files/deep/050807-124755.jpg /tmp
__author__ = "Fabiano Tarlao"
__copyright__ = "Copyright 2018, Fabiano Tarlao"
__credits__ = ["Fabiano Tarlao"]
__license__ = "GPL3"
__version__ = "0.9.4"
__maintainer__ = "Fabiano Tarlao"
__status__ = "Beta"
import shutil
import os
import random
import check_mi
import sys
def damage_file(filename, offset, size, full_noise=False):
num_noise = 0 # random.randrange(256)
bytes = bytearray(size)
for i in range(len(bytes)):
bytes[i] = random.randrange(256) if full_noise else num_noise
fh = open(filename, "r+b")
fh.seek(offset)
fh.write(bytes)
fh.close()
def damage_clone(filename, dest_filename, offset, size, full_noise=False):
shutil.copy(filename, dest_filename)
damage_file(dest_filename, offset, size, full_noise=full_noise)
def truncate_clone(filename, dest_filename, file_size):
shutil.copy(filename, dest_filename)
fh = open(dest_filename, "r+b")
fh.truncate(file_size)
def random_damage_clone(filename, dest_filename, size, full_noise=False):
statinfo = os.stat(filename)
file_size = statinfo.st_size
offset = random.randrange(file_size)
damage_clone(filename, dest_filename, offset, min(size, file_size - offset), full_noise=full_noise)
def random_truncate_clone(filename, dest_filename, max_perc):
statinfo = os.stat(filename)
file_size = int(statinfo.st_size * (1.0 - random.random() * (max_perc / 100.0)))
truncate_clone(filename, dest_filename, file_size)
NUMBER_PER_CASE = 10
DAMAGE_SIZES = [512, 16556, 256000, 512000]
PERC_TRUNC = [1, 10]
def main():
if len(sys.argv) != 3:
test_file = 'test_folder/files/deep/050807-124755.jpg'
temp_folder = '/tmp'
else:
test_file = sys.argv[1]
temp_folder = sys.argv[2]
orig_statinfo = os.stat(test_file)
dest_test_file = os.path.join(temp_folder, os.path.basename(test_file))
random.seed = 1
print("Original image size:", orig_statinfo.st_size)
for damage_size in DAMAGE_SIZES:
if damage_size >= orig_statinfo.st_size:
break
errors_0 = 0
errors_1 = 0
errors_2 = 0
for i in range(NUMBER_PER_CASE):
random_damage_clone(test_file, dest_test_file, damage_size, full_noise=True)
res_def = check_mi.check_file(dest_test_file, strict_level=0)
if not res_def[0]:
errors_0 += 1
res_1 = check_mi.check_file(dest_test_file, strict_level=1)
if not res_1[0]:
errors_1 += 1
res_2 = check_mi.check_file(dest_test_file, strict_level=2)
if not res_2[0]:
errors_2 += 1
print("DAMAGE SIZE[bytes] random noise", damage_size)
print("Detected damages 0:", 100 * float(errors_0) / NUMBER_PER_CASE, "%")
print("Detected damages 1:", 100 * float(errors_1) / NUMBER_PER_CASE, "%")
print("Detected damages 2:", 100 * float(errors_2) / NUMBER_PER_CASE, "%")
errors_0 = 0
errors_1 = 0
errors_2 = 0
for i in range(NUMBER_PER_CASE):
random_damage_clone(test_file, dest_test_file, damage_size, full_noise=False)
res_def = check_mi.check_file(dest_test_file, strict_level=0)
if not res_def[0]:
errors_0 += 1
res_1 = check_mi.check_file(dest_test_file, strict_level=1)
if not res_1[0]:
errors_1 += 1
res_2 = check_mi.check_file(dest_test_file, strict_level=2)
if not res_2[0]:
errors_2 += 1
print("DAMAGE SIZE[bytes] zero-fill portion", damage_size)
print("Detected damages 0:", 100 * float(errors_0) / NUMBER_PER_CASE, "%")
print("Detected damages 1:", 100 * float(errors_1) / NUMBER_PER_CASE, "%")
print("Detected damages 2:", 100 * float(errors_2) / NUMBER_PER_CASE, "%")
for perc in PERC_TRUNC:
errors_0 = 0
errors_1 = 0
errors_2 = 0
for i in range(NUMBER_PER_CASE):
random_truncate_clone(test_file, dest_test_file, perc)
res_def = check_mi.check_file(dest_test_file, strict_level=0)
if not res_def[0]:
errors_0 += 1
res_1 = check_mi.check_file(dest_test_file, strict_level=1)
if not res_1[0]:
errors_1 += 1
res_2 = check_mi.check_file(dest_test_file, strict_level=2)
if not res_2[0]:
errors_2 += 1
print("TRUNCATE SIZE %", perc)
print("Detected damages 0:", 100 * float(errors_0) / NUMBER_PER_CASE, "%")
print("Detected damages 1:", 100 * float(errors_1) / NUMBER_PER_CASE, "%")
print("Detected damages 2:", 100 * float(errors_2) / NUMBER_PER_CASE, "%")
if __name__ == "__main__":
main()