-
Notifications
You must be signed in to change notification settings - Fork 465
Expand file tree
/
Copy pathv1.ts
More file actions
184 lines (156 loc) · 5.01 KB
/
Copy pathv1.ts
File metadata and controls
184 lines (156 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
export default removeBackground;
export {
preload,
removeBackground,
removeForeground,
alphamask,
segmentForeground,
applySegmentationMask,
clearCache
};
export type { Config, ImageSource };
import memoize from 'lodash-es/memoize';
import { initInference, runInference } from '../inference';
import { clearCache as clearResourceCache } from '../resource';
import { Config, validateConfig } from '../schema';
import * as utils from '../utils';
import { ImageSource } from '../utils';
const init = memoize(initInference, (config) => JSON.stringify(config));
async function preload(configuration?: Config): Promise<void> {
await init(configuration);
return;
}
async function clearCache(): Promise<void> {
await clearResourceCache();
}
/**
* Removes the background from an image.
*
* @param image - The image to remove the background from.
* @param configuration - Optional configuration for the background removal process.
* @returns A Promise that resolves to the resulting image with the background removed.
*/
async function removeBackground(
image: ImageSource,
configuration?: Config
): Promise<Blob> {
const { config, session } = await init(configuration);
if (config.progress) config.progress('compute:decode', 0, 4);
const inputImageTensor = await utils.imageSourceToImageData(image, config);
config.progress?.('compute:inference', 1, 4);
const [alphamask, imageTensor] = await runInference(
inputImageTensor,
config,
session
);
config.progress?.('compute:mask', 2, 4);
const outImageTensor = imageTensor;
const [width, height] = outImageTensor.shape;
const stride = width * height;
for (let i = 0; i < stride; i += 1) {
outImageTensor.data[4 * i + 3] = alphamask.data[i];
}
config.progress?.('compute:encode', 3, 4);
const outImage = await utils.imageEncode(
outImageTensor,
config.output.quality,
config.output.format
);
config.progress?.('compute:encode', 4, 4);
return outImage;
}
/**
* Removes the foreground from an image.
*
* @param image - The image to remove the foreground from.
* @param configuration - Optional configuration for the foreground removal process.
* @returns A Promise that resolves to the resulting image with the foreground removed.
*/
async function removeForeground(
image: ImageSource,
configuration?: Config
): Promise<Blob> {
const { config, session } = await init(configuration);
const imageTensor = await utils.imageSourceToImageData(image, config);
const [alphamask, imageInput] = await runInference(
imageTensor,
config,
session
);
const outImageTensor = imageInput;
const [width, height, channels] = outImageTensor.shape;
const stride = width * height;
for (let i = 0; i < stride; i += 1) {
outImageTensor.data[4 * i + 3] = 255 - alphamask.data[i];
}
const outImage = await utils.imageEncode(
outImageTensor,
config.output.quality,
config.output.format
);
return outImage;
}
/**
* Segments the foreground of an image using a given configuration.
*
* @param image - The image source to segment.
* @param configuration - The optional configuration for the segmentation.
* @returns A Promise that resolves to the segmented foreground as a Blob.
*/
const alphamask = segmentForeground;
async function segmentForeground(
image: ImageSource,
configuration?: Config
): Promise<Blob> {
const { config, session } = await init(configuration);
const imageTensor = await utils.imageSourceToImageData(image, config);
let [height, width, channels] = imageTensor.shape;
const [alphamask, imageInput] = await runInference(
imageTensor,
config,
session
);
const stride = width * height;
const outImageTensor = imageTensor;
for (let i = 0; i < stride; i += 1) {
const index = 4 * i;
let alpha = alphamask.data[i];
outImageTensor.data[index] = 255;
outImageTensor.data[index + 1] = 255;
outImageTensor.data[index + 2] = 255;
outImageTensor.data[index + 3] = alpha;
}
const outImage = await utils.imageEncode(
outImageTensor,
config.output.quality,
config.output.format
);
return outImage;
}
async function applySegmentationMask(
image,
mask,
config?: Config
): Promise<Blob> {
config = validateConfig(config);
const imageTensor = await utils.imageSourceToImageData(image, config);
const [imageHeight, imageWidth, imageChannels] = imageTensor.shape;
const maskTensor = await utils.imageSourceToImageData(mask, config);
const [maskHeight, maskWidth, maskChannels] = maskTensor.shape;
const alphaMask =
maskHeight !== imageHeight || maskWidth !== imageWidth
? utils.tensorResizeBilinear(maskTensor, imageWidth, imageHeight)
: maskTensor;
const stride = imageWidth * imageHeight;
for (let i = 0; i < stride; i += 1) {
const idxImage = imageChannels * i;
const idxMask = maskChannels * i;
imageTensor.data[idxImage + 3] = alphaMask.data[idxMask + 3];
}
const outImage = await utils.imageEncode(
imageTensor,
config.output.quality,
config.output.format
);
return outImage;
}