Regular Mode
In regular mode, if you call getOutput
to get the output stream in the cameraReady
callback, because the effects are not working yet at this point, the stream obtained will be the same as the input stream fed into the SDK. After the effects start to work, the SDK will apply them to the output stream automatically. You don’t need to call getOutput
again. You can use this method if you want to display a video image as soon as possible but do not need to apply effects to the video the moment it is played.
In contrast, if you call getOutput
in the ready
callback, the output stream obtained will have been processed. Because the ready
callback occurs later than cameraReady
, you can call getOutput
in the ready
callback if you want to apply effects to the video the moment it is displayed, but do not expect the video to be played as soon as possible.
Sample code:
const authData = {
licenseKey: 'xxxxxxxxx',
appId: 'xxx',
authFunc: authFunc
};
const config = {
auth: authData,
beautify: {
whiten: 0.1,
dermabrasion: 0.5,
lift: 0,
shave: 0,
eye: 0.2,
chin: 0,
},
input: inputStream
}
const sdk = new ArSdk(
config
)
sdk.on('created', () => {
sdk.getEffectList({
Type: 'Preset',
Label: 'Makeup',
}).then(res => {
effectList = res
});
sdk.getCommonFilter().then(res => {
filterList = res
})
})
sdk.on('cameraReady', async () => {
const output = await sdk.getOutput()
...
})
sdk.on('ready', async () => {
const output = await sdk.getOutput()
...
})
Pre-Initialization
When the SDK is loaded for the first time, it needs to download static resources in order to initialize the detection module. As a result, the loading of the SDK is affected by network conditions. Given that in some scenarios, you may want to display a video image as soon as possible, we offer a pre-initialization plan that loads static resources in advance.
Use cases for pre-initialization
Case 1: The effect SDK is not needed when the webpage is initialized. A video is displayed only after the user performs certain operation.
Case 2: Effects are needed for page B, and page B is directed from page A.
In such cases, you can load resources in advance (as early as possible), feed an input stream to the SDK when necessary, and then get a processed output stream.
For example, in case 1, you can load resources when the webpage is initialized; in case 2, you can get an SDK instance on page A and pass it to page B.
The code below works for case 1
<button id="start">Enable the camera</button>
const authData = {
licenseKey: 'xxxxxxxxx',
appId: 'xxx',
authFunc: authFunc
};
const config = {
auth: authData,
beautify: {
whiten: 0.1,
dermabrasion: 0.5,
lift: 0,
shave: 0,
eye: 0.2,
chin: 0,
},
}
const sdk = new ArSdk(
config
)
sdk.on('created', () => {
sdk.getEffectList({
Type: 'Preset',
Label: 'Makeup',
}).then(res => {
effectList = res
});
sdk.getCommonFilter().then(res => {
filterList = res
})
})
sdk.on('resourceReady', () => {
})
sdk.on('ready', async () => {
const output = await sdk.getOutput()
...
})
document.getElementById('start').onclick = async function(){
const devices = await navigator.mediaDevices.enumerateDevices()
const cameraDevice = devices.find(d=>d.kind === 'videoinput')
navigator.mediaDevices.getUserMedia({
audio: false,
video: {
deviceId: cameraDevice.deviceId
...
}
}).then(mediaStream => {
sdk.initCore({
input: mediaStream
})
})
}
Was this page helpful?