tempest-react-sdk 0.38.2 → 0.39.0
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/README.md +1 -1
- 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 +1 -1
- package/dist/vision/core/canvas.cjs.map +1 -1
- package/dist/vision/core/canvas.js +4 -4
- package/dist/vision/core/canvas.js.map +1 -1
- package/dist/vision/core/exceptions.cjs +1 -1
- package/dist/vision/core/exceptions.cjs.map +1 -1
- package/dist/vision/core/exceptions.js +2 -2
- 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 +2 -2
- package/dist/vision/core/metadata.cjs.map +1 -1
- package/dist/vision/core/metadata.js +10 -7
- 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 +1 -1
- package/dist/vision/core/session.cjs.map +1 -1
- package/dist/vision/core/session.js +8 -5
- 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/fusion.cjs +2 -0
- package/dist/vision/fusion.cjs.map +1 -0
- package/dist/vision/fusion.js +42 -0
- package/dist/vision/fusion.js.map +1 -0
- package/dist/vision/index.cjs +1 -1
- package/dist/vision/index.cjs.map +1 -1
- package/dist/vision/index.js +23 -20
- package/dist/vision/index.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 +1 -1
- package/dist/vision/labels.cjs.map +1 -1
- package/dist/vision/labels.js +7 -4
- 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 +1 -1
- package/dist/vision/postprocess/detection.cjs.map +1 -1
- package/dist/vision/postprocess/detection.js +3 -10
- package/dist/vision/postprocess/detection.js.map +1 -1
- package/dist/vision/postprocess/segmentation.cjs +1 -1
- package/dist/vision/postprocess/segmentation.cjs.map +1 -1
- package/dist/vision/postprocess/segmentation.js +11 -15
- 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/preprocess/pipeline.cjs +2 -0
- package/dist/vision/preprocess/pipeline.cjs.map +1 -0
- package/dist/vision/preprocess/pipeline.js +61 -0
- package/dist/vision/preprocess/pipeline.js.map +1 -0
- package/dist/vision/results.cjs +1 -1
- package/dist/vision/results.cjs.map +1 -1
- package/dist/vision/results.js +23 -2
- package/dist/vision/results.js.map +1 -1
- package/dist/vision/tasks/base.cjs +1 -1
- package/dist/vision/tasks/base.cjs.map +1 -1
- package/dist/vision/tasks/base.js +9 -2
- 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/detectClassify.cjs +2 -0
- package/dist/vision/tasks/detectClassify.cjs.map +1 -0
- package/dist/vision/tasks/detectClassify.js +221 -0
- package/dist/vision/tasks/detectClassify.js.map +1 -0
- package/dist/vision/tasks/detector.cjs +1 -1
- package/dist/vision/tasks/detector.cjs.map +1 -1
- package/dist/vision/tasks/detector.js +54 -29
- package/dist/vision/tasks/detector.js.map +1 -1
- package/dist/vision/tasks/segmenter.cjs +1 -1
- package/dist/vision/tasks/segmenter.cjs.map +1 -1
- package/dist/vision/tasks/segmenter.js +53 -28
- 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.cjs +1 -1
- package/dist/vision.d.ts +1517 -956
- package/dist/vision.js +25 -22
- 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
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"detectClassify.cjs","names":[],"sources":["../../../src/vision/tasks/detectClassify.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Run a fused detect→classify pipeline in the browser.\n *\n * The file this loads was built by the Python SDK's `ort_vision_sdk.compose`,\n * and it already contains both models plus the crop-and-resize bridge between\n * them. That matters far more in a tab than on a server: two models mean two\n * `.onnx` downloads, two WASM/WebGPU session initializations, and a per-crop\n * round trip through JavaScript to slice, resize and restack the regions before\n * the second model can see them. A fused pipeline has one download, one session\n * and no round trip — the crops are produced and consumed inside the graph.\n */\n\nimport type * as ort from \"onnxruntime-web\";\n\nimport { FusionError } from \"../core/exceptions\";\nimport { type ModelSource, type OrtSessionOptions, OrtSession } from \"../core/session\";\nimport { SpeedTimer } from \"../core/timing\";\nimport {\n INPUT_IMAGE,\n INPUT_PAD,\n INPUT_SCALE,\n INPUT_SOURCE,\n OUTPUT_BOXES,\n OUTPUT_CLASSES,\n OUTPUT_NUM_DETECTIONS,\n OUTPUT_PROBS,\n OUTPUT_SCORES,\n type FusionSpec,\n readFusionSpec,\n} from \"../fusion\";\nimport { type ImageInput, loadImage } from \"../io/image\";\nimport { type LabelSpec, resolveLabels } from \"../labels\";\nimport { softmax, topK } from \"../postprocess/classification\";\nimport { toCHW, toFloat32, toFloat32Tensor } from \"../preprocess/image\";\nimport { LetterboxPipeline, zeroTensorData } from \"../preprocess/pipeline\";\nimport { Boxes, DetectClassifyResults } from \"../results\";\nimport {\n BoundingBox,\n RGBImage,\n type ClassProbability,\n type ClassificationResult,\n type DetectionResult,\n} from \"../types\";\nimport { VisionTask, requireDetections } from \"./base\";\n\nexport interface DetectClassifyOptions extends OrtSessionOptions {\n /**\n * Class label spec for the **detection** stage — see {@link resolveLabels}.\n * Defaults to the names recorded at fusion time, falling back to the COCO\n * 80-class preset when the fusion recorded none.\n */\n readonly labels?: LabelSpec;\n /**\n * Class label spec for the **classification** stage. Defaults to the recorded\n * names, falling back to generated `class_<id>` names.\n */\n readonly classifierLabels?: LabelSpec;\n /**\n * If `true`, a run that finds nothing throws {@link NoDetectionsError}\n * instead of returning an empty envelope. Default `false`, because looking\n * and finding nothing is a successful inference. Turn it on when an empty\n * result means the surrounding pipeline should stop rather than carry on with\n * zero rows. Can be overridden per `predict` call.\n */\n readonly raiseOnEmpty?: boolean;\n}\n\nexport interface DetectClassifyPredictOptions {\n /**\n * Drop detections scoring below this. The graph's own NMS threshold was fixed\n * at fusion time and cannot be lowered here — this only filters further.\n */\n readonly confThreshold?: number;\n /** If set, keep only detections whose detector `classId` is in this list. */\n readonly classes?: readonly number[];\n /** Truncate each detection's `classification.probabilities` to its top-k entries. */\n readonly topK?: number;\n /** Override the constructor's `raiseOnEmpty` setting for this call. */\n readonly raiseOnEmpty?: boolean;\n}\n\n/**\n * Detector and classifier running as a single ONNX model.\n *\n * Everything the pipeline needs to know about itself — the resolution to\n * letterbox to, whether it wants the full-resolution image as well, whether its\n * classifier output still needs a softmax, the class names of both stages — was\n * written into the file at fusion time and is read back here. Nothing is\n * restated on the JavaScript side, so nothing can drift out of step with the\n * Python side that built it.\n *\n * @example\n * ```typescript\n * const pipeline = await DetectClassify.create(\"/models/pipeline.onnx\");\n * const result = (await pipeline.predict(\"/images/flock.jpg\"))[0];\n * for (const detection of result) {\n * console.log(detection.name, detection.conf, detection.classification?.name);\n * }\n * ```\n */\nexport class DetectClassify extends VisionTask {\n private constructor(\n session: OrtSession,\n private readonly _spec: FusionSpec,\n private readonly _labels: readonly string[],\n private readonly _names: Readonly<Record<number, string>>,\n private readonly _classifierLabels: readonly string[],\n private readonly _classifierNames: Readonly<Record<number, string>>,\n private readonly _raiseOnEmpty: boolean,\n ) {\n super(session);\n }\n\n private _pipelineCache: LetterboxPipeline | null = null;\n\n /**\n * Run the model once on zero-filled inputs, paying one-time costs up front.\n *\n * Worth more here than on a single-stage task: a fused pipeline is two models\n * plus the bridge in one graph, so the first inference compiles shaders for\n * all of it. Calling this while a loading spinner is still up moves that cost\n * somewhere the user is already waiting.\n *\n * @param runs How many warm-up inferences to run. One is enough for WASM;\n * WebGPU sometimes settles on the second.\n */\n async warmup(runs: number = 1): Promise<void> {\n const [width, height] = this._spec.inputSize;\n for (let i = 0; i < runs; i++) {\n const feeds: Record<string, ort.Tensor> = {\n [INPUT_IMAGE]: toFloat32Tensor(zeroTensorData(width, height), [\n 1,\n 3,\n height,\n width,\n ]),\n };\n if (this._spec.needsSourceImage) {\n feeds[INPUT_SOURCE] = toFloat32Tensor(zeroTensorData(width, height), [\n 1,\n 3,\n height,\n width,\n ]);\n feeds[INPUT_SCALE] = toFloat32Tensor(new Float32Array([1]), [1]);\n feeds[INPUT_PAD] = toFloat32Tensor(new Float32Array([0, 0]), [2]);\n }\n await this._session.run(feeds);\n }\n }\n\n /**\n * The fused preprocessing pipeline, built on first use.\n *\n * Lazily, because constructing it allocates canvases: a pipeline built in an\n * environment without a canvas implementation stays constructible, and only\n * fails if it is actually asked to preprocess something.\n */\n private get _pipeline(): LetterboxPipeline {\n if (this._pipelineCache === null) {\n const [width, height] = this._spec.inputSize;\n this._pipelineCache = new LetterboxPipeline(width, height);\n }\n return this._pipelineCache;\n }\n\n /**\n * Load a fused pipeline and resolve both label spaces.\n *\n * @param model The fused `.onnx` — a URL, an `ArrayBuffer`, or bytes.\n * @param options Label overrides plus the usual session options.\n * @throws {@link FusionError} when the model carries no pipeline metadata,\n * i.e. it is a plain detector or classifier rather than something\n * `ort_vision_sdk.compose` produced.\n */\n static async create(\n model: ModelSource,\n options: DetectClassifyOptions = {},\n ): Promise<DetectClassify> {\n const session = await OrtSession.create(model, options);\n const spec = readFusionSpec(session.metadata);\n if (spec === null) {\n throw new FusionError(\n \"This model carries no fused-pipeline metadata, so DetectClassify cannot tell how to \" +\n \"drive it. Build one with ort_vision_sdk.compose.fuse_detect_classify, or load a \" +\n \"plain model with Detector/Classifier instead.\",\n );\n }\n\n const labels = resolveLabels(options.labels ?? spec.detectorNames ?? \"coco\");\n const classifierLabels = resolveLabels(options.classifierLabels ?? spec.classifierNames, {\n numClasses: classifierClasses(session) ?? undefined,\n });\n return new DetectClassify(\n session,\n spec,\n labels,\n indexNames(labels),\n classifierLabels,\n indexNames(classifierLabels),\n options.raiseOnEmpty ?? false,\n );\n }\n\n /** The pipeline configuration recorded in the model at fusion time. */\n get spec(): FusionSpec {\n return this._spec;\n }\n\n /** The `[width, height]` the detection stage runs at. */\n get inputSize(): readonly [number, number] {\n return this._spec.inputSize;\n }\n\n /** Detector class labels indexed by class id. */\n get labels(): readonly string[] {\n return this._labels;\n }\n\n /** Detector class id → class name (matches Ultralytics' `model.names`). */\n get names(): Readonly<Record<number, string>> {\n return this._names;\n }\n\n /** Classifier class labels indexed by class id. */\n get classifierLabels(): readonly string[] {\n return this._classifierLabels;\n }\n\n /** Classifier class id → class name. */\n get classifierNames(): Readonly<Record<number, string>> {\n return this._classifierNames;\n }\n\n /**\n * Alias for {@link predict} — call the pipeline like a torch `nn.Module`.\n *\n * Use as `pipeline.call(img)` since JavaScript class instances are not\n * callable; for direct invocation, prefer `pipeline.predict(img)`.\n */\n async call(\n image: ImageInput,\n options: DetectClassifyPredictOptions = {},\n ): Promise<DetectClassifyResults[]> {\n return this.predict(image, options);\n }\n\n /**\n * Run the pipeline on a single image.\n *\n * The returned envelope carries a {@link Speed} breakdown in `speed`. Its\n * `inference` figure covers detection *and* classification, since the\n * pipeline runs them as one graph and no boundary between them is observable\n * from outside.\n */\n async predict(\n image: ImageInput,\n options: DetectClassifyPredictOptions = {},\n ): Promise<DetectClassifyResults[]> {\n const timer = new SpeedTimer();\n const path = typeof image === \"string\" ? image : null;\n const original = await loadImage(image);\n timer.stage(\"load\");\n const { feeds, scale, padLeft, padTop } = this._preprocess(original);\n timer.stage(\"preprocess\");\n const outputs = await this._session.run(feeds);\n this._pipeline.release();\n timer.stage(\"inference\");\n\n const probsTensor = output(outputs, OUTPUT_PROBS);\n const boxes = floats(outputs, OUTPUT_BOXES);\n const scores = floats(outputs, OUTPUT_SCORES);\n const classes = integers(outputs, OUTPUT_CLASSES);\n const probs = probsTensor.data as Float32Array;\n const reported = integers(outputs, OUTPUT_NUM_DETECTIONS)[0] ?? 0;\n const rows = Math.min(reported, Math.floor(boxes.length / 4));\n const classCount = probsTensor.dims[probsTensor.dims.length - 1] ?? 0;\n\n const allowed = options.classes === undefined ? null : new Set(options.classes);\n const floor = options.confThreshold ?? 0;\n const detections: DetectionResult[] = [];\n for (let row = 0; row < rows; row++) {\n const classId = classes[row] ?? 0;\n const confidence = scores[row] ?? 0;\n if (confidence < floor || (allowed !== null && !allowed.has(classId))) continue;\n\n const bbox = this._toOriginal(boxes, row, { scale, padLeft, padTop, original });\n const cropped = crop(original, bbox);\n detections.push(\n detection(\n classId,\n this._names[classId] ?? `class_${classId}`,\n confidence,\n bbox,\n cropped,\n this._classify(\n probs.subarray(row * classCount, (row + 1) * classCount),\n cropped,\n options.topK,\n ),\n ),\n );\n }\n\n requireDetections(detections.length, {\n raiseOnEmpty: options.raiseOnEmpty ?? this._raiseOnEmpty,\n confThreshold: Math.max(floor, this._spec.confThreshold),\n classes: options.classes,\n path,\n });\n\n const origShape: readonly [number, number] = [original.height, original.width];\n timer.stage(\"postprocess\");\n return [\n new DetectClassifyResults(\n bulkBoxes(detections, origShape),\n detections,\n this._names,\n this._classifierNames,\n original,\n origShape,\n path,\n timer.speed(),\n ),\n ];\n }\n\n /**\n * Letterbox the image and build the graph's feeds.\n *\n * The detector input runs through {@link LetterboxPipeline}, which fuses the\n * resize, the padding and the HWC-to-CHW float conversion into one\n * `drawImage` plus one readback, and reuses its output buffer between frames.\n * That buffer goes straight to ONNX Runtime, so `_pipeline.release()` must not\n * be called until the run resolves.\n *\n * A pipeline fused with `cropSource: \"original\"` also takes the untouched\n * image as a second input, plus the scale and padding of the letterbox — that\n * is what lets the graph undo the letterbox transform internally and crop at\n * native resolution instead of from the downscaled copy. That one is **not**\n * letterboxed by definition, so it does not go through the fused path.\n */\n private _preprocess(image: RGBImage): {\n feeds: Record<string, ort.Tensor>;\n scale: number;\n padLeft: number;\n padTop: number;\n } {\n const [width, height] = this._spec.inputSize;\n const boxed = this._pipeline.run(image);\n const feeds: Record<string, ort.Tensor> = {\n [INPUT_IMAGE]: toFloat32Tensor(boxed.data, [1, 3, height, width]),\n };\n\n if (this._spec.needsSourceImage) {\n feeds[INPUT_SOURCE] = tensorOf(image);\n feeds[INPUT_SCALE] = toFloat32Tensor(new Float32Array([boxed.scale]), [1]);\n feeds[INPUT_PAD] = toFloat32Tensor(\n new Float32Array([boxed.padLeft, boxed.padTop]),\n [2],\n );\n }\n return { feeds, scale: boxed.scale, padLeft: boxed.padLeft, padTop: boxed.padTop };\n }\n\n /**\n * Map one letterboxed xyxy row back onto the original image.\n *\n * The graph always reports boxes in the detector's letterboxed pixel space,\n * whichever crop source it was fused with, so both sources agree here.\n */\n private _toOriginal(\n boxes: Float32Array,\n row: number,\n context: {\n scale: number;\n padLeft: number;\n padTop: number;\n original: RGBImage;\n },\n ): BoundingBox {\n const { scale, padLeft, padTop, original } = context;\n const at = (offset: number): number => boxes[row * 4 + offset] ?? 0;\n const clampX = (value: number): number => Math.min(Math.max(value, 0), original.width);\n const clampY = (value: number): number => Math.min(Math.max(value, 0), original.height);\n return new BoundingBox(\n clampX((at(0) - padLeft) / scale),\n clampY((at(1) - padTop) / scale),\n clampX((at(2) - padLeft) / scale),\n clampY((at(3) - padTop) / scale),\n );\n }\n\n /**\n * Turn one row of the classifier output into a result object.\n *\n * @param row The output row for this detection.\n * @param image The crop the row describes, carried so callers can display\n * what was classified.\n * @param k Optional truncation of the probability list.\n */\n private _classify(\n row: Float32Array,\n image: RGBImage,\n k: number | undefined,\n ): ClassificationResult {\n const scores = this._spec.applySoftmax ? softmax(row) : row;\n const { indices, values } = topK(scores, k ?? null);\n const probabilities: ClassProbability[] = [];\n for (let i = 0; i < indices.length; i++) {\n const classId = indices[i] ?? 0;\n const probability = values[i] ?? 0;\n const className = this._classifierLabels[classId] ?? `class_${classId}`;\n probabilities.push({\n classId,\n className,\n probability,\n cls: classId,\n name: className,\n conf: probability,\n });\n }\n const top = probabilities[0] ?? {\n classId: 0,\n className: \"class_0\",\n probability: 0,\n cls: 0,\n name: \"class_0\",\n conf: 0,\n };\n return {\n classId: top.classId,\n className: top.className,\n confidence: top.probability,\n cls: top.classId,\n name: top.className,\n conf: top.probability,\n image,\n probabilities,\n };\n }\n}\n\n/**\n * Read the classifier stage's class count off the `probs` output shape.\n *\n * @param session The loaded pipeline session.\n * @returns The class count, or `null` when the graph leaves that axis dynamic\n * or declares no `probs` output — in which case label resolution falls back\n * to whatever the fusion recorded.\n */\nfunction classifierClasses(session: OrtSession): number | null {\n const index = session.outputNames.indexOf(OUTPUT_PROBS);\n if (index < 0) return null;\n const shape = session.outputShapes[index];\n if (shape === undefined || shape.length === 0) return null;\n return shape[shape.length - 1] ?? null;\n}\n\n/**\n * Build a class id → name record from an ordered label list.\n *\n * @param labels Labels indexed by class id.\n * @returns The equivalent record.\n */\nfunction indexNames(labels: readonly string[]): Readonly<Record<number, string>> {\n const names: Record<number, string> = {};\n for (let i = 0; i < labels.length; i++) names[i] = labels[i] as string;\n return names;\n}\n\n/**\n * Fetch an output tensor by name.\n *\n * @param outputs The run's results.\n * @param name The output's name in the pipeline contract.\n * @returns The tensor.\n * @throws {@link FusionError} when the graph does not carry that output, which\n * means the file is not a pipeline this version can drive.\n */\nfunction output(outputs: Record<string, ort.Tensor>, name: string): ort.Tensor {\n const tensor = outputs[name];\n if (tensor === undefined) {\n throw new FusionError(`The fused pipeline is missing its '${name}' output.`);\n }\n return tensor;\n}\n\n/**\n * Fetch a float output by name.\n *\n * @param outputs The run's results.\n * @param name The output's name in the pipeline contract.\n * @returns The output's data.\n * @throws {@link FusionError} when the graph does not carry that output.\n */\nfunction floats(outputs: Record<string, ort.Tensor>, name: string): Float32Array {\n return output(outputs, name).data as Float32Array;\n}\n\n/**\n * Fetch an integer output by name, normalizing ORT's 64-bit representation.\n *\n * ONNX Runtime Web returns `int64` tensors as `BigInt64Array`, whose values do\n * not compare or index like numbers. Class ids and detection counts are always\n * small, so widening them to `number` here is lossless and keeps every caller\n * free of `BigInt` handling.\n *\n * @param outputs The run's results.\n * @param name The output's name in the pipeline contract.\n * @returns The output's values as plain numbers.\n * @throws {@link FusionError} when the graph does not carry that output.\n */\nfunction integers(outputs: Record<string, ort.Tensor>, name: string): number[] {\n const data = output(outputs, name).data as BigInt64Array | Int32Array | Float32Array;\n const values: number[] = [];\n for (let i = 0; i < data.length; i++) values.push(Number(data[i]));\n return values;\n}\n\n/**\n * Convert an image to the `[1, 3, H, W]` float32 tensor the graph expects.\n *\n * @param image The image to convert.\n * @returns The batched CHW tensor, scaled to `[0, 1]`.\n */\nfunction tensorOf(image: RGBImage): ort.Tensor {\n const chw = toCHW(toFloat32(image), image.width, image.height, 3);\n return toFloat32Tensor(chw, [1, 3, image.height, image.width]);\n}\n\n/**\n * Cut the box region out of the original image.\n *\n * @param image The source image.\n * @param bbox The box, in original-image pixel coordinates.\n * @returns The cropped region, or a zero-sized image for a box with no area.\n */\nfunction crop(image: RGBImage, bbox: BoundingBox): RGBImage {\n const [rawX1, rawY1, rawX2, rawY2] = bbox.asIntXyxy();\n const x1 = Math.max(0, rawX1);\n const y1 = Math.max(0, rawY1);\n const x2 = Math.min(image.width, rawX2);\n const y2 = Math.min(image.height, rawY2);\n if (x2 <= x1 || y2 <= y1) return new RGBImage(new Uint8Array(0), 0, 0);\n\n const width = x2 - x1;\n const height = y2 - y1;\n const out = new Uint8Array(width * height * 3);\n for (let row = 0; row < height; row++) {\n const offset = ((y1 + row) * image.width + x1) * 3;\n out.set(image.data.subarray(offset, offset + width * 3), row * width * 3);\n }\n return new RGBImage(out, width, height);\n}\n\n/**\n * Assemble one detection, filling the Ultralytics-style aliases.\n *\n * @param classId Detector class index.\n * @param className Detector class name.\n * @param confidence Detection score.\n * @param bbox Box in original-image coordinates.\n * @param croppedImage The region the box covers.\n * @param classification What the classification stage said about that region.\n * @returns The detection object.\n */\nfunction detection(\n classId: number,\n className: string,\n confidence: number,\n bbox: BoundingBox,\n croppedImage: RGBImage,\n classification: ClassificationResult,\n): DetectionResult {\n return {\n classId,\n className,\n confidence,\n bbox,\n cls: classId,\n name: className,\n conf: confidence,\n box: bbox,\n croppedImage,\n classification,\n };\n}\n\n/**\n * Assemble the bulk-array `Boxes` view from per-instance detections.\n *\n * @param detections The surviving detections.\n * @param origShape `[height, width]` of the original image.\n * @returns The bulk view, empty when nothing survived.\n */\nfunction bulkBoxes(\n detections: readonly DetectionResult[],\n origShape: readonly [number, number],\n): Boxes {\n const xyxy = new Float32Array(detections.length * 4);\n const cls = new Int32Array(detections.length);\n const conf = new Float32Array(detections.length);\n detections.forEach((entry, index) => {\n const [x1, y1, x2, y2] = entry.bbox.xyxy;\n xyxy[index * 4] = x1;\n xyxy[index * 4 + 1] = y1;\n xyxy[index * 4 + 2] = x2;\n xyxy[index * 4 + 3] = y2;\n cls[index] = entry.classId;\n conf[index] = entry.confidence;\n });\n return new Boxes(xyxy, cls, conf, origShape);\n}\n"],"mappings":"wYAqGA,IAAa,EAAb,MAAa,UAAuB,EAAA,UAAW,CAGtB,MACA,QACA,OACA,kBACA,iBACA,cAPrB,YACI,EACA,EACA,EACA,EACA,EACA,EACA,EACF,CACE,MAAM,CAAO,EAPI,KAAA,MAAA,EACA,KAAA,QAAA,EACA,KAAA,OAAA,EACA,KAAA,kBAAA,EACA,KAAA,iBAAA,EACA,KAAA,cAAA,CAGrB,CAEA,eAAmD,KAanD,MAAM,OAAO,EAAe,EAAkB,CAC1C,GAAM,CAAC,EAAO,GAAU,KAAK,MAAM,UACnC,IAAK,IAAI,EAAI,EAAG,EAAI,EAAM,IAAK,CAC3B,IAAM,EAAoC,EACrC,EAAA,aAAc,EAAA,gBAAgB,EAAA,eAAe,EAAO,CAAM,EAAG,CAC1D,EACA,EACA,EACA,CACJ,CAAC,CACL,EACI,KAAK,MAAM,mBACX,EAAM,EAAA,cAAgB,EAAA,gBAAgB,EAAA,eAAe,EAAO,CAAM,EAAG,CACjE,EACA,EACA,EACA,CACJ,CAAC,EACD,EAAM,EAAA,aAAe,EAAA,gBAAgB,IAAI,aAAa,CAAC,CAAC,CAAC,EAAG,CAAC,CAAC,CAAC,EAC/D,EAAM,EAAA,WAAa,EAAA,gBAAgB,IAAI,aAAa,CAAC,EAAG,CAAC,CAAC,EAAG,CAAC,CAAC,CAAC,GAEpE,MAAM,KAAK,SAAS,IAAI,CAAK,CACjC,CACJ,CASA,IAAY,WAA+B,CAC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O,IAAI,aAAa,EAAW,MAAM,EAU/C,OATA,EAAW,SAAS,EAAO,IAAU,CACjC,GAAM,CAAC,EAAI,EAAI,EAAI,GAAM,EAAM,KAAK,KACpC,EAAK,EAAQ,GAAK,EAClB,EAAK,EAAQ,EAAI,GAAK,EACtB,EAAK,EAAQ,EAAI,GAAK,EACtB,EAAK,EAAQ,EAAI,GAAK,EACtB,EAAI,GAAS,EAAM,QACnB,EAAK,GAAS,EAAM,UACxB,CAAC,EACM,IAAI,EAAA,MAAM,EAAM,EAAK,EAAM,CAAS,CAC/C"}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { FusionError as e } from "../core/exceptions.js";
|
|
2
|
+
import { BoundingBox as t, RGBImage as n } from "../types.js";
|
|
3
|
+
import { SpeedTimer as r } from "../core/timing.js";
|
|
4
|
+
import { Boxes as i, DetectClassifyResults as a } from "../results.js";
|
|
5
|
+
import { resolveLabels as o } from "../labels.js";
|
|
6
|
+
import { OrtSession as s } from "../core/session.js";
|
|
7
|
+
import { loadImage as c } from "../io/image.js";
|
|
8
|
+
import { INPUT_IMAGE as l, INPUT_PAD as u, INPUT_SCALE as d, INPUT_SOURCE as f, OUTPUT_BOXES as p, OUTPUT_CLASSES as m, OUTPUT_PROBS as h, OUTPUT_SCORES as g, readFusionSpec as _ } from "../fusion.js";
|
|
9
|
+
import { toCHW as v, toFloat32 as y, toFloat32Tensor as b } from "../preprocess/image.js";
|
|
10
|
+
import { LetterboxPipeline as x, zeroTensorData as S } from "../preprocess/pipeline.js";
|
|
11
|
+
import { softmax as C, topK as w } from "../postprocess/classification.js";
|
|
12
|
+
import { VisionTask as T, requireDetections as E } from "./base.js";
|
|
13
|
+
//#region src/vision/tasks/detectClassify.ts
|
|
14
|
+
var D = class n extends T {
|
|
15
|
+
_spec;
|
|
16
|
+
_labels;
|
|
17
|
+
_names;
|
|
18
|
+
_classifierLabels;
|
|
19
|
+
_classifierNames;
|
|
20
|
+
_raiseOnEmpty;
|
|
21
|
+
constructor(e, t, n, r, i, a, o) {
|
|
22
|
+
super(e), this._spec = t, this._labels = n, this._names = r, this._classifierLabels = i, this._classifierNames = a, this._raiseOnEmpty = o;
|
|
23
|
+
}
|
|
24
|
+
_pipelineCache = null;
|
|
25
|
+
async warmup(e = 1) {
|
|
26
|
+
let [t, n] = this._spec.inputSize;
|
|
27
|
+
for (let r = 0; r < e; r++) {
|
|
28
|
+
let e = { [l]: b(S(t, n), [
|
|
29
|
+
1,
|
|
30
|
+
3,
|
|
31
|
+
n,
|
|
32
|
+
t
|
|
33
|
+
]) };
|
|
34
|
+
this._spec.needsSourceImage && (e[f] = b(S(t, n), [
|
|
35
|
+
1,
|
|
36
|
+
3,
|
|
37
|
+
n,
|
|
38
|
+
t
|
|
39
|
+
]), e[d] = b(new Float32Array([1]), [1]), e[u] = b(new Float32Array([0, 0]), [2])), await this._session.run(e);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
get _pipeline() {
|
|
43
|
+
if (this._pipelineCache === null) {
|
|
44
|
+
let [e, t] = this._spec.inputSize;
|
|
45
|
+
this._pipelineCache = new x(e, t);
|
|
46
|
+
}
|
|
47
|
+
return this._pipelineCache;
|
|
48
|
+
}
|
|
49
|
+
static async create(t, r = {}) {
|
|
50
|
+
let i = await s.create(t, r), a = _(i.metadata);
|
|
51
|
+
if (a === null) throw new e("This model carries no fused-pipeline metadata, so DetectClassify cannot tell how to drive it. Build one with ort_vision_sdk.compose.fuse_detect_classify, or load a plain model with Detector/Classifier instead.");
|
|
52
|
+
let c = o(r.labels ?? a.detectorNames ?? "coco"), l = o(r.classifierLabels ?? a.classifierNames, { numClasses: O(i) ?? void 0 });
|
|
53
|
+
return new n(i, a, c, k(c), l, k(l), r.raiseOnEmpty ?? !1);
|
|
54
|
+
}
|
|
55
|
+
get spec() {
|
|
56
|
+
return this._spec;
|
|
57
|
+
}
|
|
58
|
+
get inputSize() {
|
|
59
|
+
return this._spec.inputSize;
|
|
60
|
+
}
|
|
61
|
+
get labels() {
|
|
62
|
+
return this._labels;
|
|
63
|
+
}
|
|
64
|
+
get names() {
|
|
65
|
+
return this._names;
|
|
66
|
+
}
|
|
67
|
+
get classifierLabels() {
|
|
68
|
+
return this._classifierLabels;
|
|
69
|
+
}
|
|
70
|
+
get classifierNames() {
|
|
71
|
+
return this._classifierNames;
|
|
72
|
+
}
|
|
73
|
+
async call(e, t = {}) {
|
|
74
|
+
return this.predict(e, t);
|
|
75
|
+
}
|
|
76
|
+
async predict(e, t = {}) {
|
|
77
|
+
let n = new r(), i = typeof e == "string" ? e : null, o = await c(e);
|
|
78
|
+
n.stage("load");
|
|
79
|
+
let { feeds: s, scale: l, padLeft: u, padTop: d } = this._preprocess(o);
|
|
80
|
+
n.stage("preprocess");
|
|
81
|
+
let f = await this._session.run(s);
|
|
82
|
+
this._pipeline.release(), n.stage("inference");
|
|
83
|
+
let _ = A(f, h), v = j(f, p), y = j(f, g), b = M(f, m), x = _.data, S = M(f, "num_detections")[0] ?? 0, C = Math.min(S, Math.floor(v.length / 4)), w = _.dims[_.dims.length - 1] ?? 0, T = t.classes === void 0 ? null : new Set(t.classes), D = t.confThreshold ?? 0, O = [];
|
|
84
|
+
for (let e = 0; e < C; e++) {
|
|
85
|
+
let n = b[e] ?? 0, r = y[e] ?? 0;
|
|
86
|
+
if (r < D || T !== null && !T.has(n)) continue;
|
|
87
|
+
let i = this._toOriginal(v, e, {
|
|
88
|
+
scale: l,
|
|
89
|
+
padLeft: u,
|
|
90
|
+
padTop: d,
|
|
91
|
+
original: o
|
|
92
|
+
}), a = P(o, i);
|
|
93
|
+
O.push(F(n, this._names[n] ?? `class_${n}`, r, i, a, this._classify(x.subarray(e * w, (e + 1) * w), a, t.topK)));
|
|
94
|
+
}
|
|
95
|
+
E(O.length, {
|
|
96
|
+
raiseOnEmpty: t.raiseOnEmpty ?? this._raiseOnEmpty,
|
|
97
|
+
confThreshold: Math.max(D, this._spec.confThreshold),
|
|
98
|
+
classes: t.classes,
|
|
99
|
+
path: i
|
|
100
|
+
});
|
|
101
|
+
let k = [o.height, o.width];
|
|
102
|
+
return n.stage("postprocess"), [new a(I(O, k), O, this._names, this._classifierNames, o, k, i, n.speed())];
|
|
103
|
+
}
|
|
104
|
+
_preprocess(e) {
|
|
105
|
+
let [t, n] = this._spec.inputSize, r = this._pipeline.run(e), i = { [l]: b(r.data, [
|
|
106
|
+
1,
|
|
107
|
+
3,
|
|
108
|
+
n,
|
|
109
|
+
t
|
|
110
|
+
]) };
|
|
111
|
+
return this._spec.needsSourceImage && (i[f] = N(e), i[d] = b(new Float32Array([r.scale]), [1]), i[u] = b(new Float32Array([r.padLeft, r.padTop]), [2])), {
|
|
112
|
+
feeds: i,
|
|
113
|
+
scale: r.scale,
|
|
114
|
+
padLeft: r.padLeft,
|
|
115
|
+
padTop: r.padTop
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
_toOriginal(e, n, r) {
|
|
119
|
+
let { scale: i, padLeft: a, padTop: o, original: s } = r, c = (t) => e[n * 4 + t] ?? 0, l = (e) => Math.min(Math.max(e, 0), s.width), u = (e) => Math.min(Math.max(e, 0), s.height);
|
|
120
|
+
return new t(l((c(0) - a) / i), u((c(1) - o) / i), l((c(2) - a) / i), u((c(3) - o) / i));
|
|
121
|
+
}
|
|
122
|
+
_classify(e, t, n) {
|
|
123
|
+
let { indices: r, values: i } = w(this._spec.applySoftmax ? C(e) : e, n ?? null), a = [];
|
|
124
|
+
for (let e = 0; e < r.length; e++) {
|
|
125
|
+
let t = r[e] ?? 0, n = i[e] ?? 0, o = this._classifierLabels[t] ?? `class_${t}`;
|
|
126
|
+
a.push({
|
|
127
|
+
classId: t,
|
|
128
|
+
className: o,
|
|
129
|
+
probability: n,
|
|
130
|
+
cls: t,
|
|
131
|
+
name: o,
|
|
132
|
+
conf: n
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
let o = a[0] ?? {
|
|
136
|
+
classId: 0,
|
|
137
|
+
className: "class_0",
|
|
138
|
+
probability: 0,
|
|
139
|
+
cls: 0,
|
|
140
|
+
name: "class_0",
|
|
141
|
+
conf: 0
|
|
142
|
+
};
|
|
143
|
+
return {
|
|
144
|
+
classId: o.classId,
|
|
145
|
+
className: o.className,
|
|
146
|
+
confidence: o.probability,
|
|
147
|
+
cls: o.classId,
|
|
148
|
+
name: o.className,
|
|
149
|
+
conf: o.probability,
|
|
150
|
+
image: t,
|
|
151
|
+
probabilities: a
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
function O(e) {
|
|
156
|
+
let t = e.outputNames.indexOf(h);
|
|
157
|
+
if (t < 0) return null;
|
|
158
|
+
let n = e.outputShapes[t];
|
|
159
|
+
return n === void 0 || n.length === 0 ? null : n[n.length - 1] ?? null;
|
|
160
|
+
}
|
|
161
|
+
function k(e) {
|
|
162
|
+
let t = {};
|
|
163
|
+
for (let n = 0; n < e.length; n++) t[n] = e[n];
|
|
164
|
+
return t;
|
|
165
|
+
}
|
|
166
|
+
function A(t, n) {
|
|
167
|
+
let r = t[n];
|
|
168
|
+
if (r === void 0) throw new e(`The fused pipeline is missing its '${n}' output.`);
|
|
169
|
+
return r;
|
|
170
|
+
}
|
|
171
|
+
function j(e, t) {
|
|
172
|
+
return A(e, t).data;
|
|
173
|
+
}
|
|
174
|
+
function M(e, t) {
|
|
175
|
+
let n = A(e, t).data, r = [];
|
|
176
|
+
for (let e = 0; e < n.length; e++) r.push(Number(n[e]));
|
|
177
|
+
return r;
|
|
178
|
+
}
|
|
179
|
+
function N(e) {
|
|
180
|
+
return b(v(y(e), e.width, e.height, 3), [
|
|
181
|
+
1,
|
|
182
|
+
3,
|
|
183
|
+
e.height,
|
|
184
|
+
e.width
|
|
185
|
+
]);
|
|
186
|
+
}
|
|
187
|
+
function P(e, t) {
|
|
188
|
+
let [r, i, a, o] = t.asIntXyxy(), s = Math.max(0, r), c = Math.max(0, i), l = Math.min(e.width, a), u = Math.min(e.height, o);
|
|
189
|
+
if (l <= s || u <= c) return new n(/* @__PURE__ */ new Uint8Array(), 0, 0);
|
|
190
|
+
let d = l - s, f = u - c, p = new Uint8Array(d * f * 3);
|
|
191
|
+
for (let t = 0; t < f; t++) {
|
|
192
|
+
let n = ((c + t) * e.width + s) * 3;
|
|
193
|
+
p.set(e.data.subarray(n, n + d * 3), t * d * 3);
|
|
194
|
+
}
|
|
195
|
+
return new n(p, d, f);
|
|
196
|
+
}
|
|
197
|
+
function F(e, t, n, r, i, a) {
|
|
198
|
+
return {
|
|
199
|
+
classId: e,
|
|
200
|
+
className: t,
|
|
201
|
+
confidence: n,
|
|
202
|
+
bbox: r,
|
|
203
|
+
cls: e,
|
|
204
|
+
name: t,
|
|
205
|
+
conf: n,
|
|
206
|
+
box: r,
|
|
207
|
+
croppedImage: i,
|
|
208
|
+
classification: a
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
function I(e, t) {
|
|
212
|
+
let n = new Float32Array(e.length * 4), r = new Int32Array(e.length), a = new Float32Array(e.length);
|
|
213
|
+
return e.forEach((e, t) => {
|
|
214
|
+
let [i, o, s, c] = e.bbox.xyxy;
|
|
215
|
+
n[t * 4] = i, n[t * 4 + 1] = o, n[t * 4 + 2] = s, n[t * 4 + 3] = c, r[t] = e.classId, a[t] = e.confidence;
|
|
216
|
+
}), new i(n, r, a, t);
|
|
217
|
+
}
|
|
218
|
+
//#endregion
|
|
219
|
+
export { D as DetectClassify };
|
|
220
|
+
|
|
221
|
+
//# sourceMappingURL=detectClassify.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"detectClassify.js","names":[],"sources":["../../../src/vision/tasks/detectClassify.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Run a fused detect→classify pipeline in the browser.\n *\n * The file this loads was built by the Python SDK's `ort_vision_sdk.compose`,\n * and it already contains both models plus the crop-and-resize bridge between\n * them. That matters far more in a tab than on a server: two models mean two\n * `.onnx` downloads, two WASM/WebGPU session initializations, and a per-crop\n * round trip through JavaScript to slice, resize and restack the regions before\n * the second model can see them. A fused pipeline has one download, one session\n * and no round trip — the crops are produced and consumed inside the graph.\n */\n\nimport type * as ort from \"onnxruntime-web\";\n\nimport { FusionError } from \"../core/exceptions\";\nimport { type ModelSource, type OrtSessionOptions, OrtSession } from \"../core/session\";\nimport { SpeedTimer } from \"../core/timing\";\nimport {\n INPUT_IMAGE,\n INPUT_PAD,\n INPUT_SCALE,\n INPUT_SOURCE,\n OUTPUT_BOXES,\n OUTPUT_CLASSES,\n OUTPUT_NUM_DETECTIONS,\n OUTPUT_PROBS,\n OUTPUT_SCORES,\n type FusionSpec,\n readFusionSpec,\n} from \"../fusion\";\nimport { type ImageInput, loadImage } from \"../io/image\";\nimport { type LabelSpec, resolveLabels } from \"../labels\";\nimport { softmax, topK } from \"../postprocess/classification\";\nimport { toCHW, toFloat32, toFloat32Tensor } from \"../preprocess/image\";\nimport { LetterboxPipeline, zeroTensorData } from \"../preprocess/pipeline\";\nimport { Boxes, DetectClassifyResults } from \"../results\";\nimport {\n BoundingBox,\n RGBImage,\n type ClassProbability,\n type ClassificationResult,\n type DetectionResult,\n} from \"../types\";\nimport { VisionTask, requireDetections } from \"./base\";\n\nexport interface DetectClassifyOptions extends OrtSessionOptions {\n /**\n * Class label spec for the **detection** stage — see {@link resolveLabels}.\n * Defaults to the names recorded at fusion time, falling back to the COCO\n * 80-class preset when the fusion recorded none.\n */\n readonly labels?: LabelSpec;\n /**\n * Class label spec for the **classification** stage. Defaults to the recorded\n * names, falling back to generated `class_<id>` names.\n */\n readonly classifierLabels?: LabelSpec;\n /**\n * If `true`, a run that finds nothing throws {@link NoDetectionsError}\n * instead of returning an empty envelope. Default `false`, because looking\n * and finding nothing is a successful inference. Turn it on when an empty\n * result means the surrounding pipeline should stop rather than carry on with\n * zero rows. Can be overridden per `predict` call.\n */\n readonly raiseOnEmpty?: boolean;\n}\n\nexport interface DetectClassifyPredictOptions {\n /**\n * Drop detections scoring below this. The graph's own NMS threshold was fixed\n * at fusion time and cannot be lowered here — this only filters further.\n */\n readonly confThreshold?: number;\n /** If set, keep only detections whose detector `classId` is in this list. */\n readonly classes?: readonly number[];\n /** Truncate each detection's `classification.probabilities` to its top-k entries. */\n readonly topK?: number;\n /** Override the constructor's `raiseOnEmpty` setting for this call. */\n readonly raiseOnEmpty?: boolean;\n}\n\n/**\n * Detector and classifier running as a single ONNX model.\n *\n * Everything the pipeline needs to know about itself — the resolution to\n * letterbox to, whether it wants the full-resolution image as well, whether its\n * classifier output still needs a softmax, the class names of both stages — was\n * written into the file at fusion time and is read back here. Nothing is\n * restated on the JavaScript side, so nothing can drift out of step with the\n * Python side that built it.\n *\n * @example\n * ```typescript\n * const pipeline = await DetectClassify.create(\"/models/pipeline.onnx\");\n * const result = (await pipeline.predict(\"/images/flock.jpg\"))[0];\n * for (const detection of result) {\n * console.log(detection.name, detection.conf, detection.classification?.name);\n * }\n * ```\n */\nexport class DetectClassify extends VisionTask {\n private constructor(\n session: OrtSession,\n private readonly _spec: FusionSpec,\n private readonly _labels: readonly string[],\n private readonly _names: Readonly<Record<number, string>>,\n private readonly _classifierLabels: readonly string[],\n private readonly _classifierNames: Readonly<Record<number, string>>,\n private readonly _raiseOnEmpty: boolean,\n ) {\n super(session);\n }\n\n private _pipelineCache: LetterboxPipeline | null = null;\n\n /**\n * Run the model once on zero-filled inputs, paying one-time costs up front.\n *\n * Worth more here than on a single-stage task: a fused pipeline is two models\n * plus the bridge in one graph, so the first inference compiles shaders for\n * all of it. Calling this while a loading spinner is still up moves that cost\n * somewhere the user is already waiting.\n *\n * @param runs How many warm-up inferences to run. One is enough for WASM;\n * WebGPU sometimes settles on the second.\n */\n async warmup(runs: number = 1): Promise<void> {\n const [width, height] = this._spec.inputSize;\n for (let i = 0; i < runs; i++) {\n const feeds: Record<string, ort.Tensor> = {\n [INPUT_IMAGE]: toFloat32Tensor(zeroTensorData(width, height), [\n 1,\n 3,\n height,\n width,\n ]),\n };\n if (this._spec.needsSourceImage) {\n feeds[INPUT_SOURCE] = toFloat32Tensor(zeroTensorData(width, height), [\n 1,\n 3,\n height,\n width,\n ]);\n feeds[INPUT_SCALE] = toFloat32Tensor(new Float32Array([1]), [1]);\n feeds[INPUT_PAD] = toFloat32Tensor(new Float32Array([0, 0]), [2]);\n }\n await this._session.run(feeds);\n }\n }\n\n /**\n * The fused preprocessing pipeline, built on first use.\n *\n * Lazily, because constructing it allocates canvases: a pipeline built in an\n * environment without a canvas implementation stays constructible, and only\n * fails if it is actually asked to preprocess something.\n */\n private get _pipeline(): LetterboxPipeline {\n if (this._pipelineCache === null) {\n const [width, height] = this._spec.inputSize;\n this._pipelineCache = new LetterboxPipeline(width, height);\n }\n return this._pipelineCache;\n }\n\n /**\n * Load a fused pipeline and resolve both label spaces.\n *\n * @param model The fused `.onnx` — a URL, an `ArrayBuffer`, or bytes.\n * @param options Label overrides plus the usual session options.\n * @throws {@link FusionError} when the model carries no pipeline metadata,\n * i.e. it is a plain detector or classifier rather than something\n * `ort_vision_sdk.compose` produced.\n */\n static async create(\n model: ModelSource,\n options: DetectClassifyOptions = {},\n ): Promise<DetectClassify> {\n const session = await OrtSession.create(model, options);\n const spec = readFusionSpec(session.metadata);\n if (spec === null) {\n throw new FusionError(\n \"This model carries no fused-pipeline metadata, so DetectClassify cannot tell how to \" +\n \"drive it. Build one with ort_vision_sdk.compose.fuse_detect_classify, or load a \" +\n \"plain model with Detector/Classifier instead.\",\n );\n }\n\n const labels = resolveLabels(options.labels ?? spec.detectorNames ?? \"coco\");\n const classifierLabels = resolveLabels(options.classifierLabels ?? spec.classifierNames, {\n numClasses: classifierClasses(session) ?? undefined,\n });\n return new DetectClassify(\n session,\n spec,\n labels,\n indexNames(labels),\n classifierLabels,\n indexNames(classifierLabels),\n options.raiseOnEmpty ?? false,\n );\n }\n\n /** The pipeline configuration recorded in the model at fusion time. */\n get spec(): FusionSpec {\n return this._spec;\n }\n\n /** The `[width, height]` the detection stage runs at. */\n get inputSize(): readonly [number, number] {\n return this._spec.inputSize;\n }\n\n /** Detector class labels indexed by class id. */\n get labels(): readonly string[] {\n return this._labels;\n }\n\n /** Detector class id → class name (matches Ultralytics' `model.names`). */\n get names(): Readonly<Record<number, string>> {\n return this._names;\n }\n\n /** Classifier class labels indexed by class id. */\n get classifierLabels(): readonly string[] {\n return this._classifierLabels;\n }\n\n /** Classifier class id → class name. */\n get classifierNames(): Readonly<Record<number, string>> {\n return this._classifierNames;\n }\n\n /**\n * Alias for {@link predict} — call the pipeline like a torch `nn.Module`.\n *\n * Use as `pipeline.call(img)` since JavaScript class instances are not\n * callable; for direct invocation, prefer `pipeline.predict(img)`.\n */\n async call(\n image: ImageInput,\n options: DetectClassifyPredictOptions = {},\n ): Promise<DetectClassifyResults[]> {\n return this.predict(image, options);\n }\n\n /**\n * Run the pipeline on a single image.\n *\n * The returned envelope carries a {@link Speed} breakdown in `speed`. Its\n * `inference` figure covers detection *and* classification, since the\n * pipeline runs them as one graph and no boundary between them is observable\n * from outside.\n */\n async predict(\n image: ImageInput,\n options: DetectClassifyPredictOptions = {},\n ): Promise<DetectClassifyResults[]> {\n const timer = new SpeedTimer();\n const path = typeof image === \"string\" ? image : null;\n const original = await loadImage(image);\n timer.stage(\"load\");\n const { feeds, scale, padLeft, padTop } = this._preprocess(original);\n timer.stage(\"preprocess\");\n const outputs = await this._session.run(feeds);\n this._pipeline.release();\n timer.stage(\"inference\");\n\n const probsTensor = output(outputs, OUTPUT_PROBS);\n const boxes = floats(outputs, OUTPUT_BOXES);\n const scores = floats(outputs, OUTPUT_SCORES);\n const classes = integers(outputs, OUTPUT_CLASSES);\n const probs = probsTensor.data as Float32Array;\n const reported = integers(outputs, OUTPUT_NUM_DETECTIONS)[0] ?? 0;\n const rows = Math.min(reported, Math.floor(boxes.length / 4));\n const classCount = probsTensor.dims[probsTensor.dims.length - 1] ?? 0;\n\n const allowed = options.classes === undefined ? null : new Set(options.classes);\n const floor = options.confThreshold ?? 0;\n const detections: DetectionResult[] = [];\n for (let row = 0; row < rows; row++) {\n const classId = classes[row] ?? 0;\n const confidence = scores[row] ?? 0;\n if (confidence < floor || (allowed !== null && !allowed.has(classId))) continue;\n\n const bbox = this._toOriginal(boxes, row, { scale, padLeft, padTop, original });\n const cropped = crop(original, bbox);\n detections.push(\n detection(\n classId,\n this._names[classId] ?? `class_${classId}`,\n confidence,\n bbox,\n cropped,\n this._classify(\n probs.subarray(row * classCount, (row + 1) * classCount),\n cropped,\n options.topK,\n ),\n ),\n );\n }\n\n requireDetections(detections.length, {\n raiseOnEmpty: options.raiseOnEmpty ?? this._raiseOnEmpty,\n confThreshold: Math.max(floor, this._spec.confThreshold),\n classes: options.classes,\n path,\n });\n\n const origShape: readonly [number, number] = [original.height, original.width];\n timer.stage(\"postprocess\");\n return [\n new DetectClassifyResults(\n bulkBoxes(detections, origShape),\n detections,\n this._names,\n this._classifierNames,\n original,\n origShape,\n path,\n timer.speed(),\n ),\n ];\n }\n\n /**\n * Letterbox the image and build the graph's feeds.\n *\n * The detector input runs through {@link LetterboxPipeline}, which fuses the\n * resize, the padding and the HWC-to-CHW float conversion into one\n * `drawImage` plus one readback, and reuses its output buffer between frames.\n * That buffer goes straight to ONNX Runtime, so `_pipeline.release()` must not\n * be called until the run resolves.\n *\n * A pipeline fused with `cropSource: \"original\"` also takes the untouched\n * image as a second input, plus the scale and padding of the letterbox — that\n * is what lets the graph undo the letterbox transform internally and crop at\n * native resolution instead of from the downscaled copy. That one is **not**\n * letterboxed by definition, so it does not go through the fused path.\n */\n private _preprocess(image: RGBImage): {\n feeds: Record<string, ort.Tensor>;\n scale: number;\n padLeft: number;\n padTop: number;\n } {\n const [width, height] = this._spec.inputSize;\n const boxed = this._pipeline.run(image);\n const feeds: Record<string, ort.Tensor> = {\n [INPUT_IMAGE]: toFloat32Tensor(boxed.data, [1, 3, height, width]),\n };\n\n if (this._spec.needsSourceImage) {\n feeds[INPUT_SOURCE] = tensorOf(image);\n feeds[INPUT_SCALE] = toFloat32Tensor(new Float32Array([boxed.scale]), [1]);\n feeds[INPUT_PAD] = toFloat32Tensor(\n new Float32Array([boxed.padLeft, boxed.padTop]),\n [2],\n );\n }\n return { feeds, scale: boxed.scale, padLeft: boxed.padLeft, padTop: boxed.padTop };\n }\n\n /**\n * Map one letterboxed xyxy row back onto the original image.\n *\n * The graph always reports boxes in the detector's letterboxed pixel space,\n * whichever crop source it was fused with, so both sources agree here.\n */\n private _toOriginal(\n boxes: Float32Array,\n row: number,\n context: {\n scale: number;\n padLeft: number;\n padTop: number;\n original: RGBImage;\n },\n ): BoundingBox {\n const { scale, padLeft, padTop, original } = context;\n const at = (offset: number): number => boxes[row * 4 + offset] ?? 0;\n const clampX = (value: number): number => Math.min(Math.max(value, 0), original.width);\n const clampY = (value: number): number => Math.min(Math.max(value, 0), original.height);\n return new BoundingBox(\n clampX((at(0) - padLeft) / scale),\n clampY((at(1) - padTop) / scale),\n clampX((at(2) - padLeft) / scale),\n clampY((at(3) - padTop) / scale),\n );\n }\n\n /**\n * Turn one row of the classifier output into a result object.\n *\n * @param row The output row for this detection.\n * @param image The crop the row describes, carried so callers can display\n * what was classified.\n * @param k Optional truncation of the probability list.\n */\n private _classify(\n row: Float32Array,\n image: RGBImage,\n k: number | undefined,\n ): ClassificationResult {\n const scores = this._spec.applySoftmax ? softmax(row) : row;\n const { indices, values } = topK(scores, k ?? null);\n const probabilities: ClassProbability[] = [];\n for (let i = 0; i < indices.length; i++) {\n const classId = indices[i] ?? 0;\n const probability = values[i] ?? 0;\n const className = this._classifierLabels[classId] ?? `class_${classId}`;\n probabilities.push({\n classId,\n className,\n probability,\n cls: classId,\n name: className,\n conf: probability,\n });\n }\n const top = probabilities[0] ?? {\n classId: 0,\n className: \"class_0\",\n probability: 0,\n cls: 0,\n name: \"class_0\",\n conf: 0,\n };\n return {\n classId: top.classId,\n className: top.className,\n confidence: top.probability,\n cls: top.classId,\n name: top.className,\n conf: top.probability,\n image,\n probabilities,\n };\n }\n}\n\n/**\n * Read the classifier stage's class count off the `probs` output shape.\n *\n * @param session The loaded pipeline session.\n * @returns The class count, or `null` when the graph leaves that axis dynamic\n * or declares no `probs` output — in which case label resolution falls back\n * to whatever the fusion recorded.\n */\nfunction classifierClasses(session: OrtSession): number | null {\n const index = session.outputNames.indexOf(OUTPUT_PROBS);\n if (index < 0) return null;\n const shape = session.outputShapes[index];\n if (shape === undefined || shape.length === 0) return null;\n return shape[shape.length - 1] ?? null;\n}\n\n/**\n * Build a class id → name record from an ordered label list.\n *\n * @param labels Labels indexed by class id.\n * @returns The equivalent record.\n */\nfunction indexNames(labels: readonly string[]): Readonly<Record<number, string>> {\n const names: Record<number, string> = {};\n for (let i = 0; i < labels.length; i++) names[i] = labels[i] as string;\n return names;\n}\n\n/**\n * Fetch an output tensor by name.\n *\n * @param outputs The run's results.\n * @param name The output's name in the pipeline contract.\n * @returns The tensor.\n * @throws {@link FusionError} when the graph does not carry that output, which\n * means the file is not a pipeline this version can drive.\n */\nfunction output(outputs: Record<string, ort.Tensor>, name: string): ort.Tensor {\n const tensor = outputs[name];\n if (tensor === undefined) {\n throw new FusionError(`The fused pipeline is missing its '${name}' output.`);\n }\n return tensor;\n}\n\n/**\n * Fetch a float output by name.\n *\n * @param outputs The run's results.\n * @param name The output's name in the pipeline contract.\n * @returns The output's data.\n * @throws {@link FusionError} when the graph does not carry that output.\n */\nfunction floats(outputs: Record<string, ort.Tensor>, name: string): Float32Array {\n return output(outputs, name).data as Float32Array;\n}\n\n/**\n * Fetch an integer output by name, normalizing ORT's 64-bit representation.\n *\n * ONNX Runtime Web returns `int64` tensors as `BigInt64Array`, whose values do\n * not compare or index like numbers. Class ids and detection counts are always\n * small, so widening them to `number` here is lossless and keeps every caller\n * free of `BigInt` handling.\n *\n * @param outputs The run's results.\n * @param name The output's name in the pipeline contract.\n * @returns The output's values as plain numbers.\n * @throws {@link FusionError} when the graph does not carry that output.\n */\nfunction integers(outputs: Record<string, ort.Tensor>, name: string): number[] {\n const data = output(outputs, name).data as BigInt64Array | Int32Array | Float32Array;\n const values: number[] = [];\n for (let i = 0; i < data.length; i++) values.push(Number(data[i]));\n return values;\n}\n\n/**\n * Convert an image to the `[1, 3, H, W]` float32 tensor the graph expects.\n *\n * @param image The image to convert.\n * @returns The batched CHW tensor, scaled to `[0, 1]`.\n */\nfunction tensorOf(image: RGBImage): ort.Tensor {\n const chw = toCHW(toFloat32(image), image.width, image.height, 3);\n return toFloat32Tensor(chw, [1, 3, image.height, image.width]);\n}\n\n/**\n * Cut the box region out of the original image.\n *\n * @param image The source image.\n * @param bbox The box, in original-image pixel coordinates.\n * @returns The cropped region, or a zero-sized image for a box with no area.\n */\nfunction crop(image: RGBImage, bbox: BoundingBox): RGBImage {\n const [rawX1, rawY1, rawX2, rawY2] = bbox.asIntXyxy();\n const x1 = Math.max(0, rawX1);\n const y1 = Math.max(0, rawY1);\n const x2 = Math.min(image.width, rawX2);\n const y2 = Math.min(image.height, rawY2);\n if (x2 <= x1 || y2 <= y1) return new RGBImage(new Uint8Array(0), 0, 0);\n\n const width = x2 - x1;\n const height = y2 - y1;\n const out = new Uint8Array(width * height * 3);\n for (let row = 0; row < height; row++) {\n const offset = ((y1 + row) * image.width + x1) * 3;\n out.set(image.data.subarray(offset, offset + width * 3), row * width * 3);\n }\n return new RGBImage(out, width, height);\n}\n\n/**\n * Assemble one detection, filling the Ultralytics-style aliases.\n *\n * @param classId Detector class index.\n * @param className Detector class name.\n * @param confidence Detection score.\n * @param bbox Box in original-image coordinates.\n * @param croppedImage The region the box covers.\n * @param classification What the classification stage said about that region.\n * @returns The detection object.\n */\nfunction detection(\n classId: number,\n className: string,\n confidence: number,\n bbox: BoundingBox,\n croppedImage: RGBImage,\n classification: ClassificationResult,\n): DetectionResult {\n return {\n classId,\n className,\n confidence,\n bbox,\n cls: classId,\n name: className,\n conf: confidence,\n box: bbox,\n croppedImage,\n classification,\n };\n}\n\n/**\n * Assemble the bulk-array `Boxes` view from per-instance detections.\n *\n * @param detections The surviving detections.\n * @param origShape `[height, width]` of the original image.\n * @returns The bulk view, empty when nothing survived.\n */\nfunction bulkBoxes(\n detections: readonly DetectionResult[],\n origShape: readonly [number, number],\n): Boxes {\n const xyxy = new Float32Array(detections.length * 4);\n const cls = new Int32Array(detections.length);\n const conf = new Float32Array(detections.length);\n detections.forEach((entry, index) => {\n const [x1, y1, x2, y2] = entry.bbox.xyxy;\n xyxy[index * 4] = x1;\n xyxy[index * 4 + 1] = y1;\n xyxy[index * 4 + 2] = x2;\n xyxy[index * 4 + 3] = y2;\n cls[index] = entry.classId;\n conf[index] = entry.confidence;\n });\n return new Boxes(xyxy, cls, conf, origShape);\n}\n"],"mappings":";;;;;;;;;;;;;AAqGA,IAAa,IAAb,MAAa,UAAuB,EAAW;CAGtB;CACA;CACA;CACA;CACA;CACA;CAPrB,YACI,GACA,GACA,GACA,GACA,GACA,GACA,GACF;EADmB,AAEjB,MAAM,CAAO,GAPI,KAAA,QAAA,GACA,KAAA,UAAA,GACA,KAAA,SAAA,GACA,KAAA,oBAAA,GACA,KAAA,mBAAA,GACA,KAAA,gBAAA;CAGrB;CAEA,iBAAmD;CAanD,MAAM,OAAO,IAAe,GAAkB;EAC1C,IAAM,CAAC,GAAO,KAAU,KAAK,MAAM;EACnC,KAAK,IAAI,IAAI,GAAG,IAAI,GAAM,KAAK;GAC3B,IAAM,IAAoC,GACrC,IAAc,EAAgB,EAAe,GAAO,CAAM,GAAG;IAC1D;IACA;IACA;IACA;GACJ,CAAC,EACL;GAWA,AAVI,KAAK,MAAM,qBACX,EAAM,KAAgB,EAAgB,EAAe,GAAO,CAAM,GAAG;IACjE;IACA;IACA;IACA;GACJ,CAAC,GACD,EAAM,KAAe,EAAgB,IAAI,aAAa,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAC/D,EAAM,KAAa,EAAgB,IAAI,aAAa,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAEpE,MAAM,KAAK,SA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ALA,EAAK,IAAQ,KAAK,GAClB,EAAK,IAAQ,IAAI,KAAK,GACtB,EAAK,IAAQ,IAAI,KAAK,GACtB,EAAK,IAAQ,IAAI,KAAK,GACtB,EAAI,KAAS,EAAM,SACnB,EAAK,KAAS,EAAM;CACxB,CAAC,GACM,IAAI,EAAM,GAAM,GAAK,GAAM,CAAS;AAC/C"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("../types.cjs"),t=require("../core/timing.cjs"),n=require("../results.cjs"),r=require("../labels.cjs"),i=require("../core/graph.cjs"),a=require("../core/metadata.cjs"),o=require("../core/session.cjs"),s=require("../io/image.cjs"),c=require("../preprocess/image.cjs"),l=require("../postprocess/detection.cjs"),
|
|
1
|
+
const e=require("../types.cjs"),t=require("../core/timing.cjs"),n=require("../results.cjs"),r=require("../labels.cjs"),i=require("../core/graph.cjs"),a=require("../core/metadata.cjs"),o=require("../core/session.cjs"),s=require("../io/image.cjs"),c=require("../preprocess/image.cjs"),l=require("../preprocess/pipeline.cjs"),u=require("../postprocess/detection.cjs"),d=require("./base.cjs");var f=class f extends d.VisionTask{_head;_labels;_names;_inputSize;_confThreshold;_iouThreshold;_maxDetections;_raiseOnEmpty;constructor(e,t,n,r,i,a,o,s,c){super(e),this._head=t,this._labels=n,this._names=r,this._inputSize=i,this._confThreshold=a,this._iouThreshold=o,this._maxDetections=s,this._raiseOnEmpty=c}_pipelineCache=null;async warmup(e=1){let[t,n]=this._inputSize;for(let r=0;r<e;r++){let e=c.toFloat32Tensor(l.zeroTensorData(t,n),[1,3,n,t]);await this._session.run({[this._session.inputName]:e})}}get _pipeline(){return this._pipelineCache===null&&(this._pipelineCache=new l.LetterboxPipeline(this._inputSize[0],this._inputSize[1])),this._pipelineCache}static async create(e,t={}){let n=t.head??`yolo`;if(n!==`yolo`)throw Error(`Unsupported detector head '${n}'. Supported: 'yolo'.`);let s=await o.OrtSession.create(e,t),c=t.numClasses??i.detectionNumClasses(s.outputShape)??void 0,l=r.resolveLabels(t.labels??a.modelNames(s.metadata)??r.defaultLabels(c),{numClasses:c}),u={};for(let e=0;e<l.length;e++)u[e]=l[e];return new f(s,n,l,u,i.resolveInputSize({graphShape:s.inputShape,requested:t.inputSize,fallback:[640,640]}),t.confThreshold??.25,t.iouThreshold??.45,t.maxDetections??300,t.raiseOnEmpty??!1)}get head(){return this._head}get labels(){return this._labels}get names(){return this._names}get inputSize(){return this._inputSize}get numClasses(){return this._labels.length}async call(e,t={}){return this.predict(e,t)}async predict(e,r={}){let i=new t.SpeedTimer,a=typeof e==`string`?e:null,o=await s.loadImage(e);i.stage(`load`);let{tensor:c,scale:l,padLeft:f,padTop:p}=this._preprocess(o);i.stage(`preprocess`);let m=await this._session.run({[this._session.inputName]:c});this._pipeline.release(),i.stage(`inference`);let h=this._session.outputNames[0];if(h===void 0)throw Error(`Detector model has no outputs.`);let g=m[h];if(g===void 0)throw Error(`Detector model output ${h} missing from run() result.`);let _=r.confThreshold??this._confThreshold,v=u.decodeYolo(g.data,g.dims,{originalWidth:o.width,originalHeight:o.height,padLeft:f,padTop:p,scale:l,confThreshold:_,iouThreshold:r.iouThreshold??this._iouThreshold,maxDetections:this._maxDetections}),y=r.classes===void 0?v:(()=>{let e=new Set(r.classes);return v.filter(t=>e.has(t.classId))})();d.requireDetections(y.length,{raiseOnEmpty:r.raiseOnEmpty??this._raiseOnEmpty,confThreshold:_,classes:r.classes,path:a});let b=y.map(e=>this._buildResult(o,e.bbox,e.classId,e.confidence)),x=[o.height,o.width],S=this._buildBoxes(b,x);return i.stage(`postprocess`),[new n.DetectionResults(S,b,this._names,o,x,a,i.speed())]}_preprocess(e){let[t,n]=this._inputSize,r=this._pipeline.run(e);return{tensor:c.toFloat32Tensor(r.data,[1,3,n,t]),scale:r.scale,padLeft:r.padLeft,padTop:r.padTop}}_buildResult(t,n,r,i){let[a,o,s,c]=n.asIntXyxy(),l=Math.max(0,a),u=Math.max(0,o),d=Math.min(t.width,s),f=Math.min(t.height,c),p;if(d>l&&f>u){let n=d-l,r=f-u,i=new Uint8Array(n*r*3);for(let e=0;e<r;e++){let r=((u+e)*t.width+l)*3;i.set(t.data.subarray(r,r+n*3),e*n*3)}p=new e.RGBImage(i,n,r)}else p=new e.RGBImage(new Uint8Array,0,0);let m=this._names[r]??`class_${r}`;return{classId:r,className:m,confidence:i,bbox:n,cls:r,name:m,conf:i,box:n,croppedImage:p}}_buildBoxes(e,t){let r=e.length,i=new Float32Array(r*4),a=new Int32Array(r),o=new Float32Array(r);for(let t=0;t<r;t++){let n=e[t];i[t*4]=n.bbox.x1,i[t*4+1]=n.bbox.y1,i[t*4+2]=n.bbox.x2,i[t*4+3]=n.bbox.y2,a[t]=n.classId,o[t]=n.confidence}return new n.Boxes(i,a,o,t)}};exports.Detector=f;
|
|
2
2
|
//# sourceMappingURL=detector.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"detector.cjs","names":[],"sources":["../../../src/vision/tasks/detector.ts"],"sourcesContent":["/**\n * Object detection task using anchor-free YOLO ONNX models (v8/v9/v10/v11/v12).\n */\n\nimport type * as ort from \"onnxruntime-web\";\n\nimport { type ModelSource, type OrtSessionOptions, OrtSession } from \"../core/session\";\nimport { SpeedTimer } from \"../core/timing\";\n\nimport { type ImageInput, loadImage } from \"../io/image\";\nimport { detectionNumClasses, resolveInputSize } from \"../core/graph\";\nimport { modelNames } from \"../core/metadata\";\nimport { type LabelSpec, resolveLabels } from \"../labels\";\nimport { decodeYolo } from \"../postprocess/detection\";\nimport { letterbox, toCHW, toFloat32, toFloat32Tensor } from \"../preprocess/image\";\nimport { Boxes, DetectionResults } from \"../results\";\nimport { VisionTask } from \"./base\";\nimport { type BoundingBox, type DetectionResult, RGBImage } from \"../types\";\n\n/**\n * Decoder family for the model's detection head.\n *\n * - `\"yolo\"`: anchor-free YOLO head with output shape `[1, 4 + nc, N]` —\n * covers YOLOv8, v9, v10, v11, v12, v26 detect exports.\n *\n * The SDK does **not** auto-detect the head from the model — the caller is\n * responsible for picking a head that matches their export. Future families\n * (v5/v6/v7 with `[1, N, 5+nc]`) will be added as new literal members.\n */\nexport type DetectorHead = \"yolo\";\n\nexport interface DetectorOptions extends OrtSessionOptions {\n /**\n * Decoder family for the detection head. Default `\"yolo\"` covers\n * YOLOv8/v9/v10/v11/v12/v26.\n */\n readonly head?: DetectorHead;\n /** Class label spec — see {@link resolveLabels}. Defaults to the COCO 80-class preset. */\n readonly labels?: LabelSpec;\n /** Number of classes — used to validate the supplied labels. */\n readonly numClasses?: number;\n /**\n * Model input `[width, height]` in pixels for letterboxing.\n *\n * Only used when the model's graph leaves its spatial axes dynamic: a graph\n * that declares a static size always wins, since that is the only shape ONNX\n * Runtime will accept. Defaults to `[640, 640]`.\n */\n readonly inputSize?: readonly [number, number];\n /** Default minimum class score to keep a candidate. */\n readonly confThreshold?: number;\n /** Default IoU threshold for non-maximum suppression. */\n readonly iouThreshold?: number;\n /** Maximum number of detections per image. */\n readonly maxDetections?: number;\n}\n\nexport interface DetectorPredictOptions {\n /** Override the default confidence threshold. */\n readonly confThreshold?: number;\n /** Override the default IoU threshold. */\n readonly iouThreshold?: number;\n /**\n * If set, keep only detections whose `classId` is in this list.\n * Mirrors Ultralytics' `model.predict(img, classes=[0, 16])`.\n */\n readonly classes?: readonly number[];\n}\n\n/**\n * Object detector for anchor-free YOLO ONNX models (v8/v9/v10/v11/v12).\n *\n * `predict()` returns `Promise<DetectionResults[]>` (length 1 for a single\n * image), mirroring Ultralytics' `YOLO(\"img.jpg\")`. Iterate the envelope for\n * per-instance dataclasses, or use the bulk `boxes` view (`.xyxy`, `.xywh`,\n * `.xyxyn`, `.xywhn`, `.cls`, `.conf`).\n *\n * @example\n * ```typescript\n * const det = await Detector.create(\"/models/yolov8n.onnx\");\n * const results = await det.predict(\"/images/street.jpg\");\n * const r = results[0];\n * console.log(r.boxes.xyxy, r.boxes.cls, r.boxes.conf, r.names);\n * for (const d of r) {\n * console.log(d.cls, d.conf, d.box.xyxy);\n * }\n * ```\n */\nexport class Detector extends VisionTask {\n private constructor(\n session: OrtSession,\n private readonly _head: DetectorHead,\n private readonly _labels: readonly string[],\n private readonly _names: Readonly<Record<number, string>>,\n private readonly _inputSize: readonly [number, number],\n private readonly _confThreshold: number,\n private readonly _iouThreshold: number,\n private readonly _maxDetections: number,\n ) {\n super(session);\n }\n\n /** Load the model and resolve labels. */\n static async create(model: ModelSource, options: DetectorOptions = {}): Promise<Detector> {\n const head: DetectorHead = options.head ?? \"yolo\";\n if (head !== \"yolo\") {\n throw new Error(`Unsupported detector head '${head}'. Supported: 'yolo'.`);\n }\n const session = await OrtSession.create(model, options);\n const labels = resolveLabels(options.labels ?? modelNames(session.metadata) ?? \"coco\", {\n numClasses: options.numClasses ?? detectionNumClasses(session.outputShape) ?? undefined,\n });\n const names: Record<number, string> = {};\n for (let i = 0; i < labels.length; i++) {\n names[i] = labels[i] as string;\n }\n return new Detector(\n session,\n head,\n labels,\n names,\n resolveInputSize({\n graphShape: session.inputShape,\n requested: options.inputSize,\n fallback: [640, 640],\n }),\n options.confThreshold ?? 0.25,\n options.iouThreshold ?? 0.45,\n options.maxDetections ?? 300,\n );\n }\n\n /** The decoder family used to interpret the model's output. */\n get head(): DetectorHead {\n return this._head;\n }\n\n /** Class labels indexed by class id. */\n get labels(): readonly string[] {\n return this._labels;\n }\n\n /** Class id → class name dict (matches Ultralytics' `model.names`). */\n get names(): Readonly<Record<number, string>> {\n return this._names;\n }\n\n /**\n * The `[width, height]` this task preprocesses to.\n *\n * Resolved at creation time from the model's graph when it declares a static\n * input, so reading it back tells you the resolution inference really runs at\n * — not merely what was requested.\n */\n get inputSize(): readonly [number, number] {\n return this._inputSize;\n }\n\n /** Number of classes the model predicts. */\n get numClasses(): number {\n return this._labels.length;\n }\n\n /**\n * Alias for {@link predict} — call the detector like a torch `nn.Module`.\n *\n * Use as `det.call(img)` since JavaScript class instances are not callable;\n * for direct invocation, prefer `det.predict(img)`. The full\n * {@link DetectorPredictOptions} (including `classes`) is supported.\n */\n async call(\n image: ImageInput,\n options: DetectorPredictOptions = {},\n ): Promise<DetectionResults[]> {\n return this.predict(image, options);\n }\n\n /**\n * Run detection on a single image.\n *\n * The returned envelope carries a {@link Speed} breakdown in `speed`,\n * mirroring Ultralytics' `results[0].speed`.\n */\n async predict(\n image: ImageInput,\n options: DetectorPredictOptions = {},\n ): Promise<DetectionResults[]> {\n const timer = new SpeedTimer();\n const path = typeof image === \"string\" ? image : null;\n const original = await loadImage(image);\n timer.stage(\"load\");\n const { tensor, scale, padLeft, padTop } = this._preprocess(original);\n timer.stage(\"preprocess\");\n const outputs = await this._session.run({ [this._session.inputName]: tensor });\n timer.stage(\"inference\");\n\n const firstOutputName = this._session.outputNames[0];\n if (firstOutputName === undefined) {\n throw new Error(\"Detector model has no outputs.\");\n }\n const raw = outputs[firstOutputName];\n if (raw === undefined) {\n throw new Error(`Detector model output ${firstOutputName} missing from run() result.`);\n }\n\n const decodedAll = decodeYolo(raw.data as Float32Array, raw.dims, {\n originalWidth: original.width,\n originalHeight: original.height,\n padLeft,\n padTop,\n scale,\n confThreshold: options.confThreshold ?? this._confThreshold,\n iouThreshold: options.iouThreshold ?? this._iouThreshold,\n maxDetections: this._maxDetections,\n });\n\n const decoded =\n options.classes !== undefined\n ? (() => {\n const allowed = new Set(options.classes);\n return decodedAll.filter((d) => allowed.has(d.classId));\n })()\n : decodedAll;\n\n const detections = decoded.map((d) =>\n this._buildResult(original, d.bbox, d.classId, d.confidence),\n );\n\n const orig: readonly [number, number] = [original.height, original.width];\n const boxes = this._buildBoxes(detections, orig);\n timer.stage(\"postprocess\");\n return [\n new DetectionResults(\n boxes,\n detections,\n this._names,\n original,\n orig,\n path,\n timer.speed(),\n ),\n ];\n }\n\n private _preprocess(image: RGBImage): {\n tensor: ort.Tensor;\n scale: number;\n padLeft: number;\n padTop: number;\n } {\n const [tw, th] = this._inputSize;\n const lb = letterbox(image, tw, th);\n const f32 = toFloat32(lb.image);\n const chw = toCHW(f32, lb.image.width, lb.image.height, 3);\n return {\n tensor: toFloat32Tensor(chw, [1, 3, lb.image.height, lb.image.width]),\n scale: lb.scale,\n padLeft: lb.padLeft,\n padTop: lb.padTop,\n };\n }\n\n private _buildResult(\n original: RGBImage,\n bbox: BoundingBox,\n classId: number,\n confidence: number,\n ): DetectionResult {\n const [x1, y1, x2, y2] = bbox.asIntXyxy();\n const cx1 = Math.max(0, x1);\n const cy1 = Math.max(0, y1);\n const cx2 = Math.min(original.width, x2);\n const cy2 = Math.min(original.height, y2);\n\n let cropped: RGBImage;\n if (cx2 > cx1 && cy2 > cy1) {\n const cw = cx2 - cx1;\n const ch = cy2 - cy1;\n const out = new Uint8Array(cw * ch * 3);\n for (let row = 0; row < ch; row++) {\n const srcOffset = ((cy1 + row) * original.width + cx1) * 3;\n out.set(original.data.subarray(srcOffset, srcOffset + cw * 3), row * cw * 3);\n }\n cropped = new RGBImage(out, cw, ch);\n } else {\n cropped = new RGBImage(new Uint8Array(0), 0, 0);\n }\n\n const className = this._names[classId] ?? `class_${classId}`;\n\n return {\n classId,\n className,\n confidence,\n bbox,\n cls: classId,\n name: className,\n conf: confidence,\n box: bbox,\n croppedImage: cropped,\n };\n }\n\n private _buildBoxes(\n detections: readonly DetectionResult[],\n origShape: readonly [number, number],\n ): Boxes {\n const n = detections.length;\n const xyxy = new Float32Array(n * 4);\n const cls = new Int32Array(n);\n const conf = new Float32Array(n);\n for (let i = 0; i < n; i++) {\n const d = detections[i] as DetectionResult;\n xyxy[i * 4] = d.bbox.x1;\n xyxy[i * 4 + 1] = d.bbox.y1;\n xyxy[i * 4 + 2] = d.bbox.x2;\n xyxy[i * 4 + 3] = d.bbox.y2;\n cls[i] = d.classId;\n conf[i] = d.confidence;\n }\n return new Boxes(xyxy, cls, conf, origShape);\n }\n}\n"],"mappings":"6VAwFA,IAAa,EAAb,MAAa,UAAiB,EAAA,UAAW,CAGhB,MACA,QACA,OACA,WACA,eACA,cACA,eARrB,YACI,EACA,EACA,EACA,EACA,EACA,EAC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B,EAAK,EAAI,GAAK,EAAE,KAAK,GACrB,EAAK,EAAI,EAAI,GAAK,EAAE,KAAK,GACzB,EAAK,EAAI,EAAI,GAAK,EAAE,KAAK,GACzB,EAAK,EAAI,EAAI,GAAK,EAAE,KAAK,GACzB,EAAI,GAAK,EAAE,QACX,EAAK,GAAK,EAAE,UAChB,CACA,OAAO,IAAI,EAAA,MAAM,EAAM,EAAK,EAAM,CAAS,CAC/C,CACJ"}
|
|
1
|
+
{"version":3,"file":"detector.cjs","names":[],"sources":["../../../src/vision/tasks/detector.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Object detection task using anchor-free YOLO ONNX models (v8/v9/v10/v11/v12).\n */\n\nimport type * as ort from \"onnxruntime-web\";\n\nimport { type ModelSource, type OrtSessionOptions, OrtSession } from \"../core/session\";\nimport { SpeedTimer } from \"../core/timing\";\n\nimport { type ImageInput, loadImage } from \"../io/image\";\nimport { detectionNumClasses, resolveInputSize } from \"../core/graph\";\nimport { modelNames } from \"../core/metadata\";\nimport { type LabelSpec, defaultLabels, resolveLabels } from \"../labels\";\nimport { decodeYolo } from \"../postprocess/detection\";\nimport { toFloat32Tensor } from \"../preprocess/image\";\nimport { LetterboxPipeline, zeroTensorData } from \"../preprocess/pipeline\";\nimport { Boxes, DetectionResults } from \"../results\";\nimport { VisionTask, requireDetections } from \"./base\";\nimport { type BoundingBox, type DetectionResult, RGBImage } from \"../types\";\n\n/**\n * Decoder family for the model's detection head.\n *\n * - `\"yolo\"`: anchor-free YOLO head with output shape `[1, 4 + nc, N]` —\n * covers YOLOv8, v9, v10, v11, v12, v26 detect exports.\n *\n * The SDK does **not** auto-detect the head from the model — the caller is\n * responsible for picking a head that matches their export. Future families\n * (v5/v6/v7 with `[1, N, 5+nc]`) will be added as new literal members.\n */\nexport type DetectorHead = \"yolo\";\n\nexport interface DetectorOptions extends OrtSessionOptions {\n /**\n * Decoder family for the detection head. Default `\"yolo\"` covers\n * YOLOv8/v9/v10/v11/v12/v26.\n */\n readonly head?: DetectorHead;\n /** Class label spec — see {@link resolveLabels}. Defaults to the COCO 80-class preset. */\n readonly labels?: LabelSpec;\n /** Number of classes — used to validate the supplied labels. */\n readonly numClasses?: number;\n /**\n * Model input `[width, height]` in pixels for letterboxing.\n *\n * Only used when the model's graph leaves its spatial axes dynamic: a graph\n * that declares a static size always wins, since that is the only shape ONNX\n * Runtime will accept. Defaults to `[640, 640]`.\n */\n readonly inputSize?: readonly [number, number];\n /** Default minimum class score to keep a candidate. */\n readonly confThreshold?: number;\n /** Default IoU threshold for non-maximum suppression. */\n readonly iouThreshold?: number;\n /** Maximum number of detections per image. */\n readonly maxDetections?: number;\n /**\n * If `true`, a run that finds nothing throws {@link NoDetectionsError}\n * instead of returning an empty envelope. Default `false`, because looking\n * and finding nothing is a successful inference. Turn it on when an empty\n * result means the surrounding pipeline should stop rather than carry on with\n * zero rows. Can be overridden per `predict` call.\n */\n readonly raiseOnEmpty?: boolean;\n}\n\nexport interface DetectorPredictOptions {\n /** Override the default confidence threshold. */\n readonly confThreshold?: number;\n /** Override the default IoU threshold. */\n readonly iouThreshold?: number;\n /**\n * If set, keep only detections whose `classId` is in this list.\n * Mirrors Ultralytics' `model.predict(img, classes=[0, 16])`.\n */\n readonly classes?: readonly number[];\n /** Override the constructor's `raiseOnEmpty` setting for this call. */\n readonly raiseOnEmpty?: boolean;\n}\n\n/**\n * Object detector for anchor-free YOLO ONNX models (v8/v9/v10/v11/v12).\n *\n * `predict()` returns `Promise<DetectionResults[]>` (length 1 for a single\n * image), mirroring Ultralytics' `YOLO(\"img.jpg\")`. Iterate the envelope for\n * per-instance dataclasses, or use the bulk `boxes` view (`.xyxy`, `.xywh`,\n * `.xyxyn`, `.xywhn`, `.cls`, `.conf`).\n *\n * @example\n * ```typescript\n * const det = await Detector.create(\"/models/yolov8n.onnx\");\n * const results = await det.predict(\"/images/street.jpg\");\n * const r = results[0];\n * console.log(r.boxes.xyxy, r.boxes.cls, r.boxes.conf, r.names);\n * for (const d of r) {\n * console.log(d.cls, d.conf, d.box.xyxy);\n * }\n * ```\n */\nexport class Detector extends VisionTask {\n private constructor(\n session: OrtSession,\n private readonly _head: DetectorHead,\n private readonly _labels: readonly string[],\n private readonly _names: Readonly<Record<number, string>>,\n private readonly _inputSize: readonly [number, number],\n private readonly _confThreshold: number,\n private readonly _iouThreshold: number,\n private readonly _maxDetections: number,\n private readonly _raiseOnEmpty: boolean,\n ) {\n super(session);\n }\n\n private _pipelineCache: LetterboxPipeline | null = null;\n\n /**\n * Run the model once on a zero-filled tensor, paying one-time costs up front.\n *\n * The first inference of a session is not representative: WebGPU compiles its\n * shaders on it and the WASM backend faults in its arenas, which on a phone\n * can turn the first frame into seconds while every later frame is tens of\n * milliseconds. Calling this while a loading spinner is still up moves that\n * cost somewhere the user is already waiting.\n *\n * @param runs How many warm-up inferences to run. One is enough for WASM;\n * WebGPU sometimes settles on the second.\n */\n async warmup(runs: number = 1): Promise<void> {\n const [tw, th] = this._inputSize;\n for (let i = 0; i < runs; i++) {\n const tensor = toFloat32Tensor(zeroTensorData(tw, th), [1, 3, th, tw]);\n await this._session.run({ [this._session.inputName]: tensor });\n }\n }\n\n /**\n * The fused preprocessing pipeline, built on first use.\n *\n * Lazily, because constructing it allocates canvases: a task built in an\n * environment without a canvas implementation stays constructible, and only\n * fails if it is actually asked to preprocess something.\n */\n private get _pipeline(): LetterboxPipeline {\n if (this._pipelineCache === null) {\n this._pipelineCache = new LetterboxPipeline(this._inputSize[0], this._inputSize[1]);\n }\n return this._pipelineCache;\n }\n\n /** Load the model and resolve labels. */\n static async create(model: ModelSource, options: DetectorOptions = {}): Promise<Detector> {\n const head: DetectorHead = options.head ?? \"yolo\";\n if (head !== \"yolo\") {\n throw new Error(`Unsupported detector head '${head}'. Supported: 'yolo'.`);\n }\n const session = await OrtSession.create(model, options);\n const numClasses =\n options.numClasses ?? detectionNumClasses(session.outputShape) ?? undefined;\n const labels = resolveLabels(\n options.labels ?? modelNames(session.metadata) ?? defaultLabels(numClasses),\n { numClasses },\n );\n const names: Record<number, string> = {};\n for (let i = 0; i < labels.length; i++) {\n names[i] = labels[i] as string;\n }\n return new Detector(\n session,\n head,\n labels,\n names,\n resolveInputSize({\n graphShape: session.inputShape,\n requested: options.inputSize,\n fallback: [640, 640],\n }),\n options.confThreshold ?? 0.25,\n options.iouThreshold ?? 0.45,\n options.maxDetections ?? 300,\n options.raiseOnEmpty ?? false,\n );\n }\n\n /** The decoder family used to interpret the model's output. */\n get head(): DetectorHead {\n return this._head;\n }\n\n /** Class labels indexed by class id. */\n get labels(): readonly string[] {\n return this._labels;\n }\n\n /** Class id → class name dict (matches Ultralytics' `model.names`). */\n get names(): Readonly<Record<number, string>> {\n return this._names;\n }\n\n /**\n * The `[width, height]` this task preprocesses to.\n *\n * Resolved at creation time from the model's graph when it declares a static\n * input, so reading it back tells you the resolution inference really runs at\n * — not merely what was requested.\n */\n get inputSize(): readonly [number, number] {\n return this._inputSize;\n }\n\n /** Number of classes the model predicts. */\n get numClasses(): number {\n return this._labels.length;\n }\n\n /**\n * Alias for {@link predict} — call the detector like a torch `nn.Module`.\n *\n * Use as `det.call(img)` since JavaScript class instances are not callable;\n * for direct invocation, prefer `det.predict(img)`. The full\n * {@link DetectorPredictOptions} (including `classes`) is supported.\n */\n async call(\n image: ImageInput,\n options: DetectorPredictOptions = {},\n ): Promise<DetectionResults[]> {\n return this.predict(image, options);\n }\n\n /**\n * Run detection on a single image.\n *\n * The returned envelope carries a {@link Speed} breakdown in `speed`,\n * mirroring Ultralytics' `results[0].speed`.\n */\n async predict(\n image: ImageInput,\n options: DetectorPredictOptions = {},\n ): Promise<DetectionResults[]> {\n const timer = new SpeedTimer();\n const path = typeof image === \"string\" ? image : null;\n const original = await loadImage(image);\n timer.stage(\"load\");\n const { tensor, scale, padLeft, padTop } = this._preprocess(original);\n timer.stage(\"preprocess\");\n const outputs = await this._session.run({ [this._session.inputName]: tensor });\n this._pipeline.release();\n timer.stage(\"inference\");\n\n const firstOutputName = this._session.outputNames[0];\n if (firstOutputName === undefined) {\n throw new Error(\"Detector model has no outputs.\");\n }\n const raw = outputs[firstOutputName];\n if (raw === undefined) {\n throw new Error(`Detector model output ${firstOutputName} missing from run() result.`);\n }\n\n const threshold = options.confThreshold ?? this._confThreshold;\n const decodedAll = decodeYolo(raw.data as Float32Array, raw.dims, {\n originalWidth: original.width,\n originalHeight: original.height,\n padLeft,\n padTop,\n scale,\n confThreshold: threshold,\n iouThreshold: options.iouThreshold ?? this._iouThreshold,\n maxDetections: this._maxDetections,\n });\n\n const decoded =\n options.classes !== undefined\n ? (() => {\n const allowed = new Set(options.classes);\n return decodedAll.filter((d) => allowed.has(d.classId));\n })()\n : decodedAll;\n\n requireDetections(decoded.length, {\n raiseOnEmpty: options.raiseOnEmpty ?? this._raiseOnEmpty,\n confThreshold: threshold,\n classes: options.classes,\n path,\n });\n\n const detections = decoded.map((d) =>\n this._buildResult(original, d.bbox, d.classId, d.confidence),\n );\n\n const orig: readonly [number, number] = [original.height, original.width];\n const boxes = this._buildBoxes(detections, orig);\n timer.stage(\"postprocess\");\n return [\n new DetectionResults(\n boxes,\n detections,\n this._names,\n original,\n orig,\n path,\n timer.speed(),\n ),\n ];\n }\n\n /**\n * Letterbox and pack the image into the tensor the model expects.\n *\n * Runs through {@link LetterboxPipeline}, which fuses the resize, the\n * padding and the HWC-to-CHW float conversion into one `drawImage` plus one\n * readback loop, and reuses its output buffer between frames. The buffer is\n * handed straight to ONNX Runtime, so {@link _pipeline.release} must not be\n * called until the run resolves.\n */\n private _preprocess(image: RGBImage): {\n tensor: ort.Tensor;\n scale: number;\n padLeft: number;\n padTop: number;\n } {\n const [tw, th] = this._inputSize;\n const fused = this._pipeline.run(image);\n return {\n tensor: toFloat32Tensor(fused.data, [1, 3, th, tw]),\n scale: fused.scale,\n padLeft: fused.padLeft,\n padTop: fused.padTop,\n };\n }\n\n private _buildResult(\n original: RGBImage,\n bbox: BoundingBox,\n classId: number,\n confidence: number,\n ): DetectionResult {\n const [x1, y1, x2, y2] = bbox.asIntXyxy();\n const cx1 = Math.max(0, x1);\n const cy1 = Math.max(0, y1);\n const cx2 = Math.min(original.width, x2);\n const cy2 = Math.min(original.height, y2);\n\n let cropped: RGBImage;\n if (cx2 > cx1 && cy2 > cy1) {\n const cw = cx2 - cx1;\n const ch = cy2 - cy1;\n const out = new Uint8Array(cw * ch * 3);\n for (let row = 0; row < ch; row++) {\n const srcOffset = ((cy1 + row) * original.width + cx1) * 3;\n out.set(original.data.subarray(srcOffset, srcOffset + cw * 3), row * cw * 3);\n }\n cropped = new RGBImage(out, cw, ch);\n } else {\n cropped = new RGBImage(new Uint8Array(0), 0, 0);\n }\n\n const className = this._names[classId] ?? `class_${classId}`;\n\n return {\n classId,\n className,\n confidence,\n bbox,\n cls: classId,\n name: className,\n conf: confidence,\n box: bbox,\n croppedImage: cropped,\n };\n }\n\n private _buildBoxes(\n detections: readonly DetectionResult[],\n origShape: readonly [number, number],\n ): Boxes {\n const n = detections.length;\n const xyxy = new Float32Array(n * 4);\n const cls = new Int32Array(n);\n const conf = new Float32Array(n);\n for (let i = 0; i < n; i++) {\n const d = detections[i] as DetectionResult;\n xyxy[i * 4] = d.bbox.x1;\n xyxy[i * 4 + 1] = d.bbox.y1;\n xyxy[i * 4 + 2] = d.bbox.x2;\n xyxy[i * 4 + 3] = d.bbox.y2;\n cls[i] = d.classId;\n conf[i] = d.confidence;\n }\n return new Boxes(xyxy, cls, conf, origShape);\n }\n}\n"],"mappings":"qYAoGA,IAAa,EAAb,MAAa,UAAiB,EAAA,UAAW,CAGhB,MACA,QACA,OACA,WACA,eACA,cACA,eACA,cATrB,YACI,EACA,EACA,EACA,EACA,EACA,EACA,EACA,EACA,EACF,CACE,MAAM,CAAO,EATI,KAAA,MAAA,EACA,KAAA,QAAA,EACA,KAAA,OAAA,EACA,KAAA,WAAA,EACA,KAAA,eAAA,EACA,KAAA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,SAAS,EAAK,EAAI,CAAE,CACtC,KACI,GAAU,IAAI,EAAA,SAAS,IAAI,WAAe,EAAG,CAAC,EAGlD,IAAM,EAAY,KAAK,OAAO,IAAY,SAAS,IAEnD,MAAO,CACH,UACA,YACA,aACA,OACA,IAAK,EACL,KAAM,EACN,KAAM,EACN,IAAK,EACL,aAAc,CAClB,CACJ,CAEA,YACI,EACA,EACK,CACL,IAAM,EAAI,EAAW,OACf,EAAO,IAAI,aAAa,EAAI,CAAC,EAC7B,EAAM,IAAI,WAAW,CAAC,EACtB,EAAO,IAAI,aAAa,CAAC,EAC/B,IAAK,IAAI,EAAI,EAAG,EAAI,EAAG,IAAK,CACxB,IAAM,EAAI,EAAW,GACrB,EAAK,EAAI,GAAK,EAAE,KAAK,GACrB,EAAK,EAAI,EAAI,GAAK,EAAE,KAAK,GACzB,EAAK,EAAI,EAAI,GAAK,EAAE,KAAK,GACzB,EAAK,EAAI,EAAI,GAAK,EAAE,KAAK,GACzB,EAAI,GAAK,EAAE,QACX,EAAK,GAAK,EAAE,UAChB,CACA,OAAO,IAAI,EAAA,MAAM,EAAM,EAAK,EAAM,CAAS,CAC/C,CACJ"}
|
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { RGBImage as e } from "../types.js";
|
|
2
2
|
import { SpeedTimer as t } from "../core/timing.js";
|
|
3
3
|
import { Boxes as n, DetectionResults as r } from "../results.js";
|
|
4
|
-
import {
|
|
5
|
-
import { detectionNumClasses as
|
|
6
|
-
import { modelNames as
|
|
7
|
-
import { OrtSession as
|
|
8
|
-
import { loadImage as
|
|
9
|
-
import {
|
|
4
|
+
import { defaultLabels as i, resolveLabels as a } from "../labels.js";
|
|
5
|
+
import { detectionNumClasses as o, resolveInputSize as s } from "../core/graph.js";
|
|
6
|
+
import { modelNames as c } from "../core/metadata.js";
|
|
7
|
+
import { OrtSession as l } from "../core/session.js";
|
|
8
|
+
import { loadImage as u } from "../io/image.js";
|
|
9
|
+
import { toFloat32Tensor as d } from "../preprocess/image.js";
|
|
10
|
+
import { LetterboxPipeline as f, zeroTensorData as p } from "../preprocess/pipeline.js";
|
|
10
11
|
import { decodeYolo as m } from "../postprocess/detection.js";
|
|
11
|
-
import { VisionTask as h } from "./base.js";
|
|
12
|
+
import { VisionTask as h, requireDetections as g } from "./base.js";
|
|
12
13
|
//#region src/vision/tasks/detector.ts
|
|
13
|
-
var
|
|
14
|
+
var _ = class _ extends h {
|
|
14
15
|
_head;
|
|
15
16
|
_labels;
|
|
16
17
|
_names;
|
|
@@ -18,19 +19,36 @@ var g = class g extends h {
|
|
|
18
19
|
_confThreshold;
|
|
19
20
|
_iouThreshold;
|
|
20
21
|
_maxDetections;
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
_raiseOnEmpty;
|
|
23
|
+
constructor(e, t, n, r, i, a, o, s, c) {
|
|
24
|
+
super(e), this._head = t, this._labels = n, this._names = r, this._inputSize = i, this._confThreshold = a, this._iouThreshold = o, this._maxDetections = s, this._raiseOnEmpty = c;
|
|
25
|
+
}
|
|
26
|
+
_pipelineCache = null;
|
|
27
|
+
async warmup(e = 1) {
|
|
28
|
+
let [t, n] = this._inputSize;
|
|
29
|
+
for (let r = 0; r < e; r++) {
|
|
30
|
+
let e = d(p(t, n), [
|
|
31
|
+
1,
|
|
32
|
+
3,
|
|
33
|
+
n,
|
|
34
|
+
t
|
|
35
|
+
]);
|
|
36
|
+
await this._session.run({ [this._session.inputName]: e });
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
get _pipeline() {
|
|
40
|
+
return this._pipelineCache === null && (this._pipelineCache = new f(this._inputSize[0], this._inputSize[1])), this._pipelineCache;
|
|
23
41
|
}
|
|
24
42
|
static async create(e, t = {}) {
|
|
25
43
|
let n = t.head ?? "yolo";
|
|
26
44
|
if (n !== "yolo") throw Error(`Unsupported detector head '${n}'. Supported: 'yolo'.`);
|
|
27
|
-
let r = await
|
|
28
|
-
for (let e = 0; e <
|
|
29
|
-
return new
|
|
45
|
+
let r = await l.create(e, t), u = t.numClasses ?? o(r.outputShape) ?? void 0, d = a(t.labels ?? c(r.metadata) ?? i(u), { numClasses: u }), f = {};
|
|
46
|
+
for (let e = 0; e < d.length; e++) f[e] = d[e];
|
|
47
|
+
return new _(r, n, d, f, s({
|
|
30
48
|
graphShape: r.inputShape,
|
|
31
49
|
requested: t.inputSize,
|
|
32
50
|
fallback: [640, 640]
|
|
33
|
-
}), t.confThreshold ?? .25, t.iouThreshold ?? .45, t.maxDetections ?? 300);
|
|
51
|
+
}), t.confThreshold ?? .25, t.iouThreshold ?? .45, t.maxDetections ?? 300, t.raiseOnEmpty ?? !1);
|
|
34
52
|
}
|
|
35
53
|
get head() {
|
|
36
54
|
return this._head;
|
|
@@ -51,39 +69,46 @@ var g = class g extends h {
|
|
|
51
69
|
return this.predict(e, t);
|
|
52
70
|
}
|
|
53
71
|
async predict(e, n = {}) {
|
|
54
|
-
let i = new t(), a = typeof e == "string" ? e : null, o = await
|
|
72
|
+
let i = new t(), a = typeof e == "string" ? e : null, o = await u(e);
|
|
55
73
|
i.stage("load");
|
|
56
|
-
let { tensor: s, scale: c, padLeft:
|
|
74
|
+
let { tensor: s, scale: c, padLeft: l, padTop: d } = this._preprocess(o);
|
|
57
75
|
i.stage("preprocess");
|
|
58
76
|
let f = await this._session.run({ [this._session.inputName]: s });
|
|
59
|
-
i.stage("inference");
|
|
77
|
+
this._pipeline.release(), i.stage("inference");
|
|
60
78
|
let p = this._session.outputNames[0];
|
|
61
79
|
if (p === void 0) throw Error("Detector model has no outputs.");
|
|
62
80
|
let h = f[p];
|
|
63
81
|
if (h === void 0) throw Error(`Detector model output ${p} missing from run() result.`);
|
|
64
|
-
let
|
|
82
|
+
let _ = n.confThreshold ?? this._confThreshold, v = m(h.data, h.dims, {
|
|
65
83
|
originalWidth: o.width,
|
|
66
84
|
originalHeight: o.height,
|
|
67
|
-
padLeft:
|
|
85
|
+
padLeft: l,
|
|
68
86
|
padTop: d,
|
|
69
87
|
scale: c,
|
|
70
|
-
confThreshold:
|
|
88
|
+
confThreshold: _,
|
|
71
89
|
iouThreshold: n.iouThreshold ?? this._iouThreshold,
|
|
72
90
|
maxDetections: this._maxDetections
|
|
73
|
-
}),
|
|
91
|
+
}), y = n.classes === void 0 ? v : (() => {
|
|
74
92
|
let e = new Set(n.classes);
|
|
75
|
-
return
|
|
76
|
-
})()
|
|
77
|
-
|
|
93
|
+
return v.filter((t) => e.has(t.classId));
|
|
94
|
+
})();
|
|
95
|
+
g(y.length, {
|
|
96
|
+
raiseOnEmpty: n.raiseOnEmpty ?? this._raiseOnEmpty,
|
|
97
|
+
confThreshold: _,
|
|
98
|
+
classes: n.classes,
|
|
99
|
+
path: a
|
|
100
|
+
});
|
|
101
|
+
let b = y.map((e) => this._buildResult(o, e.bbox, e.classId, e.confidence)), x = [o.height, o.width], S = this._buildBoxes(b, x);
|
|
102
|
+
return i.stage("postprocess"), [new r(S, b, this._names, o, x, a, i.speed())];
|
|
78
103
|
}
|
|
79
104
|
_preprocess(e) {
|
|
80
|
-
let [t, n] = this._inputSize, r =
|
|
105
|
+
let [t, n] = this._inputSize, r = this._pipeline.run(e);
|
|
81
106
|
return {
|
|
82
|
-
tensor:
|
|
107
|
+
tensor: d(r.data, [
|
|
83
108
|
1,
|
|
84
109
|
3,
|
|
85
|
-
|
|
86
|
-
|
|
110
|
+
n,
|
|
111
|
+
t
|
|
87
112
|
]),
|
|
88
113
|
scale: r.scale,
|
|
89
114
|
padLeft: r.padLeft,
|
|
@@ -123,6 +148,6 @@ var g = class g extends h {
|
|
|
123
148
|
}
|
|
124
149
|
};
|
|
125
150
|
//#endregion
|
|
126
|
-
export {
|
|
151
|
+
export { _ as Detector };
|
|
127
152
|
|
|
128
153
|
//# sourceMappingURL=detector.js.map
|