Skip to content

Commit 15332de

Browse files
Merge pull request #187 from JingyuanZhang/master
feat(core): update mediaProcessor api and test
2 parents 7a09d46 + 54af6dc commit 15332de

File tree

5 files changed

+31
-197
lines changed

5 files changed

+31
-197
lines changed

packages/paddlejs-core/__tests__/spec/mediaProcessor.test.ts

Lines changed: 8 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ describe('test mediaProcessor with scale and targetSize', () => {
1717
const mediaParams = {
1818
gapFillWith: '#000',
1919
mean: [0.485, 0.456, 0.406],
20-
scale: 256,
2120
std: [0.229, 0.224, 0.225],
2221
feedShape: { fw: 224, fh: 224 },
2322
targetShape: [1, 3, 224, 224],
@@ -29,62 +28,22 @@ describe('test mediaProcessor with scale and targetSize', () => {
2928
processror.pixelWidth = 800;
3029
processror.pixelHeight = 600;
3130

32-
test('test api resizeAndFitTargetSize', () => {
33-
const imagedata = processror.resizeAndFitTargetSize(img, mediaParams);
34-
expect(imagedata.width).toBe(224);
35-
expect(imagedata.data.slice(0, 4).join(',')).toEqual('0,0,0,255');
36-
});
31+
const imageDataInfo = {
32+
dx: 0,
33+
dy: 0,
34+
dWidth: 224,
35+
dHeight: 224
36+
};
3737

38-
test('test api fitToTargetSize', () => {
39-
const res = processror.fitToTargetSize(img, mediaParams);
40-
expect(res.sw).toBe(224);
41-
});
42-
43-
test('test api reSize', () => {
44-
const res = processror.reSize(img, mediaParams);
45-
expect(res.sw).toBe(256);
46-
expect(res.sh).toBe(256);
47-
processror.pixelWidth = 600;
48-
processror.pixelHeight = 800;
49-
const res1 = processror.reSize(img, mediaParams);
50-
expect(res1.sw).toBe(256);
51-
expect(res1.sh).toBe(256);
52-
processror.pixelWidth = 700;
53-
processror.pixelHeight = 700;
54-
const res2 = processror.reSize(img, mediaParams);
55-
expect(res2.sw).toBe(256);
56-
expect(res2.sh).toBe(256);
57-
});
5838

5939
test('test api allReshapeToRGB', () => {
60-
const imagedata = processror.resizeAndFitTargetSize(img, mediaParams);
40+
processror.fitToTargetSize(img, mediaParams);
41+
const imagedata = processror.getImageData(imageDataInfo);
6142
const data = processror.allReshapeToRGB(imagedata, mediaParams);
6243
expect(data.length).toBe(150528);
6344
});
6445

65-
test('test api grayscale', () => {
66-
const imagedata = processror.resizeAndFitTargetSize(img, mediaParams);
67-
const avg = (imagedata.data[0] + imagedata.data[1] + imagedata.data[2]) / 3;
68-
const data = processror.grayscale(imagedata);
69-
expect(data[0]).toBe(avg);
70-
expect(data[3]).toBe(255);
71-
});
7246

73-
test('test api reshape', () => {
74-
const imagedata = {
75-
data: new Array(10 * 12 * 4).fill(0)
76-
};
77-
const data = processror.reshape(imagedata, {
78-
width: 5,
79-
height: 6,
80-
mean: [0.485, 0.456, 0.406],
81-
std: [0.229, 0.224, 0.225]
82-
}, {
83-
sw: 10,
84-
sh: 12
85-
});
86-
expect(data.length).toBe(90);
87-
});
8847

8948
test('test api process', () => {
9049
const data = processror.process(img, mediaParams);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ export interface ModelConfig {
4040
mean?: number[];
4141
std?: number[];
4242
bgr?: boolean;
43-
scale?: number;
4443
type?: GraphType; // model type
4544
needPreheat?: boolean;
4645
plugins?: { // tranform graph plugins

packages/paddlejs-core/src/mediaProcessor.ts

Lines changed: 21 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export default class MediaProcessor {
1414
std: number[] = [1, 1, 1];
1515
bgr: boolean = false;
1616
result: Float32Array | number[] = [];
17-
pixelWidth: number = 224;
18-
pixelHeight: number = 224;
17+
pixelWidth: number = 1;
18+
pixelHeight: number = 1;
1919
inputFeed: InputFeed[] = [];
2020

2121
constructor() {
@@ -28,17 +28,20 @@ export default class MediaProcessor {
2828
* @param inputs
2929
*/
3030
process(media, modelConfig): InputFeed[] {
31-
const { feedShape, fill, targetSize, scale, mean, std, bgr } = modelConfig;
31+
const { feedShape, fill, mean, std, bgr } = modelConfig;
3232
const { fh, fw } = feedShape;
3333
const input = media;
3434

35+
3536
const params = {
3637
gapFillWith: fill || this.gapFillWith,
3738
mean: mean || this.mean,
3839
std: std || this.std,
3940
bgr: bgr || this.bgr,
40-
scale,
41-
targetSize,
41+
targetSize: {
42+
width: fw,
43+
height: fh
44+
},
4245
targetShape: [1, 3, fh, fw]
4346
};
4447

@@ -52,7 +55,12 @@ export default class MediaProcessor {
5255

5356
fromPixels(pixels, opt): InputFeed[] {
5457
let data: ImageData | number[] | Float32Array = [];
55-
let scaleSize;
58+
const imageDataInfo = {
59+
dx: 0,
60+
dy: 0,
61+
dWidth: opt.targetSize.width,
62+
dHeight: opt.targetSize.height
63+
};
5664

5765
if (!(pixels instanceof HTMLImageElement
5866
|| pixels instanceof HTMLVideoElement
@@ -66,79 +74,28 @@ export default class MediaProcessor {
6674

6775
this.pixelWidth = pixels.width;
6876
this.pixelHeight = pixels.height;
69-
if (opt.scale && opt.targetSize) {
70-
data = this.resizeAndFitTargetSize(pixels, opt);
71-
}
72-
else if (opt.targetSize) { // 如果有 targetSize,就是装在目标宽高里的模式
73-
scaleSize = this.fitToTargetSize(pixels, opt);
74-
data = this.getImageData(0, 0, scaleSize);
75-
}
76-
else {
77-
scaleSize = this.reSize(pixels, opt);
78-
data = this.getImageData(0, 0, scaleSize);
79-
}
80-
if (opt.gray) {
81-
data = this.grayscale(data);
82-
}
8377

84-
if (opt.reShape) {
85-
data = this.reshape(data, opt, scaleSize);
86-
}
78+
this.fitToTargetSize(pixels, opt);
79+
data = this.getImageData(imageDataInfo);
8780

8881
// process imageData in webgl
8982
if (env.get('webgl_feed_process')) {
9083
data = Float32Array.from((data as ImageData).data);
9184
return [{
92-
data: data,
85+
data,
9386
shape: [1, 4, opt.targetShape[2], opt.targetShape[3]],
9487
name: 'image'
9588
}] as InputFeed[];
9689
}
9790

9891
data = this.allReshapeToRGB(data, opt) as number[];
9992
return [{
100-
data: data,
93+
data,
10194
shape: opt.targetShape || opt.shape,
10295
name: 'image'
10396
}] as InputFeed[];
10497
}
10598

106-
/**
107-
* crop图像&重新设定图片tensor形状
108-
* @param shape
109-
*/
110-
reshape(imageData, opt, scaleSize) {
111-
const { sw, sh } = scaleSize;
112-
const { width, height } = opt;
113-
const hPadding = Math.ceil((sw - width) / 2);
114-
const vPadding = Math.ceil((sh - height) / 2);
115-
116-
const data = imageData.data;
117-
// channel RGB
118-
const red: number[] = [];
119-
const green: number[] = [];
120-
const blue: number[] = [];
121-
// 平均数
122-
const mean = opt.mean;
123-
// 标准差
124-
const std = opt.std;
125-
// 考虑channel因素获取数据
126-
for (let i = 0; i < data.length; i += 4) {
127-
// img_mean 0.485, 0.456, 0.406
128-
// img_std 0.229, 0.224, 0.225
129-
const index = i / 4;
130-
const vIndex = Math.floor(index / sw);
131-
const hIndex = index - (vIndex * sw) - 1;
132-
if (hIndex >= hPadding && hIndex < (hPadding + width)
133-
&& vIndex >= vPadding && vIndex < (vPadding + height)) {
134-
red.push(((data[i] / 255) - mean[0]) / std[0]); // red
135-
green.push(((data[i + 1] / 255) - mean[1]) / std[1]); // green
136-
blue.push(((data[i + 2] / 255) - mean[2]) / std[2]); // blue
137-
}
138-
}
139-
// 转成 GPU 加速 NCHW 格式
140-
return red.concat(green.concat(blue));
141-
}
14299

143100
/**
144101
* 全部转rgb * H * W
@@ -174,60 +131,6 @@ export default class MediaProcessor {
174131
return result;
175132
}
176133

177-
/**
178-
* 根据scale缩放图像
179-
* @param image
180-
* @param params
181-
* @return {Object} 缩放后的尺寸
182-
*/
183-
reSize(image, params) {
184-
// 原始图片宽高
185-
const width = this.pixelWidth;
186-
const height = this.pixelHeight;
187-
// 缩放后的宽高
188-
let sw = width;
189-
let sh = height;
190-
sw = sh = params.scale;
191-
192-
this.targetContext.canvas.width = sw;
193-
this.targetContext.canvas.height = sh;
194-
this.targetContext.drawImage(
195-
image, 0, 0, sw, sh);
196-
return { sw, sh };
197-
}
198-
199-
/**
200-
* 根据scale缩放图像并且缩放成目标尺寸并居中
201-
*/
202-
resizeAndFitTargetSize(image, params) {
203-
// 原始图片宽高
204-
const width = this.pixelWidth;
205-
const height = this.pixelHeight;
206-
// 缩放后的宽高
207-
let sw = width;
208-
let sh = height;
209-
// 最小边缩放到scale
210-
if (width < height) {
211-
sw = params.scale;
212-
sh = Math.round(sw * height / width);
213-
}
214-
else {
215-
sh = params.scale;
216-
sw = Math.round(sh * width / height);
217-
}
218-
219-
this.targetContext.canvas.width = sw;
220-
this.targetContext.canvas.height = sh;
221-
const targetWidth = params.targetSize.width;
222-
const targetHeight = params.targetSize.height;
223-
this.targetContext.drawImage(image, 0, 0, sw, sh);
224-
const x = (sw - targetWidth) / 2;
225-
const y = (sh - targetHeight) / 2;
226-
sw = targetWidth;
227-
sh = targetHeight;
228-
const data = this.getImageData(x, y, { sw, sh });
229-
return data;
230-
}
231134

232135
/**
233136
* 缩放成目标尺寸并居中
@@ -257,37 +160,18 @@ export default class MediaProcessor {
257160
}
258161

259162
this.targetContext.drawImage(image, x, y, sw, sh);
260-
261-
return { sw: targetWidth, sh: targetHeight };
262163
}
263164

264165
/**
265166
* 获取图像内容
266167
* @param pixels
267168
* @returns {Uint8ClampedArray}
268169
*/
269-
getImageData(x, y, scaleSize) {
170+
getImageData(imageDataInfo) {
270171

271-
const { sw, sh } = scaleSize;
172+
const { dx, dy, dWidth, dHeight } = imageDataInfo;
272173
// 复制画布上指定矩形的像素数据
273-
return this.targetContext.getImageData(x, y, sw, sh);
174+
return this.targetContext.getImageData(dx, dy, dWidth, dHeight);
274175
}
275176

276-
/**
277-
* 计算灰度图
278-
* @param imageData
279-
* @returns {*}
280-
*/
281-
grayscale(imageData) {
282-
const data = imageData.data;
283-
284-
for (let i = 0; i < data.length; i += 4) {
285-
// 3 channel 灰度处理无空间压缩
286-
const avg = (data[i] + data[i + 1] + data[i + 2]) / 3;
287-
data[i] = avg; // red
288-
data[i + 1] = avg; // green
289-
data[i + 2] = avg; // blue
290-
}
291-
return data;
292-
}
293177
}

packages/paddlejs-models/gesture/package-lock.json

Lines changed: 1 addition & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/paddlejs-models/gesture/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@paddlejs-models/gesture",
3-
"version": "0.0.1",
3+
"version": "0.0.3",
44
"description": "",
55
"main": "lib/index",
66
"scripts": {

0 commit comments

Comments
 (0)