Skip to content

Commit f148be6

Browse files
committed
If longest_edge is not set, only resize based on shortest_edge
1 parent 6231d67 commit f148be6

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

src/processors.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,27 @@ class FeatureExtractor extends Callable {
4242
*/
4343
class ImageFeatureExtractor extends FeatureExtractor {
4444

45+
// Defined here: https://github.com/python-pillow/Pillow/blob/a405e8406b83f8bfb8916e93971edc7407b8b1ff/src/libImaging/Imaging.h#L262-L268
46+
RESAMPLING_MAPPING = {
47+
0: 'nearest',
48+
1: 'lanczos',
49+
2: 'bilinear',
50+
3: 'bicubic',
51+
4: 'box',
52+
5: 'hamming',
53+
}
54+
4555
/**
4656
* Constructs a new ViTFeatureExtractor instance.
4757
*
4858
* @param {object} config - The configuration for the feature extractor.
4959
* @param {number[]} config.image_mean - The mean values for image normalization.
5060
* @param {number[]} config.image_std - The standard deviation values for image normalization.
5161
* @param {boolean} config.do_rescale - Whether to rescale the image pixel values to the [0,1] range.
62+
* @param {number} config.rescale_factor - The factor to use for rescaling the image pixel values.
5263
* @param {boolean} config.do_normalize - Whether to normalize the image pixel values.
5364
* @param {boolean} config.do_resize - Whether to resize the image.
65+
* @param {number} config.resample - What method to use for resampling.
5466
* @param {number} config.size - The size to resize the image to.
5567
*/
5668
constructor(config) {
@@ -66,6 +78,7 @@ class ImageFeatureExtractor extends FeatureExtractor {
6678
this.image_std = new Array(3).fill(this.image_std);
6779
}
6880

81+
this.resample = this.config.resample ?? 2; // 2 => bilinear
6982
this.do_rescale = this.config.do_rescale ?? true;
7083
this.rescale_factor = this.config.rescale_factor ?? (1 / 255);
7184
this.do_normalize = this.config.do_normalize;
@@ -76,6 +89,7 @@ class ImageFeatureExtractor extends FeatureExtractor {
7689
// TODO use these
7790
this.do_center_crop = this.config.do_center_crop;
7891
this.crop_size = this.config.crop_size;
92+
this.do_convert_rgb = this.config.do_convert_rgb ?? true;
7993
}
8094

8195
/**
@@ -106,12 +120,12 @@ class ImageFeatureExtractor extends FeatureExtractor {
106120
} else {
107121
// Extract known properties from `this.size`
108122
shortest_edge = this.size.shortest_edge;
109-
longest_edge = this.size.longest_edge ?? shortest_edge;
123+
longest_edge = this.size.longest_edge;
110124
}
111125

112126
// If `longest_edge` and `shortest_edge` are set, maintain aspect ratio and resize to `shortest_edge`
113127
// while keeping the largest dimension <= `longest_edge`
114-
if (longest_edge !== undefined && shortest_edge !== undefined) {
128+
if (shortest_edge !== undefined) {
115129
// http://opensourcehacker.com/2011/12/01/calculate-aspect-ratio-conserving-resize-for-images-in-javascript/
116130
// Try resize so that shortest edge is `this.shortest_edge` (target)
117131
const ratio = Math.max(shortest_edge / srcWidth, shortest_edge / srcHeight);
@@ -120,21 +134,29 @@ class ImageFeatureExtractor extends FeatureExtractor {
120134

121135
// The new width and height might be greater than `this.longest_edge`, so
122136
// we downscale again to ensure the largest dimension is `this.longest_edge`
123-
const downscaleFactor = Math.min(longest_edge / newWidth, longest_edge / newHeight, 1);
137+
const downscaleFactor = longest_edge === undefined
138+
? 1 // If `longest_edge` is not set, don't downscale
139+
: Math.min(longest_edge / newWidth, longest_edge / newHeight, 1);
124140

125141
// Perform resize
126-
image = await image.resize(Math.floor(newWidth * downscaleFactor), Math.floor(newHeight * downscaleFactor));
142+
image = await image.resize(Math.floor(newWidth * downscaleFactor), Math.floor(newHeight * downscaleFactor), {
143+
resample: this.resample,
144+
});
127145

128146
} else if (this.size.width !== undefined && this.size.height !== undefined) {
129147
// If `width` and `height` are set, resize to those dimensions
130-
image = await image.resize(this.size.width, this.size.height);
148+
image = await image.resize(this.size.width, this.size.height, {
149+
resample: this.resample,
150+
});
131151
} else {
132152
throw new Error(`Could not resize image due to unsupported \`this.size\` option in config: ${JSON.stringify(this.size)}`);
133153
}
134154
}
135155

136-
// Convert image to RGB
137-
image = image.rgb();
156+
if (this.do_convert_rgb) {
157+
// Convert image to RGB
158+
image = image.rgb();
159+
}
138160

139161
const pixelData = Float32Array.from(image.data);
140162

0 commit comments

Comments
 (0)