tempest-react-sdk 0.38.2 → 0.38.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/lib/design/collect.mjs +19 -0
- package/bin/lib/design/functions.mjs +5 -0
- package/bin/lib/design/index.mjs +8 -3
- package/bin/lib/design/index.test.mjs +31 -0
- package/bin/lib/design/scan.mjs +34 -2
- package/bin/lib/design/scan.test.mjs +78 -0
- package/bin/lib/doctor/doctor.e2e.test.mjs +85 -0
- package/bin/tempest.mjs +90 -6
- package/dist/audio/level-meter.cjs.map +1 -1
- package/dist/audio/level-meter.js.map +1 -1
- package/dist/audio/use-audio-recorder.cjs.map +1 -1
- package/dist/audio/use-audio-recorder.js.map +1 -1
- package/dist/auth/passkey.cjs.map +1 -1
- package/dist/auth/passkey.js.map +1 -1
- package/dist/auth/use-passkey.cjs.map +1 -1
- package/dist/auth/use-passkey.js.map +1 -1
- package/dist/br/BrazilMap.cjs.map +1 -1
- package/dist/br/BrazilMap.js.map +1 -1
- package/dist/br/BrazilStateCitySelect.cjs.map +1 -1
- package/dist/br/BrazilStateCitySelect.js.map +1 -1
- package/dist/br/BrazilStateMap.cjs.map +1 -1
- package/dist/br/BrazilStateMap.js.map +1 -1
- package/dist/br/PixQRCode.cjs.map +1 -1
- package/dist/br/PixQRCode.js.map +1 -1
- package/dist/br/boleto.cjs.map +1 -1
- package/dist/br/boleto.js.map +1 -1
- package/dist/br/pix.cjs.map +1 -1
- package/dist/br/pix.js.map +1 -1
- package/dist/br.d.ts +8 -0
- package/dist/capture/media-recorder.cjs.map +1 -1
- package/dist/capture/media-recorder.js.map +1 -1
- package/dist/capture/use-barcode-scanner.cjs.map +1 -1
- package/dist/capture/use-barcode-scanner.js.map +1 -1
- package/dist/capture/use-screen-capture.cjs.map +1 -1
- package/dist/capture/use-screen-capture.js.map +1 -1
- package/dist/capture/use-speech-recognition.cjs.map +1 -1
- package/dist/capture/use-speech-recognition.js.map +1 -1
- package/dist/capture/use-video-recorder.cjs.map +1 -1
- package/dist/capture/use-video-recorder.js.map +1 -1
- package/dist/charts/types.cjs.map +1 -1
- package/dist/charts/types.js.map +1 -1
- package/dist/charts.d.ts +8 -0
- package/dist/components/AIChat/AIChat.cjs.map +1 -1
- package/dist/components/AIChat/AIChat.js.map +1 -1
- package/dist/components/AIChat/AIChatComposer.cjs.map +1 -1
- package/dist/components/AIChat/AIChatComposer.js.map +1 -1
- package/dist/components/AIChat/AIChatTurn.cjs.map +1 -1
- package/dist/components/AIChat/AIChatTurn.js.map +1 -1
- package/dist/components/AppBar/AppBar.cjs.map +1 -1
- package/dist/components/AppBar/AppBar.js.map +1 -1
- package/dist/components/AudioPlayer/AudioPlayer.cjs.map +1 -1
- package/dist/components/AudioPlayer/AudioPlayer.js.map +1 -1
- package/dist/components/AudioRecorder/AudioRecorder.cjs.map +1 -1
- package/dist/components/AudioRecorder/AudioRecorder.js.map +1 -1
- package/dist/components/BarcodeScanner/BarcodeScanner.cjs.map +1 -1
- package/dist/components/BarcodeScanner/BarcodeScanner.js.map +1 -1
- package/dist/components/Button/Button.cjs.map +1 -1
- package/dist/components/Button/Button.js.map +1 -1
- package/dist/components/Calendar/Calendar.cjs.map +1 -1
- package/dist/components/Calendar/Calendar.js.map +1 -1
- package/dist/components/Carousel/Carousel.cjs.map +1 -1
- package/dist/components/Carousel/Carousel.js.map +1 -1
- package/dist/components/Chat/Chat.cjs.map +1 -1
- package/dist/components/Chat/Chat.js.map +1 -1
- package/dist/components/ChipInput/ChipInput.cjs.map +1 -1
- package/dist/components/ChipInput/ChipInput.js.map +1 -1
- package/dist/components/CodeBlock/CodeBlock.cjs.map +1 -1
- package/dist/components/CodeBlock/CodeBlock.js.map +1 -1
- package/dist/components/Combobox/Combobox.cjs.map +1 -1
- package/dist/components/Combobox/Combobox.js.map +1 -1
- package/dist/components/Command/Command.cjs.map +1 -1
- package/dist/components/Command/Command.js.map +1 -1
- package/dist/components/ConfirmDialog/ConfirmDialog.cjs.map +1 -1
- package/dist/components/ConfirmDialog/ConfirmDialog.js.map +1 -1
- package/dist/components/ContextMenu/ContextMenu.cjs.map +1 -1
- package/dist/components/ContextMenu/ContextMenu.js.map +1 -1
- package/dist/components/CopyButton/CopyButton.cjs.map +1 -1
- package/dist/components/CopyButton/CopyButton.js.map +1 -1
- package/dist/components/DataTable/DataTable.cjs.map +1 -1
- package/dist/components/DataTable/DataTable.js.map +1 -1
- package/dist/components/DataTable/DataTable.module.cjs.map +1 -1
- package/dist/components/DataTable/DataTable.module.js.map +1 -1
- package/dist/components/DataTable/EditableCell.cjs.map +1 -1
- package/dist/components/DataTable/EditableCell.js.map +1 -1
- package/dist/components/DatePicker/DatePicker.cjs.map +1 -1
- package/dist/components/DatePicker/DatePicker.js.map +1 -1
- package/dist/components/DateRangePicker/DateRangePicker.cjs.map +1 -1
- package/dist/components/DateRangePicker/DateRangePicker.js.map +1 -1
- package/dist/components/Drawer/Drawer.cjs.map +1 -1
- package/dist/components/Drawer/Drawer.js.map +1 -1
- package/dist/components/DropdownMenu/DropdownMenu.cjs.map +1 -1
- package/dist/components/DropdownMenu/DropdownMenu.js.map +1 -1
- package/dist/components/Dropzone/Dropzone.cjs.map +1 -1
- package/dist/components/Dropzone/Dropzone.js.map +1 -1
- package/dist/components/FileUpload/FileUpload.cjs.map +1 -1
- package/dist/components/FileUpload/FileUpload.js.map +1 -1
- package/dist/components/FilterBar/FilterBar.cjs.map +1 -1
- package/dist/components/FilterBar/FilterBar.js.map +1 -1
- package/dist/components/ImageCropper/ImageCropper.cjs.map +1 -1
- package/dist/components/ImageCropper/ImageCropper.js.map +1 -1
- package/dist/components/InstallBanner/InstallBanner.cjs.map +1 -1
- package/dist/components/InstallBanner/InstallBanner.js.map +1 -1
- package/dist/components/Kanban/Kanban.cjs.map +1 -1
- package/dist/components/Kanban/Kanban.js.map +1 -1
- package/dist/components/Lightbox/Lightbox.cjs.map +1 -1
- package/dist/components/Lightbox/Lightbox.js.map +1 -1
- package/dist/components/ListTile/ListTile.cjs.map +1 -1
- package/dist/components/ListTile/ListTile.js.map +1 -1
- package/dist/components/Markdown/Markdown.cjs.map +1 -1
- package/dist/components/Markdown/Markdown.js.map +1 -1
- package/dist/components/Markdown/markdown-parse.cjs.map +1 -1
- package/dist/components/Markdown/markdown-parse.js.map +1 -1
- package/dist/components/Menubar/Menubar.cjs.map +1 -1
- package/dist/components/Menubar/Menubar.js.map +1 -1
- package/dist/components/Modal/Modal.cjs.map +1 -1
- package/dist/components/Modal/Modal.js.map +1 -1
- package/dist/components/ModalsManager/ModalsManager.cjs.map +1 -1
- package/dist/components/ModalsManager/ModalsManager.js.map +1 -1
- package/dist/components/MultiSelect/MultiSelect.cjs.map +1 -1
- package/dist/components/MultiSelect/MultiSelect.js.map +1 -1
- package/dist/components/NavigationMenu/NavigationMenu.cjs.map +1 -1
- package/dist/components/NavigationMenu/NavigationMenu.js.map +1 -1
- package/dist/components/NotificationCenter/NotificationCenter.cjs.map +1 -1
- package/dist/components/NotificationCenter/NotificationCenter.js.map +1 -1
- package/dist/components/Page/Page.cjs.map +1 -1
- package/dist/components/Page/Page.js.map +1 -1
- package/dist/components/Pagination/Pagination.cjs.map +1 -1
- package/dist/components/Pagination/Pagination.js.map +1 -1
- package/dist/components/PasswordInput/PasswordInput.cjs.map +1 -1
- package/dist/components/PasswordInput/PasswordInput.js.map +1 -1
- package/dist/components/PinInput/PinInput.cjs.map +1 -1
- package/dist/components/PinInput/PinInput.js.map +1 -1
- package/dist/components/Popover/Popover.cjs.map +1 -1
- package/dist/components/Popover/Popover.js.map +1 -1
- package/dist/components/Progress/Progress.cjs.map +1 -1
- package/dist/components/Progress/Progress.js.map +1 -1
- package/dist/components/QRCode/qr-encode.cjs.map +1 -1
- package/dist/components/QRCode/qr-encode.js.map +1 -1
- package/dist/components/Radio/Radio.cjs.map +1 -1
- package/dist/components/Radio/Radio.js.map +1 -1
- package/dist/components/RangeSlider/RangeSlider.cjs.map +1 -1
- package/dist/components/RangeSlider/RangeSlider.js.map +1 -1
- package/dist/components/RatingStars/RatingStars.cjs.map +1 -1
- package/dist/components/RatingStars/RatingStars.js.map +1 -1
- package/dist/components/RefreshIndicator/RefreshIndicator.cjs.map +1 -1
- package/dist/components/RefreshIndicator/RefreshIndicator.js.map +1 -1
- package/dist/components/Resizable/Resizable.cjs.map +1 -1
- package/dist/components/Resizable/Resizable.js.map +1 -1
- package/dist/components/Scheduler/Scheduler.cjs.map +1 -1
- package/dist/components/Scheduler/Scheduler.js.map +1 -1
- package/dist/components/Sidebar/Sidebar.cjs.map +1 -1
- package/dist/components/Sidebar/Sidebar.js.map +1 -1
- package/dist/components/SignaturePad/SignaturePad.cjs.map +1 -1
- package/dist/components/SignaturePad/SignaturePad.js.map +1 -1
- package/dist/components/Slider/Slider.cjs.map +1 -1
- package/dist/components/Slider/Slider.js.map +1 -1
- package/dist/components/Sparkline/Sparkline.cjs.map +1 -1
- package/dist/components/Sparkline/Sparkline.js.map +1 -1
- package/dist/components/StepperInput/StepperInput.cjs.map +1 -1
- package/dist/components/StepperInput/StepperInput.js.map +1 -1
- package/dist/components/Table/Table.cjs.map +1 -1
- package/dist/components/Table/Table.js.map +1 -1
- package/dist/components/TimePicker/TimePicker.cjs.map +1 -1
- package/dist/components/TimePicker/TimePicker.js.map +1 -1
- package/dist/components/Toast/ToastProvider.cjs.map +1 -1
- package/dist/components/Toast/ToastProvider.js.map +1 -1
- package/dist/components/Tour/Tour.cjs.map +1 -1
- package/dist/components/Tour/Tour.js.map +1 -1
- package/dist/components/Transfer/Transfer.cjs.map +1 -1
- package/dist/components/Transfer/Transfer.js.map +1 -1
- package/dist/components/TreeView/TreeView.cjs.map +1 -1
- package/dist/components/TreeView/TreeView.js.map +1 -1
- package/dist/components/VirtualList/VirtualList.cjs.map +1 -1
- package/dist/components/VirtualList/VirtualList.js.map +1 -1
- package/dist/components/VirtualTable/VirtualTable.cjs.map +1 -1
- package/dist/components/VirtualTable/VirtualTable.js.map +1 -1
- package/dist/components/Wizard/Wizard.cjs.map +1 -1
- package/dist/components/Wizard/Wizard.js.map +1 -1
- package/dist/editor/RichTextEditor.cjs.map +1 -1
- package/dist/editor/RichTextEditor.js.map +1 -1
- package/dist/forms/FormField.cjs.map +1 -1
- package/dist/forms/FormField.js.map +1 -1
- package/dist/geo/TrajectoryMap.cjs.map +1 -1
- package/dist/geo/TrajectoryMap.js.map +1 -1
- package/dist/geo/estimate.cjs.map +1 -1
- package/dist/geo/estimate.js.map +1 -1
- package/dist/geo/projection.cjs.map +1 -1
- package/dist/geo/projection.js.map +1 -1
- package/dist/hooks/use-event-listener.cjs.map +1 -1
- package/dist/hooks/use-event-listener.js.map +1 -1
- package/dist/hooks/use-local-storage.cjs.map +1 -1
- package/dist/hooks/use-local-storage.js.map +1 -1
- package/dist/hooks/use-sortable.cjs.map +1 -1
- package/dist/hooks/use-sortable.js.map +1 -1
- package/dist/http/api-client.cjs.map +1 -1
- package/dist/http/api-client.js.map +1 -1
- package/dist/http/errors.cjs.map +1 -1
- package/dist/http/errors.js.map +1 -1
- package/dist/http/resumable-upload.cjs.map +1 -1
- package/dist/http/resumable-upload.js.map +1 -1
- package/dist/http/upload-with-progress.cjs.map +1 -1
- package/dist/http/upload-with-progress.js.map +1 -1
- package/dist/i18n/I18nProvider.cjs.map +1 -1
- package/dist/i18n/I18nProvider.js.map +1 -1
- package/dist/imaging/canvas.cjs.map +1 -1
- package/dist/imaging/canvas.js.map +1 -1
- package/dist/imaging.d.ts +5 -0
- package/dist/logger/logger.cjs.map +1 -1
- package/dist/logger/logger.js.map +1 -1
- package/dist/oauth/GoogleSignIn.cjs.map +1 -1
- package/dist/oauth/GoogleSignIn.js.map +1 -1
- package/dist/offline/create-offline-sync.cjs.map +1 -1
- package/dist/offline/create-offline-sync.js.map +1 -1
- package/dist/perf/cache-size.cjs +1 -1
- package/dist/perf/cache-size.cjs.map +1 -1
- package/dist/perf/cache-size.js +27 -6
- package/dist/perf/cache-size.js.map +1 -1
- package/dist/sse/create-event-stream.cjs.map +1 -1
- package/dist/sse/create-event-stream.js.map +1 -1
- package/dist/styles.css +1 -1
- package/dist/sw/background-sync.cjs.map +1 -1
- package/dist/sw/background-sync.js.map +1 -1
- package/dist/sw/cache.cjs.map +1 -1
- package/dist/sw/cache.js.map +1 -1
- package/dist/sw.d.ts +12 -0
- package/dist/tabular/compact.cjs.map +1 -1
- package/dist/tabular/compact.js.map +1 -1
- package/dist/tabular/predictor.cjs.map +1 -1
- package/dist/tabular/predictor.js.map +1 -1
- package/dist/tabular.d.ts +7 -2
- package/dist/tempest-react-sdk.d.ts +133 -6
- package/dist/theme/ThemeProvider.cjs.map +1 -1
- package/dist/theme/ThemeProvider.js.map +1 -1
- package/dist/theme/color.cjs.map +1 -1
- package/dist/theme/color.js.map +1 -1
- package/dist/theme/create-theme.cjs.map +1 -1
- package/dist/theme/create-theme.js.map +1 -1
- package/dist/theme/data-viz-ramps.cjs +1 -1
- package/dist/theme/data-viz-ramps.cjs.map +1 -1
- package/dist/theme/data-viz-ramps.js +1 -1
- package/dist/theme/data-viz-ramps.js.map +1 -1
- package/dist/utils/storage.cjs.map +1 -1
- package/dist/utils/storage.js.map +1 -1
- package/dist/vision/core/canvas.cjs.map +1 -1
- package/dist/vision/core/canvas.js.map +1 -1
- package/dist/vision/core/exceptions.cjs.map +1 -1
- package/dist/vision/core/exceptions.js.map +1 -1
- package/dist/vision/core/graph.cjs.map +1 -1
- package/dist/vision/core/graph.js.map +1 -1
- package/dist/vision/core/metadata.cjs.map +1 -1
- package/dist/vision/core/metadata.js.map +1 -1
- package/dist/vision/core/providers.cjs.map +1 -1
- package/dist/vision/core/providers.js.map +1 -1
- package/dist/vision/core/session.cjs.map +1 -1
- package/dist/vision/core/session.js.map +1 -1
- package/dist/vision/core/timing.cjs.map +1 -1
- package/dist/vision/core/timing.js.map +1 -1
- package/dist/vision/io/image.cjs.map +1 -1
- package/dist/vision/io/image.js.map +1 -1
- package/dist/vision/labels.cjs.map +1 -1
- package/dist/vision/labels.js.map +1 -1
- package/dist/vision/luminance.cjs.map +1 -1
- package/dist/vision/luminance.js.map +1 -1
- package/dist/vision/postprocess/classification.cjs.map +1 -1
- package/dist/vision/postprocess/classification.js.map +1 -1
- package/dist/vision/postprocess/detection.cjs.map +1 -1
- package/dist/vision/postprocess/detection.js.map +1 -1
- package/dist/vision/postprocess/segmentation.cjs.map +1 -1
- package/dist/vision/postprocess/segmentation.js.map +1 -1
- package/dist/vision/preprocess/image.cjs.map +1 -1
- package/dist/vision/preprocess/image.js.map +1 -1
- package/dist/vision/results.cjs.map +1 -1
- package/dist/vision/results.js.map +1 -1
- package/dist/vision/tasks/base.cjs.map +1 -1
- package/dist/vision/tasks/base.js.map +1 -1
- package/dist/vision/tasks/classifier.cjs.map +1 -1
- package/dist/vision/tasks/classifier.js.map +1 -1
- package/dist/vision/tasks/detector.cjs.map +1 -1
- package/dist/vision/tasks/detector.js.map +1 -1
- package/dist/vision/tasks/segmenter.cjs.map +1 -1
- package/dist/vision/tasks/segmenter.js.map +1 -1
- package/dist/vision/types.cjs.map +1 -1
- package/dist/vision/types.js.map +1 -1
- package/dist/vision.d.ts +6 -0
- package/dist/vite/tempest-icons.cjs.map +1 -1
- package/dist/vite/tempest-icons.js.map +1 -1
- package/dist/vite/tempest-pwa-icons.cjs.map +1 -1
- package/dist/vite/tempest-pwa-icons.js.map +1 -1
- package/dist/ws/create-web-socket.cjs.map +1 -1
- package/dist/ws/create-web-socket.js.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"luminance.js","names":[],"sources":["../../src/vision/luminance.ts"],"sourcesContent":["/**\n * Frame-brightness helpers — measure the mean luminance of an already-decoded\n * frame (`<img>`, `<video>`, `<canvas>`, `ImageBitmap` or `OffscreenCanvas`) so\n * a UI can reject underexposed captures before paying the cost of downstream\n * inference.\n *\n * These are framework-agnostic pure functions; {@link useLiveLuminance} wires\n * {@link computeImageLuminance} into a React `requestAnimationFrame` loop for\n * live camera feedback.\n */\n\n/**\n * Longest edge (in pixels) the source is downsampled to before sampling.\n * Averaging over a small downsample is statistically equivalent for a\n * brightness threshold and orders of magnitude faster than reading every pixel\n * of a full-resolution camera frame.\n */\nexport const LUMINANCE_SAMPLE_MAX_EDGE = 256;\n\n/**\n * Drawable source we can sample luminance from.\n *\n * The list tracks what `CanvasRenderingContext2D.drawImage` accepts and we can\n * read a pixel size off, which is what the implementation actually needs.\n * `ImageBitmap` matters for the decode-downscaled path: `createImageBitmap(blob,\n * { resizeWidth })` is how a caller avoids materialising a full-resolution\n * phone photo, and the frame it hands back is the frame whose brightness has to\n * be checked.\n */\nexport type LuminanceSource =\n HTMLImageElement | HTMLVideoElement | HTMLCanvasElement | ImageBitmap | OffscreenCanvas;\n\n/**\n * Natural pixel size of the source (`0`/`0` while it is still unloaded).\n *\n * `ImageBitmap` and `OffscreenCanvas` both expose plain `width`/`height`, so\n * they fall through to the same branch as a canvas — but they are named\n * explicitly rather than left to the `naturalWidth || width` fallback, which\n * only reads as intentional for an `<img>`.\n */\nfunction sourceSize(source: LuminanceSource): { width: number; height: number } {\n if (source instanceof HTMLVideoElement) {\n return { width: source.videoWidth, height: source.videoHeight };\n }\n if (source instanceof HTMLImageElement) {\n return {\n width: source.naturalWidth || source.width,\n height: source.naturalHeight || source.height,\n };\n }\n return { width: source.width, height: source.height };\n}\n\n/**\n * Mean BT.709 luminance (`0.2126*R + 0.7152*G + 0.0722*B`) of a decoded frame,\n * scaled to `0..255`. See {@link LuminanceSource} for what counts as one.\n *\n * The source is downsampled so its longest edge is at most\n * {@link LUMINANCE_SAMPLE_MAX_EDGE} before pixels are read. The 2D context is\n * created with `willReadFrequently` so repeated sampling (live feedback) stays\n * on the fast path.\n *\n * Pass `reusableCanvas` to avoid allocating a fresh canvas every frame in a hot\n * loop; when omitted a one-shot detached canvas is created.\n *\n * @param source - the decoded frame to sample.\n * @param reusableCanvas - optional canvas reused across frames to avoid GC churn.\n * @returns The mean luminance in `0..255`, or `0` when the source is unloaded\n * (zero-sized) or a 2D context is unavailable.\n */\nexport function computeImageLuminance(\n source: LuminanceSource,\n reusableCanvas?: HTMLCanvasElement,\n): number {\n const { width: srcW, height: srcH } = sourceSize(source);\n if (srcW === 0 || srcH === 0) return 0;\n\n const scale = Math.min(1, LUMINANCE_SAMPLE_MAX_EDGE / Math.max(srcW, srcH));\n const w = Math.max(1, Math.round(srcW * scale));\n const h = Math.max(1, Math.round(srcH * scale));\n\n const canvas = reusableCanvas ?? document.createElement(\"canvas\");\n canvas.width = w;\n canvas.height = h;\n const ctx = canvas.getContext(\"2d\", { willReadFrequently: true });\n if (!ctx) return 0;\n ctx.drawImage(source, 0, 0, w, h);\n\n const data = ctx.getImageData(0, 0, w, h).data;\n let sum = 0;\n const pixelCount = w * h;\n for (let i = 0; i < data.length; i += 4) {\n sum += 0.2126 * data[i] + 0.7152 * data[i + 1] + 0.0722 * data[i + 2];\n }\n return sum / pixelCount;\n}\n\n/**\n * Whether a measured luminance clears a brightness threshold.\n *\n * `threshold` is intentionally required — a sensible value is\n * application-specific (it depends on the model, the lighting the model was\n * trained on, and the acceptable false-reject rate), so the SDK does not bake\n * in a default.\n *\n * @param luminance - measured mean luminance in `0..255`.\n * @param threshold - minimum acceptable luminance in `0..255`.\n * @returns `true` when `luminance >= threshold`.\n */\nexport function isLuminanceAcceptable(luminance: number, threshold: number): boolean {\n return luminance >= threshold;\n}\n\n/**\n * Error raised when a captured frame is too dark to be analysed reliably.\n * Carries the measured luminance and the threshold it failed so callers can\n * surface actionable feedback.\n */\nexport class LowLuminanceError extends Error {\n /** Measured mean luminance, `0..255`. */\n readonly luminance: number;\n /** Threshold that was checked against, `0..255`. */\n readonly threshold: number;\n\n /**\n * @param luminance - the measured mean luminance in `0..255`.\n * @param threshold - the threshold the measurement failed to reach.\n */\n constructor(luminance: number, threshold: number) {\n super(\"Image is too dark to analyse. Capture again in a brighter environment.\");\n this.name = \"LowLuminanceError\";\n this.luminance = luminance;\n this.threshold = threshold;\n }\n}\n"],"mappings":";
|
|
1
|
+
{"version":3,"file":"luminance.js","names":[],"sources":["../../src/vision/luminance.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Frame-brightness helpers — measure the mean luminance of an already-decoded\n * frame (`<img>`, `<video>`, `<canvas>`, `ImageBitmap` or `OffscreenCanvas`) so\n * a UI can reject underexposed captures before paying the cost of downstream\n * inference.\n *\n * These are framework-agnostic pure functions; {@link useLiveLuminance} wires\n * {@link computeImageLuminance} into a React `requestAnimationFrame` loop for\n * live camera feedback.\n */\n\n/**\n * Longest edge (in pixels) the source is downsampled to before sampling.\n * Averaging over a small downsample is statistically equivalent for a\n * brightness threshold and orders of magnitude faster than reading every pixel\n * of a full-resolution camera frame.\n */\nexport const LUMINANCE_SAMPLE_MAX_EDGE = 256;\n\n/**\n * Drawable source we can sample luminance from.\n *\n * The list tracks what `CanvasRenderingContext2D.drawImage` accepts and we can\n * read a pixel size off, which is what the implementation actually needs.\n * `ImageBitmap` matters for the decode-downscaled path: `createImageBitmap(blob,\n * { resizeWidth })` is how a caller avoids materialising a full-resolution\n * phone photo, and the frame it hands back is the frame whose brightness has to\n * be checked.\n */\nexport type LuminanceSource =\n HTMLImageElement | HTMLVideoElement | HTMLCanvasElement | ImageBitmap | OffscreenCanvas;\n\n/**\n * Natural pixel size of the source (`0`/`0` while it is still unloaded).\n *\n * `ImageBitmap` and `OffscreenCanvas` both expose plain `width`/`height`, so\n * they fall through to the same branch as a canvas — but they are named\n * explicitly rather than left to the `naturalWidth || width` fallback, which\n * only reads as intentional for an `<img>`.\n */\nfunction sourceSize(source: LuminanceSource): { width: number; height: number } {\n if (source instanceof HTMLVideoElement) {\n return { width: source.videoWidth, height: source.videoHeight };\n }\n if (source instanceof HTMLImageElement) {\n return {\n width: source.naturalWidth || source.width,\n height: source.naturalHeight || source.height,\n };\n }\n return { width: source.width, height: source.height };\n}\n\n/**\n * Mean BT.709 luminance (`0.2126*R + 0.7152*G + 0.0722*B`) of a decoded frame,\n * scaled to `0..255`. See {@link LuminanceSource} for what counts as one.\n *\n * The source is downsampled so its longest edge is at most\n * {@link LUMINANCE_SAMPLE_MAX_EDGE} before pixels are read. The 2D context is\n * created with `willReadFrequently` so repeated sampling (live feedback) stays\n * on the fast path.\n *\n * Pass `reusableCanvas` to avoid allocating a fresh canvas every frame in a hot\n * loop; when omitted a one-shot detached canvas is created.\n *\n * @param source - the decoded frame to sample.\n * @param reusableCanvas - optional canvas reused across frames to avoid GC churn.\n * @returns The mean luminance in `0..255`, or `0` when the source is unloaded\n * (zero-sized) or a 2D context is unavailable.\n */\nexport function computeImageLuminance(\n source: LuminanceSource,\n reusableCanvas?: HTMLCanvasElement,\n): number {\n const { width: srcW, height: srcH } = sourceSize(source);\n if (srcW === 0 || srcH === 0) return 0;\n\n const scale = Math.min(1, LUMINANCE_SAMPLE_MAX_EDGE / Math.max(srcW, srcH));\n const w = Math.max(1, Math.round(srcW * scale));\n const h = Math.max(1, Math.round(srcH * scale));\n\n const canvas = reusableCanvas ?? document.createElement(\"canvas\");\n canvas.width = w;\n canvas.height = h;\n const ctx = canvas.getContext(\"2d\", { willReadFrequently: true });\n if (!ctx) return 0;\n ctx.drawImage(source, 0, 0, w, h);\n\n const data = ctx.getImageData(0, 0, w, h).data;\n let sum = 0;\n const pixelCount = w * h;\n for (let i = 0; i < data.length; i += 4) {\n sum += 0.2126 * data[i] + 0.7152 * data[i + 1] + 0.0722 * data[i + 2];\n }\n return sum / pixelCount;\n}\n\n/**\n * Whether a measured luminance clears a brightness threshold.\n *\n * `threshold` is intentionally required — a sensible value is\n * application-specific (it depends on the model, the lighting the model was\n * trained on, and the acceptable false-reject rate), so the SDK does not bake\n * in a default.\n *\n * @param luminance - measured mean luminance in `0..255`.\n * @param threshold - minimum acceptable luminance in `0..255`.\n * @returns `true` when `luminance >= threshold`.\n */\nexport function isLuminanceAcceptable(luminance: number, threshold: number): boolean {\n return luminance >= threshold;\n}\n\n/**\n * Error raised when a captured frame is too dark to be analysed reliably.\n * Carries the measured luminance and the threshold it failed so callers can\n * surface actionable feedback.\n */\nexport class LowLuminanceError extends Error {\n /** Measured mean luminance, `0..255`. */\n readonly luminance: number;\n /** Threshold that was checked against, `0..255`. */\n readonly threshold: number;\n\n /**\n * @param luminance - the measured mean luminance in `0..255`.\n * @param threshold - the threshold the measurement failed to reach.\n */\n constructor(luminance: number, threshold: number) {\n super(\"Image is too dark to analyse. Capture again in a brighter environment.\");\n this.name = \"LowLuminanceError\";\n this.luminance = luminance;\n this.threshold = threshold;\n }\n}\n"],"mappings":";AAkBA,IAAa,IAA4B;AAuBzC,SAAS,EAAW,GAA4D;CAU5E,OATI,aAAkB,mBACX;EAAE,OAAO,EAAO;EAAY,QAAQ,EAAO;CAAY,IAE9D,aAAkB,mBACX;EACH,OAAO,EAAO,gBAAgB,EAAO;EACrC,QAAQ,EAAO,iBAAiB,EAAO;CAC3C,IAEG;EAAE,OAAO,EAAO;EAAO,QAAQ,EAAO;CAAO;AACxD;AAmBA,SAAgB,EACZ,GACA,GACM;CACN,IAAM,EAAE,OAAO,GAAM,QAAQ,MAAS,EAAW,CAAM;CACvD,IAAI,MAAS,KAAK,MAAS,GAAG,OAAO;CAErC,IAAM,IAAQ,KAAK,IAAI,GAAA,MAA+B,KAAK,IAAI,GAAM,CAAI,CAAC,GACpE,IAAI,KAAK,IAAI,GAAG,KAAK,MAAM,IAAO,CAAK,CAAC,GACxC,IAAI,KAAK,IAAI,GAAG,KAAK,MAAM,IAAO,CAAK,CAAC,GAExC,IAAS,KAAkB,SAAS,cAAc,QAAQ;CAEhE,AADA,EAAO,QAAQ,GACf,EAAO,SAAS;CAChB,IAAM,IAAM,EAAO,WAAW,MAAM,EAAE,oBAAoB,GAAK,CAAC;CAChE,IAAI,CAAC,GAAK,OAAO;CACjB,EAAI,UAAU,GAAQ,GAAG,GAAG,GAAG,CAAC;CAEhC,IAAM,IAAO,EAAI,aAAa,GAAG,GAAG,GAAG,CAAC,CAAC,CAAC,MACtC,IAAM,GACJ,IAAa,IAAI;CACvB,KAAK,IAAI,IAAI,GAAG,IAAI,EAAK,QAAQ,KAAK,GAClC,KAAO,QAAS,EAAK,KAAK,QAAS,EAAK,IAAI,KAAK,QAAS,EAAK,IAAI;CAEvE,OAAO,IAAM;AACjB;AAcA,SAAgB,EAAsB,GAAmB,GAA4B;CACjF,OAAO,KAAa;AACxB;AAOA,IAAa,IAAb,cAAuC,MAAM;CAEzC;CAEA;CAMA,YAAY,GAAmB,GAAmB;EAI9C,AAHA,MAAM,wEAAwE,GAC9E,KAAK,OAAO,qBACZ,KAAK,YAAY,GACjB,KAAK,YAAY;CACrB;AACJ"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"classification.cjs","names":[],"sources":["../../../src/vision/postprocess/classification.ts"],"sourcesContent":["/**\n * Classification head postprocessing — softmax + top-k.\n */\n\n/** Apply numerically-stable softmax to a 1-D vector of logits. */\nexport function softmax(logits: Float32Array | readonly number[]): Float32Array {\n const n = logits.length;\n const out = new Float32Array(n);\n let max = -Infinity;\n for (let i = 0; i < n; i++) {\n const v = logits[i] as number;\n if (v > max) max = v;\n }\n let sum = 0;\n for (let i = 0; i < n; i++) {\n const e = Math.exp((logits[i] as number) - max);\n out[i] = e;\n sum += e;\n }\n for (let i = 0; i < n; i++) {\n out[i] = (out[i] as number) / sum;\n }\n return out;\n}\n\nexport interface TopKResult {\n readonly indices: Int32Array;\n readonly values: Float32Array;\n}\n\n/**\n * Return the top-k entries of a 1-D probability vector, sorted descending.\n *\n * @param k Number of entries to return; `null` returns all entries.\n */\nexport function topK(probabilities: Float32Array, k: number | null): TopKResult {\n const n = probabilities.length;\n const kEff = k === null ? n : Math.min(Math.max(0, k), n);\n\n const idx = new Array<number>(n);\n for (let i = 0; i < n; i++) idx[i] = i;\n idx.sort((a, b) => (probabilities[b] as number) - (probabilities[a] as number));\n\n const indices = new Int32Array(kEff);\n const values = new Float32Array(kEff);\n for (let i = 0; i < kEff; i++) {\n const j = idx[i] as number;\n indices[i] = j;\n values[i] = probabilities[j] as number;\n }\n return { indices, values };\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"classification.cjs","names":[],"sources":["../../../src/vision/postprocess/classification.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Classification head postprocessing — softmax + top-k.\n */\n\n/** Apply numerically-stable softmax to a 1-D vector of logits. */\nexport function softmax(logits: Float32Array | readonly number[]): Float32Array {\n const n = logits.length;\n const out = new Float32Array(n);\n let max = -Infinity;\n for (let i = 0; i < n; i++) {\n const v = logits[i] as number;\n if (v > max) max = v;\n }\n let sum = 0;\n for (let i = 0; i < n; i++) {\n const e = Math.exp((logits[i] as number) - max);\n out[i] = e;\n sum += e;\n }\n for (let i = 0; i < n; i++) {\n out[i] = (out[i] as number) / sum;\n }\n return out;\n}\n\nexport interface TopKResult {\n readonly indices: Int32Array;\n readonly values: Float32Array;\n}\n\n/**\n * Return the top-k entries of a 1-D probability vector, sorted descending.\n *\n * @param k Number of entries to return; `null` returns all entries.\n */\nexport function topK(probabilities: Float32Array, k: number | null): TopKResult {\n const n = probabilities.length;\n const kEff = k === null ? n : Math.min(Math.max(0, k), n);\n\n const idx = new Array<number>(n);\n for (let i = 0; i < n; i++) idx[i] = i;\n idx.sort((a, b) => (probabilities[b] as number) - (probabilities[a] as number));\n\n const indices = new Int32Array(kEff);\n const values = new Float32Array(kEff);\n for (let i = 0; i < kEff; i++) {\n const j = idx[i] as number;\n indices[i] = j;\n values[i] = probabilities[j] as number;\n }\n return { indices, values };\n}\n"],"mappings":"AAMA,SAAgB,EAAQ,EAAwD,CAC5E,IAAM,EAAI,EAAO,OACX,EAAM,IAAI,aAAa,CAAC,EAC1B,EAAM,KACV,IAAK,IAAI,EAAI,EAAG,EAAI,EAAG,IAAK,CACxB,IAAM,EAAI,EAAO,GACb,EAAI,IAAK,EAAM,EACvB,CACA,IAAI,EAAM,EACV,IAAK,IAAI,EAAI,EAAG,EAAI,EAAG,IAAK,CACxB,IAAM,EAAI,KAAK,IAAK,EAAO,GAAgB,CAAG,EAC9C,EAAI,GAAK,EACT,GAAO,CACX,CACA,IAAK,IAAI,EAAI,EAAG,EAAI,EAAG,IACnB,EAAI,GAAM,EAAI,GAAgB,EAElC,OAAO,CACX,CAYA,SAAgB,EAAK,EAA6B,EAA8B,CAC5E,IAAM,EAAI,EAAc,OAClB,EAAO,IAAM,KAAO,EAAI,KAAK,IAAI,KAAK,IAAI,EAAG,CAAC,EAAG,CAAC,EAElD,EAAU,MAAc,CAAC,EAC/B,IAAK,IAAI,EAAI,EAAG,EAAI,EAAG,IAAK,EAAI,GAAK,EACrC,EAAI,MAAM,EAAG,IAAO,EAAc,GAAiB,EAAc,EAAa,EAE9E,IAAM,EAAU,IAAI,WAAW,CAAI,EAC7B,EAAS,IAAI,aAAa,CAAI,EACpC,IAAK,IAAI,EAAI,EAAG,EAAI,EAAM,IAAK,CAC3B,IAAM,EAAI,EAAI,GACd,EAAQ,GAAK,EACb,EAAO,GAAK,EAAc,EAC9B,CACA,MAAO,CAAE,UAAS,QAAO,CAC7B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"classification.js","names":[],"sources":["../../../src/vision/postprocess/classification.ts"],"sourcesContent":["/**\n * Classification head postprocessing — softmax + top-k.\n */\n\n/** Apply numerically-stable softmax to a 1-D vector of logits. */\nexport function softmax(logits: Float32Array | readonly number[]): Float32Array {\n const n = logits.length;\n const out = new Float32Array(n);\n let max = -Infinity;\n for (let i = 0; i < n; i++) {\n const v = logits[i] as number;\n if (v > max) max = v;\n }\n let sum = 0;\n for (let i = 0; i < n; i++) {\n const e = Math.exp((logits[i] as number) - max);\n out[i] = e;\n sum += e;\n }\n for (let i = 0; i < n; i++) {\n out[i] = (out[i] as number) / sum;\n }\n return out;\n}\n\nexport interface TopKResult {\n readonly indices: Int32Array;\n readonly values: Float32Array;\n}\n\n/**\n * Return the top-k entries of a 1-D probability vector, sorted descending.\n *\n * @param k Number of entries to return; `null` returns all entries.\n */\nexport function topK(probabilities: Float32Array, k: number | null): TopKResult {\n const n = probabilities.length;\n const kEff = k === null ? n : Math.min(Math.max(0, k), n);\n\n const idx = new Array<number>(n);\n for (let i = 0; i < n; i++) idx[i] = i;\n idx.sort((a, b) => (probabilities[b] as number) - (probabilities[a] as number));\n\n const indices = new Int32Array(kEff);\n const values = new Float32Array(kEff);\n for (let i = 0; i < kEff; i++) {\n const j = idx[i] as number;\n indices[i] = j;\n values[i] = probabilities[j] as number;\n }\n return { indices, values };\n}\n"],"mappings":";
|
|
1
|
+
{"version":3,"file":"classification.js","names":[],"sources":["../../../src/vision/postprocess/classification.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Classification head postprocessing — softmax + top-k.\n */\n\n/** Apply numerically-stable softmax to a 1-D vector of logits. */\nexport function softmax(logits: Float32Array | readonly number[]): Float32Array {\n const n = logits.length;\n const out = new Float32Array(n);\n let max = -Infinity;\n for (let i = 0; i < n; i++) {\n const v = logits[i] as number;\n if (v > max) max = v;\n }\n let sum = 0;\n for (let i = 0; i < n; i++) {\n const e = Math.exp((logits[i] as number) - max);\n out[i] = e;\n sum += e;\n }\n for (let i = 0; i < n; i++) {\n out[i] = (out[i] as number) / sum;\n }\n return out;\n}\n\nexport interface TopKResult {\n readonly indices: Int32Array;\n readonly values: Float32Array;\n}\n\n/**\n * Return the top-k entries of a 1-D probability vector, sorted descending.\n *\n * @param k Number of entries to return; `null` returns all entries.\n */\nexport function topK(probabilities: Float32Array, k: number | null): TopKResult {\n const n = probabilities.length;\n const kEff = k === null ? n : Math.min(Math.max(0, k), n);\n\n const idx = new Array<number>(n);\n for (let i = 0; i < n; i++) idx[i] = i;\n idx.sort((a, b) => (probabilities[b] as number) - (probabilities[a] as number));\n\n const indices = new Int32Array(kEff);\n const values = new Float32Array(kEff);\n for (let i = 0; i < kEff; i++) {\n const j = idx[i] as number;\n indices[i] = j;\n values[i] = probabilities[j] as number;\n }\n return { indices, values };\n}\n"],"mappings":";AAMA,SAAgB,EAAQ,GAAwD;CAC5E,IAAM,IAAI,EAAO,QACX,IAAM,IAAI,aAAa,CAAC,GAC1B,IAAM;CACV,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KAAK;EACxB,IAAM,IAAI,EAAO;EACjB,AAAI,IAAI,MAAK,IAAM;CACvB;CACA,IAAI,IAAM;CACV,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KAAK;EACxB,IAAM,IAAI,KAAK,IAAK,EAAO,KAAgB,CAAG;EAE9C,AADA,EAAI,KAAK,GACT,KAAO;CACX;CACA,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KACnB,EAAI,KAAM,EAAI,KAAgB;CAElC,OAAO;AACX;AAYA,SAAgB,EAAK,GAA6B,GAA8B;CAC5E,IAAM,IAAI,EAAc,QAClB,IAAO,MAAM,OAAO,IAAI,KAAK,IAAI,KAAK,IAAI,GAAG,CAAC,GAAG,CAAC,GAElD,IAAU,MAAc,CAAC;CAC/B,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KAAK,EAAI,KAAK;CACrC,EAAI,MAAM,GAAG,MAAO,EAAc,KAAiB,EAAc,EAAa;CAE9E,IAAM,IAAU,IAAI,WAAW,CAAI,GAC7B,IAAS,IAAI,aAAa,CAAI;CACpC,KAAK,IAAI,IAAI,GAAG,IAAI,GAAM,KAAK;EAC3B,IAAM,IAAI,EAAI;EAEd,AADA,EAAQ,KAAK,GACb,EAAO,KAAK,EAAc;CAC9B;CACA,OAAO;EAAE;EAAS;CAAO;AAC7B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"detection.cjs","names":[],"sources":["../../../src/vision/postprocess/detection.ts"],"sourcesContent":["/**\n * Detection head postprocessing: anchor-free YOLO decoding + non-maximum suppression.\n *\n * The shared {@link decodeYoloAnchors} helper does the per-anchor work that\n * is identical for plain detection and segmentation (transpose, xywh→xyxy,\n * letterbox unmap, per-class NMS, sort & cap). {@link decodeYolo} is a thin\n * wrapper around it; the segmentation module ({@link ./segmentation.js})\n * calls the helper directly so it can also recover the per-anchor mask\n * coefficients.\n *\n * Works for any YOLO export with the post-v8 anchor-free head:\n * **YOLOv8 / v9 / v10 / v11 / v12** detect heads, all of which share the\n * `[1, 4 + nc, N]` output layout.\n */\n\nimport { BoundingBox } from \"../types\";\n\n/**\n * Greedy non-maximum suppression on axis-aligned bounding boxes.\n *\n * Mirrors `torchvision.ops.nms` (keeps boxes with the highest score, drops\n * any subsequent box whose IoU exceeds the threshold).\n *\n * @param boxes Flat array of length `4 * N` in xyxy order: `[x1,y1,x2,y2, ...]`.\n * @param scores Detection score per box, length `N`.\n * @param iouThreshold Boxes with IoU above this threshold relative to a kept box are suppressed.\n * @returns Indices of kept boxes, in descending score order.\n */\nexport function nms(boxes: Float32Array, scores: Float32Array, iouThreshold: number): Int32Array {\n const n = scores.length;\n if (n === 0) return new Int32Array(0);\n\n const areas = new Float32Array(n);\n for (let i = 0; i < n; i++) {\n const x1 = boxes[i * 4] as number;\n const y1 = boxes[i * 4 + 1] as number;\n const x2 = boxes[i * 4 + 2] as number;\n const y2 = boxes[i * 4 + 3] as number;\n areas[i] = Math.max(0, x2 - x1) * Math.max(0, y2 - y1);\n }\n\n const order = new Array<number>(n);\n for (let i = 0; i < n; i++) order[i] = i;\n order.sort((a, b) => (scores[b] as number) - (scores[a] as number));\n\n const suppressed = new Uint8Array(n);\n const keep: number[] = [];\n\n for (let oi = 0; oi < order.length; oi++) {\n const i = order[oi] as number;\n if (suppressed[i]) continue;\n keep.push(i);\n\n const ax1 = boxes[i * 4] as number;\n const ay1 = boxes[i * 4 + 1] as number;\n const ax2 = boxes[i * 4 + 2] as number;\n const ay2 = boxes[i * 4 + 3] as number;\n const ai = areas[i] as number;\n\n for (let oj = oi + 1; oj < order.length; oj++) {\n const j = order[oj] as number;\n if (suppressed[j]) continue;\n\n const bx1 = boxes[j * 4] as number;\n const by1 = boxes[j * 4 + 1] as number;\n const bx2 = boxes[j * 4 + 2] as number;\n const by2 = boxes[j * 4 + 3] as number;\n\n const ix1 = Math.max(ax1, bx1);\n const iy1 = Math.max(ay1, by1);\n const ix2 = Math.min(ax2, bx2);\n const iy2 = Math.min(ay2, by2);\n const iw = Math.max(0, ix2 - ix1);\n const ih = Math.max(0, iy2 - iy1);\n const inter = iw * ih;\n const union = ai + (areas[j] as number) - inter;\n const iou = union > 0 ? inter / union : 0;\n if (iou > iouThreshold) suppressed[j] = 1;\n }\n }\n\n return Int32Array.from(keep);\n}\n\n/**\n * Per-class NMS — boxes are suppressed only by other boxes of the same class.\n *\n * Mirrors `torchvision.ops.batched_nms`.\n *\n * @param boxes Flat array of length `4 * N` in xyxy order.\n * @param scores Detection score per box, length `N`.\n * @param idxs Class index per box, length `N`. Boxes with different `idxs`\n * never suppress each other.\n * @param iouThreshold IoU threshold for suppression within a class.\n */\nexport function batchedNms(\n boxes: Float32Array,\n scores: Float32Array,\n idxs: Int32Array,\n iouThreshold: number,\n): Int32Array {\n if (scores.length === 0) return new Int32Array(0);\n\n const byClass = new Map<number, number[]>();\n for (let i = 0; i < idxs.length; i++) {\n const c = idxs[i] as number;\n const list = byClass.get(c);\n if (list === undefined) byClass.set(c, [i]);\n else list.push(i);\n }\n\n const keep: number[] = [];\n for (const indices of byClass.values()) {\n const m = indices.length;\n const subBoxes = new Float32Array(m * 4);\n const subScores = new Float32Array(m);\n for (let k = 0; k < m; k++) {\n const i = indices[k] as number;\n subBoxes[k * 4] = boxes[i * 4] as number;\n subBoxes[k * 4 + 1] = boxes[i * 4 + 1] as number;\n subBoxes[k * 4 + 2] = boxes[i * 4 + 2] as number;\n subBoxes[k * 4 + 3] = boxes[i * 4 + 3] as number;\n subScores[k] = scores[i] as number;\n }\n const subKeep = nms(subBoxes, subScores, iouThreshold);\n for (let k = 0; k < subKeep.length; k++) {\n keep.push(indices[subKeep[k] as number] as number);\n }\n }\n\n keep.sort((a, b) => (scores[b] as number) - (scores[a] as number));\n return Int32Array.from(keep);\n}\n\nexport interface DecodeYoloAnchorsOptions {\n /** Number of class-score channels following the 4 box channels. */\n readonly numClasses: number;\n readonly originalWidth: number;\n readonly originalHeight: number;\n readonly padLeft: number;\n readonly padTop: number;\n readonly scale: number;\n readonly confThreshold: number;\n readonly iouThreshold: number;\n readonly maxDetections: number;\n}\n\nexport interface DecodedAnchors {\n /** Indices into the original `numAnchors` axis, in descending confidence order. */\n readonly anchorIndices: Int32Array;\n /** `[k, 4]` boxes in original-image pixel coords, flat row-major xyxy. */\n readonly boxesXyxy: Float32Array;\n /** Predicted class id per survivor. */\n readonly classIds: Int32Array;\n /** Confidence per survivor. */\n readonly confidences: Float32Array;\n}\n\n/**\n * Shared YOLO per-anchor decode used by both detection and segmentation\n * (v8 / v9 / v10 / v11 / v12).\n *\n * Only the first `4 + numClasses` channels are read; later channels (e.g.\n * mask coefficients) are ignored — callers can fetch them via the returned\n * {@link DecodedAnchors.anchorIndices}.\n *\n * @param data Flat per-anchor output, length `channels * numAnchors`.\n * @param dims Dims as reported by ORT, e.g. `[1, 84, 8400]` (det) or\n * `[1, 116, 8400]` (seg). The leading batch dim must be 1.\n */\nexport function decodeYoloAnchors(\n data: Float32Array,\n dims: readonly number[],\n options: DecodeYoloAnchorsOptions,\n): DecodedAnchors {\n let normalized = dims;\n if (normalized.length === 3) {\n if (normalized[0] !== 1) {\n throw new Error(`decodeYoloAnchors: expected batch size 1, got ${normalized[0]}.`);\n }\n normalized = [normalized[1] as number, normalized[2] as number];\n }\n if (normalized.length !== 2) {\n throw new Error(\n `decodeYoloAnchors: expected 2-D output after batch removal, got dims=${JSON.stringify(dims)}.`,\n );\n }\n const channels = normalized[0] as number;\n const numAnchors = normalized[1] as number;\n\n const {\n numClasses,\n originalWidth,\n originalHeight,\n padLeft,\n padTop,\n scale,\n confThreshold,\n iouThreshold,\n maxDetections,\n } = options;\n\n if (numClasses < 1 || numClasses + 4 > channels) {\n throw new Error(\n `decodeYoloAnchors: invalid numClasses=${numClasses} for channels=${channels}.`,\n );\n }\n if (data.length !== channels * numAnchors) {\n throw new Error(\n `decodeYoloAnchors: data length ${data.length} does not match channels*numAnchors=${channels * numAnchors}.`,\n );\n }\n\n type Candidate = {\n anchorIdx: number;\n x1: number;\n y1: number;\n x2: number;\n y2: number;\n classId: number;\n confidence: number;\n };\n const candidates: Candidate[] = [];\n\n for (let a = 0; a < numAnchors; a++) {\n let bestCls = 0;\n let bestScore = -Infinity;\n for (let c = 0; c < numClasses; c++) {\n const s = data[(4 + c) * numAnchors + a];\n if (s !== undefined && s > bestScore) {\n bestScore = s;\n bestCls = c;\n }\n }\n if (bestScore < confThreshold) continue;\n\n const cx = data[a] as number;\n const cy = data[numAnchors + a] as number;\n const w = data[2 * numAnchors + a] as number;\n const h = data[3 * numAnchors + a] as number;\n\n let x1 = cx - w / 2;\n let y1 = cy - h / 2;\n let x2 = cx + w / 2;\n let y2 = cy + h / 2;\n\n x1 = (x1 - padLeft) / scale;\n y1 = (y1 - padTop) / scale;\n x2 = (x2 - padLeft) / scale;\n y2 = (y2 - padTop) / scale;\n\n x1 = Math.max(0, Math.min(originalWidth, x1));\n y1 = Math.max(0, Math.min(originalHeight, y1));\n x2 = Math.max(0, Math.min(originalWidth, x2));\n y2 = Math.max(0, Math.min(originalHeight, y2));\n\n candidates.push({ anchorIdx: a, x1, y1, x2, y2, classId: bestCls, confidence: bestScore });\n }\n\n if (candidates.length === 0) return emptyDecoded();\n\n // Build flat arrays then delegate to batchedNms — same algorithm as before\n // but funnelled through the public per-class NMS helper.\n const flatBoxes = new Float32Array(candidates.length * 4);\n const scoresArr = new Float32Array(candidates.length);\n const idxsArr = new Int32Array(candidates.length);\n for (let i = 0; i < candidates.length; i++) {\n const c = candidates[i] as Candidate;\n flatBoxes[i * 4] = c.x1;\n flatBoxes[i * 4 + 1] = c.y1;\n flatBoxes[i * 4 + 2] = c.x2;\n flatBoxes[i * 4 + 3] = c.y2;\n scoresArr[i] = c.confidence;\n idxsArr[i] = c.classId;\n }\n const kept = batchedNms(flatBoxes, scoresArr, idxsArr, iouThreshold);\n if (kept.length === 0) return emptyDecoded();\n\n const limited = Array.from(kept).slice(0, maxDetections);\n const k = limited.length;\n const anchorIndices = new Int32Array(k);\n const boxesXyxy = new Float32Array(k * 4);\n const classIds = new Int32Array(k);\n const confidences = new Float32Array(k);\n for (let i = 0; i < k; i++) {\n const c = candidates[limited[i] as number] as Candidate;\n anchorIndices[i] = c.anchorIdx;\n boxesXyxy[i * 4] = c.x1;\n boxesXyxy[i * 4 + 1] = c.y1;\n boxesXyxy[i * 4 + 2] = c.x2;\n boxesXyxy[i * 4 + 3] = c.y2;\n classIds[i] = c.classId;\n confidences[i] = c.confidence;\n }\n return { anchorIndices, boxesXyxy, classIds, confidences };\n}\n\nfunction emptyDecoded(): DecodedAnchors {\n return {\n anchorIndices: new Int32Array(0),\n boxesXyxy: new Float32Array(0),\n classIds: new Int32Array(0),\n confidences: new Float32Array(0),\n };\n}\n\nexport interface DecodeYoloOptions {\n readonly originalWidth: number;\n readonly originalHeight: number;\n readonly padLeft: number;\n readonly padTop: number;\n readonly scale: number;\n readonly confThreshold: number;\n readonly iouThreshold: number;\n readonly maxDetections: number;\n}\n\nexport interface DecodedDetection {\n readonly bbox: BoundingBox;\n readonly classId: number;\n readonly confidence: number;\n}\n\n/**\n * Decode an anchor-free YOLO detection output into a list of detections.\n *\n * Works for **YOLOv8 / v9 / v10 / v11 / v12** detect heads.\n *\n * Expected raw shape: `[1, 4 + numClasses, N]`. `numClasses` is inferred\n * from the channel count.\n */\nexport function decodeYolo(\n output: Float32Array,\n outputDims: readonly number[],\n options: DecodeYoloOptions,\n): DecodedDetection[] {\n const channels = outputDims.length === 3 ? outputDims[1] : outputDims[0];\n if (channels === undefined || channels < 5) {\n throw new Error(`decodeYolo: invalid output channel count ${channels} (expected >= 5).`);\n }\n const numClasses = channels - 4;\n\n const decoded = decodeYoloAnchors(output, outputDims, {\n numClasses,\n ...options,\n });\n\n const results: DecodedDetection[] = [];\n for (let i = 0; i < decoded.classIds.length; i++) {\n results.push({\n bbox: new BoundingBox(\n decoded.boxesXyxy[i * 4] as number,\n decoded.boxesXyxy[i * 4 + 1] as number,\n decoded.boxesXyxy[i * 4 + 2] as number,\n decoded.boxesXyxy[i * 4 + 3] as number,\n ),\n classId: decoded.classIds[i] as number,\n confidence: decoded.confidences[i] as number,\n });\n }\n return results;\n}\n\nlet _warnedDecodeYoloV8 = false;\nlet _warnedDecodeYoloV8Anchors = false;\n\n/**\n * @deprecated since 0.2.0 — use {@link decodeYolo}. Same behavior; the\n * decoder covers v8/v9/v10/v11/v12 detect heads. Will be removed in 0.4.0.\n */\nexport function decodeYoloV8(\n output: Float32Array,\n outputDims: readonly number[],\n options: DecodeYoloOptions,\n): DecodedDetection[] {\n if (!_warnedDecodeYoloV8) {\n _warnedDecodeYoloV8 = true;\n console.warn(\n \"[@ort-vision-sdk/web] decodeYoloV8 is deprecated since 0.2.0; use decodeYolo. \" +\n \"The alias will be removed in 0.4.0.\",\n );\n }\n return decodeYolo(output, outputDims, options);\n}\n\n/**\n * @deprecated since 0.2.0 — use {@link decodeYoloAnchors}. Will be removed in 0.4.0.\n */\nexport function decodeYoloV8Anchors(\n data: Float32Array,\n dims: readonly number[],\n options: DecodeYoloAnchorsOptions,\n): DecodedAnchors {\n if (!_warnedDecodeYoloV8Anchors) {\n _warnedDecodeYoloV8Anchors = true;\n console.warn(\n \"[@ort-vision-sdk/web] decodeYoloV8Anchors is deprecated since 0.2.0; use decodeYoloAnchors. \" +\n \"The alias will be removed in 0.4.0.\",\n );\n }\n return decodeYoloAnchors(data, dims, options);\n}\n\n/** @deprecated since 0.2.0 — use {@link DecodeYoloAnchorsOptions}. */\nexport type DecodeYoloV8AnchorsOptions = DecodeYoloAnchorsOptions;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Content":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Detection head postprocessing: anchor-free YOLO decoding + non-maximum suppression.\n *\n * The shared {@link decodeYoloAnchors} helper does the per-anchor work that\n * is identical for plain detection and segmentation (transpose, xywh→xyxy,\n * letterbox unmap, per-class NMS, sort & cap). {@link decodeYolo} is a thin\n * wrapper around it; the segmentation module ({@link ./segmentation.js})\n * calls the helper directly so it can also recover the per-anchor mask\n * coefficients.\n *\n * Works for any YOLO export with the post-v8 anchor-free head:\n * **YOLOv8 / v9 / v10 / v11 / v12** detect heads, all of which share the\n * `[1, 4 + nc, N]` output layout.\n */\n\nimport { BoundingBox } from \"../types\";\n\n/**\n * Greedy non-maximum suppression on axis-aligned bounding boxes.\n *\n * Mirrors `torchvision.ops.nms` (keeps boxes with the highest score, drops\n * any subsequent box whose IoU exceeds the threshold).\n *\n * @param boxes Flat array of length `4 * N` in xyxy order: `[x1,y1,x2,y2, ...]`.\n * @param scores Detection score per box, length `N`.\n * @param iouThreshold Boxes with IoU above this threshold relative to a kept box are suppressed.\n * @returns Indices of kept boxes, in descending score order.\n */\nexport function nms(boxes: Float32Array, scores: Float32Array, iouThreshold: number): Int32Array {\n const n = scores.length;\n if (n === 0) return new Int32Array(0);\n\n const areas = new Float32Array(n);\n for (let i = 0; i < n; i++) {\n const x1 = boxes[i * 4] as number;\n const y1 = boxes[i * 4 + 1] as number;\n const x2 = boxes[i * 4 + 2] as number;\n const y2 = boxes[i * 4 + 3] as number;\n areas[i] = Math.max(0, x2 - x1) * Math.max(0, y2 - y1);\n }\n\n const order = new Array<number>(n);\n for (let i = 0; i < n; i++) order[i] = i;\n order.sort((a, b) => (scores[b] as number) - (scores[a] as number));\n\n const suppressed = new Uint8Array(n);\n const keep: number[] = [];\n\n for (let oi = 0; oi < order.length; oi++) {\n const i = order[oi] as number;\n if (suppressed[i]) continue;\n keep.push(i);\n\n const ax1 = boxes[i * 4] as number;\n const ay1 = boxes[i * 4 + 1] as number;\n const ax2 = boxes[i * 4 + 2] as number;\n const ay2 = boxes[i * 4 + 3] as number;\n const ai = areas[i] as number;\n\n for (let oj = oi + 1; oj < order.length; oj++) {\n const j = order[oj] as number;\n if (suppressed[j]) continue;\n\n const bx1 = boxes[j * 4] as number;\n const by1 = boxes[j * 4 + 1] as number;\n const bx2 = boxes[j * 4 + 2] as number;\n const by2 = boxes[j * 4 + 3] as number;\n\n const ix1 = Math.max(ax1, bx1);\n const iy1 = Math.max(ay1, by1);\n const ix2 = Math.min(ax2, bx2);\n const iy2 = Math.min(ay2, by2);\n const iw = Math.max(0, ix2 - ix1);\n const ih = Math.max(0, iy2 - iy1);\n const inter = iw * ih;\n const union = ai + (areas[j] as number) - inter;\n const iou = union > 0 ? inter / union : 0;\n if (iou > iouThreshold) suppressed[j] = 1;\n }\n }\n\n return Int32Array.from(keep);\n}\n\n/**\n * Per-class NMS — boxes are suppressed only by other boxes of the same class.\n *\n * Mirrors `torchvision.ops.batched_nms`.\n *\n * @param boxes Flat array of length `4 * N` in xyxy order.\n * @param scores Detection score per box, length `N`.\n * @param idxs Class index per box, length `N`. Boxes with different `idxs`\n * never suppress each other.\n * @param iouThreshold IoU threshold for suppression within a class.\n */\nexport function batchedNms(\n boxes: Float32Array,\n scores: Float32Array,\n idxs: Int32Array,\n iouThreshold: number,\n): Int32Array {\n if (scores.length === 0) return new Int32Array(0);\n\n const byClass = new Map<number, number[]>();\n for (let i = 0; i < idxs.length; i++) {\n const c = idxs[i] as number;\n const list = byClass.get(c);\n if (list === undefined) byClass.set(c, [i]);\n else list.push(i);\n }\n\n const keep: number[] = [];\n for (const indices of byClass.values()) {\n const m = indices.length;\n const subBoxes = new Float32Array(m * 4);\n const subScores = new Float32Array(m);\n for (let k = 0; k < m; k++) {\n const i = indices[k] as number;\n subBoxes[k * 4] = boxes[i * 4] as number;\n subBoxes[k * 4 + 1] = boxes[i * 4 + 1] as number;\n subBoxes[k * 4 + 2] = boxes[i * 4 + 2] as number;\n subBoxes[k * 4 + 3] = boxes[i * 4 + 3] as number;\n subScores[k] = scores[i] as number;\n }\n const subKeep = nms(subBoxes, subScores, iouThreshold);\n for (let k = 0; k < subKeep.length; k++) {\n keep.push(indices[subKeep[k] as number] as number);\n }\n }\n\n keep.sort((a, b) => (scores[b] as number) - (scores[a] as number));\n return Int32Array.from(keep);\n}\n\nexport interface DecodeYoloAnchorsOptions {\n /** Number of class-score channels following the 4 box channels. */\n readonly numClasses: number;\n readonly originalWidth: number;\n readonly originalHeight: number;\n readonly padLeft: number;\n readonly padTop: number;\n readonly scale: number;\n readonly confThreshold: number;\n readonly iouThreshold: number;\n readonly maxDetections: number;\n}\n\nexport interface DecodedAnchors {\n /** Indices into the original `numAnchors` axis, in descending confidence order. */\n readonly anchorIndices: Int32Array;\n /** `[k, 4]` boxes in original-image pixel coords, flat row-major xyxy. */\n readonly boxesXyxy: Float32Array;\n /** Predicted class id per survivor. */\n readonly classIds: Int32Array;\n /** Confidence per survivor. */\n readonly confidences: Float32Array;\n}\n\n/**\n * Shared YOLO per-anchor decode used by both detection and segmentation\n * (v8 / v9 / v10 / v11 / v12).\n *\n * Only the first `4 + numClasses` channels are read; later channels (e.g.\n * mask coefficients) are ignored — callers can fetch them via the returned\n * {@link DecodedAnchors.anchorIndices}.\n *\n * @param data Flat per-anchor output, length `channels * numAnchors`.\n * @param dims Dims as reported by ORT, e.g. `[1, 84, 8400]` (det) or\n * `[1, 116, 8400]` (seg). The leading batch dim must be 1.\n */\nexport function decodeYoloAnchors(\n data: Float32Array,\n dims: readonly number[],\n options: DecodeYoloAnchorsOptions,\n): DecodedAnchors {\n let normalized = dims;\n if (normalized.length === 3) {\n if (normalized[0] !== 1) {\n throw new Error(`decodeYoloAnchors: expected batch size 1, got ${normalized[0]}.`);\n }\n normalized = [normalized[1] as number, normalized[2] as number];\n }\n if (normalized.length !== 2) {\n throw new Error(\n `decodeYoloAnchors: expected 2-D output after batch removal, got dims=${JSON.stringify(dims)}.`,\n );\n }\n const channels = normalized[0] as number;\n const numAnchors = normalized[1] as number;\n\n const {\n numClasses,\n originalWidth,\n originalHeight,\n padLeft,\n padTop,\n scale,\n confThreshold,\n iouThreshold,\n maxDetections,\n } = options;\n\n if (numClasses < 1 || numClasses + 4 > channels) {\n throw new Error(\n `decodeYoloAnchors: invalid numClasses=${numClasses} for channels=${channels}.`,\n );\n }\n if (data.length !== channels * numAnchors) {\n throw new Error(\n `decodeYoloAnchors: data length ${data.length} does not match channels*numAnchors=${channels * numAnchors}.`,\n );\n }\n\n type Candidate = {\n anchorIdx: number;\n x1: number;\n y1: number;\n x2: number;\n y2: number;\n classId: number;\n confidence: number;\n };\n const candidates: Candidate[] = [];\n\n for (let a = 0; a < numAnchors; a++) {\n let bestCls = 0;\n let bestScore = -Infinity;\n for (let c = 0; c < numClasses; c++) {\n const s = data[(4 + c) * numAnchors + a];\n if (s !== undefined && s > bestScore) {\n bestScore = s;\n bestCls = c;\n }\n }\n if (bestScore < confThreshold) continue;\n\n const cx = data[a] as number;\n const cy = data[numAnchors + a] as number;\n const w = data[2 * numAnchors + a] as number;\n const h = data[3 * numAnchors + a] as number;\n\n let x1 = cx - w / 2;\n let y1 = cy - h / 2;\n let x2 = cx + w / 2;\n let y2 = cy + h / 2;\n\n x1 = (x1 - padLeft) / scale;\n y1 = (y1 - padTop) / scale;\n x2 = (x2 - padLeft) / scale;\n y2 = (y2 - padTop) / scale;\n\n x1 = Math.max(0, Math.min(originalWidth, x1));\n y1 = Math.max(0, Math.min(originalHeight, y1));\n x2 = Math.max(0, Math.min(originalWidth, x2));\n y2 = Math.max(0, Math.min(originalHeight, y2));\n\n candidates.push({ anchorIdx: a, x1, y1, x2, y2, classId: bestCls, confidence: bestScore });\n }\n\n if (candidates.length === 0) return emptyDecoded();\n\n // Build flat arrays then delegate to batchedNms — same algorithm as before\n // but funnelled through the public per-class NMS helper.\n const flatBoxes = new Float32Array(candidates.length * 4);\n const scoresArr = new Float32Array(candidates.length);\n const idxsArr = new Int32Array(candidates.length);\n for (let i = 0; i < candidates.length; i++) {\n const c = candidates[i] as Candidate;\n flatBoxes[i * 4] = c.x1;\n flatBoxes[i * 4 + 1] = c.y1;\n flatBoxes[i * 4 + 2] = c.x2;\n flatBoxes[i * 4 + 3] = c.y2;\n scoresArr[i] = c.confidence;\n idxsArr[i] = c.classId;\n }\n const kept = batchedNms(flatBoxes, scoresArr, idxsArr, iouThreshold);\n if (kept.length === 0) return emptyDecoded();\n\n const limited = Array.from(kept).slice(0, maxDetections);\n const k = limited.length;\n const anchorIndices = new Int32Array(k);\n const boxesXyxy = new Float32Array(k * 4);\n const classIds = new Int32Array(k);\n const confidences = new Float32Array(k);\n for (let i = 0; i < k; i++) {\n const c = candidates[limited[i] as number] as Candidate;\n anchorIndices[i] = c.anchorIdx;\n boxesXyxy[i * 4] = c.x1;\n boxesXyxy[i * 4 + 1] = c.y1;\n boxesXyxy[i * 4 + 2] = c.x2;\n boxesXyxy[i * 4 + 3] = c.y2;\n classIds[i] = c.classId;\n confidences[i] = c.confidence;\n }\n return { anchorIndices, boxesXyxy, classIds, confidences };\n}\n\nfunction emptyDecoded(): DecodedAnchors {\n return {\n anchorIndices: new Int32Array(0),\n boxesXyxy: new Float32Array(0),\n classIds: new Int32Array(0),\n confidences: new Float32Array(0),\n };\n}\n\nexport interface DecodeYoloOptions {\n readonly originalWidth: number;\n readonly originalHeight: number;\n readonly padLeft: number;\n readonly padTop: number;\n readonly scale: number;\n readonly confThreshold: number;\n readonly iouThreshold: number;\n readonly maxDetections: number;\n}\n\nexport interface DecodedDetection {\n readonly bbox: BoundingBox;\n readonly classId: number;\n readonly confidence: number;\n}\n\n/**\n * Decode an anchor-free YOLO detection output into a list of detections.\n *\n * Works for **YOLOv8 / v9 / v10 / v11 / v12** detect heads.\n *\n * Expected raw shape: `[1, 4 + numClasses, N]`. `numClasses` is inferred\n * from the channel count.\n */\nexport function decodeYolo(\n output: Float32Array,\n outputDims: readonly number[],\n options: DecodeYoloOptions,\n): DecodedDetection[] {\n const channels = outputDims.length === 3 ? outputDims[1] : outputDims[0];\n if (channels === undefined || channels < 5) {\n throw new Error(`decodeYolo: invalid output channel count ${channels} (expected >= 5).`);\n }\n const numClasses = channels - 4;\n\n const decoded = decodeYoloAnchors(output, outputDims, {\n numClasses,\n ...options,\n });\n\n const results: DecodedDetection[] = [];\n for (let i = 0; i < decoded.classIds.length; i++) {\n results.push({\n bbox: new BoundingBox(\n decoded.boxesXyxy[i * 4] as number,\n decoded.boxesXyxy[i * 4 + 1] as number,\n decoded.boxesXyxy[i * 4 + 2] as number,\n decoded.boxesXyxy[i * 4 + 3] as number,\n ),\n classId: decoded.classIds[i] as number,\n confidence: decoded.confidences[i] as number,\n });\n }\n return results;\n}\n\nlet _warnedDecodeYoloV8 = false;\nlet _warnedDecodeYoloV8Anchors = false;\n\n/**\n * @deprecated since 0.2.0 — use {@link decodeYolo}. Same behavior; the\n * decoder covers v8/v9/v10/v11/v12 detect heads. Will be removed in 0.4.0.\n */\nexport function decodeYoloV8(\n output: Float32Array,\n outputDims: readonly number[],\n options: DecodeYoloOptions,\n): DecodedDetection[] {\n if (!_warnedDecodeYoloV8) {\n _warnedDecodeYoloV8 = true;\n console.warn(\n \"[@ort-vision-sdk/web] decodeYoloV8 is deprecated since 0.2.0; use decodeYolo. \" +\n \"The alias will be removed in 0.4.0.\",\n );\n }\n return decodeYolo(output, outputDims, options);\n}\n\n/**\n * @deprecated since 0.2.0 — use {@link decodeYoloAnchors}. Will be removed in 0.4.0.\n */\nexport function decodeYoloV8Anchors(\n data: Float32Array,\n dims: readonly number[],\n options: DecodeYoloAnchorsOptions,\n): DecodedAnchors {\n if (!_warnedDecodeYoloV8Anchors) {\n _warnedDecodeYoloV8Anchors = true;\n console.warn(\n \"[@ort-vision-sdk/web] decodeYoloV8Anchors is deprecated since 0.2.0; use decodeYoloAnchors. \" +\n \"The alias will be removed in 0.4.0.\",\n );\n }\n return decodeYoloAnchors(data, dims, options);\n}\n\n/** @deprecated since 0.2.0 — use {@link DecodeYoloAnchorsOptions}. */\nexport type DecodeYoloV8AnchorsOptions = DecodeYoloAnchorsOptions;\n\n/** @deprecated since 0.2.0 — use {@link DecodeYoloOptions}. */\nexport type DecodeYoloV8Options = DecodeYoloOptions;\n"],"mappings":"gCA6BA,SAAgB,EAAI,EAAqB,EAAsB,EAAkC,CAC7F,IAAM,EAAI,EAAO,OACjB,GAAI,IAAM,EAAG,OAAO,IAAI,WAExB,IAAM,EAAQ,IAAI,aAAa,CAAC,EAChC,IAAK,IAAI,EAAI,EAAG,EAAI,EAAG,IAAK,CACxB,IAAM,EAAK,EAAM,EAAI,GACf,EAAK,EAAM,EAAI,EAAI,GACnB,EAAK,EAAM,EAAI,EAAI,GACnB,EAAK,EAAM,EAAI,EAAI,GACzB,EAAM,GAAK,KAAK,IAAI,EAAG,EAAK,CAAE,EAAI,KAAK,IAAI,EAAG,EAAK,CAAE,CACzD,CAEA,IAAM,EAAY,MAAc,CAAC,EACjC,IAAK,IAAI,EAAI,EAAG,EAAI,EAAG,IAAK,EAAM,GAAK,EACvC,EAAM,MAAM,EAAG,IAAO,EAAO,GAAiB,EAAO,EAAa,EAElE,IAAM,EAAa,IAAI,WAAW,CAAC,EAC7B,EAAiB,CAAC,EAExB,IAAK,IAAI,EAAK,EAAG,EAAK,EAAM,OAAQ,IAAM,CACtC,IAAM,EAAI,EAAM,GAChB,GAAI,EAAW,GAAI,SACnB,EAAK,KAAK,CAAC,EAEX,IAAM,EAAM,EAAM,EAAI,GAChB,EAAM,EAAM,EAAI,EAAI,GACpB,EAAM,EAAM,EAAI,EAAI,GACpB,EAAM,EAAM,EAAI,EAAI,GACpB,EAAK,EAAM,GAEjB,IAAK,IAAI,EAAK,EAAK,EAAG,EAAK,EAAM,OAAQ,IAAM,CAC3C,IAAM,EAAI,EAAM,GAChB,GAAI,EAAW,GAAI,SAEnB,IAAM,EAAM,EAAM,EAAI,GAChB,EAAM,EAAM,EAAI,EAAI,GAC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AAM,EAAM,CAAO,CAChD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"detection.js","names":[],"sources":["../../../src/vision/postprocess/detection.ts"],"sourcesContent":["/**\n * Detection head postprocessing: anchor-free YOLO decoding + non-maximum suppression.\n *\n * The shared {@link decodeYoloAnchors} helper does the per-anchor work that\n * is identical for plain detection and segmentation (transpose, xywh→xyxy,\n * letterbox unmap, per-class NMS, sort & cap). {@link decodeYolo} is a thin\n * wrapper around it; the segmentation module ({@link ./segmentation.js})\n * calls the helper directly so it can also recover the per-anchor mask\n * coefficients.\n *\n * Works for any YOLO export with the post-v8 anchor-free head:\n * **YOLOv8 / v9 / v10 / v11 / v12** detect heads, all of which share the\n * `[1, 4 + nc, N]` output layout.\n */\n\nimport { BoundingBox } from \"../types\";\n\n/**\n * Greedy non-maximum suppression on axis-aligned bounding boxes.\n *\n * Mirrors `torchvision.ops.nms` (keeps boxes with the highest score, drops\n * any subsequent box whose IoU exceeds the threshold).\n *\n * @param boxes Flat array of length `4 * N` in xyxy order: `[x1,y1,x2,y2, ...]`.\n * @param scores Detection score per box, length `N`.\n * @param iouThreshold Boxes with IoU above this threshold relative to a kept box are suppressed.\n * @returns Indices of kept boxes, in descending score order.\n */\nexport function nms(boxes: Float32Array, scores: Float32Array, iouThreshold: number): Int32Array {\n const n = scores.length;\n if (n === 0) return new Int32Array(0);\n\n const areas = new Float32Array(n);\n for (let i = 0; i < n; i++) {\n const x1 = boxes[i * 4] as number;\n const y1 = boxes[i * 4 + 1] as number;\n const x2 = boxes[i * 4 + 2] as number;\n const y2 = boxes[i * 4 + 3] as number;\n areas[i] = Math.max(0, x2 - x1) * Math.max(0, y2 - y1);\n }\n\n const order = new Array<number>(n);\n for (let i = 0; i < n; i++) order[i] = i;\n order.sort((a, b) => (scores[b] as number) - (scores[a] as number));\n\n const suppressed = new Uint8Array(n);\n const keep: number[] = [];\n\n for (let oi = 0; oi < order.length; oi++) {\n const i = order[oi] as number;\n if (suppressed[i]) continue;\n keep.push(i);\n\n const ax1 = boxes[i * 4] as number;\n const ay1 = boxes[i * 4 + 1] as number;\n const ax2 = boxes[i * 4 + 2] as number;\n const ay2 = boxes[i * 4 + 3] as number;\n const ai = areas[i] as number;\n\n for (let oj = oi + 1; oj < order.length; oj++) {\n const j = order[oj] as number;\n if (suppressed[j]) continue;\n\n const bx1 = boxes[j * 4] as number;\n const by1 = boxes[j * 4 + 1] as number;\n const bx2 = boxes[j * 4 + 2] as number;\n const by2 = boxes[j * 4 + 3] as number;\n\n const ix1 = Math.max(ax1, bx1);\n const iy1 = Math.max(ay1, by1);\n const ix2 = Math.min(ax2, bx2);\n const iy2 = Math.min(ay2, by2);\n const iw = Math.max(0, ix2 - ix1);\n const ih = Math.max(0, iy2 - iy1);\n const inter = iw * ih;\n const union = ai + (areas[j] as number) - inter;\n const iou = union > 0 ? inter / union : 0;\n if (iou > iouThreshold) suppressed[j] = 1;\n }\n }\n\n return Int32Array.from(keep);\n}\n\n/**\n * Per-class NMS — boxes are suppressed only by other boxes of the same class.\n *\n * Mirrors `torchvision.ops.batched_nms`.\n *\n * @param boxes Flat array of length `4 * N` in xyxy order.\n * @param scores Detection score per box, length `N`.\n * @param idxs Class index per box, length `N`. Boxes with different `idxs`\n * never suppress each other.\n * @param iouThreshold IoU threshold for suppression within a class.\n */\nexport function batchedNms(\n boxes: Float32Array,\n scores: Float32Array,\n idxs: Int32Array,\n iouThreshold: number,\n): Int32Array {\n if (scores.length === 0) return new Int32Array(0);\n\n const byClass = new Map<number, number[]>();\n for (let i = 0; i < idxs.length; i++) {\n const c = idxs[i] as number;\n const list = byClass.get(c);\n if (list === undefined) byClass.set(c, [i]);\n else list.push(i);\n }\n\n const keep: number[] = [];\n for (const indices of byClass.values()) {\n const m = indices.length;\n const subBoxes = new Float32Array(m * 4);\n const subScores = new Float32Array(m);\n for (let k = 0; k < m; k++) {\n const i = indices[k] as number;\n subBoxes[k * 4] = boxes[i * 4] as number;\n subBoxes[k * 4 + 1] = boxes[i * 4 + 1] as number;\n subBoxes[k * 4 + 2] = boxes[i * 4 + 2] as number;\n subBoxes[k * 4 + 3] = boxes[i * 4 + 3] as number;\n subScores[k] = scores[i] as number;\n }\n const subKeep = nms(subBoxes, subScores, iouThreshold);\n for (let k = 0; k < subKeep.length; k++) {\n keep.push(indices[subKeep[k] as number] as number);\n }\n }\n\n keep.sort((a, b) => (scores[b] as number) - (scores[a] as number));\n return Int32Array.from(keep);\n}\n\nexport interface DecodeYoloAnchorsOptions {\n /** Number of class-score channels following the 4 box channels. */\n readonly numClasses: number;\n readonly originalWidth: number;\n readonly originalHeight: number;\n readonly padLeft: number;\n readonly padTop: number;\n readonly scale: number;\n readonly confThreshold: number;\n readonly iouThreshold: number;\n readonly maxDetections: number;\n}\n\nexport interface DecodedAnchors {\n /** Indices into the original `numAnchors` axis, in descending confidence order. */\n readonly anchorIndices: Int32Array;\n /** `[k, 4]` boxes in original-image pixel coords, flat row-major xyxy. */\n readonly boxesXyxy: Float32Array;\n /** Predicted class id per survivor. */\n readonly classIds: Int32Array;\n /** Confidence per survivor. */\n readonly confidences: Float32Array;\n}\n\n/**\n * Shared YOLO per-anchor decode used by both detection and segmentation\n * (v8 / v9 / v10 / v11 / v12).\n *\n * Only the first `4 + numClasses` channels are read; later channels (e.g.\n * mask coefficients) are ignored — callers can fetch them via the returned\n * {@link DecodedAnchors.anchorIndices}.\n *\n * @param data Flat per-anchor output, length `channels * numAnchors`.\n * @param dims Dims as reported by ORT, e.g. `[1, 84, 8400]` (det) or\n * `[1, 116, 8400]` (seg). The leading batch dim must be 1.\n */\nexport function decodeYoloAnchors(\n data: Float32Array,\n dims: readonly number[],\n options: DecodeYoloAnchorsOptions,\n): DecodedAnchors {\n let normalized = dims;\n if (normalized.length === 3) {\n if (normalized[0] !== 1) {\n throw new Error(`decodeYoloAnchors: expected batch size 1, got ${normalized[0]}.`);\n }\n normalized = [normalized[1] as number, normalized[2] as number];\n }\n if (normalized.length !== 2) {\n throw new Error(\n `decodeYoloAnchors: expected 2-D output after batch removal, got dims=${JSON.stringify(dims)}.`,\n );\n }\n const channels = normalized[0] as number;\n const numAnchors = normalized[1] as number;\n\n const {\n numClasses,\n originalWidth,\n originalHeight,\n padLeft,\n padTop,\n scale,\n confThreshold,\n iouThreshold,\n maxDetections,\n } = options;\n\n if (numClasses < 1 || numClasses + 4 > channels) {\n throw new Error(\n `decodeYoloAnchors: invalid numClasses=${numClasses} for channels=${channels}.`,\n );\n }\n if (data.length !== channels * numAnchors) {\n throw new Error(\n `decodeYoloAnchors: data length ${data.length} does not match channels*numAnchors=${channels * numAnchors}.`,\n );\n }\n\n type Candidate = {\n anchorIdx: number;\n x1: number;\n y1: number;\n x2: number;\n y2: number;\n classId: number;\n confidence: number;\n };\n const candidates: Candidate[] = [];\n\n for (let a = 0; a < numAnchors; a++) {\n let bestCls = 0;\n let bestScore = -Infinity;\n for (let c = 0; c < numClasses; c++) {\n const s = data[(4 + c) * numAnchors + a];\n if (s !== undefined && s > bestScore) {\n bestScore = s;\n bestCls = c;\n }\n }\n if (bestScore < confThreshold) continue;\n\n const cx = data[a] as number;\n const cy = data[numAnchors + a] as number;\n const w = data[2 * numAnchors + a] as number;\n const h = data[3 * numAnchors + a] as number;\n\n let x1 = cx - w / 2;\n let y1 = cy - h / 2;\n let x2 = cx + w / 2;\n let y2 = cy + h / 2;\n\n x1 = (x1 - padLeft) / scale;\n y1 = (y1 - padTop) / scale;\n x2 = (x2 - padLeft) / scale;\n y2 = (y2 - padTop) / scale;\n\n x1 = Math.max(0, Math.min(originalWidth, x1));\n y1 = Math.max(0, Math.min(originalHeight, y1));\n x2 = Math.max(0, Math.min(originalWidth, x2));\n y2 = Math.max(0, Math.min(originalHeight, y2));\n\n candidates.push({ anchorIdx: a, x1, y1, x2, y2, classId: bestCls, confidence: bestScore });\n }\n\n if (candidates.length === 0) return emptyDecoded();\n\n // Build flat arrays then delegate to batchedNms — same algorithm as before\n // but funnelled through the public per-class NMS helper.\n const flatBoxes = new Float32Array(candidates.length * 4);\n const scoresArr = new Float32Array(candidates.length);\n const idxsArr = new Int32Array(candidates.length);\n for (let i = 0; i < candidates.length; i++) {\n const c = candidates[i] as Candidate;\n flatBoxes[i * 4] = c.x1;\n flatBoxes[i * 4 + 1] = c.y1;\n flatBoxes[i * 4 + 2] = c.x2;\n flatBoxes[i * 4 + 3] = c.y2;\n scoresArr[i] = c.confidence;\n idxsArr[i] = c.classId;\n }\n const kept = batchedNms(flatBoxes, scoresArr, idxsArr, iouThreshold);\n if (kept.length === 0) return emptyDecoded();\n\n const limited = Array.from(kept).slice(0, maxDetections);\n const k = limited.length;\n const anchorIndices = new Int32Array(k);\n const boxesXyxy = new Float32Array(k * 4);\n const classIds = new Int32Array(k);\n const confidences = new Float32Array(k);\n for (let i = 0; i < k; i++) {\n const c = candidates[limited[i] as number] as Candidate;\n anchorIndices[i] = c.anchorIdx;\n boxesXyxy[i * 4] = c.x1;\n boxesXyxy[i * 4 + 1] = c.y1;\n boxesXyxy[i * 4 + 2] = c.x2;\n boxesXyxy[i * 4 + 3] = c.y2;\n classIds[i] = c.classId;\n confidences[i] = c.confidence;\n }\n return { anchorIndices, boxesXyxy, classIds, confidences };\n}\n\nfunction emptyDecoded(): DecodedAnchors {\n return {\n anchorIndices: new Int32Array(0),\n boxesXyxy: new Float32Array(0),\n classIds: new Int32Array(0),\n confidences: new Float32Array(0),\n };\n}\n\nexport interface DecodeYoloOptions {\n readonly originalWidth: number;\n readonly originalHeight: number;\n readonly padLeft: number;\n readonly padTop: number;\n readonly scale: number;\n readonly confThreshold: number;\n readonly iouThreshold: number;\n readonly maxDetections: number;\n}\n\nexport interface DecodedDetection {\n readonly bbox: BoundingBox;\n readonly classId: number;\n readonly confidence: number;\n}\n\n/**\n * Decode an anchor-free YOLO detection output into a list of detections.\n *\n * Works for **YOLOv8 / v9 / v10 / v11 / v12** detect heads.\n *\n * Expected raw shape: `[1, 4 + numClasses, N]`. `numClasses` is inferred\n * from the channel count.\n */\nexport function decodeYolo(\n output: Float32Array,\n outputDims: readonly number[],\n options: DecodeYoloOptions,\n): DecodedDetection[] {\n const channels = outputDims.length === 3 ? outputDims[1] : outputDims[0];\n if (channels === undefined || channels < 5) {\n throw new Error(`decodeYolo: invalid output channel count ${channels} (expected >= 5).`);\n }\n const numClasses = channels - 4;\n\n const decoded = decodeYoloAnchors(output, outputDims, {\n numClasses,\n ...options,\n });\n\n const results: DecodedDetection[] = [];\n for (let i = 0; i < decoded.classIds.length; i++) {\n results.push({\n bbox: new BoundingBox(\n decoded.boxesXyxy[i * 4] as number,\n decoded.boxesXyxy[i * 4 + 1] as number,\n decoded.boxesXyxy[i * 4 + 2] as number,\n decoded.boxesXyxy[i * 4 + 3] as number,\n ),\n classId: decoded.classIds[i] as number,\n confidence: decoded.confidences[i] as number,\n });\n }\n return results;\n}\n\nlet _warnedDecodeYoloV8 = false;\nlet _warnedDecodeYoloV8Anchors = false;\n\n/**\n * @deprecated since 0.2.0 — use {@link decodeYolo}. Same behavior; the\n * decoder covers v8/v9/v10/v11/v12 detect heads. Will be removed in 0.4.0.\n */\nexport function decodeYoloV8(\n output: Float32Array,\n outputDims: readonly number[],\n options: DecodeYoloOptions,\n): DecodedDetection[] {\n if (!_warnedDecodeYoloV8) {\n _warnedDecodeYoloV8 = true;\n console.warn(\n \"[@ort-vision-sdk/web] decodeYoloV8 is deprecated since 0.2.0; use decodeYolo. \" +\n \"The alias will be removed in 0.4.0.\",\n );\n }\n return decodeYolo(output, outputDims, options);\n}\n\n/**\n * @deprecated since 0.2.0 — use {@link decodeYoloAnchors}. Will be removed in 0.4.0.\n */\nexport function decodeYoloV8Anchors(\n data: Float32Array,\n dims: readonly number[],\n options: DecodeYoloAnchorsOptions,\n): DecodedAnchors {\n if (!_warnedDecodeYoloV8Anchors) {\n _warnedDecodeYoloV8Anchors = true;\n console.warn(\n \"[@ort-vision-sdk/web] decodeYoloV8Anchors is deprecated since 0.2.0; use decodeYoloAnchors. \" +\n \"The alias will be removed in 0.4.0.\",\n );\n }\n return decodeYoloAnchors(data, dims, options);\n}\n\n/** @deprecated since 0.2.0 — use {@link DecodeYoloAnchorsOptions}. */\nexport type DecodeYoloV8AnchorsOptions = DecodeYoloAnchorsOptions;\n\n/** @deprecated since 0.2.0 — use {@link DecodeYoloOptions}. */\nexport type DecodeYoloV8Options = DecodeYoloOptions;\n"],"mappings":";;AA4BA,SAAgB,EAAI,GAAqB,GAAsB,GAAkC;CAC7F,IAAM,IAAI,EAAO;CACjB,IAAI,MAAM,GAAG,uBAAO,IAAI,WAAY;CAEpC,IAAM,IAAQ,IAAI,aAAa,CAAC;CAChC,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KAAK;EACxB,IAAM,IAAK,EAAM,IAAI,IACf,IAAK,EAAM,IAAI,IAAI,IACnB,IAAK,EAAM,IAAI,IAAI,IACnB,IAAK,EAAM,IAAI,IAAI;EACzB,EAAM,KAAK,KAAK,IAAI,GAAG,IAAK,CAAE,IAAI,KAAK,IAAI,GAAG,IAAK,CAAE;CACzD;CAEA,IAAM,IAAY,MAAc,CAAC;CACjC,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KAAK,EAAM,KAAK;CACvC,EAAM,MAAM,GAAG,MAAO,EAAO,KAAiB,EAAO,EAAa;CAElE,IAAM,IAAa,IAAI,WAAW,CAAC,GAC7B,IAAiB,CAAC;CAExB,KAAK,IAAI,IAAK,GAAG,IAAK,EAAM,QAAQ,KAAM;EACtC,IAAM,IAAI,EAAM;EAChB,IAAI,EAAW,IAAI;EACnB,EAAK,KAAK,CAAC;EAEX,IAAM,IAAM,EAAM,IAAI,IAChB,IAAM,EAAM,IAAI,IAAI,IACpB,IAAM,EAAM,IAAI,IAAI,IACpB,IAAM,EAAM,IAAI,IAAI,IACpB,IAAK,EAAM;EAEjB,KAAK,IAAI,IAAK,IAAK,GAAG,IAAK,EAAM,QAAQ,KAAM;GAC3C,IAAM,IAAI,EAAM;GAChB,IAAI,EAAW,IAAI;GAEnB,IAAM,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,IAEG,EAAW,GAAQ,GAAY,CAAO;AACjD;AAKA,SAAgB,EACZ,GACA,GACA,GACc;CAQd,OAPK,MACD,IAA6B,IAC7B,QAAQ,KACJ,iIAEJ,IAEG,EAAkB,GAAM,GAAM,CAAO;AAChD"}
|
|
1
|
+
{"version":3,"file":"detection.js","names":[],"sources":["../../../src/vision/postprocess/detection.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Detection head postprocessing: anchor-free YOLO decoding + non-maximum suppression.\n *\n * The shared {@link decodeYoloAnchors} helper does the per-anchor work that\n * is identical for plain detection and segmentation (transpose, xywh→xyxy,\n * letterbox unmap, per-class NMS, sort & cap). {@link decodeYolo} is a thin\n * wrapper around it; the segmentation module ({@link ./segmentation.js})\n * calls the helper directly so it can also recover the per-anchor mask\n * coefficients.\n *\n * Works for any YOLO export with the post-v8 anchor-free head:\n * **YOLOv8 / v9 / v10 / v11 / v12** detect heads, all of which share the\n * `[1, 4 + nc, N]` output layout.\n */\n\nimport { BoundingBox } from \"../types\";\n\n/**\n * Greedy non-maximum suppression on axis-aligned bounding boxes.\n *\n * Mirrors `torchvision.ops.nms` (keeps boxes with the highest score, drops\n * any subsequent box whose IoU exceeds the threshold).\n *\n * @param boxes Flat array of length `4 * N` in xyxy order: `[x1,y1,x2,y2, ...]`.\n * @param scores Detection score per box, length `N`.\n * @param iouThreshold Boxes with IoU above this threshold relative to a kept box are suppressed.\n * @returns Indices of kept boxes, in descending score order.\n */\nexport function nms(boxes: Float32Array, scores: Float32Array, iouThreshold: number): Int32Array {\n const n = scores.length;\n if (n === 0) return new Int32Array(0);\n\n const areas = new Float32Array(n);\n for (let i = 0; i < n; i++) {\n const x1 = boxes[i * 4] as number;\n const y1 = boxes[i * 4 + 1] as number;\n const x2 = boxes[i * 4 + 2] as number;\n const y2 = boxes[i * 4 + 3] as number;\n areas[i] = Math.max(0, x2 - x1) * Math.max(0, y2 - y1);\n }\n\n const order = new Array<number>(n);\n for (let i = 0; i < n; i++) order[i] = i;\n order.sort((a, b) => (scores[b] as number) - (scores[a] as number));\n\n const suppressed = new Uint8Array(n);\n const keep: number[] = [];\n\n for (let oi = 0; oi < order.length; oi++) {\n const i = order[oi] as number;\n if (suppressed[i]) continue;\n keep.push(i);\n\n const ax1 = boxes[i * 4] as number;\n const ay1 = boxes[i * 4 + 1] as number;\n const ax2 = boxes[i * 4 + 2] as number;\n const ay2 = boxes[i * 4 + 3] as number;\n const ai = areas[i] as number;\n\n for (let oj = oi + 1; oj < order.length; oj++) {\n const j = order[oj] as number;\n if (suppressed[j]) continue;\n\n const bx1 = boxes[j * 4] as number;\n const by1 = boxes[j * 4 + 1] as number;\n const bx2 = boxes[j * 4 + 2] as number;\n const by2 = boxes[j * 4 + 3] as number;\n\n const ix1 = Math.max(ax1, bx1);\n const iy1 = Math.max(ay1, by1);\n const ix2 = Math.min(ax2, bx2);\n const iy2 = Math.min(ay2, by2);\n const iw = Math.max(0, ix2 - ix1);\n const ih = Math.max(0, iy2 - iy1);\n const inter = iw * ih;\n const union = ai + (areas[j] as number) - inter;\n const iou = union > 0 ? inter / union : 0;\n if (iou > iouThreshold) suppressed[j] = 1;\n }\n }\n\n return Int32Array.from(keep);\n}\n\n/**\n * Per-class NMS — boxes are suppressed only by other boxes of the same class.\n *\n * Mirrors `torchvision.ops.batched_nms`.\n *\n * @param boxes Flat array of length `4 * N` in xyxy order.\n * @param scores Detection score per box, length `N`.\n * @param idxs Class index per box, length `N`. Boxes with different `idxs`\n * never suppress each other.\n * @param iouThreshold IoU threshold for suppression within a class.\n */\nexport function batchedNms(\n boxes: Float32Array,\n scores: Float32Array,\n idxs: Int32Array,\n iouThreshold: number,\n): Int32Array {\n if (scores.length === 0) return new Int32Array(0);\n\n const byClass = new Map<number, number[]>();\n for (let i = 0; i < idxs.length; i++) {\n const c = idxs[i] as number;\n const list = byClass.get(c);\n if (list === undefined) byClass.set(c, [i]);\n else list.push(i);\n }\n\n const keep: number[] = [];\n for (const indices of byClass.values()) {\n const m = indices.length;\n const subBoxes = new Float32Array(m * 4);\n const subScores = new Float32Array(m);\n for (let k = 0; k < m; k++) {\n const i = indices[k] as number;\n subBoxes[k * 4] = boxes[i * 4] as number;\n subBoxes[k * 4 + 1] = boxes[i * 4 + 1] as number;\n subBoxes[k * 4 + 2] = boxes[i * 4 + 2] as number;\n subBoxes[k * 4 + 3] = boxes[i * 4 + 3] as number;\n subScores[k] = scores[i] as number;\n }\n const subKeep = nms(subBoxes, subScores, iouThreshold);\n for (let k = 0; k < subKeep.length; k++) {\n keep.push(indices[subKeep[k] as number] as number);\n }\n }\n\n keep.sort((a, b) => (scores[b] as number) - (scores[a] as number));\n return Int32Array.from(keep);\n}\n\nexport interface DecodeYoloAnchorsOptions {\n /** Number of class-score channels following the 4 box channels. */\n readonly numClasses: number;\n readonly originalWidth: number;\n readonly originalHeight: number;\n readonly padLeft: number;\n readonly padTop: number;\n readonly scale: number;\n readonly confThreshold: number;\n readonly iouThreshold: number;\n readonly maxDetections: number;\n}\n\nexport interface DecodedAnchors {\n /** Indices into the original `numAnchors` axis, in descending confidence order. */\n readonly anchorIndices: Int32Array;\n /** `[k, 4]` boxes in original-image pixel coords, flat row-major xyxy. */\n readonly boxesXyxy: Float32Array;\n /** Predicted class id per survivor. */\n readonly classIds: Int32Array;\n /** Confidence per survivor. */\n readonly confidences: Float32Array;\n}\n\n/**\n * Shared YOLO per-anchor decode used by both detection and segmentation\n * (v8 / v9 / v10 / v11 / v12).\n *\n * Only the first `4 + numClasses` channels are read; later channels (e.g.\n * mask coefficients) are ignored — callers can fetch them via the returned\n * {@link DecodedAnchors.anchorIndices}.\n *\n * @param data Flat per-anchor output, length `channels * numAnchors`.\n * @param dims Dims as reported by ORT, e.g. `[1, 84, 8400]` (det) or\n * `[1, 116, 8400]` (seg). The leading batch dim must be 1.\n */\nexport function decodeYoloAnchors(\n data: Float32Array,\n dims: readonly number[],\n options: DecodeYoloAnchorsOptions,\n): DecodedAnchors {\n let normalized = dims;\n if (normalized.length === 3) {\n if (normalized[0] !== 1) {\n throw new Error(`decodeYoloAnchors: expected batch size 1, got ${normalized[0]}.`);\n }\n normalized = [normalized[1] as number, normalized[2] as number];\n }\n if (normalized.length !== 2) {\n throw new Error(\n `decodeYoloAnchors: expected 2-D output after batch removal, got dims=${JSON.stringify(dims)}.`,\n );\n }\n const channels = normalized[0] as number;\n const numAnchors = normalized[1] as number;\n\n const {\n numClasses,\n originalWidth,\n originalHeight,\n padLeft,\n padTop,\n scale,\n confThreshold,\n iouThreshold,\n maxDetections,\n } = options;\n\n if (numClasses < 1 || numClasses + 4 > channels) {\n throw new Error(\n `decodeYoloAnchors: invalid numClasses=${numClasses} for channels=${channels}.`,\n );\n }\n if (data.length !== channels * numAnchors) {\n throw new Error(\n `decodeYoloAnchors: data length ${data.length} does not match channels*numAnchors=${channels * numAnchors}.`,\n );\n }\n\n type Candidate = {\n anchorIdx: number;\n x1: number;\n y1: number;\n x2: number;\n y2: number;\n classId: number;\n confidence: number;\n };\n const candidates: Candidate[] = [];\n\n for (let a = 0; a < numAnchors; a++) {\n let bestCls = 0;\n let bestScore = -Infinity;\n for (let c = 0; c < numClasses; c++) {\n const s = data[(4 + c) * numAnchors + a];\n if (s !== undefined && s > bestScore) {\n bestScore = s;\n bestCls = c;\n }\n }\n if (bestScore < confThreshold) continue;\n\n const cx = data[a] as number;\n const cy = data[numAnchors + a] as number;\n const w = data[2 * numAnchors + a] as number;\n const h = data[3 * numAnchors + a] as number;\n\n let x1 = cx - w / 2;\n let y1 = cy - h / 2;\n let x2 = cx + w / 2;\n let y2 = cy + h / 2;\n\n x1 = (x1 - padLeft) / scale;\n y1 = (y1 - padTop) / scale;\n x2 = (x2 - padLeft) / scale;\n y2 = (y2 - padTop) / scale;\n\n x1 = Math.max(0, Math.min(originalWidth, x1));\n y1 = Math.max(0, Math.min(originalHeight, y1));\n x2 = Math.max(0, Math.min(originalWidth, x2));\n y2 = Math.max(0, Math.min(originalHeight, y2));\n\n candidates.push({ anchorIdx: a, x1, y1, x2, y2, classId: bestCls, confidence: bestScore });\n }\n\n if (candidates.length === 0) return emptyDecoded();\n\n // Build flat arrays then delegate to batchedNms — same algorithm as before\n // but funnelled through the public per-class NMS helper.\n const flatBoxes = new Float32Array(candidates.length * 4);\n const scoresArr = new Float32Array(candidates.length);\n const idxsArr = new Int32Array(candidates.length);\n for (let i = 0; i < candidates.length; i++) {\n const c = candidates[i] as Candidate;\n flatBoxes[i * 4] = c.x1;\n flatBoxes[i * 4 + 1] = c.y1;\n flatBoxes[i * 4 + 2] = c.x2;\n flatBoxes[i * 4 + 3] = c.y2;\n scoresArr[i] = c.confidence;\n idxsArr[i] = c.classId;\n }\n const kept = batchedNms(flatBoxes, scoresArr, idxsArr, iouThreshold);\n if (kept.length === 0) return emptyDecoded();\n\n const limited = Array.from(kept).slice(0, maxDetections);\n const k = limited.length;\n const anchorIndices = new Int32Array(k);\n const boxesXyxy = new Float32Array(k * 4);\n const classIds = new Int32Array(k);\n const confidences = new Float32Array(k);\n for (let i = 0; i < k; i++) {\n const c = candidates[limited[i] as number] as Candidate;\n anchorIndices[i] = c.anchorIdx;\n boxesXyxy[i * 4] = c.x1;\n boxesXyxy[i * 4 + 1] = c.y1;\n boxesXyxy[i * 4 + 2] = c.x2;\n boxesXyxy[i * 4 + 3] = c.y2;\n classIds[i] = c.classId;\n confidences[i] = c.confidence;\n }\n return { anchorIndices, boxesXyxy, classIds, confidences };\n}\n\nfunction emptyDecoded(): DecodedAnchors {\n return {\n anchorIndices: new Int32Array(0),\n boxesXyxy: new Float32Array(0),\n classIds: new Int32Array(0),\n confidences: new Float32Array(0),\n };\n}\n\nexport interface DecodeYoloOptions {\n readonly originalWidth: number;\n readonly originalHeight: number;\n readonly padLeft: number;\n readonly padTop: number;\n readonly scale: number;\n readonly confThreshold: number;\n readonly iouThreshold: number;\n readonly maxDetections: number;\n}\n\nexport interface DecodedDetection {\n readonly bbox: BoundingBox;\n readonly classId: number;\n readonly confidence: number;\n}\n\n/**\n * Decode an anchor-free YOLO detection output into a list of detections.\n *\n * Works for **YOLOv8 / v9 / v10 / v11 / v12** detect heads.\n *\n * Expected raw shape: `[1, 4 + numClasses, N]`. `numClasses` is inferred\n * from the channel count.\n */\nexport function decodeYolo(\n output: Float32Array,\n outputDims: readonly number[],\n options: DecodeYoloOptions,\n): DecodedDetection[] {\n const channels = outputDims.length === 3 ? outputDims[1] : outputDims[0];\n if (channels === undefined || channels < 5) {\n throw new Error(`decodeYolo: invalid output channel count ${channels} (expected >= 5).`);\n }\n const numClasses = channels - 4;\n\n const decoded = decodeYoloAnchors(output, outputDims, {\n numClasses,\n ...options,\n });\n\n const results: DecodedDetection[] = [];\n for (let i = 0; i < decoded.classIds.length; i++) {\n results.push({\n bbox: new BoundingBox(\n decoded.boxesXyxy[i * 4] as number,\n decoded.boxesXyxy[i * 4 + 1] as number,\n decoded.boxesXyxy[i * 4 + 2] as number,\n decoded.boxesXyxy[i * 4 + 3] as number,\n ),\n classId: decoded.classIds[i] as number,\n confidence: decoded.confidences[i] as number,\n });\n }\n return results;\n}\n\nlet _warnedDecodeYoloV8 = false;\nlet _warnedDecodeYoloV8Anchors = false;\n\n/**\n * @deprecated since 0.2.0 — use {@link decodeYolo}. Same behavior; the\n * decoder covers v8/v9/v10/v11/v12 detect heads. Will be removed in 0.4.0.\n */\nexport function decodeYoloV8(\n output: Float32Array,\n outputDims: readonly number[],\n options: DecodeYoloOptions,\n): DecodedDetection[] {\n if (!_warnedDecodeYoloV8) {\n _warnedDecodeYoloV8 = true;\n console.warn(\n \"[@ort-vision-sdk/web] decodeYoloV8 is deprecated since 0.2.0; use decodeYolo. \" +\n \"The alias will be removed in 0.4.0.\",\n );\n }\n return decodeYolo(output, outputDims, options);\n}\n\n/**\n * @deprecated since 0.2.0 — use {@link decodeYoloAnchors}. Will be removed in 0.4.0.\n */\nexport function decodeYoloV8Anchors(\n data: Float32Array,\n dims: readonly number[],\n options: DecodeYoloAnchorsOptions,\n): DecodedAnchors {\n if (!_warnedDecodeYoloV8Anchors) {\n _warnedDecodeYoloV8Anchors = true;\n console.warn(\n \"[@ort-vision-sdk/web] decodeYoloV8Anchors is deprecated since 0.2.0; use decodeYoloAnchors. \" +\n \"The alias will be removed in 0.4.0.\",\n );\n }\n return decodeYoloAnchors(data, dims, options);\n}\n\n/** @deprecated since 0.2.0 — use {@link DecodeYoloAnchorsOptions}. */\nexport type DecodeYoloV8AnchorsOptions = DecodeYoloAnchorsOptions;\n\n/** @deprecated since 0.2.0 — use {@link DecodeYoloOptions}. */\nexport type DecodeYoloV8Options = DecodeYoloOptions;\n"],"mappings":";;AA6BA,SAAgB,EAAI,GAAqB,GAAsB,GAAkC;CAC7F,IAAM,IAAI,EAAO;CACjB,IAAI,MAAM,GAAG,uBAAO,IAAI,WAAY;CAEpC,IAAM,IAAQ,IAAI,aAAa,CAAC;CAChC,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KAAK;EACxB,IAAM,IAAK,EAAM,IAAI,IACf,IAAK,EAAM,IAAI,IAAI,IACnB,IAAK,EAAM,IAAI,IAAI,IACnB,IAAK,EAAM,IAAI,IAAI;EACzB,EAAM,KAAK,KAAK,IAAI,GAAG,IAAK,CAAE,IAAI,KAAK,IAAI,GAAG,IAAK,CAAE;CACzD;CAEA,IAAM,IAAY,MAAc,CAAC;CACjC,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KAAK,EAAM,KAAK;CACvC,EAAM,MAAM,GAAG,MAAO,EAAO,KAAiB,EAAO,EAAa;CAElE,IAAM,IAAa,IAAI,WAAW,CAAC,GAC7B,IAAiB,CAAC;CAExB,KAAK,IAAI,IAAK,GAAG,IAAK,EAAM,QAAQ,KAAM;EACtC,IAAM,IAAI,EAAM;EAChB,IAAI,EAAW,IAAI;EACnB,EAAK,KAAK,CAAC;EAEX,IAAM,IAAM,EAAM,IAAI,IAChB,IAAM,EAAM,IAAI,IAAI,IACpB,IAA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AAS,EAAQ,SAAS;EAC1B,YAAY,EAAQ,YAAY;CACpC,CAAC;CAEL,OAAO;AACX;AAEA,IAAI,IAAsB,IACtB,IAA6B;AAMjC,SAAgB,EACZ,GACA,GACA,GACkB;CAQlB,OAPK,MACD,IAAsB,IACtB,QAAQ,KACJ,mHAEJ,IAEG,EAAW,GAAQ,GAAY,CAAO;AACjD;AAKA,SAAgB,EACZ,GACA,GACA,GACc;CAQd,OAPK,MACD,IAA6B,IAC7B,QAAQ,KACJ,iIAEJ,IAEG,EAAkB,GAAM,GAAM,CAAO;AAChD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"segmentation.cjs","names":[],"sources":["../../../src/vision/postprocess/segmentation.ts"],"sourcesContent":["/**\n * Segmentation head postprocessing: YOLO instance-segmentation decoding.\n *\n * Compatible with YOLOv8-seg / YOLOv11-seg exports (and any later seg head\n * sharing the layout). The model produces two output tensors:\n *\n * - `output0` of shape `(1, 4 + numClasses + numMaskCoefs, numAnchors)` — the\n * same per-anchor predictions as plain YOLO detection plus an extra\n * `numMaskCoefs` (typically 32) channels of mask coefficients.\n * - `output1` of shape `(1, numMaskCoefs, maskH, maskW)` — a set of \"prototype\"\n * masks shared across all anchors.\n *\n * The per-anchor decode (xywh→xyxy, undo letterbox, per-class NMS, sort & cap)\n * is delegated to {@link decodeYoloAnchors}; this module only handles the\n * mask-specific work: matmul against prototypes, sigmoid, bilinear resize and\n * thresholding.\n */\n\nimport { decodeYoloAnchors } from \"./detection\";\nimport { BoundingBox, Mask } from \"../types\";\n\nexport interface DecodeYoloSegOptions {\n readonly numClasses: number;\n /** Model input `[width, height]` (post-letterbox). */\n readonly inputWidth: number;\n readonly inputHeight: number;\n /** Original image `[width, height]`. */\n readonly originalWidth: number;\n readonly originalHeight: number;\n /** Letterbox horizontal padding in input-tensor pixels. */\n readonly padLeft: number;\n /** Letterbox vertical padding in input-tensor pixels. */\n readonly padTop: number;\n /** Letterbox scale factor. */\n readonly scale: number;\n readonly confThreshold: number;\n readonly iouThreshold: number;\n readonly maxDetections: number;\n /** Probability cutoff applied to the soft mask. Defaults to `0.5`. */\n readonly maskThreshold?: number;\n}\n\nexport interface DecodedSegmentation {\n readonly bbox: BoundingBox;\n readonly classId: number;\n readonly confidence: number;\n /** Binary mask cropped to `bbox`. Width/height match `bbox.asIntXyxy()` extents. */\n readonly mask: Mask;\n}\n\n/**\n * Decode YOLO segmentation raw outputs into a list of segmented instances.\n *\n * Compatible with YOLOv8-seg / YOLOv11-seg.\n *\n * @param perAnchorData Flat `output0`, length `(4 + numClasses + numMaskCoefs) * numAnchors`.\n * @param perAnchorDims Dims as reported by ORT, e.g. `[1, 116, 8400]`.\n * @param prototypeData Flat `output1`, length `numMaskCoefs * maskH * maskW`.\n * @param prototypeDims Dims as reported by ORT, e.g. `[1, 32, 160, 160]`.\n */\nexport function decodeYoloSeg(\n perAnchorData: Float32Array,\n perAnchorDims: readonly number[],\n prototypeData: Float32Array,\n prototypeDims: readonly number[],\n options: DecodeYoloSegOptions,\n): DecodedSegmentation[] {\n // Strip batch dim from prototypes; perAnchor batch is validated by the helper.\n let pDims = prototypeDims;\n if (pDims.length === 4) {\n if (pDims[0] !== 1) {\n throw new Error(`decodeYoloSeg: expected batch size 1 in prototypes, got ${pDims[0]}.`);\n }\n pDims = [pDims[1] as number, pDims[2] as number, pDims[3] as number];\n }\n if (pDims.length !== 3) {\n throw new Error(\n `decodeYoloSeg: expected 3-D prototypes after batch removal, got dims=${JSON.stringify(prototypeDims)}.`,\n );\n }\n const numMaskCoefs = pDims[0] as number;\n const maskH = pDims[1] as number;\n const maskW = pDims[2] as number;\n\n const channels = perAnchorDims.length === 3 ? perAnchorDims[1] : perAnchorDims[0];\n const numAnchors = perAnchorDims.length === 3 ? perAnchorDims[2] : perAnchorDims[1];\n if (channels === undefined || numAnchors === undefined) {\n throw new Error(\n `decodeYoloSeg: cannot read channels/numAnchors from dims=${JSON.stringify(perAnchorDims)}.`,\n );\n }\n\n const expectedChannels = 4 + options.numClasses + numMaskCoefs;\n if (channels !== expectedChannels) {\n throw new Error(\n `decodeYoloSeg: channels=${channels} does not match 4 + numClasses(${options.numClasses}) + numMaskCoefs(${numMaskCoefs}) = ${expectedChannels}.`,\n );\n }\n if (prototypeData.length !== numMaskCoefs * maskH * maskW) {\n throw new Error(\n `decodeYoloSeg: prototype length ${prototypeData.length} does not match dims=${JSON.stringify(prototypeDims)}.`,\n );\n }\n\n const decoded = decodeYoloAnchors(perAnchorData, perAnchorDims, {\n numClasses: options.numClasses,\n originalWidth: options.originalWidth,\n originalHeight: options.originalHeight,\n padLeft: options.padLeft,\n padTop: options.padTop,\n scale: options.scale,\n confThreshold: options.confThreshold,\n iouThreshold: options.iouThreshold,\n maxDetections: options.maxDetections,\n });\n\n if (decoded.anchorIndices.length === 0) return [];\n\n const maskThreshold = options.maskThreshold ?? 0.5;\n const scaleX = maskW / options.inputWidth;\n const scaleY = maskH / options.inputHeight;\n const protoPlane = maskH * maskW;\n const coefBase = (4 + options.numClasses) * numAnchors;\n\n const results: DecodedSegmentation[] = [];\n\n for (let i = 0; i < decoded.anchorIndices.length; i++) {\n const a = decoded.anchorIndices[i] as number;\n const x1 = decoded.boxesXyxy[i * 4] as number;\n const y1 = decoded.boxesXyxy[i * 4 + 1] as number;\n const x2 = decoded.boxesXyxy[i * 4 + 2] as number;\n const y2 = decoded.boxesXyxy[i * 4 + 3] as number;\n const bbox = new BoundingBox(x1, y1, x2, y2);\n const classId = decoded.classIds[i] as number;\n const confidence = decoded.confidences[i] as number;\n\n const bboxW = Math.max(0, Math.trunc(x2) - Math.trunc(x1));\n const bboxH = Math.max(0, Math.trunc(y2) - Math.trunc(y1));\n\n if (bboxW === 0 || bboxH === 0) {\n results.push({ bbox, classId, confidence, mask: new Mask(new Uint8Array(0), 0, 0) });\n continue;\n }\n\n // Bbox in input-tensor coords, then in low-res mask coords.\n const ibx1 = x1 * options.scale + options.padLeft;\n const iby1 = y1 * options.scale + options.padTop;\n const ibx2 = x2 * options.scale + options.padLeft;\n const iby2 = y2 * options.scale + options.padTop;\n\n const mbx1 = Math.max(0, Math.floor(ibx1 * scaleX));\n const mby1 = Math.max(0, Math.floor(iby1 * scaleY));\n const mbx2 = Math.min(maskW, Math.ceil(ibx2 * scaleX));\n const mby2 = Math.min(maskH, Math.ceil(iby2 * scaleY));\n\n if (mbx2 <= mbx1 || mby2 <= mby1) {\n results.push({\n bbox,\n classId,\n confidence,\n mask: new Mask(new Uint8Array(bboxW * bboxH), bboxW, bboxH),\n });\n continue;\n }\n\n // Compute soft mask only within the prototype region under this bbox.\n const cropW = mbx2 - mbx1;\n const cropH = mby2 - mby1;\n const softCrop = new Float32Array(cropW * cropH);\n for (let y = 0; y < cropH; y++) {\n const py = mby1 + y;\n for (let x = 0; x < cropW; x++) {\n const px = mbx1 + x;\n let sum = 0;\n for (let kk = 0; kk < numMaskCoefs; kk++) {\n const coef = perAnchorData[coefBase + kk * numAnchors + a];\n const proto = prototypeData[kk * protoPlane + py * maskW + px];\n sum += coef * proto;\n }\n softCrop[y * cropW + x] = sigmoid(sum);\n }\n }\n\n const resized = resizeBilinear(softCrop, cropW, cropH, bboxW, bboxH);\n const binary = new Uint8Array(bboxW * bboxH);\n for (let j = 0; j < binary.length; j++) {\n binary[j] = resized[j] >= maskThreshold ? 255 : 0;\n }\n\n results.push({ bbox, classId, confidence, mask: new Mask(binary, bboxW, bboxH) });\n }\n\n return results;\n}\n\nfunction sigmoid(x: number): number {\n if (x >= 0) {\n return 1 / (1 + Math.exp(-x));\n }\n const e = Math.exp(x);\n return e / (1 + e);\n}\n\nlet _warnedDecodeYoloV8Seg = false;\n\n/** @deprecated since 0.2.0 — use {@link decodeYoloSeg}. Will be removed in 0.4.0. */\nexport function decodeYoloV8Seg(\n perAnchorData: Float32Array,\n perAnchorDims: readonly number[],\n prototypeData: Float32Array,\n prototypeDims: readonly number[],\n options: DecodeYoloSegOptions,\n): DecodedSegmentation[] {\n if (!_warnedDecodeYoloV8Seg) {\n _warnedDecodeYoloV8Seg = true;\n console.warn(\n \"[@ort-vision-sdk/web] decodeYoloV8Seg is deprecated since 0.2.0; use decodeYoloSeg. \" +\n \"The alias will be removed in 0.4.0.\",\n );\n }\n return decodeYoloSeg(perAnchorData, perAnchorDims, prototypeData, prototypeDims, options);\n}\n\n/** @deprecated since 0.2.0 — use {@link DecodeYoloSegOptions}. */\nexport type DecodeYoloV8SegOptions = DecodeYoloSegOptions;\n\nfunction resizeBilinear(\n src: Float32Array,\n srcWidth: number,\n srcHeight: number,\n targetWidth: number,\n targetHeight: number,\n): Float32Array {\n const out = new Float32Array(targetWidth * targetHeight);\n if (targetWidth === 0 || targetHeight === 0 || srcWidth === 0 || srcHeight === 0) {\n return out;\n }\n if (targetWidth === srcWidth && targetHeight === srcHeight) {\n out.set(src);\n return out;\n }\n const sx = srcWidth / targetWidth;\n const sy = srcHeight / targetHeight;\n for (let y = 0; y < targetHeight; y++) {\n const yy = (y + 0.5) * sy - 0.5;\n const y0 = Math.max(0, Math.floor(yy));\n const y1 = Math.min(srcHeight - 1, y0 + 1);\n const wy = Math.max(0, Math.min(1, yy - y0));\n for (let x = 0; x < targetWidth; x++) {\n const xx = (x + 0.5) * sx - 0.5;\n const x0 = Math.max(0, Math.floor(xx));\n const x1 = Math.min(srcWidth - 1, x0 + 1);\n const wx = Math.max(0, Math.min(1, xx - x0));\n const v00 = src[y0 * srcWidth + x0];\n const v01 = src[y0 * srcWidth + x1];\n const v10 = src[y1 * srcWidth + x0];\n const v11 = src[y1 * srcWidth + x1];\n const top = v00 * (1 - wx) + v01 * wx;\n const bot = v10 * (1 - wx) + v11 * wx;\n out[y * targetWidth + x] = top * (1 - wy) + bot * wy;\n }\n }\n return out;\n}\n"],"mappings":"6DA4DA,SAAgB,EACZ,EACA,EACA,EACA,EACA,EACqB,CAErB,IAAI,EAAQ,EACZ,GAAI,EAAM,SAAW,EAAG,CACpB,GAAI,EAAM,KAAO,EACb,MAAU,MAAM,2DAA2D,EAAM,GA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AAK,EAAY,EACvB,IAAK,IAAI,EAAI,EAAG,EAAI,EAAc,IAAK,CACnC,IAAM,GAAM,EAAI,IAAO,EAAK,GACtB,EAAK,KAAK,IAAI,EAAG,KAAK,MAAM,CAAE,CAAC,EAC/B,EAAK,KAAK,IAAI,EAAY,EAAG,EAAK,CAAC,EACnC,EAAK,KAAK,IAAI,EAAG,KAAK,IAAI,EAAG,EAAK,CAAE,CAAC,EAC3C,IAAK,IAAI,EAAI,EAAG,EAAI,EAAa,IAAK,CAClC,IAAM,GAAM,EAAI,IAAO,EAAK,GACtB,EAAK,KAAK,IAAI,EAAG,KAAK,MAAM,CAAE,CAAC,EAC/B,EAAK,KAAK,IAAI,EAAW,EAAG,EAAK,CAAC,EAClC,EAAK,KAAK,IAAI,EAAG,KAAK,IAAI,EAAG,EAAK,CAAE,CAAC,EACrC,EAAM,EAAI,EAAK,EAAW,GAC1B,EAAM,EAAI,EAAK,EAAW,GAC1B,EAAM,EAAI,EAAK,EAAW,GAC1B,EAAM,EAAI,EAAK,EAAW,GAC1B,EAAM,GAAO,EAAI,GAAM,EAAM,EAC7B,EAAM,GAAO,EAAI,GAAM,EAAM,EACnC,EAAI,EAAI,EAAc,GAAK,GAAO,EAAI,GAAM,EAAM,CACtD,CACJ,CACA,OAAO,CACX"}
|
|
1
|
+
{"version":3,"file":"segmentation.cjs","names":[],"sources":["../../../src/vision/postprocess/segmentation.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Segmentation head postprocessing: YOLO instance-segmentation decoding.\n *\n * Compatible with YOLOv8-seg / YOLOv11-seg exports (and any later seg head\n * sharing the layout). The model produces two output tensors:\n *\n * - `output0` of shape `(1, 4 + numClasses + numMaskCoefs, numAnchors)` — the\n * same per-anchor predictions as plain YOLO detection plus an extra\n * `numMaskCoefs` (typically 32) channels of mask coefficients.\n * - `output1` of shape `(1, numMaskCoefs, maskH, maskW)` — a set of \"prototype\"\n * masks shared across all anchors.\n *\n * The per-anchor decode (xywh→xyxy, undo letterbox, per-class NMS, sort & cap)\n * is delegated to {@link decodeYoloAnchors}; this module only handles the\n * mask-specific work: matmul against prototypes, sigmoid, bilinear resize and\n * thresholding.\n */\n\nimport { decodeYoloAnchors } from \"./detection\";\nimport { BoundingBox, Mask } from \"../types\";\n\nexport interface DecodeYoloSegOptions {\n readonly numClasses: number;\n /** Model input `[width, height]` (post-letterbox). */\n readonly inputWidth: number;\n readonly inputHeight: number;\n /** Original image `[width, height]`. */\n readonly originalWidth: number;\n readonly originalHeight: number;\n /** Letterbox horizontal padding in input-tensor pixels. */\n readonly padLeft: number;\n /** Letterbox vertical padding in input-tensor pixels. */\n readonly padTop: number;\n /** Letterbox scale factor. */\n readonly scale: number;\n readonly confThreshold: number;\n readonly iouThreshold: number;\n readonly maxDetections: number;\n /** Probability cutoff applied to the soft mask. Defaults to `0.5`. */\n readonly maskThreshold?: number;\n}\n\nexport interface DecodedSegmentation {\n readonly bbox: BoundingBox;\n readonly classId: number;\n readonly confidence: number;\n /** Binary mask cropped to `bbox`. Width/height match `bbox.asIntXyxy()` extents. */\n readonly mask: Mask;\n}\n\n/**\n * Decode YOLO segmentation raw outputs into a list of segmented instances.\n *\n * Compatible with YOLOv8-seg / YOLOv11-seg.\n *\n * @param perAnchorData Flat `output0`, length `(4 + numClasses + numMaskCoefs) * numAnchors`.\n * @param perAnchorDims Dims as reported by ORT, e.g. `[1, 116, 8400]`.\n * @param prototypeData Flat `output1`, length `numMaskCoefs * maskH * maskW`.\n * @param prototypeDims Dims as reported by ORT, e.g. `[1, 32, 160, 160]`.\n */\nexport function decodeYoloSeg(\n perAnchorData: Float32Array,\n perAnchorDims: readonly number[],\n prototypeData: Float32Array,\n prototypeDims: readonly number[],\n options: DecodeYoloSegOptions,\n): DecodedSegmentation[] {\n // Strip batch dim from prototypes; perAnchor batch is validated by the helper.\n let pDims = prototypeDims;\n if (pDims.length === 4) {\n if (pDims[0] !== 1) {\n throw new Error(`decodeYoloSeg: expected batch size 1 in prototypes, got ${pDims[0]}.`);\n }\n pDims = [pDims[1] as number, pDims[2] as number, pDims[3] as number];\n }\n if (pDims.length !== 3) {\n throw new Error(\n `decodeYoloSeg: expected 3-D prototypes after batch removal, got dims=${JSON.stringify(prototypeDims)}.`,\n );\n }\n const numMaskCoefs = pDims[0] as number;\n const maskH = pDims[1] as number;\n const maskW = pDims[2] as number;\n\n const channels = perAnchorDims.length === 3 ? perAnchorDims[1] : perAnchorDims[0];\n const numAnchors = perAnchorDims.length === 3 ? perAnchorDims[2] : perAnchorDims[1];\n if (channels === undefined || numAnchors === undefined) {\n throw new Error(\n `decodeYoloSeg: cannot read channels/numAnchors from dims=${JSON.stringify(perAnchorDims)}.`,\n );\n }\n\n const expectedChannels = 4 + options.numClasses + numMaskCoefs;\n if (channels !== expectedChannels) {\n throw new Error(\n `decodeYoloSeg: channels=${channels} does not match 4 + numClasses(${options.numClasses}) + numMaskCoefs(${numMaskCoefs}) = ${expectedChannels}.`,\n );\n }\n if (prototypeData.length !== numMaskCoefs * maskH * maskW) {\n throw new Error(\n `decodeYoloSeg: prototype length ${prototypeData.length} does not match dims=${JSON.stringify(prototypeDims)}.`,\n );\n }\n\n const decoded = decodeYoloAnchors(perAnchorData, perAnchorDims, {\n numClasses: options.numClasses,\n originalWidth: options.originalWidth,\n originalHeight: options.originalHeight,\n padLeft: options.padLeft,\n padTop: options.padTop,\n scale: options.scale,\n confThreshold: options.confThreshold,\n iouThreshold: options.iouThreshold,\n maxDetections: options.maxDetections,\n });\n\n if (decoded.anchorIndices.length === 0) return [];\n\n const maskThreshold = options.maskThreshold ?? 0.5;\n const scaleX = maskW / options.inputWidth;\n const scaleY = maskH / options.inputHeight;\n const protoPlane = maskH * maskW;\n const coefBase = (4 + options.numClasses) * numAnchors;\n\n const results: DecodedSegmentation[] = [];\n\n for (let i = 0; i < decoded.anchorIndices.length; i++) {\n const a = decoded.anchorIndices[i] as number;\n const x1 = decoded.boxesXyxy[i * 4] as number;\n const y1 = decoded.boxesXyxy[i * 4 + 1] as number;\n const x2 = decoded.boxesXyxy[i * 4 + 2] as number;\n const y2 = decoded.boxesXyxy[i * 4 + 3] as number;\n const bbox = new BoundingBox(x1, y1, x2, y2);\n const classId = decoded.classIds[i] as number;\n const confidence = decoded.confidences[i] as number;\n\n const bboxW = Math.max(0, Math.trunc(x2) - Math.trunc(x1));\n const bboxH = Math.max(0, Math.trunc(y2) - Math.trunc(y1));\n\n if (bboxW === 0 || bboxH === 0) {\n results.push({ bbox, classId, confidence, mask: new Mask(new Uint8Array(0), 0, 0) });\n continue;\n }\n\n // Bbox in input-tensor coords, then in low-res mask coords.\n const ibx1 = x1 * options.scale + options.padLeft;\n const iby1 = y1 * options.scale + options.padTop;\n const ibx2 = x2 * options.scale + options.padLeft;\n const iby2 = y2 * options.scale + options.padTop;\n\n const mbx1 = Math.max(0, Math.floor(ibx1 * scaleX));\n const mby1 = Math.max(0, Math.floor(iby1 * scaleY));\n const mbx2 = Math.min(maskW, Math.ceil(ibx2 * scaleX));\n const mby2 = Math.min(maskH, Math.ceil(iby2 * scaleY));\n\n if (mbx2 <= mbx1 || mby2 <= mby1) {\n results.push({\n bbox,\n classId,\n confidence,\n mask: new Mask(new Uint8Array(bboxW * bboxH), bboxW, bboxH),\n });\n continue;\n }\n\n // Compute soft mask only within the prototype region under this bbox.\n const cropW = mbx2 - mbx1;\n const cropH = mby2 - mby1;\n const softCrop = new Float32Array(cropW * cropH);\n for (let y = 0; y < cropH; y++) {\n const py = mby1 + y;\n for (let x = 0; x < cropW; x++) {\n const px = mbx1 + x;\n let sum = 0;\n for (let kk = 0; kk < numMaskCoefs; kk++) {\n const coef = perAnchorData[coefBase + kk * numAnchors + a];\n const proto = prototypeData[kk * protoPlane + py * maskW + px];\n sum += coef * proto;\n }\n softCrop[y * cropW + x] = sigmoid(sum);\n }\n }\n\n const resized = resizeBilinear(softCrop, cropW, cropH, bboxW, bboxH);\n const binary = new Uint8Array(bboxW * bboxH);\n for (let j = 0; j < binary.length; j++) {\n binary[j] = resized[j] >= maskThreshold ? 255 : 0;\n }\n\n results.push({ bbox, classId, confidence, mask: new Mask(binary, bboxW, bboxH) });\n }\n\n return results;\n}\n\nfunction sigmoid(x: number): number {\n if (x >= 0) {\n return 1 / (1 + Math.exp(-x));\n }\n const e = Math.exp(x);\n return e / (1 + e);\n}\n\nlet _warnedDecodeYoloV8Seg = false;\n\n/** @deprecated since 0.2.0 — use {@link decodeYoloSeg}. Will be removed in 0.4.0. */\nexport function decodeYoloV8Seg(\n perAnchorData: Float32Array,\n perAnchorDims: readonly number[],\n prototypeData: Float32Array,\n prototypeDims: readonly number[],\n options: DecodeYoloSegOptions,\n): DecodedSegmentation[] {\n if (!_warnedDecodeYoloV8Seg) {\n _warnedDecodeYoloV8Seg = true;\n console.warn(\n \"[@ort-vision-sdk/web] decodeYoloV8Seg is deprecated since 0.2.0; use decodeYoloSeg. \" +\n \"The alias will be removed in 0.4.0.\",\n );\n }\n return decodeYoloSeg(perAnchorData, perAnchorDims, prototypeData, prototypeDims, options);\n}\n\n/** @deprecated since 0.2.0 — use {@link DecodeYoloSegOptions}. */\nexport type DecodeYoloV8SegOptions = DecodeYoloSegOptions;\n\nfunction resizeBilinear(\n src: Float32Array,\n srcWidth: number,\n srcHeight: number,\n targetWidth: number,\n targetHeight: number,\n): Float32Array {\n const out = new Float32Array(targetWidth * targetHeight);\n if (targetWidth === 0 || targetHeight === 0 || srcWidth === 0 || srcHeight === 0) {\n return out;\n }\n if (targetWidth === srcWidth && targetHeight === srcHeight) {\n out.set(src);\n return out;\n }\n const sx = srcWidth / targetWidth;\n const sy = srcHeight / targetHeight;\n for (let y = 0; y < targetHeight; y++) {\n const yy = (y + 0.5) * sy - 0.5;\n const y0 = Math.max(0, Math.floor(yy));\n const y1 = Math.min(srcHeight - 1, y0 + 1);\n const wy = Math.max(0, Math.min(1, yy - y0));\n for (let x = 0; x < targetWidth; x++) {\n const xx = (x + 0.5) * sx - 0.5;\n const x0 = Math.max(0, Math.floor(xx));\n const x1 = Math.min(srcWidth - 1, x0 + 1);\n const wx = Math.max(0, Math.min(1, xx - x0));\n const v00 = src[y0 * srcWidth + x0];\n const v01 = src[y0 * srcWidth + x1];\n const v10 = src[y1 * srcWidth + x0];\n const v11 = src[y1 * srcWidth + x1];\n const top = v00 * (1 - wx) + v01 * wx;\n const bot = v10 * (1 - wx) + v11 * wx;\n out[y * targetWidth + x] = top * (1 - wy) + bot * wy;\n }\n }\n return out;\n}\n"],"mappings":"6DA6DA,SAAgB,EACZ,EACA,EACA,EACA,EACA,EACqB,CAErB,IAAI,EAAQ,EACZ,GAAI,EAAM,SAAW,EAAG,CACpB,GAAI,EAAM,KAAO,EACb,MAAU,MAAM,2DAA2D,EAAM,GAAG,EAAE,EAE1F,EAAQ,CAAC,EAAM,GAAc,EAAM,GAAc,EAAM,EAAY,CACvE,CACA,GAAI,EAAM,SAAW,EACjB,MAAU,MACN,wEAAwE,KAAK,UAAU,CAAa,EAAE,EAC1G,EAEJ,IAAM,EAAe,EAAM,GACrB,EAAQ,EAAM,GACd,EAAQ,EAAM,GAEd,EAAW,EAAc,SAAW,EAAI,EAAc,GAAK,EAAc,GACzE,EAAa,EAAc,SAAW,EAAI,EAAc,GAAK,EAAc,GACjF,GAAI,IAAa,IAAA,IAAa,IAAe,IAAA,GACzC,MAAU,MACN,4DAA4D,KAAK,UAAU,CAAa,EAAE,EAC9F,EAGJ,IAAM,EAAmB,EAAI,EAAQ,WAAa,EAClD,GAAI,IAAa,EACb,MAAU,MACN,2BAA2B,EAAS,iCAAiC,EAAQ,WAAW,mBAAmB,EAAa,MAAM,EAAiB,EACnJ,EAEJ,GAAI,EAAc,SAAW,EAAe,EAAQ,EAChD,MAAU,MACN,mCAAmC,EAAc,OAAO,uBAAuB,KAAK,UAAU,CAAa,EAAE,EACjH,EAGJ,IAAM,EAAU,EAAA,kBAAkB,EAAe,EAAe,CAC5D,WAAY,EAAQ,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I,EAAc,GAAK,GAAO,EAAI,GAAM,EAAM,CACtD,CACJ,CACA,OAAO,CACX"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"segmentation.js","names":[],"sources":["../../../src/vision/postprocess/segmentation.ts"],"sourcesContent":["/**\n * Segmentation head postprocessing: YOLO instance-segmentation decoding.\n *\n * Compatible with YOLOv8-seg / YOLOv11-seg exports (and any later seg head\n * sharing the layout). The model produces two output tensors:\n *\n * - `output0` of shape `(1, 4 + numClasses + numMaskCoefs, numAnchors)` — the\n * same per-anchor predictions as plain YOLO detection plus an extra\n * `numMaskCoefs` (typically 32) channels of mask coefficients.\n * - `output1` of shape `(1, numMaskCoefs, maskH, maskW)` — a set of \"prototype\"\n * masks shared across all anchors.\n *\n * The per-anchor decode (xywh→xyxy, undo letterbox, per-class NMS, sort & cap)\n * is delegated to {@link decodeYoloAnchors}; this module only handles the\n * mask-specific work: matmul against prototypes, sigmoid, bilinear resize and\n * thresholding.\n */\n\nimport { decodeYoloAnchors } from \"./detection\";\nimport { BoundingBox, Mask } from \"../types\";\n\nexport interface DecodeYoloSegOptions {\n readonly numClasses: number;\n /** Model input `[width, height]` (post-letterbox). */\n readonly inputWidth: number;\n readonly inputHeight: number;\n /** Original image `[width, height]`. */\n readonly originalWidth: number;\n readonly originalHeight: number;\n /** Letterbox horizontal padding in input-tensor pixels. */\n readonly padLeft: number;\n /** Letterbox vertical padding in input-tensor pixels. */\n readonly padTop: number;\n /** Letterbox scale factor. */\n readonly scale: number;\n readonly confThreshold: number;\n readonly iouThreshold: number;\n readonly maxDetections: number;\n /** Probability cutoff applied to the soft mask. Defaults to `0.5`. */\n readonly maskThreshold?: number;\n}\n\nexport interface DecodedSegmentation {\n readonly bbox: BoundingBox;\n readonly classId: number;\n readonly confidence: number;\n /** Binary mask cropped to `bbox`. Width/height match `bbox.asIntXyxy()` extents. */\n readonly mask: Mask;\n}\n\n/**\n * Decode YOLO segmentation raw outputs into a list of segmented instances.\n *\n * Compatible with YOLOv8-seg / YOLOv11-seg.\n *\n * @param perAnchorData Flat `output0`, length `(4 + numClasses + numMaskCoefs) * numAnchors`.\n * @param perAnchorDims Dims as reported by ORT, e.g. `[1, 116, 8400]`.\n * @param prototypeData Flat `output1`, length `numMaskCoefs * maskH * maskW`.\n * @param prototypeDims Dims as reported by ORT, e.g. `[1, 32, 160, 160]`.\n */\nexport function decodeYoloSeg(\n perAnchorData: Float32Array,\n perAnchorDims: readonly number[],\n prototypeData: Float32Array,\n prototypeDims: readonly number[],\n options: DecodeYoloSegOptions,\n): DecodedSegmentation[] {\n // Strip batch dim from prototypes; perAnchor batch is validated by the helper.\n let pDims = prototypeDims;\n if (pDims.length === 4) {\n if (pDims[0] !== 1) {\n throw new Error(`decodeYoloSeg: expected batch size 1 in prototypes, got ${pDims[0]}.`);\n }\n pDims = [pDims[1] as number, pDims[2] as number, pDims[3] as number];\n }\n if (pDims.length !== 3) {\n throw new Error(\n `decodeYoloSeg: expected 3-D prototypes after batch removal, got dims=${JSON.stringify(prototypeDims)}.`,\n );\n }\n const numMaskCoefs = pDims[0] as number;\n const maskH = pDims[1] as number;\n const maskW = pDims[2] as number;\n\n const channels = perAnchorDims.length === 3 ? perAnchorDims[1] : perAnchorDims[0];\n const numAnchors = perAnchorDims.length === 3 ? perAnchorDims[2] : perAnchorDims[1];\n if (channels === undefined || numAnchors === undefined) {\n throw new Error(\n `decodeYoloSeg: cannot read channels/numAnchors from dims=${JSON.stringify(perAnchorDims)}.`,\n );\n }\n\n const expectedChannels = 4 + options.numClasses + numMaskCoefs;\n if (channels !== expectedChannels) {\n throw new Error(\n `decodeYoloSeg: channels=${channels} does not match 4 + numClasses(${options.numClasses}) + numMaskCoefs(${numMaskCoefs}) = ${expectedChannels}.`,\n );\n }\n if (prototypeData.length !== numMaskCoefs * maskH * maskW) {\n throw new Error(\n `decodeYoloSeg: prototype length ${prototypeData.length} does not match dims=${JSON.stringify(prototypeDims)}.`,\n );\n }\n\n const decoded = decodeYoloAnchors(perAnchorData, perAnchorDims, {\n numClasses: options.numClasses,\n originalWidth: options.originalWidth,\n originalHeight: options.originalHeight,\n padLeft: options.padLeft,\n padTop: options.padTop,\n scale: options.scale,\n confThreshold: options.confThreshold,\n iouThreshold: options.iouThreshold,\n maxDetections: options.maxDetections,\n });\n\n if (decoded.anchorIndices.length === 0) return [];\n\n const maskThreshold = options.maskThreshold ?? 0.5;\n const scaleX = maskW / options.inputWidth;\n const scaleY = maskH / options.inputHeight;\n const protoPlane = maskH * maskW;\n const coefBase = (4 + options.numClasses) * numAnchors;\n\n const results: DecodedSegmentation[] = [];\n\n for (let i = 0; i < decoded.anchorIndices.length; i++) {\n const a = decoded.anchorIndices[i] as number;\n const x1 = decoded.boxesXyxy[i * 4] as number;\n const y1 = decoded.boxesXyxy[i * 4 + 1] as number;\n const x2 = decoded.boxesXyxy[i * 4 + 2] as number;\n const y2 = decoded.boxesXyxy[i * 4 + 3] as number;\n const bbox = new BoundingBox(x1, y1, x2, y2);\n const classId = decoded.classIds[i] as number;\n const confidence = decoded.confidences[i] as number;\n\n const bboxW = Math.max(0, Math.trunc(x2) - Math.trunc(x1));\n const bboxH = Math.max(0, Math.trunc(y2) - Math.trunc(y1));\n\n if (bboxW === 0 || bboxH === 0) {\n results.push({ bbox, classId, confidence, mask: new Mask(new Uint8Array(0), 0, 0) });\n continue;\n }\n\n // Bbox in input-tensor coords, then in low-res mask coords.\n const ibx1 = x1 * options.scale + options.padLeft;\n const iby1 = y1 * options.scale + options.padTop;\n const ibx2 = x2 * options.scale + options.padLeft;\n const iby2 = y2 * options.scale + options.padTop;\n\n const mbx1 = Math.max(0, Math.floor(ibx1 * scaleX));\n const mby1 = Math.max(0, Math.floor(iby1 * scaleY));\n const mbx2 = Math.min(maskW, Math.ceil(ibx2 * scaleX));\n const mby2 = Math.min(maskH, Math.ceil(iby2 * scaleY));\n\n if (mbx2 <= mbx1 || mby2 <= mby1) {\n results.push({\n bbox,\n classId,\n confidence,\n mask: new Mask(new Uint8Array(bboxW * bboxH), bboxW, bboxH),\n });\n continue;\n }\n\n // Compute soft mask only within the prototype region under this bbox.\n const cropW = mbx2 - mbx1;\n const cropH = mby2 - mby1;\n const softCrop = new Float32Array(cropW * cropH);\n for (let y = 0; y < cropH; y++) {\n const py = mby1 + y;\n for (let x = 0; x < cropW; x++) {\n const px = mbx1 + x;\n let sum = 0;\n for (let kk = 0; kk < numMaskCoefs; kk++) {\n const coef = perAnchorData[coefBase + kk * numAnchors + a];\n const proto = prototypeData[kk * protoPlane + py * maskW + px];\n sum += coef * proto;\n }\n softCrop[y * cropW + x] = sigmoid(sum);\n }\n }\n\n const resized = resizeBilinear(softCrop, cropW, cropH, bboxW, bboxH);\n const binary = new Uint8Array(bboxW * bboxH);\n for (let j = 0; j < binary.length; j++) {\n binary[j] = resized[j] >= maskThreshold ? 255 : 0;\n }\n\n results.push({ bbox, classId, confidence, mask: new Mask(binary, bboxW, bboxH) });\n }\n\n return results;\n}\n\nfunction sigmoid(x: number): number {\n if (x >= 0) {\n return 1 / (1 + Math.exp(-x));\n }\n const e = Math.exp(x);\n return e / (1 + e);\n}\n\nlet _warnedDecodeYoloV8Seg = false;\n\n/** @deprecated since 0.2.0 — use {@link decodeYoloSeg}. Will be removed in 0.4.0. */\nexport function decodeYoloV8Seg(\n perAnchorData: Float32Array,\n perAnchorDims: readonly number[],\n prototypeData: Float32Array,\n prototypeDims: readonly number[],\n options: DecodeYoloSegOptions,\n): DecodedSegmentation[] {\n if (!_warnedDecodeYoloV8Seg) {\n _warnedDecodeYoloV8Seg = true;\n console.warn(\n \"[@ort-vision-sdk/web] decodeYoloV8Seg is deprecated since 0.2.0; use decodeYoloSeg. \" +\n \"The alias will be removed in 0.4.0.\",\n );\n }\n return decodeYoloSeg(perAnchorData, perAnchorDims, prototypeData, prototypeDims, options);\n}\n\n/** @deprecated since 0.2.0 — use {@link DecodeYoloSegOptions}. */\nexport type DecodeYoloV8SegOptions = DecodeYoloSegOptions;\n\nfunction resizeBilinear(\n src: Float32Array,\n srcWidth: number,\n srcHeight: number,\n targetWidth: number,\n targetHeight: number,\n): Float32Array {\n const out = new Float32Array(targetWidth * targetHeight);\n if (targetWidth === 0 || targetHeight === 0 || srcWidth === 0 || srcHeight === 0) {\n return out;\n }\n if (targetWidth === srcWidth && targetHeight === srcHeight) {\n out.set(src);\n return out;\n }\n const sx = srcWidth / targetWidth;\n const sy = srcHeight / targetHeight;\n for (let y = 0; y < targetHeight; y++) {\n const yy = (y + 0.5) * sy - 0.5;\n const y0 = Math.max(0, Math.floor(yy));\n const y1 = Math.min(srcHeight - 1, y0 + 1);\n const wy = Math.max(0, Math.min(1, yy - y0));\n for (let x = 0; x < targetWidth; x++) {\n const xx = (x + 0.5) * sx - 0.5;\n const x0 = Math.max(0, Math.floor(xx));\n const x1 = Math.min(srcWidth - 1, x0 + 1);\n const wx = Math.max(0, Math.min(1, xx - x0));\n const v00 = src[y0 * srcWidth + x0];\n const v01 = src[y0 * srcWidth + x1];\n const v10 = src[y1 * srcWidth + x0];\n const v11 = src[y1 * srcWidth + x1];\n const top = v00 * (1 - wx) + v01 * wx;\n const bot = v10 * (1 - wx) + v11 * wx;\n out[y * targetWidth + x] = top * (1 - wy) + bot * wy;\n }\n }\n return out;\n}\n"],"mappings":";;;AA4DA,SAAgB,EACZ,GACA,GACA,GACA,GACA,GACqB;CAErB,IAAI,IAAQ;CACZ,IAAI,EAAM,WAAW,GAAG;EACpB,IAAI,EAAM,OAAO,GACb,MAAU,MAAM,2DAA2D,EAAM,GAAG,EAAE;EAE1F,IAAQ;GAAC,EAAM;GAAc,EAAM;GAAc,EAAM;EAAY;CACvE;CACA,IAAI,EAAM,WAAW,GACjB,MAAU,MACN,wEAAwE,KAAK,UAAU,CAAa,EAAE,EAC1G;CAEJ,IAAM,IAAe,EAAM,IACrB,IAAQ,EAAM,IACd,IAAQ,EAAM,IAEd,IAAW,EAAc,WAAW,IAAI,EAAc,KAAK,EAAc,IACzE,IAAa,EAAc,WAAW,IAAI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;GAClC,IAAM,KAAM,IAAI,MAAO,IAAK,IACtB,IAAK,KAAK,IAAI,GAAG,KAAK,MAAM,CAAE,CAAC,GAC/B,IAAK,KAAK,IAAI,IAAW,GAAG,IAAK,CAAC,GAClC,IAAK,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,IAAK,CAAE,CAAC,GACrC,IAAM,EAAI,IAAK,IAAW,IAC1B,IAAM,EAAI,IAAK,IAAW,IAC1B,IAAM,EAAI,IAAK,IAAW,IAC1B,IAAM,EAAI,IAAK,IAAW,IAC1B,IAAM,KAAO,IAAI,KAAM,IAAM,GAC7B,IAAM,KAAO,IAAI,KAAM,IAAM;GACnC,EAAI,IAAI,IAAc,KAAK,KAAO,IAAI,KAAM,IAAM;EACtD;CACJ;CACA,OAAO;AACX"}
|
|
1
|
+
{"version":3,"file":"segmentation.js","names":[],"sources":["../../../src/vision/postprocess/segmentation.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Segmentation head postprocessing: YOLO instance-segmentation decoding.\n *\n * Compatible with YOLOv8-seg / YOLOv11-seg exports (and any later seg head\n * sharing the layout). The model produces two output tensors:\n *\n * - `output0` of shape `(1, 4 + numClasses + numMaskCoefs, numAnchors)` — the\n * same per-anchor predictions as plain YOLO detection plus an extra\n * `numMaskCoefs` (typically 32) channels of mask coefficients.\n * - `output1` of shape `(1, numMaskCoefs, maskH, maskW)` — a set of \"prototype\"\n * masks shared across all anchors.\n *\n * The per-anchor decode (xywh→xyxy, undo letterbox, per-class NMS, sort & cap)\n * is delegated to {@link decodeYoloAnchors}; this module only handles the\n * mask-specific work: matmul against prototypes, sigmoid, bilinear resize and\n * thresholding.\n */\n\nimport { decodeYoloAnchors } from \"./detection\";\nimport { BoundingBox, Mask } from \"../types\";\n\nexport interface DecodeYoloSegOptions {\n readonly numClasses: number;\n /** Model input `[width, height]` (post-letterbox). */\n readonly inputWidth: number;\n readonly inputHeight: number;\n /** Original image `[width, height]`. */\n readonly originalWidth: number;\n readonly originalHeight: number;\n /** Letterbox horizontal padding in input-tensor pixels. */\n readonly padLeft: number;\n /** Letterbox vertical padding in input-tensor pixels. */\n readonly padTop: number;\n /** Letterbox scale factor. */\n readonly scale: number;\n readonly confThreshold: number;\n readonly iouThreshold: number;\n readonly maxDetections: number;\n /** Probability cutoff applied to the soft mask. Defaults to `0.5`. */\n readonly maskThreshold?: number;\n}\n\nexport interface DecodedSegmentation {\n readonly bbox: BoundingBox;\n readonly classId: number;\n readonly confidence: number;\n /** Binary mask cropped to `bbox`. Width/height match `bbox.asIntXyxy()` extents. */\n readonly mask: Mask;\n}\n\n/**\n * Decode YOLO segmentation raw outputs into a list of segmented instances.\n *\n * Compatible with YOLOv8-seg / YOLOv11-seg.\n *\n * @param perAnchorData Flat `output0`, length `(4 + numClasses + numMaskCoefs) * numAnchors`.\n * @param perAnchorDims Dims as reported by ORT, e.g. `[1, 116, 8400]`.\n * @param prototypeData Flat `output1`, length `numMaskCoefs * maskH * maskW`.\n * @param prototypeDims Dims as reported by ORT, e.g. `[1, 32, 160, 160]`.\n */\nexport function decodeYoloSeg(\n perAnchorData: Float32Array,\n perAnchorDims: readonly number[],\n prototypeData: Float32Array,\n prototypeDims: readonly number[],\n options: DecodeYoloSegOptions,\n): DecodedSegmentation[] {\n // Strip batch dim from prototypes; perAnchor batch is validated by the helper.\n let pDims = prototypeDims;\n if (pDims.length === 4) {\n if (pDims[0] !== 1) {\n throw new Error(`decodeYoloSeg: expected batch size 1 in prototypes, got ${pDims[0]}.`);\n }\n pDims = [pDims[1] as number, pDims[2] as number, pDims[3] as number];\n }\n if (pDims.length !== 3) {\n throw new Error(\n `decodeYoloSeg: expected 3-D prototypes after batch removal, got dims=${JSON.stringify(prototypeDims)}.`,\n );\n }\n const numMaskCoefs = pDims[0] as number;\n const maskH = pDims[1] as number;\n const maskW = pDims[2] as number;\n\n const channels = perAnchorDims.length === 3 ? perAnchorDims[1] : perAnchorDims[0];\n const numAnchors = perAnchorDims.length === 3 ? perAnchorDims[2] : perAnchorDims[1];\n if (channels === undefined || numAnchors === undefined) {\n throw new Error(\n `decodeYoloSeg: cannot read channels/numAnchors from dims=${JSON.stringify(perAnchorDims)}.`,\n );\n }\n\n const expectedChannels = 4 + options.numClasses + numMaskCoefs;\n if (channels !== expectedChannels) {\n throw new Error(\n `decodeYoloSeg: channels=${channels} does not match 4 + numClasses(${options.numClasses}) + numMaskCoefs(${numMaskCoefs}) = ${expectedChannels}.`,\n );\n }\n if (prototypeData.length !== numMaskCoefs * maskH * maskW) {\n throw new Error(\n `decodeYoloSeg: prototype length ${prototypeData.length} does not match dims=${JSON.stringify(prototypeDims)}.`,\n );\n }\n\n const decoded = decodeYoloAnchors(perAnchorData, perAnchorDims, {\n numClasses: options.numClasses,\n originalWidth: options.originalWidth,\n originalHeight: options.originalHeight,\n padLeft: options.padLeft,\n padTop: options.padTop,\n scale: options.scale,\n confThreshold: options.confThreshold,\n iouThreshold: options.iouThreshold,\n maxDetections: options.maxDetections,\n });\n\n if (decoded.anchorIndices.length === 0) return [];\n\n const maskThreshold = options.maskThreshold ?? 0.5;\n const scaleX = maskW / options.inputWidth;\n const scaleY = maskH / options.inputHeight;\n const protoPlane = maskH * maskW;\n const coefBase = (4 + options.numClasses) * numAnchors;\n\n const results: DecodedSegmentation[] = [];\n\n for (let i = 0; i < decoded.anchorIndices.length; i++) {\n const a = decoded.anchorIndices[i] as number;\n const x1 = decoded.boxesXyxy[i * 4] as number;\n const y1 = decoded.boxesXyxy[i * 4 + 1] as number;\n const x2 = decoded.boxesXyxy[i * 4 + 2] as number;\n const y2 = decoded.boxesXyxy[i * 4 + 3] as number;\n const bbox = new BoundingBox(x1, y1, x2, y2);\n const classId = decoded.classIds[i] as number;\n const confidence = decoded.confidences[i] as number;\n\n const bboxW = Math.max(0, Math.trunc(x2) - Math.trunc(x1));\n const bboxH = Math.max(0, Math.trunc(y2) - Math.trunc(y1));\n\n if (bboxW === 0 || bboxH === 0) {\n results.push({ bbox, classId, confidence, mask: new Mask(new Uint8Array(0), 0, 0) });\n continue;\n }\n\n // Bbox in input-tensor coords, then in low-res mask coords.\n const ibx1 = x1 * options.scale + options.padLeft;\n const iby1 = y1 * options.scale + options.padTop;\n const ibx2 = x2 * options.scale + options.padLeft;\n const iby2 = y2 * options.scale + options.padTop;\n\n const mbx1 = Math.max(0, Math.floor(ibx1 * scaleX));\n const mby1 = Math.max(0, Math.floor(iby1 * scaleY));\n const mbx2 = Math.min(maskW, Math.ceil(ibx2 * scaleX));\n const mby2 = Math.min(maskH, Math.ceil(iby2 * scaleY));\n\n if (mbx2 <= mbx1 || mby2 <= mby1) {\n results.push({\n bbox,\n classId,\n confidence,\n mask: new Mask(new Uint8Array(bboxW * bboxH), bboxW, bboxH),\n });\n continue;\n }\n\n // Compute soft mask only within the prototype region under this bbox.\n const cropW = mbx2 - mbx1;\n const cropH = mby2 - mby1;\n const softCrop = new Float32Array(cropW * cropH);\n for (let y = 0; y < cropH; y++) {\n const py = mby1 + y;\n for (let x = 0; x < cropW; x++) {\n const px = mbx1 + x;\n let sum = 0;\n for (let kk = 0; kk < numMaskCoefs; kk++) {\n const coef = perAnchorData[coefBase + kk * numAnchors + a];\n const proto = prototypeData[kk * protoPlane + py * maskW + px];\n sum += coef * proto;\n }\n softCrop[y * cropW + x] = sigmoid(sum);\n }\n }\n\n const resized = resizeBilinear(softCrop, cropW, cropH, bboxW, bboxH);\n const binary = new Uint8Array(bboxW * bboxH);\n for (let j = 0; j < binary.length; j++) {\n binary[j] = resized[j] >= maskThreshold ? 255 : 0;\n }\n\n results.push({ bbox, classId, confidence, mask: new Mask(binary, bboxW, bboxH) });\n }\n\n return results;\n}\n\nfunction sigmoid(x: number): number {\n if (x >= 0) {\n return 1 / (1 + Math.exp(-x));\n }\n const e = Math.exp(x);\n return e / (1 + e);\n}\n\nlet _warnedDecodeYoloV8Seg = false;\n\n/** @deprecated since 0.2.0 — use {@link decodeYoloSeg}. Will be removed in 0.4.0. */\nexport function decodeYoloV8Seg(\n perAnchorData: Float32Array,\n perAnchorDims: readonly number[],\n prototypeData: Float32Array,\n prototypeDims: readonly number[],\n options: DecodeYoloSegOptions,\n): DecodedSegmentation[] {\n if (!_warnedDecodeYoloV8Seg) {\n _warnedDecodeYoloV8Seg = true;\n console.warn(\n \"[@ort-vision-sdk/web] decodeYoloV8Seg is deprecated since 0.2.0; use decodeYoloSeg. \" +\n \"The alias will be removed in 0.4.0.\",\n );\n }\n return decodeYoloSeg(perAnchorData, perAnchorDims, prototypeData, prototypeDims, options);\n}\n\n/** @deprecated since 0.2.0 — use {@link DecodeYoloSegOptions}. */\nexport type DecodeYoloV8SegOptions = DecodeYoloSegOptions;\n\nfunction resizeBilinear(\n src: Float32Array,\n srcWidth: number,\n srcHeight: number,\n targetWidth: number,\n targetHeight: number,\n): Float32Array {\n const out = new Float32Array(targetWidth * targetHeight);\n if (targetWidth === 0 || targetHeight === 0 || srcWidth === 0 || srcHeight === 0) {\n return out;\n }\n if (targetWidth === srcWidth && targetHeight === srcHeight) {\n out.set(src);\n return out;\n }\n const sx = srcWidth / targetWidth;\n const sy = srcHeight / targetHeight;\n for (let y = 0; y < targetHeight; y++) {\n const yy = (y + 0.5) * sy - 0.5;\n const y0 = Math.max(0, Math.floor(yy));\n const y1 = Math.min(srcHeight - 1, y0 + 1);\n const wy = Math.max(0, Math.min(1, yy - y0));\n for (let x = 0; x < targetWidth; x++) {\n const xx = (x + 0.5) * sx - 0.5;\n const x0 = Math.max(0, Math.floor(xx));\n const x1 = Math.min(srcWidth - 1, x0 + 1);\n const wx = Math.max(0, Math.min(1, xx - x0));\n const v00 = src[y0 * srcWidth + x0];\n const v01 = src[y0 * srcWidth + x1];\n const v10 = src[y1 * srcWidth + x0];\n const v11 = src[y1 * srcWidth + x1];\n const top = v00 * (1 - wx) + v01 * wx;\n const bot = v10 * (1 - wx) + v11 * wx;\n out[y * targetWidth + x] = top * (1 - wy) + bot * wy;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AAI,MAAgB,KAAK,MAAiB,KAAK,MAAa,KAAK,MAAc,GAC3E,OAAO;CAEX,IAAI,MAAgB,KAAY,MAAiB,GAE7C,OADA,EAAI,IAAI,CAAG,GACJ;CAEX,IAAM,IAAK,IAAW,GAChB,IAAK,IAAY;CACvB,KAAK,IAAI,IAAI,GAAG,IAAI,GAAc,KAAK;EACnC,IAAM,KAAM,IAAI,MAAO,IAAK,IACtB,IAAK,KAAK,IAAI,GAAG,KAAK,MAAM,CAAE,CAAC,GAC/B,IAAK,KAAK,IAAI,IAAY,GAAG,IAAK,CAAC,GACnC,IAAK,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,IAAK,CAAE,CAAC;EAC3C,KAAK,IAAI,IAAI,GAAG,IAAI,GAAa,KAAK;GAClC,IAAM,KAAM,IAAI,MAAO,IAAK,IACtB,IAAK,KAAK,IAAI,GAAG,KAAK,MAAM,CAAE,CAAC,GAC/B,IAAK,KAAK,IAAI,IAAW,GAAG,IAAK,CAAC,GAClC,IAAK,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,IAAK,CAAE,CAAC,GACrC,IAAM,EAAI,IAAK,IAAW,IAC1B,IAAM,EAAI,IAAK,IAAW,IAC1B,IAAM,EAAI,IAAK,IAAW,IAC1B,IAAM,EAAI,IAAK,IAAW,IAC1B,IAAM,KAAO,IAAI,KAAM,IAAM,GAC7B,IAAM,KAAO,IAAI,KAAM,IAAM;GACnC,EAAI,IAAI,IAAc,KAAK,KAAO,IAAI,KAAM,IAAM;EACtD;CACJ;CACA,OAAO;AACX"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"image.cjs","names":[],"sources":["../../../src/vision/preprocess/image.ts"],"sourcesContent":["/**\n * Composable image preprocessing primitives (resize, normalize, letterbox, layout).\n *\n * Mirrors the Python `preprocess.image` module. Operates on the canonical\n * {@link RGBImage} (HWC RGB uint8) and produces uint8 or float32 buffers\n * depending on the operation.\n */\n\nimport * as ortRuntime from \"onnxruntime-web\";\nimport type * as ort from \"onnxruntime-web\";\n\nimport { createCanvas, get2DContext, imageDataToRGB, rgbToImageData } from \"../core/canvas\";\nimport { RGBImage } from \"../types\";\n\n/** Resize an image to `(targetWidth, targetHeight)` using high-quality canvas resampling. */\nexport function resize(image: RGBImage, targetWidth: number, targetHeight: number): RGBImage {\n if (targetWidth <= 0 || targetHeight <= 0) {\n throw new Error(`Invalid resize target ${targetWidth}x${targetHeight}.`);\n }\n if (targetWidth === image.width && targetHeight === image.height) {\n return image;\n }\n\n const srcCanvas = createCanvas(image.width, image.height);\n const srcCtx = get2DContext(srcCanvas);\n srcCtx.putImageData(rgbToImageData(image), 0, 0);\n\n const dstCanvas = createCanvas(targetWidth, targetHeight);\n const dstCtx = get2DContext(dstCanvas);\n dstCtx.imageSmoothingEnabled = true;\n dstCtx.imageSmoothingQuality = \"high\";\n dstCtx.drawImage(srcCanvas as CanvasImageSource, 0, 0, targetWidth, targetHeight);\n\n const imageData = dstCtx.getImageData(0, 0, targetWidth, targetHeight);\n return imageDataToRGB(imageData);\n}\n\n/**\n * Convert a uint8 image to a normalized float32 array (HWC layout preserved).\n *\n * Applies `(pixel * scale - mean) / std` channel-wise.\n */\nexport function normalize(\n image: RGBImage,\n mean: readonly [number, number, number],\n std: readonly [number, number, number],\n scale: number = 1 / 255,\n): Float32Array {\n const out = new Float32Array(image.data.length);\n const data = image.data;\n const m0 = mean[0];\n const m1 = mean[1];\n const m2 = mean[2];\n const s0 = std[0];\n const s1 = std[1];\n const s2 = std[2];\n for (let i = 0; i < data.length; i += 3) {\n out[i] = ((data[i] as number) * scale - m0) / s0;\n out[i + 1] = ((data[i + 1] as number) * scale - m1) / s1;\n out[i + 2] = ((data[i + 2] as number) * scale - m2) / s2;\n }\n return out;\n}\n\n/** Convert a uint8 image to a `Float32Array` in `[0, 1]` (HWC layout preserved). */\nexport function toFloat32(image: RGBImage, scale: number = 1 / 255): Float32Array {\n const out = new Float32Array(image.data.length);\n const data = image.data;\n for (let i = 0; i < data.length; i++) {\n out[i] = (data[i] as number) * scale;\n }\n return out;\n}\n\n/**\n * Transpose interleaved HWC data to planar CHW layout.\n *\n * @param hwc Source array of length `width * height * channels`.\n */\nexport function toCHW(\n hwc: Float32Array,\n width: number,\n height: number,\n channels: number = 3,\n): Float32Array {\n const expected = width * height * channels;\n if (hwc.length !== expected) {\n throw new Error(\n `toCHW: expected length ${expected} for ${width}x${height}x${channels}, got ${hwc.length}.`,\n );\n }\n const chw = new Float32Array(expected);\n const plane = width * height;\n for (let y = 0; y < height; y++) {\n for (let x = 0; x < width; x++) {\n const hwcBase = (y * width + x) * channels;\n const planeIdx = y * width + x;\n for (let c = 0; c < channels; c++) {\n chw[c * plane + planeIdx] = hwc[hwcBase + c] as number;\n }\n }\n }\n return chw;\n}\n\n/** Wrap a Float32 buffer into an `ort.Tensor`. */\nexport function toFloat32Tensor(data: Float32Array, dims: readonly number[]): ort.Tensor {\n return new ortRuntime.Tensor(\"float32\", data, dims as number[]);\n}\n\n/**\n * Convert an HWC uint8 image to a CHW `Float32Array` scaled to `[0, 1]`.\n *\n * Mirrors `torchvision.transforms.ToTensor()` semantics: HWC → CHW,\n * `uint8 → float32 / 255`. Useful as input to YOLO-style detectors that\n * don't require ImageNet normalization.\n *\n * @returns CHW `Float32Array` of length `width * height * 3`.\n */\nexport function toTensor(image: RGBImage): Float32Array {\n const f32 = toFloat32(image);\n return toCHW(f32, image.width, image.height, 3);\n}\n\n/**\n * Convert an HWC BGR uint8 buffer (OpenCV layout) to the SDK's HWC RGB.\n *\n * Use when you receive image bytes from `cv2.imencode` over the wire and\n * want to feed them to the SDK without going through a canvas decode.\n *\n * @param bgr Flat BGR Uint8Array of length `width * height * 3`.\n */\nexport function fromCv2(bgr: Uint8Array, width: number, height: number): RGBImage {\n if (bgr.length !== width * height * 3) {\n throw new Error(\n `fromCv2: data length ${bgr.length} does not match width * height * 3 = ${width * height * 3}.`,\n );\n }\n const rgb = new Uint8Array(bgr.length);\n for (let i = 0; i < bgr.length; i += 3) {\n rgb[i] = bgr[i + 2] as number;\n rgb[i + 1] = bgr[i + 1] as number;\n rgb[i + 2] = bgr[i] as number;\n }\n return new RGBImage(rgb, width, height);\n}\n\n/**\n * Convert the SDK's HWC RGB image to an HWC BGR `Uint8Array` (OpenCV layout).\n *\n * Useful for round-tripping data to a Python OpenCV consumer.\n */\nexport function toCv2(image: RGBImage): Uint8Array {\n const rgb = image.data;\n const bgr = new Uint8Array(rgb.length);\n for (let i = 0; i < rgb.length; i += 3) {\n bgr[i] = rgb[i + 2] as number;\n bgr[i + 1] = rgb[i + 1] as number;\n bgr[i + 2] = rgb[i] as number;\n }\n return bgr;\n}\n\nexport interface LetterboxResult {\n /** The padded image at the target size. */\n readonly image: RGBImage;\n /** The factor applied to the original image (`< 1` if downscaled). */\n readonly scale: number;\n /** Horizontal padding in pixels (left side; right side has the same or +1). */\n readonly padLeft: number;\n /** Vertical padding in pixels (top side). */\n readonly padTop: number;\n}\n\n/**\n * Resize preserving aspect ratio, padding to `(targetWidth, targetHeight)`\n * with a constant fill color.\n *\n * Standard YOLO preprocessing — returning `scale` and `padLeft`/`padTop`\n * lets callers map detections back to the original image coordinates.\n */\nexport function letterbox(\n image: RGBImage,\n targetWidth: number,\n targetHeight: number,\n fill: readonly [number, number, number] = [114, 114, 114],\n): LetterboxResult {\n const scale = Math.min(targetWidth / image.width, targetHeight / image.height);\n const newW = Math.round(image.width * scale);\n const newH = Math.round(image.height * scale);\n const resized = resize(image, newW, newH);\n\n const out = new Uint8Array(targetWidth * targetHeight * 3);\n const f0 = fill[0];\n const f1 = fill[1];\n const f2 = fill[2];\n for (let i = 0; i < out.length; i += 3) {\n out[i] = f0;\n out[i + 1] = f1;\n out[i + 2] = f2;\n }\n\n const padLeft = Math.floor((targetWidth - newW) / 2);\n const padTop = Math.floor((targetHeight - newH) / 2);\n const rowBytes = newW * 3;\n for (let y = 0; y < newH; y++) {\n const srcOffset = y * rowBytes;\n const dstOffset = ((padTop + y) * targetWidth + padLeft) * 3;\n out.set(resized.data.subarray(srcOffset, srcOffset + rowBytes), dstOffset);\n }\n\n return {\n image: new RGBImage(out, targetWidth, targetHeight),\n scale,\n padLeft,\n padTop,\n };\n}\n"],"mappings":"oKAeA,SAAgB,EAAO,EAAiB,EAAqB,EAAgC,CACzF,GAAI,GAAe,GAAK,GAAgB,EACpC,MAAU,MAAM,yBAAyB,EAAY,GAAG,EAAa,EAAE,EAE3E,GAAI,IAAgB,EAAM,OAAS,IAAiB,EAAM,OACtD,OAAO,EAGX,IAAM,EAAY,EAAA,aAAa,EAAM,MAAO,EAAM,MAAM,EAExD,EADe,aAAa,CAC5B,CAAA,CAAO,aAAa,EAAA,eAAe,CAAK,EAAG,EAAG,CAAC,EAG/C,IAAM,EAAS,EAAA,aADG,EAAA,aAAa,EAAa,CAChB,CAAS,EAMrC,MALA,GAAO,sBAAwB,GAC/B,EAAO,sBAAwB,OAC/B,EAAO,UAAU,EAAgC,EAAG,EAAG,EAAa,CAAY,EAGzE,EAAA,eADW,EAAO,aAAa,EAAG,EAAG,EAAa,CACnC,CAAS,CACnC,CAOA,SAAgB,EACZ,EACA,EACA,EACA,EAAgB,EAAI,IACR,CACZ,IAAM,EAAM,IAAI,aAAa,EAAM,KAAK,MAAM,EACxC,EAAO,EAAM,KACb,EAAK,EAAK,GACV,EAAK,EAAK,GACV,EAAK,EAAK,GACV,EAAK,EAAI,GACT,EAAK,EAAI,GACT,EAAK,EAAI,GACf,IAAK,IAAI,EAAI,EAAG,EAAI,EAAK,OAAQ,GAAK,EAClC,EAAI,IAAO,EAAK,GAAgB,EAAQ,GAAM,EAC9C,EAAI,EAAI,IAAO,EAAK,EAAI,GAAgB,EAAQ,GAAM,EACtD,EAAI,EAAI,IAAO,EAAK,EAAI,GAAgB,EAAQ,GAAM,EAE1D,OAAO,CACX,CAGA,SAAgB,EAAU,EAAiB,EAAgB,EAAI,IAAmB,CAC9E,IAAM,EAAM,IAAI,aAAa,EAAM,KAAK,MAAM,EACxC,EAAO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EAAM,EAA6B,CAC/C,IAAM,EAAM,EAAM,KACZ,EAAM,IAAI,WAAW,EAAI,MAAM,EACrC,IAAK,IAAI,EAAI,EAAG,EAAI,EAAI,OAAQ,GAAK,EACjC,EAAI,GAAK,EAAI,EAAI,GACjB,EAAI,EAAI,GAAK,EAAI,EAAI,GACrB,EAAI,EAAI,GAAK,EAAI,GAErB,OAAO,CACX,CAoBA,SAAgB,EACZ,EACA,EACA,EACA,EAA0C,CAAC,IAAK,IAAK,GAAG,EACzC,CACf,IAAM,EAAQ,KAAK,IAAI,EAAc,EAAM,MAAO,EAAe,EAAM,MAAM,EACvE,EAAO,KAAK,MAAM,EAAM,MAAQ,CAAK,EACrC,EAAO,KAAK,MAAM,EAAM,OAAS,CAAK,EACtC,EAAU,EAAO,EAAO,EAAM,CAAI,EAElC,EAAM,IAAI,WAAW,EAAc,EAAe,CAAC,EACnD,EAAK,EAAK,GACV,EAAK,EAAK,GACV,EAAK,EAAK,GAChB,IAAK,IAAI,EAAI,EAAG,EAAI,EAAI,OAAQ,GAAK,EACjC,EAAI,GAAK,EACT,EAAI,EAAI,GAAK,EACb,EAAI,EAAI,GAAK,EAGjB,IAAM,EAAU,KAAK,OAAO,EAAc,GAAQ,CAAC,EAC7C,EAAS,KAAK,OAAO,EAAe,GAAQ,CAAC,EAC7C,EAAW,EAAO,EACxB,IAAK,IAAI,EAAI,EAAG,EAAI,EAAM,IAAK,CAC3B,IAAM,EAAY,EAAI,EAChB,IAAc,EAAS,GAAK,EAAc,GAAW,EAC3D,EAAI,IAAI,EAAQ,KAAK,SAAS,EAAW,EAAY,CAAQ,EAAG,CAAS,CAC7E,CAEA,MAAO,CACH,MAAO,IAAI,EAAA,SAAS,EAAK,EAAa,CAAY,EAClD,QACA,UACA,QACJ,CACJ"}
|
|
1
|
+
{"version":3,"file":"image.cjs","names":[],"sources":["../../../src/vision/preprocess/image.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Composable image preprocessing primitives (resize, normalize, letterbox, layout).\n *\n * Mirrors the Python `preprocess.image` module. Operates on the canonical\n * {@link RGBImage} (HWC RGB uint8) and produces uint8 or float32 buffers\n * depending on the operation.\n */\n\nimport * as ortRuntime from \"onnxruntime-web\";\nimport type * as ort from \"onnxruntime-web\";\n\nimport { createCanvas, get2DContext, imageDataToRGB, rgbToImageData } from \"../core/canvas\";\nimport { RGBImage } from \"../types\";\n\n/** Resize an image to `(targetWidth, targetHeight)` using high-quality canvas resampling. */\nexport function resize(image: RGBImage, targetWidth: number, targetHeight: number): RGBImage {\n if (targetWidth <= 0 || targetHeight <= 0) {\n throw new Error(`Invalid resize target ${targetWidth}x${targetHeight}.`);\n }\n if (targetWidth === image.width && targetHeight === image.height) {\n return image;\n }\n\n const srcCanvas = createCanvas(image.width, image.height);\n const srcCtx = get2DContext(srcCanvas);\n srcCtx.putImageData(rgbToImageData(image), 0, 0);\n\n const dstCanvas = createCanvas(targetWidth, targetHeight);\n const dstCtx = get2DContext(dstCanvas);\n dstCtx.imageSmoothingEnabled = true;\n dstCtx.imageSmoothingQuality = \"high\";\n dstCtx.drawImage(srcCanvas as CanvasImageSource, 0, 0, targetWidth, targetHeight);\n\n const imageData = dstCtx.getImageData(0, 0, targetWidth, targetHeight);\n return imageDataToRGB(imageData);\n}\n\n/**\n * Convert a uint8 image to a normalized float32 array (HWC layout preserved).\n *\n * Applies `(pixel * scale - mean) / std` channel-wise.\n */\nexport function normalize(\n image: RGBImage,\n mean: readonly [number, number, number],\n std: readonly [number, number, number],\n scale: number = 1 / 255,\n): Float32Array {\n const out = new Float32Array(image.data.length);\n const data = image.data;\n const m0 = mean[0];\n const m1 = mean[1];\n const m2 = mean[2];\n const s0 = std[0];\n const s1 = std[1];\n const s2 = std[2];\n for (let i = 0; i < data.length; i += 3) {\n out[i] = ((data[i] as number) * scale - m0) / s0;\n out[i + 1] = ((data[i + 1] as number) * scale - m1) / s1;\n out[i + 2] = ((data[i + 2] as number) * scale - m2) / s2;\n }\n return out;\n}\n\n/** Convert a uint8 image to a `Float32Array` in `[0, 1]` (HWC layout preserved). */\nexport function toFloat32(image: RGBImage, scale: number = 1 / 255): Float32Array {\n const out = new Float32Array(image.data.length);\n const data = image.data;\n for (let i = 0; i < data.length; i++) {\n out[i] = (data[i] as number) * scale;\n }\n return out;\n}\n\n/**\n * Transpose interleaved HWC data to planar CHW layout.\n *\n * @param hwc Source array of length `width * height * channels`.\n */\nexport function toCHW(\n hwc: Float32Array,\n width: number,\n height: number,\n channels: number = 3,\n): Float32Array {\n const expected = width * height * channels;\n if (hwc.length !== expected) {\n throw new Error(\n `toCHW: expected length ${expected} for ${width}x${height}x${channels}, got ${hwc.length}.`,\n );\n }\n const chw = new Float32Array(expected);\n const plane = width * height;\n for (let y = 0; y < height; y++) {\n for (let x = 0; x < width; x++) {\n const hwcBase = (y * width + x) * channels;\n const planeIdx = y * width + x;\n for (let c = 0; c < channels; c++) {\n chw[c * plane + planeIdx] = hwc[hwcBase + c] as number;\n }\n }\n }\n return chw;\n}\n\n/** Wrap a Float32 buffer into an `ort.Tensor`. */\nexport function toFloat32Tensor(data: Float32Array, dims: readonly number[]): ort.Tensor {\n return new ortRuntime.Tensor(\"float32\", data, dims as number[]);\n}\n\n/**\n * Convert an HWC uint8 image to a CHW `Float32Array` scaled to `[0, 1]`.\n *\n * Mirrors `torchvision.transforms.ToTensor()` semantics: HWC → CHW,\n * `uint8 → float32 / 255`. Useful as input to YOLO-style detectors that\n * don't require ImageNet normalization.\n *\n * @returns CHW `Float32Array` of length `width * height * 3`.\n */\nexport function toTensor(image: RGBImage): Float32Array {\n const f32 = toFloat32(image);\n return toCHW(f32, image.width, image.height, 3);\n}\n\n/**\n * Convert an HWC BGR uint8 buffer (OpenCV layout) to the SDK's HWC RGB.\n *\n * Use when you receive image bytes from `cv2.imencode` over the wire and\n * want to feed them to the SDK without going through a canvas decode.\n *\n * @param bgr Flat BGR Uint8Array of length `width * height * 3`.\n */\nexport function fromCv2(bgr: Uint8Array, width: number, height: number): RGBImage {\n if (bgr.length !== width * height * 3) {\n throw new Error(\n `fromCv2: data length ${bgr.length} does not match width * height * 3 = ${width * height * 3}.`,\n );\n }\n const rgb = new Uint8Array(bgr.length);\n for (let i = 0; i < bgr.length; i += 3) {\n rgb[i] = bgr[i + 2] as number;\n rgb[i + 1] = bgr[i + 1] as number;\n rgb[i + 2] = bgr[i] as number;\n }\n return new RGBImage(rgb, width, height);\n}\n\n/**\n * Convert the SDK's HWC RGB image to an HWC BGR `Uint8Array` (OpenCV layout).\n *\n * Useful for round-tripping data to a Python OpenCV consumer.\n */\nexport function toCv2(image: RGBImage): Uint8Array {\n const rgb = image.data;\n const bgr = new Uint8Array(rgb.length);\n for (let i = 0; i < rgb.length; i += 3) {\n bgr[i] = rgb[i + 2] as number;\n bgr[i + 1] = rgb[i + 1] as number;\n bgr[i + 2] = rgb[i] as number;\n }\n return bgr;\n}\n\nexport interface LetterboxResult {\n /** The padded image at the target size. */\n readonly image: RGBImage;\n /** The factor applied to the original image (`< 1` if downscaled). */\n readonly scale: number;\n /** Horizontal padding in pixels (left side; right side has the same or +1). */\n readonly padLeft: number;\n /** Vertical padding in pixels (top side). */\n readonly padTop: number;\n}\n\n/**\n * Resize preserving aspect ratio, padding to `(targetWidth, targetHeight)`\n * with a constant fill color.\n *\n * Standard YOLO preprocessing — returning `scale` and `padLeft`/`padTop`\n * lets callers map detections back to the original image coordinates.\n */\nexport function letterbox(\n image: RGBImage,\n targetWidth: number,\n targetHeight: number,\n fill: readonly [number, number, number] = [114, 114, 114],\n): LetterboxResult {\n const scale = Math.min(targetWidth / image.width, targetHeight / image.height);\n const newW = Math.round(image.width * scale);\n const newH = Math.round(image.height * scale);\n const resized = resize(image, newW, newH);\n\n const out = new Uint8Array(targetWidth * targetHeight * 3);\n const f0 = fill[0];\n const f1 = fill[1];\n const f2 = fill[2];\n for (let i = 0; i < out.length; i += 3) {\n out[i] = f0;\n out[i + 1] = f1;\n out[i + 2] = f2;\n }\n\n const padLeft = Math.floor((targetWidth - newW) / 2);\n const padTop = Math.floor((targetHeight - newH) / 2);\n const rowBytes = newW * 3;\n for (let y = 0; y < newH; y++) {\n const srcOffset = y * rowBytes;\n const dstOffset = ((padTop + y) * targetWidth + padLeft) * 3;\n out.set(resized.data.subarray(srcOffset, srcOffset + rowBytes), dstOffset);\n }\n\n return {\n image: new RGBImage(out, targetWidth, targetHeight),\n scale,\n padLeft,\n padTop,\n };\n}\n"],"mappings":"oKAgBA,SAAgB,EAAO,EAAiB,EAAqB,EAAgC,CACzF,GAAI,GAAe,GAAK,GAAgB,EACpC,MAAU,MAAM,yBAAyB,EAAY,GAAG,EAAa,EAAE,EAE3E,GAAI,IAAgB,EAAM,OAAS,IAAiB,EAAM,OACtD,OAAO,EAGX,IAAM,EAAY,EAAA,aAAa,EAAM,MAAO,EAAM,MAAM,EAExD,EADe,aAAa,CAC5B,CAAA,CAAO,aAAa,EAAA,eAAe,CAAK,EAAG,EAAG,CAAC,EAG/C,IAAM,EAAS,EAAA,aADG,EAAA,aAAa,EAAa,CAChB,CAAS,EAMrC,MALA,GAAO,sBAAwB,GAC/B,EAAO,sBAAwB,OAC/B,EAAO,UAAU,EAAgC,EAAG,EAAG,EAAa,CAAY,EAGzE,EAAA,eADW,EAAO,aAAa,EAAG,EAAG,EAAa,CACnC,CAAS,CACnC,CAOA,SAAgB,EACZ,EACA,EACA,EACA,EAAgB,EAAI,IACR,CACZ,IAAM,EAAM,IAAI,aAAa,EAAM,KAAK,MAAM,EACxC,EAAO,EAAM,KACb,EAAK,EAAK,GACV,EAAK,EAAK,GACV,EAAK,EAAK,GACV,EAAK,EAAI,GACT,EAAK,EAAI,GACT,EAAK,EAAI,GACf,IAAK,IAAI,EAAI,EAAG,EAAI,EAAK,OAAQ,GAAK,EAClC,EAAI,IAAO,EAAK,GAAgB,EAAQ,GAAM,EAC9C,EAAI,EAAI,IAAO,EAAK,EAAI,GAAgB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AO,IAAI,EAAA,SAAS,EAAK,EAAa,CAAY,EAClD,QACA,UACA,QACJ,CACJ"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"image.js","names":[],"sources":["../../../src/vision/preprocess/image.ts"],"sourcesContent":["/**\n * Composable image preprocessing primitives (resize, normalize, letterbox, layout).\n *\n * Mirrors the Python `preprocess.image` module. Operates on the canonical\n * {@link RGBImage} (HWC RGB uint8) and produces uint8 or float32 buffers\n * depending on the operation.\n */\n\nimport * as ortRuntime from \"onnxruntime-web\";\nimport type * as ort from \"onnxruntime-web\";\n\nimport { createCanvas, get2DContext, imageDataToRGB, rgbToImageData } from \"../core/canvas\";\nimport { RGBImage } from \"../types\";\n\n/** Resize an image to `(targetWidth, targetHeight)` using high-quality canvas resampling. */\nexport function resize(image: RGBImage, targetWidth: number, targetHeight: number): RGBImage {\n if (targetWidth <= 0 || targetHeight <= 0) {\n throw new Error(`Invalid resize target ${targetWidth}x${targetHeight}.`);\n }\n if (targetWidth === image.width && targetHeight === image.height) {\n return image;\n }\n\n const srcCanvas = createCanvas(image.width, image.height);\n const srcCtx = get2DContext(srcCanvas);\n srcCtx.putImageData(rgbToImageData(image), 0, 0);\n\n const dstCanvas = createCanvas(targetWidth, targetHeight);\n const dstCtx = get2DContext(dstCanvas);\n dstCtx.imageSmoothingEnabled = true;\n dstCtx.imageSmoothingQuality = \"high\";\n dstCtx.drawImage(srcCanvas as CanvasImageSource, 0, 0, targetWidth, targetHeight);\n\n const imageData = dstCtx.getImageData(0, 0, targetWidth, targetHeight);\n return imageDataToRGB(imageData);\n}\n\n/**\n * Convert a uint8 image to a normalized float32 array (HWC layout preserved).\n *\n * Applies `(pixel * scale - mean) / std` channel-wise.\n */\nexport function normalize(\n image: RGBImage,\n mean: readonly [number, number, number],\n std: readonly [number, number, number],\n scale: number = 1 / 255,\n): Float32Array {\n const out = new Float32Array(image.data.length);\n const data = image.data;\n const m0 = mean[0];\n const m1 = mean[1];\n const m2 = mean[2];\n const s0 = std[0];\n const s1 = std[1];\n const s2 = std[2];\n for (let i = 0; i < data.length; i += 3) {\n out[i] = ((data[i] as number) * scale - m0) / s0;\n out[i + 1] = ((data[i + 1] as number) * scale - m1) / s1;\n out[i + 2] = ((data[i + 2] as number) * scale - m2) / s2;\n }\n return out;\n}\n\n/** Convert a uint8 image to a `Float32Array` in `[0, 1]` (HWC layout preserved). */\nexport function toFloat32(image: RGBImage, scale: number = 1 / 255): Float32Array {\n const out = new Float32Array(image.data.length);\n const data = image.data;\n for (let i = 0; i < data.length; i++) {\n out[i] = (data[i] as number) * scale;\n }\n return out;\n}\n\n/**\n * Transpose interleaved HWC data to planar CHW layout.\n *\n * @param hwc Source array of length `width * height * channels`.\n */\nexport function toCHW(\n hwc: Float32Array,\n width: number,\n height: number,\n channels: number = 3,\n): Float32Array {\n const expected = width * height * channels;\n if (hwc.length !== expected) {\n throw new Error(\n `toCHW: expected length ${expected} for ${width}x${height}x${channels}, got ${hwc.length}.`,\n );\n }\n const chw = new Float32Array(expected);\n const plane = width * height;\n for (let y = 0; y < height; y++) {\n for (let x = 0; x < width; x++) {\n const hwcBase = (y * width + x) * channels;\n const planeIdx = y * width + x;\n for (let c = 0; c < channels; c++) {\n chw[c * plane + planeIdx] = hwc[hwcBase + c] as number;\n }\n }\n }\n return chw;\n}\n\n/** Wrap a Float32 buffer into an `ort.Tensor`. */\nexport function toFloat32Tensor(data: Float32Array, dims: readonly number[]): ort.Tensor {\n return new ortRuntime.Tensor(\"float32\", data, dims as number[]);\n}\n\n/**\n * Convert an HWC uint8 image to a CHW `Float32Array` scaled to `[0, 1]`.\n *\n * Mirrors `torchvision.transforms.ToTensor()` semantics: HWC → CHW,\n * `uint8 → float32 / 255`. Useful as input to YOLO-style detectors that\n * don't require ImageNet normalization.\n *\n * @returns CHW `Float32Array` of length `width * height * 3`.\n */\nexport function toTensor(image: RGBImage): Float32Array {\n const f32 = toFloat32(image);\n return toCHW(f32, image.width, image.height, 3);\n}\n\n/**\n * Convert an HWC BGR uint8 buffer (OpenCV layout) to the SDK's HWC RGB.\n *\n * Use when you receive image bytes from `cv2.imencode` over the wire and\n * want to feed them to the SDK without going through a canvas decode.\n *\n * @param bgr Flat BGR Uint8Array of length `width * height * 3`.\n */\nexport function fromCv2(bgr: Uint8Array, width: number, height: number): RGBImage {\n if (bgr.length !== width * height * 3) {\n throw new Error(\n `fromCv2: data length ${bgr.length} does not match width * height * 3 = ${width * height * 3}.`,\n );\n }\n const rgb = new Uint8Array(bgr.length);\n for (let i = 0; i < bgr.length; i += 3) {\n rgb[i] = bgr[i + 2] as number;\n rgb[i + 1] = bgr[i + 1] as number;\n rgb[i + 2] = bgr[i] as number;\n }\n return new RGBImage(rgb, width, height);\n}\n\n/**\n * Convert the SDK's HWC RGB image to an HWC BGR `Uint8Array` (OpenCV layout).\n *\n * Useful for round-tripping data to a Python OpenCV consumer.\n */\nexport function toCv2(image: RGBImage): Uint8Array {\n const rgb = image.data;\n const bgr = new Uint8Array(rgb.length);\n for (let i = 0; i < rgb.length; i += 3) {\n bgr[i] = rgb[i + 2] as number;\n bgr[i + 1] = rgb[i + 1] as number;\n bgr[i + 2] = rgb[i] as number;\n }\n return bgr;\n}\n\nexport interface LetterboxResult {\n /** The padded image at the target size. */\n readonly image: RGBImage;\n /** The factor applied to the original image (`< 1` if downscaled). */\n readonly scale: number;\n /** Horizontal padding in pixels (left side; right side has the same or +1). */\n readonly padLeft: number;\n /** Vertical padding in pixels (top side). */\n readonly padTop: number;\n}\n\n/**\n * Resize preserving aspect ratio, padding to `(targetWidth, targetHeight)`\n * with a constant fill color.\n *\n * Standard YOLO preprocessing — returning `scale` and `padLeft`/`padTop`\n * lets callers map detections back to the original image coordinates.\n */\nexport function letterbox(\n image: RGBImage,\n targetWidth: number,\n targetHeight: number,\n fill: readonly [number, number, number] = [114, 114, 114],\n): LetterboxResult {\n const scale = Math.min(targetWidth / image.width, targetHeight / image.height);\n const newW = Math.round(image.width * scale);\n const newH = Math.round(image.height * scale);\n const resized = resize(image, newW, newH);\n\n const out = new Uint8Array(targetWidth * targetHeight * 3);\n const f0 = fill[0];\n const f1 = fill[1];\n const f2 = fill[2];\n for (let i = 0; i < out.length; i += 3) {\n out[i] = f0;\n out[i + 1] = f1;\n out[i + 2] = f2;\n }\n\n const padLeft = Math.floor((targetWidth - newW) / 2);\n const padTop = Math.floor((targetHeight - newH) / 2);\n const rowBytes = newW * 3;\n for (let y = 0; y < newH; y++) {\n const srcOffset = y * rowBytes;\n const dstOffset = ((padTop + y) * targetWidth + padLeft) * 3;\n out.set(resized.data.subarray(srcOffset, srcOffset + rowBytes), dstOffset);\n }\n\n return {\n image: new RGBImage(out, targetWidth, targetHeight),\n scale,\n padLeft,\n padTop,\n };\n}\n"],"mappings":";;;;AAeA,SAAgB,EAAO,GAAiB,GAAqB,GAAgC;CACzF,IAAI,KAAe,KAAK,KAAgB,GACpC,MAAU,MAAM,yBAAyB,EAAY,GAAG,EAAa,EAAE;CAE3E,IAAI,MAAgB,EAAM,SAAS,MAAiB,EAAM,QACtD,OAAO;CAGX,IAAM,IAAY,EAAa,EAAM,OAAO,EAAM,MAAM;CAExD,EAD4B,CAC5B,CAAA,CAAO,aAAa,EAAe,CAAK,GAAG,GAAG,CAAC;CAG/C,IAAM,IAAS,EADG,EAAa,GAAa,CAChB,CAAS;CAMrC,OALA,EAAO,wBAAwB,IAC/B,EAAO,wBAAwB,QAC/B,EAAO,UAAU,GAAgC,GAAG,GAAG,GAAa,CAAY,GAGzE,EADW,EAAO,aAAa,GAAG,GAAG,GAAa,CACnC,CAAS;AACnC;AAOA,SAAgB,EACZ,GACA,GACA,GACA,IAAgB,IAAI,KACR;CACZ,IAAM,IAAM,IAAI,aAAa,EAAM,KAAK,MAAM,GACxC,IAAO,EAAM,MACb,IAAK,EAAK,IACV,IAAK,EAAK,IACV,IAAK,EAAK,IACV,IAAK,EAAI,IACT,IAAK,EAAI,IACT,IAAK,EAAI;CACf,KAAK,IAAI,IAAI,GAAG,IAAI,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ChB,MAAc,IAAS,KAAK,IAAc,KAAW;EAC3D,EAAI,IAAI,EAAQ,KAAK,SAAS,GAAW,IAAY,CAAQ,GAAG,CAAS;CAC7E;CAEA,OAAO;EACH,OAAO,IAAI,EAAS,GAAK,GAAa,CAAY;EAClD;EACA;EACA;CACJ;AACJ"}
|
|
1
|
+
{"version":3,"file":"image.js","names":[],"sources":["../../../src/vision/preprocess/image.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Composable image preprocessing primitives (resize, normalize, letterbox, layout).\n *\n * Mirrors the Python `preprocess.image` module. Operates on the canonical\n * {@link RGBImage} (HWC RGB uint8) and produces uint8 or float32 buffers\n * depending on the operation.\n */\n\nimport * as ortRuntime from \"onnxruntime-web\";\nimport type * as ort from \"onnxruntime-web\";\n\nimport { createCanvas, get2DContext, imageDataToRGB, rgbToImageData } from \"../core/canvas\";\nimport { RGBImage } from \"../types\";\n\n/** Resize an image to `(targetWidth, targetHeight)` using high-quality canvas resampling. */\nexport function resize(image: RGBImage, targetWidth: number, targetHeight: number): RGBImage {\n if (targetWidth <= 0 || targetHeight <= 0) {\n throw new Error(`Invalid resize target ${targetWidth}x${targetHeight}.`);\n }\n if (targetWidth === image.width && targetHeight === image.height) {\n return image;\n }\n\n const srcCanvas = createCanvas(image.width, image.height);\n const srcCtx = get2DContext(srcCanvas);\n srcCtx.putImageData(rgbToImageData(image), 0, 0);\n\n const dstCanvas = createCanvas(targetWidth, targetHeight);\n const dstCtx = get2DContext(dstCanvas);\n dstCtx.imageSmoothingEnabled = true;\n dstCtx.imageSmoothingQuality = \"high\";\n dstCtx.drawImage(srcCanvas as CanvasImageSource, 0, 0, targetWidth, targetHeight);\n\n const imageData = dstCtx.getImageData(0, 0, targetWidth, targetHeight);\n return imageDataToRGB(imageData);\n}\n\n/**\n * Convert a uint8 image to a normalized float32 array (HWC layout preserved).\n *\n * Applies `(pixel * scale - mean) / std` channel-wise.\n */\nexport function normalize(\n image: RGBImage,\n mean: readonly [number, number, number],\n std: readonly [number, number, number],\n scale: number = 1 / 255,\n): Float32Array {\n const out = new Float32Array(image.data.length);\n const data = image.data;\n const m0 = mean[0];\n const m1 = mean[1];\n const m2 = mean[2];\n const s0 = std[0];\n const s1 = std[1];\n const s2 = std[2];\n for (let i = 0; i < data.length; i += 3) {\n out[i] = ((data[i] as number) * scale - m0) / s0;\n out[i + 1] = ((data[i + 1] as number) * scale - m1) / s1;\n out[i + 2] = ((data[i + 2] as number) * scale - m2) / s2;\n }\n return out;\n}\n\n/** Convert a uint8 image to a `Float32Array` in `[0, 1]` (HWC layout preserved). */\nexport function toFloat32(image: RGBImage, scale: number = 1 / 255): Float32Array {\n const out = new Float32Array(image.data.length);\n const data = image.data;\n for (let i = 0; i < data.length; i++) {\n out[i] = (data[i] as number) * scale;\n }\n return out;\n}\n\n/**\n * Transpose interleaved HWC data to planar CHW layout.\n *\n * @param hwc Source array of length `width * height * channels`.\n */\nexport function toCHW(\n hwc: Float32Array,\n width: number,\n height: number,\n channels: number = 3,\n): Float32Array {\n const expected = width * height * channels;\n if (hwc.length !== expected) {\n throw new Error(\n `toCHW: expected length ${expected} for ${width}x${height}x${channels}, got ${hwc.length}.`,\n );\n }\n const chw = new Float32Array(expected);\n const plane = width * height;\n for (let y = 0; y < height; y++) {\n for (let x = 0; x < width; x++) {\n const hwcBase = (y * width + x) * channels;\n const planeIdx = y * width + x;\n for (let c = 0; c < channels; c++) {\n chw[c * plane + planeIdx] = hwc[hwcBase + c] as number;\n }\n }\n }\n return chw;\n}\n\n/** Wrap a Float32 buffer into an `ort.Tensor`. */\nexport function toFloat32Tensor(data: Float32Array, dims: readonly number[]): ort.Tensor {\n return new ortRuntime.Tensor(\"float32\", data, dims as number[]);\n}\n\n/**\n * Convert an HWC uint8 image to a CHW `Float32Array` scaled to `[0, 1]`.\n *\n * Mirrors `torchvision.transforms.ToTensor()` semantics: HWC → CHW,\n * `uint8 → float32 / 255`. Useful as input to YOLO-style detectors that\n * don't require ImageNet normalization.\n *\n * @returns CHW `Float32Array` of length `width * height * 3`.\n */\nexport function toTensor(image: RGBImage): Float32Array {\n const f32 = toFloat32(image);\n return toCHW(f32, image.width, image.height, 3);\n}\n\n/**\n * Convert an HWC BGR uint8 buffer (OpenCV layout) to the SDK's HWC RGB.\n *\n * Use when you receive image bytes from `cv2.imencode` over the wire and\n * want to feed them to the SDK without going through a canvas decode.\n *\n * @param bgr Flat BGR Uint8Array of length `width * height * 3`.\n */\nexport function fromCv2(bgr: Uint8Array, width: number, height: number): RGBImage {\n if (bgr.length !== width * height * 3) {\n throw new Error(\n `fromCv2: data length ${bgr.length} does not match width * height * 3 = ${width * height * 3}.`,\n );\n }\n const rgb = new Uint8Array(bgr.length);\n for (let i = 0; i < bgr.length; i += 3) {\n rgb[i] = bgr[i + 2] as number;\n rgb[i + 1] = bgr[i + 1] as number;\n rgb[i + 2] = bgr[i] as number;\n }\n return new RGBImage(rgb, width, height);\n}\n\n/**\n * Convert the SDK's HWC RGB image to an HWC BGR `Uint8Array` (OpenCV layout).\n *\n * Useful for round-tripping data to a Python OpenCV consumer.\n */\nexport function toCv2(image: RGBImage): Uint8Array {\n const rgb = image.data;\n const bgr = new Uint8Array(rgb.length);\n for (let i = 0; i < rgb.length; i += 3) {\n bgr[i] = rgb[i + 2] as number;\n bgr[i + 1] = rgb[i + 1] as number;\n bgr[i + 2] = rgb[i] as number;\n }\n return bgr;\n}\n\nexport interface LetterboxResult {\n /** The padded image at the target size. */\n readonly image: RGBImage;\n /** The factor applied to the original image (`< 1` if downscaled). */\n readonly scale: number;\n /** Horizontal padding in pixels (left side; right side has the same or +1). */\n readonly padLeft: number;\n /** Vertical padding in pixels (top side). */\n readonly padTop: number;\n}\n\n/**\n * Resize preserving aspect ratio, padding to `(targetWidth, targetHeight)`\n * with a constant fill color.\n *\n * Standard YOLO preprocessing — returning `scale` and `padLeft`/`padTop`\n * lets callers map detections back to the original image coordinates.\n */\nexport function letterbox(\n image: RGBImage,\n targetWidth: number,\n targetHeight: number,\n fill: readonly [number, number, number] = [114, 114, 114],\n): LetterboxResult {\n const scale = Math.min(targetWidth / image.width, targetHeight / image.height);\n const newW = Math.round(image.width * scale);\n const newH = Math.round(image.height * scale);\n const resized = resize(image, newW, newH);\n\n const out = new Uint8Array(targetWidth * targetHeight * 3);\n const f0 = fill[0];\n const f1 = fill[1];\n const f2 = fill[2];\n for (let i = 0; i < out.length; i += 3) {\n out[i] = f0;\n out[i + 1] = f1;\n out[i + 2] = f2;\n }\n\n const padLeft = Math.floor((targetWidth - newW) / 2);\n const padTop = Math.floor((targetHeight - newH) / 2);\n const rowBytes = newW * 3;\n for (let y = 0; y < newH; y++) {\n const srcOffset = y * rowBytes;\n const dstOffset = ((padTop + y) * targetWidth + padLeft) * 3;\n out.set(resized.data.subarray(srcOffset, srcOffset + rowBytes), dstOffset);\n }\n\n return {\n image: new RGBImage(out, targetWidth, targetHeight),\n scale,\n padLeft,\n padTop,\n };\n}\n"],"mappings":";;;;AAgBA,SAAgB,EAAO,GAAiB,GAAqB,GAAgC;CACzF,IAAI,KAAe,KAAK,KAAgB,GACpC,MAAU,MAAM,yBAAyB,EAAY,GAAG,EAAa,EAAE;CAE3E,IAAI,MAAgB,EAAM,SAAS,MAAiB,EAAM,QACtD,OAAO;CAGX,IAAM,IAAY,EAAa,EAAM,OAAO,EAAM,MAAM;CAExD,EAD4B,CAC5B,CAAA,CAAO,aAAa,EAAe,CAAK,GAAG,GAAG,CAAC;CAG/C,IAAM,IAAS,EADG,EAAa,GAAa,CAChB,CAAS;CAMrC,OALA,EAAO,wBAAwB,IAC/B,EAAO,wBAAwB,QAC/B,EAAO,UAAU,GAAgC,GAAG,GAAG,GAAa,CAAY,GAGzE,EADW,EAAO,aAAa,GAAG,GAAG,GAAa,CACnC,CAAS;AACnC;AAOA,SAAgB,EACZ,GACA,GACA,GACA,IAAgB,IAAI,KACR;CACZ,IAAM,IAAM,IAAI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