AMTBioFeature WASM JS API

所有导出的 C 函数可通过 Module._FunctionName() 在 JavaScript 中直接调用。WASM 模块通过 createAMTBioFeature() 加载。

Module Loading

import createAMTBioFeature from './amtbiofeature.js';
const Module = await createAMTBioFeature({ print, printErr });

Lifecycle

FunctionReturnDescription
Module._AMTBioFeatureWasm_Init(model_data, model_size, config_data, config_size)intInitialize SDK (pass null, 0, null, 0 for defaults)
Module._AMTBioFeature_Init()intAlias — same as above with no args; returns 0 on success
Module._AMTBioFeature_Release()voidRelease SDK and free all internal resources
Module._AMTBioFeature_ReleaseHandle(handle)voidRelease a detection handle returned by DetectPixels
Module._AMTBioFeature_Version()char*Return version string (use Module.UTF8ToString())

Configuration

FunctionReturnDescription
Module._AMTBioFeature_SetMinSize(min_size, cls)voidSet minimum object size in px (cls=-1 for all, 1 for palm)
Module._AMTBioFeature_SetMaxCount(max_count, cls)voidSet max detections per frame
Module._AMTBioFeature_SetThreshold(first, second, cls)voidSet score thresholds (range 0.0–1.0)
Module._AMTBioFeature_SetInputSize(width, height)voidSet expected input image dimensions

Detection

FunctionReturnDescription
Module._AMTBioFeature_DetectPixels(handlePtr, dataPtr, width, height, channels, format)intZero-copy palm detection from raw RGB pixels. handlePtr is output (int*), format=0 for RGB. Returns 0 on success.

Usage:

const rgb = new Uint8Array(pixelCount * 3); // RGB888 pixels
const dataPtr = Module._malloc(rgb.length);
Module.HEAPU8.set(rgb, dataPtr);
const handlePtr = Module._malloc(4);

const ret = Module._AMTBioFeature_DetectPixels(
  handlePtr, dataPtr, width, height, 3, 0
);

if (ret === 0) {
  const handle = Module.getValue(handlePtr, 'i32');
  // ... get results ...
  Module._AMTBioFeature_ReleaseHandle(handle);
}
Module._free(handlePtr);
Module._free(dataPtr);

Results

FunctionReturnDescription
Module._AMTBioFeature_GetObjectCount(handle)intNumber of detected objects
Module._AMTBioFeature_GetObject(handle, index, objPtr)intPopulate TObject struct at objPtr. Returns 0 on success.

TObject struct layout (52 bytes):

OffsetTypeField
0i32class_id (1 = palm)
4floatbox.x
8floatbox.y
12floatbox.width
16floatbox.height
20i32 (pointer)landmarks pointer to TLandmark array
24i32landmark_count (9 for palm)
28floatyaw
32floatroll
36floatpitch
40floatscore (0.0–1.0)
44i32track_id
48i32state

Quality & Direction

FunctionReturnDescription
Module._AMTBioFeature_GetQualityColor(handle, index, scorePtr)intGet visible-light palm quality score (0–100). Returns > 0 on success.
Module._AMTBioFeature_GetDetectionQuality(handle, index, scorePtr)intGet overall detection quality score. Returns > 0 on success.
Module._AMTBioFeature_GetPalmDirection(handle, index, scorePtr)intGet palm direction: >= 0.5 = left hand, < 0.5 = right hand. Returns 0 on success.

License

FunctionReturnDescription
Module._AMTBioFeature_CheckLic()intReturns 0 if license is valid
Module._AMTBioFeature_SetLic(content)intSet license key string. Returns 0 on success.

Memory (Low-level)

FunctionDescription
Module._malloc(size)Allocate size bytes in WASM linear memory, return pointer
Module._free(ptr)Free previously allocated memory
Module.HEAPU8Uint8Array view of WASM memory for reading/writing pixel data
Module.getValue(ptr, type)Read a value at ptr ('i32', 'float', etc.)
Module.UTF8ToString(ptr)Convert null-terminated C string to JS string

Complete Example

import createAMTBioFeature from './amtbiofeature.js';

const Module = await createAMTBioFeature();

// Initialize
Module._AMTBioFeature_Init();

// Configure for palm detection
Module._AMTBioFeature_SetMinSize(80, 1);    // cls=1 = palm
Module._AMTBioFeature_SetMaxCount(1, 1);
Module._AMTBioFeature_SetThreshold(0.4, 0.5, 1);

// Prepare RGB888 pixel data
const dataPtr = Module._malloc(width * height * 3);
Module.HEAPU8.set(rgbPixels, dataPtr);

// Detect
const handlePtr = Module._malloc(4);
const ret = Module._AMTBioFeature_DetectPixels(
  handlePtr, dataPtr, width, height, 3, 0
);

if (ret === 0) {
  const handle = Module.getValue(handlePtr, 'i32');
  const count = Module._AMTBioFeature_GetObjectCount(handle);

  for (let i = 0; i < count; i++) {
    const scorePtr = Module._malloc(4);

    Module._AMTBioFeature_GetQualityColor(handle, i, scorePtr);
    const quality = Module.getValue(scorePtr, 'float');

    Module._AMTBioFeature_GetPalmDirection(handle, i, scorePtr);
    const direction = Module.getValue(scorePtr, 'float');
    const hand = direction >= 0.5 ? 'Left' : 'Right';

    Module._free(scorePtr);
  }

  Module._AMTBioFeature_ReleaseHandle(handle);
}

Module._free(handlePtr);
Module._free(dataPtr);