-
Notifications
You must be signed in to change notification settings - Fork 5
/
offline_fault_injector_ui.py
211 lines (180 loc) · 5.85 KB
/
offline_fault_injector_ui.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Offline Fault Injector For Camera FI Demo Tool
"""
import os
from os import listdir
from os.path import isfile, join
import random
from class_fi_offline_ui import OfflineImageFault as ofi
# from class_list_creator import ListCreator as img_list
def main(
ndir_name,
fdir_name,
fault_type,
fault_rate,
randomized,
fimp_rate,
last_fi_name_list,
):
"""
Offline Camera Fault Injector UI module takes required variables from
CamFITool and sends them to class_fi_offline_ui accordingly. This method,
which is used to print an fault in the offline method (the method where
a fault is printed in a ready image folder and saved in another folder),
compiles the appropriate fault types determined for TOF and RGB image types.
"""
img_format = str(img_format_finder(ndir_name))
img_name_list = read_image_list(ndir_name)
# Eger tekrarlı hata enj olursa hata uygulanmıs resimler, ana listeden
# cikarilarak img_name_list olusturulur.
if last_fi_name_list is not None:
img_name_list = list_substractor(img_name_list, last_fi_name_list)
fault_rate = int(fault_rate) / 100
fimp_val = int(int(len(img_name_list)) * int(fimp_rate) / 100)
random_value = 0
## TOF Fault Types##
if fault_type == "Gaussian":
fault_type = "g"
elif fault_type == "Poisson":
fault_type = "p"
elif fault_type == "Salt&Pepper":
fault_type = "s"
## RGB Fault Types ##
elif fault_type == "Open":
fault_type = "o"
elif fault_type == "Close":
fault_type = "c"
elif fault_type == "Dilation":
fault_type = "d"
elif fault_type == "Erosion":
fault_type = "e"
elif fault_type == "Gradient":
fault_type = "gr"
elif fault_type == "Motion-blur":
fault_type = "m"
elif fault_type == "Partialloss":
fault_type = "par"
else:
pass
fi_image_name_list = []
if randomized is False:
if fimp_rate != 100:
fi_image_name_list = random_fault_applier(
img_name_list,
ndir_name,
fdir_name,
img_format,
fault_type,
fault_rate,
fimp_val,
)
done = (
"Partial Injection Completed!\nFault Injected Image Value: "
+ str(fimp_val)
+ "/"
+ str(len(img_name_list))
)
fi_image_name_list.sort()
else:
multi_fault_applier(
img_name_list, ndir_name, fdir_name, img_format, fault_type, fault_rate
)
done = "Full Injection Completed! Fault applied to all images."
else:
if fimp_rate != 100:
print(
"Randomized function cannot be applicable Fault Implementation Value type FI!"
)
length = len(img_name_list)
random_value = random.randrange(length)
fi_image_name_list = random_fault_applier(
img_name_list,
ndir_name,
fdir_name,
img_format,
fault_type,
fault_rate,
random_value,
)
done = (
"Randomized Injection Sequence Completed!\n"
+ "----------------------------------\nFault Injected Image Value: "
+ str(random_value)
+ "/"
+ str(len(img_name_list))
)
fi_image_name_list.sort()
return done, len(img_name_list), fi_image_name_list
def list_substractor(norm_img_list, fi_img_list):
"""Liste ayırıcı"""
return [x for x in norm_img_list if x not in fi_img_list]
def read_image_list(file_path):
"""
TOF saves the names of the images in the Image folder in a list.
"""
onlyfiles = [f for f in listdir(file_path) if isfile(join(file_path, f))]
image_list = [i.split(".", 1)[0] for i in onlyfiles]
return image_list
def multi_fault_applier(
img_name_list, ndir_name, fdir_name, img_format, fault_type, fault_rate
):
"""
Allows multiple faults to be applied to a list of images.
"""
for i, img in enumerate(img_name_list):
image_name = [i, img]
apply_fault = ofi(
ndir_name, fdir_name, image_name[1], img_format, fault_type, fault_rate
)
apply_fault.main()
def random_fault_applier(
img_name_list,
ndir_name,
fdir_name,
img_format,
fault_type,
fault_rate,
random_value,
):
"""
It is a test function for injecting faults on a random number of images.
"""
fi_image_name_list = []
for i, img in enumerate(img_name_list):
image_name = [i, img]
apply_fault = ofi(
ndir_name, fdir_name, image_name[1], img_format, "nf", fault_rate
)
if fault_type in {"s", "g", "p"}:
apply_fault.tof_image_fault()
else:
apply_fault.rgb_image_fault()
i_val = 0
y_val = 0
while i_val != random_value:
x_val = random.randint(1, 4)
image_name = img_name_list[y_val]
if x_val % 2 == 0:
apply_fault = ofi(
ndir_name, fdir_name, image_name, img_format, fault_type, fault_rate
)
fi_image_name_list.append(image_name)
if fault_type in {"s", "g", "p"}:
apply_fault.tof_image_fault()
else:
apply_fault.rgb_image_fault()
i_val += 1
y_val += 1
if y_val >= len(img_name_list):
y_val = 0
return fi_image_name_list
def img_format_finder(ndir_name):
"""
This function reads the format of the images taken from the normal
image database, and the faulty images are output in the same format.
"""
one_image = os.listdir(ndir_name)[0]
img_format = "." + one_image.split(".", 2)[1]
return img_format