forked from xz-group/AdverseDrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adversary_generator.py
executable file
·161 lines (146 loc) · 5.88 KB
/
adversary_generator.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
import numpy as np
import cv2
import os
import imutils
class AdversaryGenerator:
def __init__(self, city_name, sizeX=200, sizeY=200, transparency=True,
path='adversary/', record=False):
"""
Library containing different shapes, along with the ability
to create .png images with the shapes.
adversary generated will be of form `adversary_city_name.png` in the path directory
sizeX and sizeY pertain to the size of the canvas on which the
attack pattern is drawn on CARLA (recommended 200x200).
POSITION_RANGE = (0, 200)
ROTATION_RANGE = (0, 180)
WIDTH_RANGE = (0, 50)
LENGTH_RANGE = (0, 200)
COLOR_TUPLE_RANGE = (0, 255)
"""
self.city_name = city_name
self.sizeX = sizeX
self.sizeY = sizeY
if transparency:
self.channels = 4 # alpha channel
else:
self.channels = 3
self.path = path
self.record = record
self.counter = 0
if self.record:
os.system('mkdir -p {}adversaries/'.format(self.path))
self.clear_canvas()
# adversary MUST be generated in the 'adversary' directory and MUST have
# the naming convention 'adversary_townname.png'
self.image_label = 'adversary_' + self.city_name + '.png'
self.draw_image()
def clear_canvas(self):
"""
Clears the canvas to make it available for a new pattern
"""
self.canvas = np.zeros((self.sizeX, self.sizeY, self.channels), dtype=np.uint8)
def draw_image(self):
"""
Writes the canvas to a png file.
Uncomment below code to save patterns at every 'draw_image' call in a
separate directory
"""
if self.record:
if self.counter == 0:
cv2.imwrite("{}adversaries/baseline.png".format(self.path, self.counter), self.canvas)
else:
cv2.imwrite("{}adversaries/adversary_{:04}.png".format(self.path, self.counter), self.canvas)
self.counter += 1
cv2.imwrite("{}{}".format(self.path, self.image_label), self.canvas)
def lines_adversary(self, adversary_params):
"""
Generates a .png image of a line with parameters described in adversary_params
adversary_params = {
'pos': 10, -> int (0 -> 200)
'rot': 20, -> float (0 -> 180)
'width': 20, -> int (1 -> 200)
'color': (0, 0, 0, 255) -> int tuple (0->255)
}
"""
self.clear_canvas()
pos = adversary_params['pos']
rot = adversary_params['rot']
width = adversary_params['width']
color = adversary_params['color']
cv2.rectangle(self.canvas, (pos, 0),
(pos + width, self.sizeY), color, -1)
self.canvas = imutils.rotate(self.canvas, angle=rot)
self.draw_image()
# cv2.imwrite("{}{}".format(self.path, self.image_label), self.canvas)
def multi_lines(self, adversary_params):
"""
generates a multi-lines .png image with the lines' parameters define in adversary_params.
adversary_params in this case would be dictionary of dictionaries, more
general version of 'lines_adversary'
Ex:
adversary_params = {
0:{
'pos': 10,
'rot': 20,
'width': 20,
'length': 100,
'color': (0, 0, 0, 255)
},
1:{
'pos': 100,
'rot': 80,
'width': 40,
'length': 10,
'color': (0, 255, 0, 255)
}
}
"""
self.clear_canvas()
for line_id in sorted(adversary_params.keys()):
overlay = np.zeros((self.sizeX, self.sizeY, self.channels), dtype=np.uint8)
pos = adversary_params[line_id]['pos']
rot = adversary_params[line_id]['rot']
width = adversary_params[line_id]['width']
length = adversary_params[line_id]['length']
color = adversary_params[line_id]['color']
cv2.rectangle(overlay, (pos, 0),
(pos + width, length), color, -1)
overlay = imutils.rotate(overlay, angle=rot)
overlay_pos = (0, 0)
self.canvas = self.overlay_image_alpha(self.canvas, overlay, 0, 0)
self.draw_image()
# cv2.imwrite("{}{}".format(self.path, self.image_label), self.canvas)
def overlay_image_alpha(self, background, foreground, x=0, y=0):
"""
Overlay img_overlay on top of img at the position specified by
pos and blend using alpha_mask.
Source: https://stackoverflow.com/a/52742571
"""
rows, cols, channels = foreground.shape
trans_indices = foreground[...,3] != 0 # Where not transparent
overlay_copy = background[y:y+rows, x:x+cols]
overlay_copy[trans_indices] = foreground[trans_indices]
background[y:y+rows, x:x+cols] = overlay_copy
return background
if __name__ == '__main__':
# Example usage: using adversary generator to create 2-line attacks
# five times, while recording each of them
advgen = AdversaryGenerator('example', record=True)
for i in range(5):
adversary_params = {
0:{
'pos': np.random.randint(200),
'rot': 20,
'width': 20,
'length': 100,
'color': (0, 0, 0, 255)
},
1:{
'pos': np.random.randint(200),
'rot': 80,
'width': 40,
'length': 10,
'color': (0, 255, 0, 255)
}
}
advgen.multi_lines(adversary_params)