Skip to content

Commit 453c136

Browse files
committed
fix(core): fix mediaProcessor param scale
1 parent 17e4b77 commit 453c136

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

packages/paddlejs-core/src/mediaProcessor.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type Color = string;
88

99
export default class MediaProcessor {
1010
targetContext: CanvasRenderingContext2D = {} as CanvasRenderingContext2D;
11-
gapFillWith: Color = '#000';
11+
gapFillWith: Color = '#fff';
1212
mean: number[] = [0, 0, 0];
1313
std: number[] = [1, 1, 1];
1414
bgr: boolean = false;
@@ -53,7 +53,9 @@ export default class MediaProcessor {
5353
let data: ImageData | number[] = [];
5454
let scaleSize;
5555

56-
if (!(pixels instanceof HTMLImageElement || pixels instanceof HTMLVideoElement)) {
56+
if (!(pixels instanceof HTMLImageElement
57+
|| pixels instanceof HTMLVideoElement
58+
|| pixels instanceof HTMLCanvasElement)) {
5759
return [{
5860
data: data,
5961
shape: opt.shape || opt.targetShape,
@@ -63,19 +65,17 @@ export default class MediaProcessor {
6365

6466
this.pixelWidth = pixels.width;
6567
this.pixelHeight = pixels.height;
66-
67-
if (opt.scale && opt.targetSize) { // Moblienet的情况
68+
if (opt.scale && opt.targetSize) {
6869
data = this.resizeAndFitTargetSize(pixels, opt);
6970
}
70-
else if (opt.targetSize) { // 如果有targetSize,就是装在目标宽高里的模式 TinyYolo的情况
71+
else if (opt.targetSize) { // 如果有 targetSize,就是装在目标宽高里的模式
7172
scaleSize = this.fitToTargetSize(pixels, opt);
7273
data = this.getImageData(0, 0, scaleSize);
7374
}
7475
else {
7576
scaleSize = this.reSize(pixels, opt);
7677
data = this.getImageData(0, 0, scaleSize);
7778
}
78-
7979
if (opt.gray) {
8080
data = this.grayscale(data);
8181
}
@@ -142,18 +142,17 @@ export default class MediaProcessor {
142142
* @param opt.mean 均值
143143
* @param opt.std 方差
144144
* @param opt.targetShape 输出shape
145-
* @param opt.normalizeType 0:将数据映射为0~1, 1:映射为-1~1之间
146145
*/
147146
allReshapeToRGB(imageData, opt) {
148147

149148
// mean和std是介于0-1之间的
150-
const { mean, std, normalizeType = 0, targetShape } = opt;
149+
const { mean, std, targetShape } = opt;
151150
const [, c, h, w] = targetShape;
152151
const data = imageData.data || imageData;
153152

154153
const result = this.result;
155154
let offset = 0;
156-
155+
const normalizeType = 0; // 将数据映射为0~1, 1:映射为-1~1之间
157156
// h w c
158157
for (let i = 0; i < h; ++i) {
159158
const iw = i * w;
@@ -168,7 +167,6 @@ export default class MediaProcessor {
168167
}
169168
}
170169
}
171-
172170
return result;
173171
}
174172

@@ -261,7 +259,7 @@ export default class MediaProcessor {
261259
/**
262260
* 缩放成目标尺寸并居中
263261
*/
264-
fitToTargetSize(image, params, center?) {
262+
fitToTargetSize(image, params) {
265263
// 目标尺寸
266264
const targetWidth = params.targetSize.width;
267265
const targetHeight = params.targetSize.height;
@@ -284,12 +282,9 @@ export default class MediaProcessor {
284282
sh = Math.round(sw * this.pixelHeight / this.pixelWidth);
285283
y = Math.floor((targetHeight - sh) / 2);
286284
}
287-
if (center) {
288-
this.targetContext.drawImage(image, x, y, sw, sh);
289-
}
290-
else {
291-
this.targetContext.drawImage(image, 0, 0, sw, sh);
292-
}
285+
286+
this.targetContext.drawImage(image, x, y, sw, sh);
287+
293288
return { sw: targetWidth, sh: targetHeight };
294289
}
295290

packages/paddlejs-core/src/runner.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@ export default class Runner {
5050
needPreheat: boolean = true;
5151

5252
constructor(options: ModelConfig | null) {
53-
const opts = {
54-
fill: '#fff',
55-
scale: 256
56-
};
57-
this.modelConfig = Object.assign(opts, options);
53+
this.modelConfig = Object.assign({}, options);
5854
this.needPreheat = options.needPreheat === undefined ? true : options.needPreheat;
5955
this.modelName = options.modelName || Date.now().toString();
6056
this.weightMap = [];
@@ -209,7 +205,6 @@ export default class Runner {
209205
});
210206
}
211207
}
212-
213208
else {
214209
preheatFeedData = vars.find(item => item.name === 'image');
215210
if (preheatFeedData) {
@@ -264,6 +259,7 @@ export default class Runner {
264259
if (op.type === 'fetch') {
265260
return;
266261
}
262+
267263
op.execute(this.isExecuted);
268264
if (env.get('debug')
269265
&& op.opData?.outputTensors

0 commit comments

Comments
 (0)