Skip to content

Commit

Permalink
Add do_thumbnail for donut image processing
Browse files Browse the repository at this point in the history
  • Loading branch information
xenova committed Sep 20, 2023
1 parent a2a5aa0 commit f614c3e
Showing 1 changed file with 53 additions and 6 deletions.
59 changes: 53 additions & 6 deletions src/processors.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
import {
max,
softmax,
FFT
FFT,
} from './utils/maths.js';


Expand Down Expand Up @@ -169,14 +169,51 @@ export class ImageFeatureExtractor extends FeatureExtractor {
this.do_normalize = this.config.do_normalize;

this.do_resize = this.config.do_resize;
this.do_thumbnail = this.config.do_thumbnail;
this.size = this.config.size;

this.do_center_crop = this.config.do_center_crop;
this.crop_size = this.config.crop_size;
this.do_convert_rgb = this.config.do_convert_rgb ?? true;

this.pad_size = this.config.pad_size;
this.do_pad = (this.config.do_pad ?? false) && this.pad_size;
this.do_pad = this.config.do_pad;

if (this.do_pad && !this.pad_size) {
// Should pad, but no pad size specified
// We infer the pad size from the resize size
this.pad_size = this.size
}
}

/**
* Resize the image to make a thumbnail. The image is resized so that no dimension is larger than any
* corresponding dimension of the specified size.
* @param {RawImage} image The image to be resized.
* @param {{height:number, width:number}} size The size `{"height": h, "width": w}` to resize the image to.
* @param {string | 0 | 1 | 2 | 3 | 4 | 5} [resample=2] The resampling filter to use.
* @returns {Promise<RawImage>} The resized image.
*/
async thumbnail(image, size, resample = 2) {
const input_height = image.height;
const input_width = image.width;

const output_height = size.height;
const output_width = size.width;

// We always resize to the smallest of either the input or output size.
let height = Math.min(input_height, output_height)
let width = Math.min(input_width, output_width)

if (height === input_height && width === input_width) {
return image;
}
if (input_height > input_width) {
width = Math.floor(input_width * height / input_height);
} else if (input_width > input_height) {
height = Math.floor(input_height * width / input_width);
}
return await image.resize(width, height, { resample });
}

/**
Expand Down Expand Up @@ -206,8 +243,13 @@ export class ImageFeatureExtractor extends FeatureExtractor {
let shortest_edge;
let longest_edge;

if (this.do_thumbnail) {
// NOTE: custom logic for `Donut` models
const { height, width } = this.size;
shortest_edge = Math.min(height, width)
}
// Support both formats for backwards compatibility
if (Number.isInteger(this.size)) {
else if (Number.isInteger(this.size)) {
shortest_edge = this.size;
longest_edge = this.config.max_size ?? shortest_edge;

Expand Down Expand Up @@ -235,9 +277,9 @@ export class ImageFeatureExtractor extends FeatureExtractor {
? 1 // If `longest_edge` is not set, don't downscale
: Math.min(longest_edge / newWidth, longest_edge / newHeight);

// To avoid certain floating point precision issues, we round to 3 decimal places
const finalWidth = Math.floor(Number((newWidth * longResizeFactor).toPrecision(3)));
const finalHeight = Math.floor(Number((newHeight * longResizeFactor).toPrecision(3)));
// To avoid certain floating point precision issues, we round to 2 decimal places
const finalWidth = Math.floor(Number((newWidth * longResizeFactor).toFixed(2)));
const finalHeight = Math.floor(Number((newHeight * longResizeFactor).toFixed(2)));

// Perform resize
image = await image.resize(finalWidth, finalHeight, {
Expand All @@ -254,6 +296,11 @@ export class ImageFeatureExtractor extends FeatureExtractor {
}
}

// Resize the image using thumbnail method.
if (this.do_thumbnail) {
image = await this.thumbnail(image, this.size, this.resample);
}

if (this.do_center_crop) {

let crop_width;
Expand Down

0 comments on commit f614c3e

Please sign in to comment.