Repository navigation
Expand file tree
/
Copy pathsample_image_stream_guard.py
More file actions
586 lines (533 loc) · 17.6 KB
/
Copy pathsample_image_stream_guard.py
File metadata and controls
586 lines (533 loc) · 17.6 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
#!/usr/bin/env python3
"""Correctness, lifetime, validation, and latency gate for ImageStream."""
import ctypes
import gc
import statistics
import time
import weakref
import numpy as np
from inspireface.modules.core import (
HF_CAMERA_ROTATION_0,
HF_CAMERA_ROTATION_90,
HF_STREAM_BGRA,
HF_STREAM_BGR,
HF_STREAM_GRAY,
HF_STREAM_I420,
HF_STREAM_YUV_NV21,
HF_STREAM_RGB,
HFCreateImageStream,
HFCreateImageBitmapFromImageStreamProcess,
HFDeBugGetUnreleasedStreamsCount,
HFImageData,
HFImageBitmap,
HFImageBitmapData,
HFImageBitmapGetData,
HFImageStream,
HFReleaseImageBitmap,
HFReleaseImageStream,
)
from inspireface.modules.exception import InvalidInputError, check_error
from inspireface.modules.inspireface import ImageStream
BASELINE_CONTIGUOUS_P50_MS = 0.002834
BASELINE_CONTIGUOUS_P95_MS = 0.005542
CONTIGUOUS_ITERATIONS = 2000
NONCONTIGUOUS_ITERATIONS = 200
class Guard:
def __init__(self):
self.groups = {
"pixel_accuracy": True,
"input_ownership": True,
"format_contract": True,
"invalid_input": True,
"lifecycle": True,
"latency": True,
}
self.failures = []
def expect(self, condition, group, message):
if condition:
return
self.groups[group] = False
self.failures.append(f"{group}: {message}")
def expect_invalid(self, operation, message):
try:
operation()
except InvalidInputError:
return
except Exception as error:
self.expect(
False,
"invalid_input",
f"{message}: raised {type(error).__name__} instead of InvalidInputError",
)
return
self.expect(False, "invalid_input", f"{message}: input was accepted")
def passed(self):
return not self.failures
def stream_count():
count = ctypes.c_int32()
check_error(
HFDeBugGetUnreleasedStreamsCount(ctypes.byref(count)),
"Query unreleased ImageStream count",
)
return count.value
def decode_handle(handle):
bitmap = HFImageBitmap()
check_error(
HFCreateImageBitmapFromImageStreamProcess(
handle,
ctypes.byref(bitmap),
0,
1.0,
),
"Decode ImageStream into bitmap",
)
try:
bitmap_data = HFImageBitmapData()
check_error(
HFImageBitmapGetData(bitmap, ctypes.byref(bitmap_data)),
"Read decoded bitmap",
)
element_count = (
bitmap_data.width * bitmap_data.height * bitmap_data.channels
)
flat = np.ctypeslib.as_array(
bitmap_data.data,
shape=(element_count,),
)
return flat.copy().reshape(
bitmap_data.height,
bitmap_data.width,
bitmap_data.channels,
)
finally:
check_error(HFReleaseImageBitmap(bitmap), "Release decoded bitmap")
def decode_stream(stream):
return decode_handle(stream.handle)
def decode_legacy_ndarray(
image,
stream_format=HF_STREAM_BGR,
width=None,
height=None,
):
"""Decode through the pre-change pointer-only construction contract."""
if not image.flags.c_contiguous:
raise ValueError("legacy reference input must be contiguous")
image_data = HFImageData()
image_data.data = image.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8))
image_data.width = image.shape[1] if width is None else width
image_data.height = image.shape[0] if height is None else height
image_data.format = stream_format
image_data.rotation = HF_CAMERA_ROTATION_0
handle = HFImageStream()
check_error(
HFCreateImageStream(ctypes.byref(image_data), ctypes.byref(handle)),
"Create legacy reference ImageStream",
)
try:
return decode_handle(handle)
finally:
check_error(HFReleaseImageStream(handle), "Release legacy reference ImageStream")
def max_pixel_delta(left, right):
return int(np.abs(left.astype(np.int16) - right.astype(np.int16)).max())
def make_pattern(height, width, seed):
y, x = np.indices((height, width), dtype=np.uint32)
channels = [
(x * 17 + y * 3 + seed * 11) % 256,
(x * 5 + y * 29 + seed * 7) % 256,
((x ^ y) * 13 + seed * 19) % 256,
]
return np.stack(channels, axis=-1).astype(np.uint8)
def percentile(values, fraction):
ordered = sorted(values)
index = max(0, min(len(ordered) - 1, int(len(ordered) * fraction) - 1))
return ordered[index]
def benchmark(image, iterations):
for _ in range(100):
stream = ImageStream.load_from_cv_image(image)
stream.release()
samples = []
for _ in range(iterations):
started = time.perf_counter_ns()
stream = ImageStream.load_from_cv_image(image)
stream.release()
samples.append((time.perf_counter_ns() - started) / 1_000_000.0)
return {
"p50": statistics.median(samples),
"p95": percentile(samples, 0.95),
"mean": statistics.mean(samples),
}
def run_pixel_accuracy_cases(guard):
cases = [
(7, 11, 1),
(29, 37, 2),
(64, 96, 3),
(91, 127, 4),
(160, 160, 5),
]
for height, width, seed in cases:
image = make_pattern(height, width, seed)
legacy_decoded = decode_legacy_ndarray(image)
with ImageStream.load_from_cv_image(image) as stream:
decoded = decode_stream(stream)
guard.expect(
np.array_equal(decoded, legacy_decoded),
"pixel_accuracy",
f"BGR pattern {height}x{width} differs from legacy construction",
)
guard.expect(
max_pixel_delta(decoded, image) <= 1,
"pixel_accuracy",
f"BGR pattern {height}x{width} exceeds native interpolation tolerance",
)
guard.expect(
stream._data_owner is image,
"input_ownership",
f"contiguous BGR pattern {height}x{width} was copied",
)
source = make_pattern(73, 202, 17)
roi = source[:, ::2, :]
expected = roi.copy()
legacy_decoded = decode_legacy_ndarray(expected)
guard.expect(not roi.flags.c_contiguous, "pixel_accuracy", "ROI fixture is contiguous")
with ImageStream.load_from_cv_image(roi) as stream:
decoded = decode_stream(stream)
guard.expect(
np.array_equal(decoded, legacy_decoded),
"pixel_accuracy",
"non-contiguous ROI differs from its contiguous legacy reference",
)
guard.expect(
stream._data_owner is not roi and stream._data_owner.flags.c_contiguous,
"input_ownership",
"non-contiguous ROI was not retained as a contiguous owner",
)
def run_format_and_buffer_cases(guard):
bgr = make_pattern(31, 47, 23)
alpha = np.full(bgr.shape[:2] + (1,), 173, dtype=np.uint8)
bgra = np.concatenate((bgr, alpha), axis=2)
legacy_bgra = decode_legacy_ndarray(bgra, HF_STREAM_BGRA)
with ImageStream.load_from_cv_image(bgra) as stream:
guard.expect(
stream.data_format == HF_STREAM_BGRA,
"format_contract",
"four-channel OpenCV image did not infer BGRA",
)
guard.expect(
np.array_equal(decode_stream(stream), legacy_bgra),
"pixel_accuracy",
"BGRA default inference differs from explicit legacy BGRA decoding",
)
rgb = bgr[:, :, ::-1].copy()
legacy_rgb = decode_legacy_ndarray(rgb, HF_STREAM_RGB)
with ImageStream.load_from_cv_image(rgb, HF_STREAM_RGB) as stream:
guard.expect(
np.array_equal(decode_stream(stream), legacy_rgb),
"pixel_accuracy",
"explicit RGB input differs from legacy RGB decoding",
)
payload = bgr.tobytes()
legacy_bgr = decode_legacy_ndarray(bgr)
with ImageStream.load_from_buffer(
payload,
bgr.shape[1],
bgr.shape[0],
HF_STREAM_BGR,
HF_CAMERA_ROTATION_0,
) as stream:
guard.expect(
stream._data_owner is payload,
"input_ownership",
"immutable bytes input was not retained",
)
guard.expect(
np.array_equal(decode_stream(stream), legacy_bgr),
"pixel_accuracy",
"bytes input differs from legacy BGR decoding",
)
mutable_payload = bytearray(payload)
with ImageStream.load_from_buffer(
mutable_payload,
bgr.shape[1],
bgr.shape[0],
HF_STREAM_BGR,
HF_CAMERA_ROTATION_0,
) as stream:
mutable_payload[0] ^= 0xFF
expected = np.frombuffer(mutable_payload, dtype=np.uint8).reshape(bgr.shape).copy()
legacy_mutated = decode_legacy_ndarray(expected)
guard.expect(
np.array_equal(decode_stream(stream), legacy_mutated),
"input_ownership",
"writable buffer was copied instead of retained zero-copy",
)
try:
mutable_payload.extend(b"\x00")
except BufferError:
guard.expect(
False,
"input_ownership",
"release kept a writable buffer export alive",
)
expanded = bytearray(len(payload) * 2)
expanded[::2] = payload
strided_view = memoryview(expanded)[::2]
with ImageStream.load_from_buffer(
strided_view,
bgr.shape[1],
bgr.shape[0],
HF_STREAM_BGR,
HF_CAMERA_ROTATION_0,
) as stream:
guard.expect(
np.array_equal(decode_stream(stream), legacy_bgr),
"pixel_accuracy",
"strided buffer was not copied in logical byte order",
)
ctypes_buffer = (ctypes.c_uint8 * len(payload)).from_buffer_copy(payload)
ctypes_pointer = ctypes.cast(ctypes_buffer, ctypes.POINTER(ctypes.c_uint8))
with ImageStream.load_from_buffer(
ctypes_pointer,
bgr.shape[1],
bgr.shape[0],
HF_STREAM_BGR,
HF_CAMERA_ROTATION_0,
) as stream:
guard.expect(
stream._data_owner is ctypes_pointer,
"input_ownership",
"ctypes pointer object was not retained",
)
guard.expect(
np.array_equal(decode_stream(stream), legacy_bgr),
"pixel_accuracy",
"ctypes pointer was interpreted as pointer-value bytes",
)
yuv_width = 10
yuv_height = 9
yuv = np.arange(
yuv_width * (yuv_height * 3 // 2),
dtype=np.uint8,
).reshape(yuv_height * 3 // 2, yuv_width)
legacy_yuv = decode_legacy_ndarray(
yuv,
HF_STREAM_YUV_NV21,
width=yuv_width,
height=yuv_height,
)
with ImageStream.load_from_ndarray(
yuv,
yuv_width,
yuv_height,
HF_STREAM_YUV_NV21,
HF_CAMERA_ROTATION_0,
) as stream:
guard.expect(
np.array_equal(decode_stream(stream), legacy_yuv),
"pixel_accuracy",
"odd-height NV21 input differs from legacy NV21 decoding",
)
def run_lifetime_cases(guard):
image = make_pattern(53, 79, 31)
expected = image.copy()
legacy_decoded = decode_legacy_ndarray(expected)
image_ref = weakref.ref(image)
stream = ImageStream.load_from_cv_image(image)
del image
gc.collect()
guard.expect(
image_ref() is not None,
"input_ownership",
"ImageStream did not retain its source ndarray",
)
guard.expect(
np.array_equal(decode_stream(stream), legacy_decoded),
"pixel_accuracy",
"source ndarray lifetime differs from legacy decoding",
)
stream.release()
stream.release()
guard.expect(stream.handle is None, "lifecycle", "release was not idempotent")
guard.expect(
stream._data_owner is None,
"input_ownership",
"release did not drop the retained input owner",
)
gc.collect()
guard.expect(
image_ref() is None,
"input_ownership",
"release kept the source ndarray alive",
)
context_stream = None
with ImageStream.load_from_cv_image(expected) as context_stream:
guard.expect(context_stream.handle is not None, "lifecycle", "context stream was closed early")
guard.expect(context_stream.handle is None, "lifecycle", "context manager did not release stream")
def run_invalid_input_cases(guard):
valid = make_pattern(12, 18, 41)
guard.expect_invalid(
lambda: ImageStream.load_from_cv_image(valid.astype(np.float32)),
"float32 OpenCV image",
)
guard.expect_invalid(
lambda: ImageStream.load_from_cv_image(np.empty((0, 18, 3), dtype=np.uint8)),
"zero-height OpenCV image",
)
guard.expect_invalid(
lambda: ImageStream.load_from_cv_image(valid, HF_STREAM_BGRA),
"channel and format mismatch",
)
guard.expect_invalid(
lambda: ImageStream.load_from_buffer(
valid.tobytes()[:-1],
valid.shape[1],
valid.shape[0],
HF_STREAM_BGR,
HF_CAMERA_ROTATION_0,
),
"short byte buffer",
)
guard.expect_invalid(
lambda: ImageStream.load_from_ndarray(
valid,
valid.shape[1] + 1,
valid.shape[0],
HF_STREAM_BGR,
HF_CAMERA_ROTATION_0,
),
"ndarray shape mismatch",
)
guard.expect_invalid(
lambda: ImageStream.load_from_buffer(
bytes(10 * (9 * 3 // 2) - 1),
10,
9,
HF_STREAM_I420,
HF_CAMERA_ROTATION_0,
),
"short YUV420 buffer",
)
guard.expect_invalid(
lambda: ImageStream.load_from_buffer(
valid.tobytes(),
valid.shape[1],
valid.shape[0],
999,
HF_CAMERA_ROTATION_0,
),
"unknown stream format",
)
guard.expect_invalid(
lambda: ImageStream.load_from_buffer(
valid.tobytes(),
valid.shape[1],
valid.shape[0],
HF_STREAM_BGR,
999,
),
"unknown rotation",
)
guard.expect_invalid(
lambda: ImageStream.load_from_buffer(
object(),
valid.shape[1],
valid.shape[0],
HF_STREAM_BGR,
HF_CAMERA_ROTATION_90,
),
"object without a buffer",
)
guard.expect_invalid(
lambda: ImageStream.load_from_buffer(
ctypes.POINTER(ctypes.c_uint8)(),
valid.shape[1],
valid.shape[0],
HF_STREAM_BGR,
HF_CAMERA_ROTATION_0,
),
"null ctypes pointer",
)
guard.expect_invalid(
lambda: ImageStream.load_from_buffer(
valid.tobytes(),
True,
valid.shape[0],
HF_STREAM_GRAY,
HF_CAMERA_ROTATION_0,
),
"boolean width",
)
def main():
guard = Guard()
initial_streams = stream_count()
started = time.perf_counter()
run_pixel_accuracy_cases(guard)
run_format_and_buffer_cases(guard)
run_lifetime_cases(guard)
run_invalid_input_cases(guard)
contiguous = np.zeros((480, 640, 3), dtype=np.uint8)
contiguous_latency = benchmark(contiguous, CONTIGUOUS_ITERATIONS)
wide = np.zeros((480, 1280, 3), dtype=np.uint8)
noncontiguous_latency = benchmark(wide[:, ::2, :], NONCONTIGUOUS_ITERATIONS)
p50_limit = max(0.020, BASELINE_CONTIGUOUS_P50_MS * 2.0)
p95_limit = max(0.050, BASELINE_CONTIGUOUS_P95_MS * 3.0)
guard.expect(
contiguous_latency["p50"] <= p50_limit,
"latency",
f"contiguous p50 {contiguous_latency['p50']:.6f} ms exceeds {p50_limit:.6f} ms",
)
guard.expect(
contiguous_latency["p95"] <= p95_limit,
"latency",
f"contiguous p95 {contiguous_latency['p95']:.6f} ms exceeds {p95_limit:.6f} ms",
)
guard.expect(
noncontiguous_latency["p95"] <= 2.500,
"latency",
f"non-contiguous p95 {noncontiguous_latency['p95']:.6f} ms exceeds 2.500 ms",
)
gc.collect()
final_streams = stream_count()
guard.expect(
final_streams == initial_streams,
"lifecycle",
f"unreleased stream count changed from {initial_streams} to {final_streams}",
)
elapsed_ms = (time.perf_counter() - started) * 1000.0
print("ImageStream guard results")
print(f" native stream count: {initial_streams} -> {final_streams}")
print(" contiguous create/release latency (480x640 BGR):")
print(
" before: "
f"p50={BASELINE_CONTIGUOUS_P50_MS:.6f} ms, "
f"p95={BASELINE_CONTIGUOUS_P95_MS:.6f} ms"
)
print(
" after: "
f"p50={contiguous_latency['p50']:.6f} ms, "
f"p95={contiguous_latency['p95']:.6f} ms, "
f"mean={contiguous_latency['mean']:.6f} ms"
)
print(
" ratio: "
f"p50={contiguous_latency['p50'] / BASELINE_CONTIGUOUS_P50_MS:.3f}x, "
f"p95={contiguous_latency['p95'] / BASELINE_CONTIGUOUS_P95_MS:.3f}x"
)
print(" non-contiguous copy latency (480x640 BGR logical image):")
print(
" after: "
f"p50={noncontiguous_latency['p50']:.6f} ms, "
f"p95={noncontiguous_latency['p95']:.6f} ms, "
f"mean={noncontiguous_latency['mean']:.6f} ms"
)
for group, passed in guard.groups.items():
print(f" {group}: {'PASS' if passed else 'FAIL'}")
print(f" total: {elapsed_ms:.3f} ms")
if guard.failures:
print("Failures:")
for failure in guard.failures:
print(f" - {failure}")
return 1
print("PASS")
return 0
if __name__ == "__main__":
raise SystemExit(main())