This repository was archived by the owner on Dec 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_rgb_demo.py
More file actions
183 lines (145 loc) · 4.82 KB
/
run_rgb_demo.py
File metadata and controls
183 lines (145 loc) · 4.82 KB
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
"""
DATA VEIL – RGB Camera Deception Demo
Simulates a simple RGB camera "scene" and then applies a veiled
version that an attacker / external system would see.
Trusted:
- clean synthetic scene (background gradient + shapes)
Veiled:
- blur
- color warp
- edge jitter
- noise
Outputs:
- examples/rgb_trusted.png
- examples/rgb_veiled.png
- examples/rgb_trusted_vs_veiled.png
"""
from pathlib import Path
import numpy as np
from PIL import Image, ImageDraw, ImageFilter
def generate_rgb_scene(width: int = 320, height: int = 240) -> Image.Image:
"""
Create a synthetic RGB "camera frame":
- vertical gradient background
- a few colored rectangles / circles
- feels like a simple robotics camera view
"""
# Background gradient
img = Image.new("RGB", (width, height))
pixels = img.load()
for y in range(height):
t = y / (height - 1)
r = int(40 + 80 * t)
g = int(40 + 60 * t)
b = int(60 + 120 * t)
for x in range(width):
pixels[x, y] = (r, g, b)
draw = ImageDraw.Draw(img)
# "Floor" strip
draw.rectangle(
(0, int(height * 0.75), width, height),
fill=(50, 50, 60),
)
# Some blocks (like boxes / obstacles)
draw.rectangle(
(40, 130, 120, 220),
fill=(180, 110, 80),
)
draw.rectangle(
(200, 140, 290, 215),
fill=(80, 160, 110),
)
# A "pillar" or tall object
draw.rectangle(
(150, 80, 180, 220),
fill=(130, 130, 150),
)
# A "light" or sensor
draw.ellipse(
(30, 40, 70, 80),
fill=(220, 220, 120),
)
return img
def apply_rgb_veil(trusted_img: Image.Image) -> Image.Image:
"""
Apply a set of distortions to simulate a veiled camera view:
- slight perspective-ish warp (via jittered columns)
- blur
- color shifts
- noise overlays
"""
w, h = trusted_img.size
# Convert to numpy for color ops
arr = np.array(trusted_img).astype(np.float32)
# Color warp: shift channels in a way that feels "off"
# Slight green boost, red attenuation, blue variance
arr[..., 0] *= 0.9 # red
arr[..., 1] *= 1.1 # green
arr[..., 2] *= 1.05 # blue
# Clip back to [0,255]
arr = np.clip(arr, 0, 255).astype(np.uint8)
img = Image.fromarray(arr, mode="RGB")
# Add a mild blur
img = img.filter(ImageFilter.GaussianBlur(radius=1.4))
# Jitter vertical strips (simulate edge misalignment / rolling shutter weirdness)
jittered = Image.new("RGB", (w, h))
strip_width = 8
for x in range(0, w, strip_width):
strip = img.crop((x, 0, min(x + strip_width, w), h))
offset = int(np.random.randint(-2, 3)) # -2 to +2 pixels
jittered.paste(strip, (x, offset if offset > 0 else 0))
img = jittered
# Add noise overlay
noise = np.random.normal(loc=0.0, scale=10.0, size=(h, w, 3))
arr = np.array(img).astype(np.float32)
arr += noise
arr = np.clip(arr, 0, 255).astype(np.uint8)
img = Image.fromarray(arr, mode="RGB")
return img
def make_rgb_side_by_side(trusted: Image.Image, veiled: Image.Image, out_path: Path) -> None:
"""
Build a side-by-side comparison:
LEFT = trusted RGB
RIGHT = veiled RGB
"""
h = min(trusted.height, veiled.height)
def resize_keep_aspect(im: Image.Image) -> Image.Image:
return im.resize(
(int(im.width * h / im.height), h),
Image.Resampling.LANCZOS,
)
left = resize_keep_aspect(trusted)
right = resize_keep_aspect(veiled)
label_height = 40
total_width = left.width + right.width
combined = Image.new("RGB", (total_width, h + label_height), (0, 0, 0))
draw = ImageDraw.Draw(combined)
combined.paste(left, (0, label_height))
combined.paste(right, (left.width, label_height))
draw.text((10, 10), "Trusted RGB camera", fill=(230, 230, 230))
draw.text((left.width + 10, 10), "Veiled RGB camera", fill=(230, 230, 230))
out_path.parent.mkdir(parents=True, exist_ok=True)
combined.save(out_path)
def demo_rgb() -> None:
"""
End-to-end RGB deception demo:
1. Generate a trusted RGB camera scene.
2. Apply Data Veil to create a veiled RGB frame.
3. Save trusted, veiled, and side-by-side images.
"""
trusted = generate_rgb_scene()
veiled = apply_rgb_veil(trusted)
out_dir = Path("examples")
out_dir.mkdir(exist_ok=True)
trusted_path = out_dir / "rgb_trusted.png"
veiled_path = out_dir / "rgb_veiled.png"
side_path = out_dir / "rgb_trusted_vs_veiled.png"
trusted.save(trusted_path)
veiled.save(veiled_path)
make_rgb_side_by_side(trusted, veiled, side_path)
print("✅ RGB demo complete.")
print(f" - {trusted_path}")
print(f" - {veiled_path}")
print(f" - {side_path}")
if __name__ == "__main__":
demo_rgb()