Skip to content

Commit

Permalink
Merge branch 'main' into add-paligemma
Browse files Browse the repository at this point in the history
  • Loading branch information
xenova committed Dec 6, 2024
2 parents f105179 + f8dbc89 commit f2b5191
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<script type="module">
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/[email protected].0';
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/[email protected].1';
</script>
```

Expand Down Expand Up @@ -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/[email protected].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/[email protected].1/dist/), which should work out-of-the-box. You can customize this as follows:

### Settings

Expand Down
2 changes: 1 addition & 1 deletion docs/snippets/2_installation.snippet
Original file line number Diff line number Diff line change
Expand Up @@ -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
<script type="module">
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/[email protected].0';
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/[email protected].1';
</script>
```
2 changes: 1 addition & 1 deletion docs/snippets/4_custom-usage.snippet
Original file line number Diff line number Diff line change
@@ -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/[email protected].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/[email protected].1/dist/), which should work out-of-the-box. You can customize this as follows:

### Settings

Expand Down
2 changes: 1 addition & 1 deletion src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
40 changes: 40 additions & 0 deletions src/utils/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit f2b5191

Please sign in to comment.