From 2ee715c672e863edd317324b8feb8522f53342a7 Mon Sep 17 00:00:00 2001 From: Joshua Lochner Date: Tue, 3 Dec 2024 13:41:14 +0200 Subject: [PATCH 1/2] [version] Update to 3.1.1 --- README.md | 4 ++-- docs/snippets/2_installation.snippet | 2 +- docs/snippets/4_custom-usage.snippet | 2 +- package-lock.json | 4 ++-- package.json | 2 +- src/env.js | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 7312afc46..bcfe2a1fe 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ npm i @huggingface/transformers Alternatively, you can use it in vanilla JS, without any bundler, by using a CDN or static hosting. For example, using [ES Modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules), you can import the library with: ```html ``` @@ -155,7 +155,7 @@ Check out the Transformers.js [template](https://huggingface.co/new-space?templa -By default, Transformers.js uses [hosted pretrained models](https://huggingface.co/models?library=transformers.js) and [precompiled WASM binaries](https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.1.0/dist/), which should work out-of-the-box. You can customize this as follows: +By default, Transformers.js uses [hosted pretrained models](https://huggingface.co/models?library=transformers.js) and [precompiled WASM binaries](https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.1.1/dist/), which should work out-of-the-box. You can customize this as follows: ### Settings diff --git a/docs/snippets/2_installation.snippet b/docs/snippets/2_installation.snippet index 353d739c8..0ef9f7992 100644 --- a/docs/snippets/2_installation.snippet +++ b/docs/snippets/2_installation.snippet @@ -7,6 +7,6 @@ npm i @huggingface/transformers Alternatively, you can use it in vanilla JS, without any bundler, by using a CDN or static hosting. For example, using [ES Modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules), you can import the library with: ```html ``` diff --git a/docs/snippets/4_custom-usage.snippet b/docs/snippets/4_custom-usage.snippet index d505f80e4..28a19fd93 100644 --- a/docs/snippets/4_custom-usage.snippet +++ b/docs/snippets/4_custom-usage.snippet @@ -1,6 +1,6 @@ -By default, Transformers.js uses [hosted pretrained models](https://huggingface.co/models?library=transformers.js) and [precompiled WASM binaries](https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.1.0/dist/), which should work out-of-the-box. You can customize this as follows: +By default, Transformers.js uses [hosted pretrained models](https://huggingface.co/models?library=transformers.js) and [precompiled WASM binaries](https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.1.1/dist/), which should work out-of-the-box. You can customize this as follows: ### Settings diff --git a/package-lock.json b/package-lock.json index 908e7d85b..60aeda522 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@huggingface/transformers", - "version": "3.1.0", + "version": "3.1.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@huggingface/transformers", - "version": "3.1.0", + "version": "3.1.1", "license": "Apache-2.0", "dependencies": { "@huggingface/jinja": "^0.3.2", diff --git a/package.json b/package.json index 372b89014..b07a2645a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@huggingface/transformers", - "version": "3.1.0", + "version": "3.1.1", "description": "State-of-the-art Machine Learning for the web. Run 🤗 Transformers directly in your browser, with no need for a server!", "main": "./src/transformers.js", "types": "./types/transformers.d.ts", diff --git a/src/env.js b/src/env.js index 287c98dfc..21d047457 100644 --- a/src/env.js +++ b/src/env.js @@ -26,7 +26,7 @@ import fs from 'fs'; import path from 'path'; import url from 'url'; -const VERSION = '3.1.0'; +const VERSION = '3.1.1'; // Check if various APIs are available (depends on environment) const IS_BROWSER_ENV = typeof window !== "undefined" && typeof window.document !== "undefined"; From f8dbc897a49b374038d8b7e2f9e29318b3e41304 Mon Sep 17 00:00:00 2001 From: BritishWerewolf Date: Thu, 5 Dec 2024 20:50:18 +0000 Subject: [PATCH 2/2] Add function to apply mask to RawImage. (#1020) * Add function to apply mask to RawImage. Add function to get a pixel and set a pixel. * Simplify to use a single loop. * Throw instead of silently converting to avoid unexpected errors. * Rename function to better reflect what it does. Remove unused functions. * Update `putAlpha` function --------- Co-authored-by: Joshua Lochner --- src/utils/image.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/utils/image.js b/src/utils/image.js index d372cd0ac..f2f47821a 100644 --- a/src/utils/image.js +++ b/src/utils/image.js @@ -303,6 +303,46 @@ export class RawImage { return this._update(newData, this.width, this.height, 4); } + /** + * Apply an alpha mask to the image. Operates in place. + * @param {RawImage} mask The mask to apply. It should have a single channel. + * @returns {RawImage} The masked image. + * @throws {Error} If the mask is not the same size as the image. + * @throws {Error} If the image does not have 4 channels. + * @throws {Error} If the mask is not a single channel. + */ + putAlpha(mask) { + if (mask.width !== this.width || mask.height !== this.height) { + throw new Error(`Expected mask size to be ${this.width}x${this.height}, but got ${mask.width}x${mask.height}`); + } + if (mask.channels !== 1) { + throw new Error(`Expected mask to have 1 channel, but got ${mask.channels}`); + } + + const this_data = this.data; + const mask_data = mask.data; + const num_pixels = this.width * this.height; + if (this.channels === 3) { + // Convert to RGBA and simultaneously apply mask to alpha channel + const newData = new Uint8ClampedArray(num_pixels * 4); + for (let i = 0, in_offset = 0, out_offset = 0; i < num_pixels; ++i) { + newData[out_offset++] = this_data[in_offset++]; + newData[out_offset++] = this_data[in_offset++]; + newData[out_offset++] = this_data[in_offset++]; + newData[out_offset++] = mask_data[i]; + } + return this._update(newData, this.width, this.height, 4); + + } else if (this.channels === 4) { + // Apply mask to alpha channel in place + for (let i = 0; i < num_pixels; ++i) { + this_data[4 * i + 3] = mask_data[i]; + } + return this; + } + throw new Error(`Expected image to have 3 or 4 channels, but got ${this.channels}`); + } + /** * Resize the image to the given dimensions. This method uses the canvas API to perform the resizing. * @param {number} width The width of the new image. `null` or `-1` will preserve the aspect ratio.