Skip to content

Commit f666abe

Browse files
authored
Merge pull request #324 from JingyuanZhang/master
feat(core): support scale preprocessor
2 parents 19c359f + aa2110b commit f666abe

File tree

2 files changed

+53
-25
lines changed

2 files changed

+53
-25
lines changed

packages/paddlejs-core/src/commons/interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export interface RunnerConfig {
7676
mean?: number[];
7777
std?: number[];
7878
bgr?: boolean;
79+
scale?: number; // 按照 scale 大小原图进行等比拉伸并裁剪
7980
type?: GraphType; // model type
8081
keepRatio?: boolean;
8182
needPreheat?: boolean;

packages/paddlejs-core/src/mediaProcessor.ts

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default class MediaProcessor {
2929
* @param inputs
3030
*/
3131
process(media, modelConfig, feedShape): InputFeed[] {
32-
const { fill, mean, std, bgr, keepRatio = true } = modelConfig;
32+
const { fill, mean, std, bgr, keepRatio = true, scale = 0 } = modelConfig;
3333
const { fc = 3, fh, fw } = feedShape;
3434
const input = media;
3535

@@ -40,6 +40,7 @@ export default class MediaProcessor {
4040
std: std || this.std,
4141
bgr: bgr || this.bgr,
4242
keepRatio,
43+
scale,
4344
targetSize: {
4445
width: fw,
4546
height: fh
@@ -81,7 +82,10 @@ export default class MediaProcessor {
8182
this.pixelHeight = (pixels as HTMLImageElement).naturalHeight || pixels.height;
8283

8384
const inGPU = env.get('webgl_gpu_pipeline') || opt.webglFeedProcess;
84-
this.fitToTargetSize(isImageElementLike ? input.path : input, imageDataInfo, opt.keepRatio, inGPU);
85+
this.fitToTargetSize(isImageElementLike ? input.path : input, imageDataInfo, {
86+
...opt,
87+
inGPU
88+
});
8589
data = this.getImageData(imageDataInfo);
8690
// process imageData in webgl
8791
if (inGPU) {
@@ -143,7 +147,12 @@ export default class MediaProcessor {
143147
/**
144148
* 缩放成目标尺寸, keepRatio 为 true 则保持比例拉伸并居中,为 false 则变形拉伸为目标尺寸
145149
*/
146-
fitToTargetSize(image, imageDataInfo, keepRatio = true, inGPU = false) {
150+
fitToTargetSize(image, imageDataInfo, opt) {
151+
const {
152+
keepRatio = true,
153+
inGPU = false,
154+
scale = 0
155+
} = opt || {};
147156
// 目标尺寸
148157
const targetWidth = imageDataInfo.dWidth;
149158
const targetHeight = imageDataInfo.dHeight;
@@ -155,37 +164,55 @@ export default class MediaProcessor {
155164
let sh = inGPU ? this.pixelHeight : targetHeight;
156165
let x = 0;
157166
let y = 0;
158-
if (keepRatio) {
159-
// target的长宽比大些 就把原图的高变成target那么高
160-
if (targetWidth / targetHeight * this.pixelHeight / this.pixelWidth >= 1) {
161-
if (inGPU) {
162-
canvasWidth = Math.round(sh * targetWidth / targetHeight);
163-
x = Math.floor((canvasWidth - sw) / 2);
164-
}
165-
else {
166-
sw = Math.round(sh * this.pixelWidth / this.pixelHeight);
167-
x = Math.floor((targetWidth - sw) / 2);
168-
}
167+
168+
if (scale) {
169+
if (sw - targetWidth < 0 || sh - targetHeight < 0) {
170+
throw new Error('scale size smaller than target size');
171+
}
172+
if (this.pixelWidth > this.pixelHeight) {
173+
sh = scale;
174+
sw = Math.round(sh * this.pixelWidth / this.pixelHeight);
169175
}
170-
// target的长宽比小些 就把原图的宽变成target那么宽
171176
else {
172-
if (inGPU) {
173-
canvasHeight = Math.round(sw * targetHeight / targetWidth);
174-
y = Math.floor((canvasHeight - sh) / 2);
177+
sw = scale;
178+
sh = Math.round(sw * this.pixelHeight / this.pixelWidth);
179+
}
180+
this.targetCanvas.width = canvasWidth = sw;
181+
this.targetCanvas.height = canvasHeight = sh;
182+
imageDataInfo.dx = (sw - targetWidth) / 2;
183+
imageDataInfo.dy = (sh - targetHeight) / 2;
184+
}
185+
else {
186+
if (keepRatio) {
187+
// target的长宽比大些 就把原图的高变成target那么高
188+
if (targetWidth / targetHeight * this.pixelHeight / this.pixelWidth >= 1) {
189+
if (inGPU) {
190+
canvasWidth = Math.round(sh * targetWidth / targetHeight);
191+
x = Math.floor((canvasWidth - sw) / 2);
192+
}
193+
else {
194+
sw = Math.round(sh * this.pixelWidth / this.pixelHeight);
195+
x = Math.floor((targetWidth - sw) / 2);
196+
}
175197
}
198+
// target的长宽比小些 就把原图的宽变成target那么宽
176199
else {
177-
sh = Math.round(sw * this.pixelHeight / this.pixelWidth);
178-
y = Math.floor((targetHeight - sh) / 2);
200+
if (inGPU) {
201+
canvasHeight = Math.round(sw * targetHeight / targetWidth);
202+
y = Math.floor((canvasHeight - sh) / 2);
203+
}
204+
else {
205+
sh = Math.round(sw * this.pixelHeight / this.pixelWidth);
206+
y = Math.floor((targetHeight - sh) / 2);
207+
}
179208
}
180209
}
210+
this.targetCanvas.width = imageDataInfo.dWidth = canvasWidth;
211+
this.targetCanvas.height = imageDataInfo.dHeight = canvasHeight;
181212
}
182213

183-
imageDataInfo.dWidth = canvasWidth;
184-
imageDataInfo.dHeight = canvasHeight;
185-
this.targetCanvas.width = canvasWidth;
186-
this.targetCanvas.height = canvasHeight;
187214
this.targetContext.fillStyle = imageDataInfo.gapFillWith;
188-
this.targetContext.fillRect(0, 0, canvasWidth, canvasHeight);
215+
this.targetContext.fillRect(0, 0, this.targetCanvas.width, this.targetCanvas.height);
189216
this.targetContext.drawImage(image, x, y, sw, sh);
190217
}
191218

0 commit comments

Comments
 (0)