diff --git a/package.json b/package.json index 7ad91bb..938573b 100644 --- a/package.json +++ b/package.json @@ -16,8 +16,8 @@ "watch": "concurrently \"npm run watch --prefix packages/web\" \"npm run watch --prefix packages/node\"", "build": "npm run build --workspaces --if-present", "lint": "npx prettier --write --ignore-unknown .", - "publish:latest": "npm run publish:latest --workspaces --if-present", - "publish:next": "npm run publish:next --workspaces --if-present", + "publish:latest": "npm run publish:latest --workspaces --if-present && npm run deploy-assets", + "publish:next": "npm run publish:next --workspaces --if-present && npm run deploy-assets", "package:version": "node ./scripts/package-version.mjs", "deploy-assets": "node -r dotenv/config ./scripts/deploy-assets.mjs", "tag": "git tag $npm_package_version & git push origin $npm_package_version", diff --git a/packages/node/package.json b/packages/node/package.json index 0b4a593..c7c8fdf 100644 --- a/packages/node/package.json +++ b/packages/node/package.json @@ -58,27 +58,28 @@ "package:pack": "npm run build && npm pack . --pack-destination ../../releases" }, "dependencies": { - "onnxruntime-node": "^1.15.1", - "sharp": "^0.32.4", - "@types/lodash": "^4.14.195", - "@types/node": "^20.3.1", - "lodash": "^4.17.21", - "ndarray": "^1.0.19", - "zod": "^3.21.4" + "onnxruntime-node": "~1.17.0", + "sharp": "~0.32.4", + "@types/lodash": "~4.14.195", + "@types/node": "~20.3.1", + "@types/ndarray": "~1.0.14", + "lodash": "~4.17.21", + "ndarray": "~1.0.19", + "zod": "~3.21.4" }, "devDependencies": { - "assert": "^2.0.0", - "esbuild": "^0.18.18", - "npm-dts": "^1.3.12", - "os-browserify": "^0.3.0", - "path-browserify": "^1.0.1", - "process": "^0.11.10", - "stream-browserify": "^3.0.0", - "ts-loader": "^9.4.3", - "tslib": "^2.5.3", - "typescript": "^5.1.6", - "util": "^0.12.5", - "webpack": "^5.85.1", - "webpack-cli": "^5.1.4" + "assert": "~2.0.0", + "esbuild": "~0.18.18", + "npm-dts": "~1.3.12", + "os-browserify": "~0.3.0", + "path-browserify": "~1.0.1", + "process": "~0.11.10", + "stream-browserify": "~3.0.0", + "ts-loader": "~9.4.3", + "tslib": "~2.5.3", + "typescript": "~5.1.6", + "util": "~0.12.5", + "webpack": "~5.85.1", + "webpack-cli": "~5.1.4" } } diff --git a/packages/node/src/codecs.ts b/packages/node/src/codecs.ts index 1ff2532..355adf3 100644 --- a/packages/node/src/codecs.ts +++ b/packages/node/src/codecs.ts @@ -11,12 +11,20 @@ async function imageDecode(blob: Blob): Promise> { case 'image/x-alpha8': { const width = parseInt(mime.params['width']); const height = parseInt(mime.params['height']); - return ndarray(await blob.arrayBuffer(), [height, width, 1]); + return ndarray(new Uint8Array(await blob.arrayBuffer()), [ + height, + width, + 1 + ]); } case 'image/x-rgba8': { const width = parseInt(mime.params['width']); const height = parseInt(mime.params['height']); - return ndarray(await blob.arrayBuffer(), [height, width, 4]); + return ndarray(new Uint8Array(await blob.arrayBuffer()), [ + height, + width, + 4 + ]); } case 'application/octet-stream': case `image/png`: @@ -55,7 +63,10 @@ async function imageEncode( switch (type) { case 'image/x-alpha8': case 'image/x-rgba8': { - const mime = MimeType.create(type, { width, height }); + const mime = MimeType.create(type, { + width: width.toString(), + height: height.toString() + }); return new Blob([imageTensor.data], { type: mime.toString() }); } case `image/png`: diff --git a/packages/node/src/inference.ts b/packages/node/src/inference.ts index aed9c4b..92d49ad 100644 --- a/packages/node/src/inference.ts +++ b/packages/node/src/inference.ts @@ -39,9 +39,9 @@ async function runInference( ); let alphamask = ndarray(predictionsDict[0].data, [resolution, resolution, 1]); - alphamask = convertFloat32ToUint8(alphamask); - alphamask = tensorResizeBilinear(alphamask, srcWidth, srcHeight); + let alphamaskU8 = convertFloat32ToUint8(alphamask); + alphamaskU8 = tensorResizeBilinear(alphamaskU8, srcWidth, srcHeight); if (config.progress) config.progress('compute:inference', 1, 1); - return alphamask; + return alphamaskU8; } diff --git a/packages/node/src/onnx.ts b/packages/node/src/onnx.ts index 291075a..32f83e4 100644 --- a/packages/node/src/onnx.ts +++ b/packages/node/src/onnx.ts @@ -45,7 +45,7 @@ async function runOnnxSession( for (const key of outputs) { const output: ort.Tensor = outputData[key]; - const shape: Number[] = output.dims as number[]; + const shape: number[] = output.dims as number[]; const data: Float32Array = output.data as Float32Array; const tensor = ndarray(data, shape); outputKVPairs.push(tensor); diff --git a/packages/node/src/utils.ts b/packages/node/src/utils.ts index d7cc242..5587b91 100644 --- a/packages/node/src/utils.ts +++ b/packages/node/src/utils.ts @@ -17,7 +17,13 @@ import { loadFromURI } from './resource'; import ndarray, { NdArray } from 'ndarray'; import { imageDecode, imageEncode } from './codecs'; -type ImageSource = ArrayBuffer | Uint8Array | Blob | URL | string; +type ImageSource = + | ArrayBuffer + | Uint8Array + | Blob + | URL + | string + | NdArray; function tensorResizeBilinear( imageTensor: NdArray, @@ -69,7 +75,7 @@ function tensorResizeBilinear( } function tensorHWCtoBCHW( - imageTensor: NdArray, + imageTensor: NdArray, mean: number[] = [128, 128, 128], std: number[] = [256, 256, 256] ): NdArray { diff --git a/packages/web/src/codecs.ts b/packages/web/src/codecs.ts index ab762ed..c6d40b5 100644 --- a/packages/web/src/codecs.ts +++ b/packages/web/src/codecs.ts @@ -9,12 +9,20 @@ async function imageDecode(blob: Blob): Promise> { case 'image/x-alpha8': { const width = parseInt(mime.params['width']); const height = parseInt(mime.params['height']); - return ndarray(await blob.arrayBuffer(), [height, width, 1]); + return ndarray(new Uint8Array(await blob.arrayBuffer()), [ + height, + width, + 1 + ]); } case 'image/x-rgba8': { const width = parseInt(mime.params['width']); const height = parseInt(mime.params['height']); - return ndarray(await blob.arrayBuffer(), [height, width, 4]); + return ndarray(new Uint8Array(await blob.arrayBuffer()), [ + height, + width, + 4 + ]); } case 'application/octet-stream': // this is an unknwon type case `image/png`: @@ -22,7 +30,11 @@ async function imageDecode(blob: Blob): Promise> { case `image/webp`: { const imageBitmap = await createImageBitmap(blob); const imageData = imageBitmapToImageData(imageBitmap); - return ndarray(imageData.data, [imageData.height, imageData.width, 4]); + return ndarray(new Uint8Array(imageData.data), [ + imageData.height, + imageData.width, + 4 + ]); } default: throw new Error( @@ -41,7 +53,10 @@ async function imageEncode( switch (format) { case 'image/x-alpha8': case 'image/x-rgba8': { - const mime = MimeType.create(format, { width, height }); + const mime = MimeType.create(format, { + width: width.toString(), + height: height.toString() + }); return new Blob([imageTensor.data], { type: mime.toString() }); } case `image/png`: diff --git a/packages/web/src/inference.ts b/packages/web/src/inference.ts index 36e24cb..ae40c29 100644 --- a/packages/web/src/inference.ts +++ b/packages/web/src/inference.ts @@ -41,9 +41,9 @@ async function runInference( ); let alphamask = ndarray(predictionsDict[0].data, [resolution, resolution, 1]); - alphamask = convertFloat32ToUint8(alphamask); - alphamask = tensorResizeBilinear(alphamask, srcWidth, srcHeight); + let alphamaskU8 = convertFloat32ToUint8(alphamask); + alphamaskU8 = tensorResizeBilinear(alphamaskU8, srcWidth, srcHeight); if (config.progress) config.progress('compute:inference', 1, 1); - return alphamask; + return alphamaskU8; } diff --git a/packages/web/src/onnx.ts b/packages/web/src/onnx.ts index 929bccb..6ec34bb 100644 --- a/packages/web/src/onnx.ts +++ b/packages/web/src/onnx.ts @@ -85,7 +85,7 @@ async function runOnnxSession( for (const key of outputs) { const output: ort.Tensor = outputData[key]; - const shape: Number[] = output.dims as number[]; + const shape: number[] = output.dims as number[]; const data: Float32Array = output.data as Float32Array; const tensor = ndarray(data, shape); outputKVPairs.push(tensor); diff --git a/packages/web/src/utils.ts b/packages/web/src/utils.ts index 0359c7c..14ce6ac 100644 --- a/packages/web/src/utils.ts +++ b/packages/web/src/utils.ts @@ -15,7 +15,14 @@ import { imageDecode, imageEncode } from './codecs'; import { ensureAbsoluteURI } from './url'; import { Config } from './schema'; -type ImageSource = ImageData | ArrayBuffer | Uint8Array | Blob | URL | string; +type ImageSource = + | ImageData + | ArrayBuffer + | Uint8Array + | Blob + | URL + | string + | NdArray; function imageBitmapToImageData(imageBitmap: ImageBitmap): ImageData { var canvas = createCanvas(imageBitmap.width, imageBitmap.height);