Module: VisageAnalyserUnityPlugin

VisageAnalyserUnityPlugin


The VisageAnalyserUnityPlugin is a native plugin (see Native Plugins) written in JavaScript for Unity. The plugin exposes face analysis features of visage|SDK for use in Unity WebGL build.

The plugin code is placed in VisageAnalyserUnityPlugin.jslib file and can be found in /lib folder.

Note: This version of the plugin is adapted to Unity version 2019.1, while for all older versions of Unity unityInstance in _estimateEmotion() function has to be renamed to gameInstance.

 Dependencies

The plugin depends on visageSDK.js file that loads visageSDK.data and visageAnalysisData.js that loads visageAnalysisData.data. Therefore, visageSDK.js has to be included in WebGL build output file - index.html, while visageAnalysisData.js has to be loaded by calling _preloadAnalysisData function with string parameter that represents link to the visageAnalysisData.js script relative to the main .html file.
For example, if visageAnalysisData.js is placed in the same folder as the main .html file:

	VisageTrackerNative._preloadAnalysisData("visageAnalysisData.js");


 Usage

For the functions to be accessible from C#, a C# wrapper interface is used. The example can be seen in the following function:

	[DllImport("__Internal")]
	public static extern void _initAnalyser(string initCallback);

The usage is demonstrated in VisageTrackerUnityDemo sample, VisageTrackerNativ.HTML5.cs file. All C# scripts are located in Assets/Scripts folder of the project.
In order to use VisageAnalyserUnityPlugin plugin in Unity projects visageSDK.js must be included in output index.html file (see link), visageAnalysisData.js must be loaded using _preloadAnalysisData function. The frame that will be forwarded to the functions for estimating age, gender and emotions has to be processed before using VisageTracker.

 Callbacks

Generally, it is common to use callback functions in JavaScript, due to its asynchronous nature.
Within the plugin, some functions are implemented so they expect a callback function name as a parameter. Callback functions are defined in C# code. Examples can be seen in _initAnalyser function.

Methods

<static> _preloadAnalysisData(fileURL)

Stores url and name of the visageAnalysisData.js file that loads visageAnalysisData.data into the file system. It is expected that the url will be relative to the main index.html file position. It has to be called before using any of the functionalities from visage|SDK. The recommendation for calling this function is within the function Awake() in Unity.
Parameters:
Name Type Description
fileURL string the name and path to the script file.

<static> _initAnalyser(callback)

Initializes VisageAnalyser.
In order to use VisageAnalyser functions visageAnalysisData.data must be preloaded by calling _preloadAnalysisData function at the very beginning of the code execution. The recommendation is to call this function in Awake() function in Unity. Parameter callback is name of function defined in Unity script. An example of a callback function definition and _initAnalyser function call:

	//callback function
	void initAnalyserCallback()
	{
		Debug.Log("AnalyserInited");
		AnalyserInitialized = true;
	}
	//call of the _initAnalyser() function:
	VisageTrackerNative._initAnalyser("initAnalyserCallback");

Parameters:
Name Type Description
callback string the name of the callback function.

<static> _estimateAge(faceIndex) → {number}

Estimates age from a facial image.
Parameters:
Name Type Description
faceIndex number value between 0 and MAX_FACES-1, used to access the data of a particular tracked face.
Returns:
Estimated age if estimation was successful or -1 if it failed.
Type
number

<static> _estimateEmotion(emotionsArray, faceIndex)

Estimates emotion from a facial image.
Parameters:
Name Type Description
emotionsArray Array 7-dimension array in which the estimated probabilities for basic emotions will be stored.
faceIndex number value between 0 and MAX_FACES-1, used to access the data of a particular tracked face.

<static> _estimateGender(faceIndex) → {number}

Estimates gender from a facial image.
Parameters:
Name Type Description
faceIndex number value between 0 and MAX_FACES-1, used to access the data of a particular tracked face.
Returns:
0 if estimated gender is female, 1 if it is a male and -1 if it failed.
Type
number