-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
223 lines (179 loc) · 7.33 KB
/
test.py
File metadata and controls
223 lines (179 loc) · 7.33 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
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
#!/usr/bin/env python3
"""Smoke-test script for WZRD submodules."""
import argparse
import os
import shutil
import sys
import time
import traceback
# ── Test asset input paths ──────────────
TEST_DAY_IMAGE = "test_imgs/day.jpg"
TEST_NIGHT_IMAGE = "test_imgs/night.jpg"
TEST_SEGMAP = "test_imgs/segmap.jpeg"
TEST_VIDEO = "test_imgs/test_video/video.mp4"
TEST_BACKGROUND = "test_imgs/test_video/background.jpg"
RESULTS_ROOT = "test_results"
# ── Per-module test functions ───────────────────────────────────────────────
def test_darken():
from wzrd import darken
_require(TEST_DAY_IMAGE)
out_dir = _result_dir("darken")
_copy_input(TEST_DAY_IMAGE, out_dir)
result = darken.darken_image_file(TEST_DAY_IMAGE, output_path=os.path.join(out_dir, "output.jpg"))
assert result['image'] is not None, "darken returned None image"
def test_subtract_video():
from wzrd import subtract_video
_require(TEST_VIDEO, TEST_BACKGROUND)
out_dir = _result_dir("subtract_video")
_copy_input(TEST_VIDEO, out_dir)
_copy_input(TEST_BACKGROUND, out_dir)
out = os.path.join(out_dir, "output.mp4")
info = subtract_video.subtract_background_video(TEST_VIDEO, TEST_BACKGROUND, output_path=out)
assert os.path.isfile(out), f"output video not created: {out}"
def test_islands():
from wzrd import islands
_require(TEST_SEGMAP)
out_dir = _result_dir("islands")
_copy_input(TEST_SEGMAP, out_dir)
regions_dir = os.path.join(out_dir, "regions")
regions, _ = islands.extract_color_regions(TEST_SEGMAP, regions_dir)
assert isinstance(regions, list), "islands did not return a list"
def test_reproject():
from wzrd import reproject
_require(TEST_VIDEO)
out_dir = _result_dir("reproject")
_copy_input(TEST_VIDEO, out_dir)
out = os.path.join(out_dir, "output.mp4")
metadata = {"x": 0, "y": 0, "width": 320, "height": 240}
info = reproject.reproject_video(TEST_VIDEO, metadata, output_path=out)
assert os.path.isfile(out), f"output video not created: {out}"
def test_detect():
from wzrd import detect
_require(TEST_NIGHT_IMAGE)
out_dir = _result_dir("detect")
_copy_input(TEST_NIGHT_IMAGE, out_dir)
cropped, info = detect.detect_projection_area(
TEST_NIGHT_IMAGE,
output_path=os.path.join(out_dir, "output.jpg"),
)
assert cropped is not None, "detect returned None image"
def test_align():
from wzrd import detect, align
_require(TEST_DAY_IMAGE, TEST_NIGHT_IMAGE)
out_dir = _result_dir("align")
_copy_input(TEST_DAY_IMAGE, out_dir)
_copy_input(TEST_NIGHT_IMAGE, out_dir)
# First detect the projection crop from the night image
cropped, _ = detect.detect_projection_area(
TEST_NIGHT_IMAGE,
output_path=os.path.join(out_dir, "detected_crop.jpg"),
)
# Then align the day image to match the detected crop
warped, info = align.align_images_file(
TEST_DAY_IMAGE, os.path.join(out_dir, "detected_crop.jpg"),
output_path=os.path.join(out_dir, "output.jpg"),
)
assert warped is not None, "align returned None image"
def test_prepare_surface_darken_only():
from wzrd.prepare_surface import prepare_surface
_require(TEST_DAY_IMAGE)
out_dir = _result_dir("prepare_surface_darken_only")
_copy_input(TEST_DAY_IMAGE, out_dir)
result = prepare_surface(
TEST_DAY_IMAGE,
output_path=os.path.join(out_dir, "output.jpg"),
)
assert result['image'] is not None, "prepare_surface returned None"
assert os.path.isfile(os.path.join(out_dir, "output.jpg"))
def test_prepare_surface_full():
from wzrd.prepare_surface import prepare_surface
_require(TEST_DAY_IMAGE, TEST_NIGHT_IMAGE)
out_dir = _result_dir("prepare_surface_full")
_copy_input(TEST_DAY_IMAGE, out_dir)
_copy_input(TEST_NIGHT_IMAGE, out_dir)
result = prepare_surface(
TEST_NIGHT_IMAGE,
day_image_path=TEST_DAY_IMAGE,
output_path=os.path.join(out_dir, "output.jpg"),
)
assert result['image'] is not None, "prepare_surface returned None"
assert os.path.isfile(os.path.join(out_dir, "output.jpg"))
# ── Test registry ───────────────────────────────────────────────────────────
TESTS = {
"darken": test_darken,
"detect": test_detect,
"align": test_align,
"prepare_surface_darken_only": test_prepare_surface_darken_only,
"prepare_surface_full": test_prepare_surface_full,
"subtract_video": test_subtract_video,
"islands": test_islands,
"reproject": test_reproject,
}
# ── Helpers ─────────────────────────────────────────────────────────────────
class _MissingFile(Exception):
pass
def _require(*paths):
for p in paths:
if not os.path.isfile(p):
raise _MissingFile(f"required input file not found: {p}")
def _result_dir(test_name):
"""Create and return test_results/<test_name>/, clearing any previous run."""
d = os.path.join(RESULTS_ROOT, test_name)
if os.path.exists(d):
shutil.rmtree(d)
os.makedirs(d, exist_ok=True)
return d
def _copy_input(src, dst_dir):
"""Copy an input file into dst_dir, preserving the basename."""
shutil.copy2(src, os.path.join(dst_dir, os.path.basename(src)))
def _run_test(name, fn):
print(f"\n{'─'*60}")
print(f" {name}")
print(f"{'─'*60}")
t0 = time.perf_counter()
try:
fn()
elapsed = time.perf_counter() - t0
print(f" PASS ({elapsed:.2f}s)")
return True
except _MissingFile as e:
elapsed = time.perf_counter() - t0
print(f" SKIP {e} ({elapsed:.2f}s)")
return None # skip
except Exception:
elapsed = time.perf_counter() - t0
traceback.print_exc()
print(f" FAIL ({elapsed:.2f}s)")
return False
# ── CLI ─────────────────────────────────────────────────────────────────────
def main():
parser = argparse.ArgumentParser(description="WZRD smoke tests")
parser.add_argument("modules", nargs="*", metavar="MODULE",
help=f"modules to test: {', '.join(TESTS)}")
parser.add_argument("--all", action="store_true", help="test every module")
args = parser.parse_args()
if args.all:
selected = list(TESTS)
elif args.modules:
bad = [m for m in args.modules if m not in TESTS]
if bad:
parser.error(f"unknown module(s): {', '.join(bad)}")
selected = args.modules
else:
parser.print_help()
sys.exit(0)
passed, failed, skipped = 0, 0, 0
for name in selected:
result = _run_test(name, TESTS[name])
if result is True:
passed += 1
elif result is False:
failed += 1
else:
skipped += 1
print(f"\n{'='*60}")
print(f" {passed} passed, {failed} failed, {skipped} skipped")
print(f"{'='*60}")
sys.exit(1 if failed else 0)
if __name__ == "__main__":
main()