Full face stack that runs entirely in the browser. Detection, 576-point 3D mesh, recognition, anti-spoof, smile — all WebAssembly, zero server. Apache 2.0.
C avx-512avx2biometricsc99computer-visioncpu-inferencedeep-learningedge-aiembedded-aiface-embeddingface-recognitionface-verificationlow-latencyonnx-runtimeproduction-readysimdzero-dependencies
Skip redundant image copies when preparing detector frames
Baurzhan Atynova8d03325f741
diff --git a/docker/server.py b/docker/server.pyindex 0b0f16e..2adf0bf 100644--- a/docker/server.py+++ b/docker/server.py@@ -240,7 +240,7 @@ def detect():with preparation_slot(_detect_capacity):try:with Image.open(request.files['file'].stream) as uploaded:- rgb = np.asarray(uploaded.convert('RGB'))+ rgb = np.asarray(uploaded if uploaded.mode == 'RGB' else uploaded.convert('RGB'))except (OSError, ValueError, Image.DecompressionBombError):return jsonify({'error': 'Invalid image'}), 400diff --git a/docs/research/multiface_requests.md b/docs/research/multiface_requests.mdindex 768208b..660b964 100644--- a/docs/research/multiface_requests.md+++ b/docs/research/multiface_requests.md@@ -49,3 +49,15 @@ We compared against abc177f in six alternating pairs of local bursts on four pin| p95 response time in ms | 333 | 267 |Across 384 requests per build, 39 succeeded before and 37 after. All accepted face results matched their sequential references. The measured benefit is less wasted work under overload. A synchronous single-request worker has no competing in-process decodes.++## Single request frame preparation++RGB uploads now go directly from the Pillow decoder to NumPy. A three-channel uint8 input that already has the detector's target dimensions skips the resize. BGR packing copies one channel at a time into the padded output.++On one pinned Linux CPU we tested complete Flask requests for four fixture images resized and encoded as JPEGs at each resolution. We alternated old and new order for 16 timed pairs per resolution after one warmup pair. The medians below are client-thread CPU time in milliseconds, including the full response with embeddings. Every returned box, score, landmark and embedding matched the previous implementation. These local requests exclude external network delay.++| JPEG resolution | Before | After |+|---|---:|---:|+| 640 by 640 | 47.34 | 43.91 |+| 1920 by 1080 | 74.63 | 72.49 |+| 3840 by 2160 | 166.53 | 154.17 |diff --git a/python/facex/yunet.py b/python/facex/yunet.pyindex d9ce1e4..a4d119f 100644--- a/python/facex/yunet.py+++ b/python/facex/yunet.py@@ -20,9 +20,17 @@ def prepare_rectangle(image, *, bgr=False, side=640):rw, rh, cw, ch = rectangle_shape(w, h, side=side)x, y = (cw - rw) // 2, (ch - rh) // 2canvas = np.zeros((ch, cw, 3), dtype=np.uint8)- bicubic = getattr(Image, "Resampling", Image).BICUBIC- pixels = np.asarray(Image.fromarray(image).resize((rw, rh), bicubic))- canvas[y:y + rh, x:x + rw] = pixels[:, :, ::-1] if bgr else pixels+ if (rw, rh) == (w, h) and image.dtype == np.uint8 and image.shape[2:] == (3,):+ pixels = image+ else:+ bicubic = getattr(Image, "Resampling", Image).BICUBIC+ pixels = np.asarray(Image.fromarray(image).resize((rw, rh), bicubic))+ region = canvas[y:y + rh, x:x + rw]+ if bgr and pixels.shape[2:] == (3,):+ for channel in range(3):+ region[:, :, channel] = pixels[:, :, 2 - channel]+ else:+ region[:] = pixels[:, :, ::-1] if bgr else pixelsreturn canvas, rw, rhdiff --git a/tests/test_rectangle_pixels.py b/tests/test_rectangle_pixels.pynew file mode 100644index 0000000..c0867fe--- /dev/null+++ b/tests/test_rectangle_pixels.py@@ -0,0 +1,31 @@+"""Check canvas bytes against the original Pillow preparation path."""+import unittest++import numpy as np+from PIL import Image++from facex.yunet import prepare_rectangle, rectangle_shape+++class RectanglePixelsTest(unittest.TestCase):+ def test_resize_padding_and_channel_order(self):+ rng = np.random.default_rng(41)+ for w, h in ((640, 640), (832, 468), (451, 797), (643, 480), (1, 127)):+ rgb = rng.integers(256, size=(h, w, 3), dtype=np.uint8)[:, ::-1]+ for side in (448, 640, 800):+ rw, rh, cw, ch = rectangle_shape(w, h, side=side)+ resized = np.asarray(Image.fromarray(rgb).resize(+ (rw, rh), getattr(Image, "Resampling", Image).BICUBIC))+ for bgr in (False, True):+ expected = np.zeros((ch, cw, 3), dtype=np.uint8)+ x, y = (cw - rw) // 2, (ch - rh) // 2+ expected[y:y+rh, x:x+rw] = resized[:, :, ::-1] if bgr else resized+ actual, aw, ah = prepare_rectangle(rgb, side=side, bgr=bgr)+ self.assertEqual((rw, rh), (aw, ah))+ np.testing.assert_array_equal(actual, expected)+ self.assertTrue(actual.flags.c_contiguous and actual.flags.writeable)+ self.assertFalse(np.shares_memory(rgb, actual))+++if __name__ == "__main__":+ unittest.main()