@sssxyd/face-liveness-detector 0.2.32 → 0.2.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.esm.js +123 -159
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +123 -159
- package/dist/index.js.map +1 -1
- package/dist/types/library-loader.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.esm.js
CHANGED
|
@@ -314,7 +314,13 @@ function _detectBrowserEngine(userAgent) {
|
|
|
314
314
|
if (/firefox/i.test(ua) && !/seamonkey/i.test(ua)) {
|
|
315
315
|
return 'gecko';
|
|
316
316
|
}
|
|
317
|
-
// 2. 检测 WebKit
|
|
317
|
+
// 2. 检测 Chromium/Blink(必须在 WebKit 之前,因为 Chrome 的 user-agent 也包含 WebKit)
|
|
318
|
+
// Chrome-based browsers: Chrome, Chromium, Edge, Brave, Opera, Vivaldi, Whale, Arc, etc.
|
|
319
|
+
if (/chrome|chromium|crios|edge|edgios|edg|brave|opera|vivaldi|whale|arc|yabrowser|samsung|kiwi|ghostery/i.test(ua)) {
|
|
320
|
+
return 'chromium';
|
|
321
|
+
}
|
|
322
|
+
// 3. 检测 WebKit(真正的 Safari 和 iOS 浏览器)
|
|
323
|
+
// 注意:真正的 WebKit 浏览器(Safari)user-agent 不包含 Chrome 标识
|
|
318
324
|
// 包括:Safari、iOS 浏览器、以及那些虽然包含 Chrome 标识但实际是 WebKit 的浏览器(Quark、支付宝、微信等)
|
|
319
325
|
if (/webkit/i.test(ua)) {
|
|
320
326
|
// WebKit 特征明显,包括以下几种情况:
|
|
@@ -323,11 +329,6 @@ function _detectBrowserEngine(userAgent) {
|
|
|
323
329
|
// - Quark、支付宝、微信等虽然包含 Chrome 标识但是基于 WebKit 的浏览器
|
|
324
330
|
return 'webkit';
|
|
325
331
|
}
|
|
326
|
-
// 3. 检测 Chromium/Blink
|
|
327
|
-
// Chrome-based browsers: Chrome, Chromium, Edge, Brave, Opera, Vivaldi, Whale, Arc, etc.
|
|
328
|
-
if (/chrome|chromium|crios|edge|edgios|edg|brave|opera|vivaldi|whale|arc|yabrowser|samsung|kiwi|ghostery/i.test(ua)) {
|
|
329
|
-
return 'chromium';
|
|
330
|
-
}
|
|
331
332
|
// 4. 其他浏览器 - 保守方案,使用 WASM
|
|
332
333
|
return 'other';
|
|
333
334
|
}
|
|
@@ -567,16 +568,11 @@ function getCvSync() {
|
|
|
567
568
|
return null;
|
|
568
569
|
}
|
|
569
570
|
/**
|
|
570
|
-
*
|
|
571
|
-
* @param modelPath - Path to model files (optional)
|
|
572
|
-
* @param wasmPath - Path to WASM files (optional)
|
|
573
|
-
* @param preferredBackend - Preferred TensorFlow backend: 'auto' | 'webgl' | 'wasm' (default: 'auto')
|
|
574
|
-
* @returns Promise that resolves with Human instance
|
|
571
|
+
* Create Human.js configuration object
|
|
575
572
|
*/
|
|
576
|
-
|
|
577
|
-
const selectedBackend = _detectOptimalBackend(preferredBackend);
|
|
573
|
+
function _createHumanConfig(backend, modelPath, wasmPath) {
|
|
578
574
|
const config = {
|
|
579
|
-
backend
|
|
575
|
+
backend,
|
|
580
576
|
face: {
|
|
581
577
|
enabled: true,
|
|
582
578
|
detector: { rotation: false, return: true },
|
|
@@ -590,22 +586,80 @@ async function loadHuman(modelPath, wasmPath, preferredBackend) {
|
|
|
590
586
|
object: { enabled: false },
|
|
591
587
|
gesture: { enabled: true }
|
|
592
588
|
};
|
|
593
|
-
// 只在提供了路径时才设置,否则让 Human.js 使用默认加载策略
|
|
594
589
|
if (modelPath) {
|
|
595
590
|
config.modelBasePath = modelPath;
|
|
596
591
|
}
|
|
597
592
|
if (wasmPath) {
|
|
598
593
|
config.wasmPath = wasmPath;
|
|
599
594
|
}
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
595
|
+
return config;
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
* Load and verify Human.js models
|
|
599
|
+
*/
|
|
600
|
+
async function _loadAndVerifyHuman(human) {
|
|
601
|
+
const modelLoadStartTime = performance.now();
|
|
602
|
+
try {
|
|
603
|
+
await human.load();
|
|
604
|
+
}
|
|
605
|
+
catch (error) {
|
|
606
|
+
const errorMsg = error instanceof Error ? error.message : 'Unknown error';
|
|
607
|
+
const errorStack = error instanceof Error ? error.stack : 'N/A';
|
|
608
|
+
console.error('[FaceDetectionEngine] Error during human.load():', {
|
|
609
|
+
errorMsg,
|
|
610
|
+
stack: errorStack,
|
|
611
|
+
backend: human.config?.backend,
|
|
612
|
+
hasModels: !!human.models,
|
|
613
|
+
modelsKeys: human.models ? Object.keys(human.models).length : 0
|
|
614
|
+
});
|
|
615
|
+
throw new Error(`Model loading error: ${errorMsg}`);
|
|
616
|
+
}
|
|
617
|
+
const loadTime = performance.now() - modelLoadStartTime;
|
|
618
|
+
console.log('[FaceDetectionEngine] Human.js loaded successfully', {
|
|
619
|
+
modelLoadTime: `${loadTime.toFixed(2)}ms`,
|
|
620
|
+
version: human.version,
|
|
621
|
+
config: human.config
|
|
606
622
|
});
|
|
623
|
+
// 验证加载后的 Human 实例有必要的方法和属性
|
|
624
|
+
if (typeof human.detect !== 'function') {
|
|
625
|
+
throw new Error('Human.detect method not available after loading');
|
|
626
|
+
}
|
|
627
|
+
if (!human.version) {
|
|
628
|
+
console.warn('[FaceDetectionEngine] Human.js loaded but version is missing');
|
|
629
|
+
}
|
|
630
|
+
// 关键验证:检查模型是否真的加载了
|
|
631
|
+
if (!human.models || Object.keys(human.models).length === 0) {
|
|
632
|
+
console.error('[FaceDetectionEngine] CRITICAL: human.models is empty after loading!');
|
|
633
|
+
throw new Error('No models were loaded - human.models is empty');
|
|
634
|
+
}
|
|
635
|
+
// 打印加载的模型信息
|
|
636
|
+
if (human.models) {
|
|
637
|
+
const loadedModels = Object.entries(human.models).map(([name, model]) => ({
|
|
638
|
+
name,
|
|
639
|
+
loaded: model?.loaded || model?.state === 'loaded',
|
|
640
|
+
type: typeof model,
|
|
641
|
+
hasModel: !!model?.model,
|
|
642
|
+
keys: Object.keys(model).length
|
|
643
|
+
}));
|
|
644
|
+
console.log('[FaceDetectionEngine] All loaded models:', {
|
|
645
|
+
backend: human.config?.backend,
|
|
646
|
+
modelBasePath: human.config?.modelBasePath,
|
|
647
|
+
wasmPath: human.config?.wasmPath,
|
|
648
|
+
totalModels: Object.keys(human.models).length,
|
|
649
|
+
models: loadedModels,
|
|
650
|
+
allModelNames: Object.keys(human.models)
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
/**
|
|
655
|
+
* Try to load Human with a specific backend
|
|
656
|
+
* @param config The configuration object
|
|
657
|
+
* @param backend The backend to try
|
|
658
|
+
* @returns Human instance or null if fails
|
|
659
|
+
*/
|
|
660
|
+
async function _tryLoadHumanWithBackend(backend, modelPath, wasmPath) {
|
|
661
|
+
const config = _createHumanConfig(backend, modelPath, wasmPath);
|
|
607
662
|
const initStartTime = performance.now();
|
|
608
|
-
console.log('[FaceDetectionEngine] Creating Human instance...');
|
|
609
663
|
let human;
|
|
610
664
|
try {
|
|
611
665
|
human = new Human(config);
|
|
@@ -613,164 +667,74 @@ async function loadHuman(modelPath, wasmPath, preferredBackend) {
|
|
|
613
667
|
catch (error) {
|
|
614
668
|
const errorMsg = error instanceof Error ? error.message : 'Unknown error during Human instantiation';
|
|
615
669
|
const stack = error instanceof Error ? error.stack : 'N/A';
|
|
616
|
-
console.error(
|
|
670
|
+
console.error(`[FaceDetectionEngine] Failed to create Human instance (${backend}):`, {
|
|
617
671
|
errorMsg,
|
|
618
672
|
stack,
|
|
619
673
|
backend: config.backend,
|
|
620
674
|
userAgent: navigator.userAgent
|
|
621
675
|
});
|
|
622
|
-
|
|
676
|
+
return null;
|
|
623
677
|
}
|
|
624
|
-
const instanceCreateTime = performance.now() - initStartTime;
|
|
625
|
-
console.log(`[FaceDetectionEngine] Human instance created, took ${instanceCreateTime.toFixed(2)}ms`);
|
|
626
678
|
// 验证 Human 实例
|
|
627
679
|
if (!human) {
|
|
628
|
-
|
|
680
|
+
console.error(`[FaceDetectionEngine] Human instance is null (${backend})`);
|
|
681
|
+
return null;
|
|
629
682
|
}
|
|
630
683
|
// 验证 Human 实例结构(早期检测 WASM 问题)
|
|
631
684
|
if (!human.config) {
|
|
632
685
|
console.warn('[FaceDetectionEngine] Warning: human.config is missing');
|
|
633
686
|
}
|
|
634
|
-
console.log('[FaceDetectionEngine] Loading Human.js models...');
|
|
635
|
-
const modelLoadStartTime = performance.now();
|
|
636
687
|
try {
|
|
637
|
-
|
|
638
|
-
const loadTimeout = new Promise((_, reject) => {
|
|
639
|
-
const timeoutId = setTimeout(() => {
|
|
640
|
-
reject(new Error('Human.js load() timeout after 60 seconds - possible issue with model loading on mobile'));
|
|
641
|
-
}, 60000);
|
|
642
|
-
});
|
|
643
|
-
// 竞速:哪个先完成就用哪个
|
|
644
|
-
try {
|
|
645
|
-
await Promise.race([
|
|
646
|
-
human.load(),
|
|
647
|
-
loadTimeout
|
|
648
|
-
]);
|
|
649
|
-
}
|
|
650
|
-
catch (raceError) {
|
|
651
|
-
// 如果是超时错误,直接抛出
|
|
652
|
-
if (raceError instanceof Error && raceError.message.includes('timeout')) {
|
|
653
|
-
throw raceError;
|
|
654
|
-
}
|
|
655
|
-
// 其他错误,提供更多上下文
|
|
656
|
-
const raceErrorMsg = raceError instanceof Error ? raceError.message : 'Unknown error';
|
|
657
|
-
const raceErrorStack = raceError instanceof Error ? raceError.stack : 'N/A';
|
|
658
|
-
console.error('[FaceDetectionEngine] Error during human.load():', {
|
|
659
|
-
errorMsg: raceErrorMsg,
|
|
660
|
-
stack: raceErrorStack,
|
|
661
|
-
backend: config.backend,
|
|
662
|
-
hasModels: !!human.models,
|
|
663
|
-
modelsKeys: human.models ? Object.keys(human.models).length : 0
|
|
664
|
-
});
|
|
665
|
-
throw new Error(`Model loading error: ${raceErrorMsg}`);
|
|
666
|
-
}
|
|
667
|
-
const loadTime = performance.now() - modelLoadStartTime;
|
|
688
|
+
await _loadAndVerifyHuman(human);
|
|
668
689
|
const totalTime = performance.now() - initStartTime;
|
|
669
|
-
console.log(
|
|
670
|
-
modelLoadTime: `${loadTime.toFixed(2)}ms`,
|
|
671
|
-
totalInitTime: `${totalTime.toFixed(2)}ms`,
|
|
672
|
-
version: human.version,
|
|
673
|
-
config: human.config
|
|
674
|
-
});
|
|
675
|
-
// 验证加载后的 Human 实例有必要的方法和属性
|
|
676
|
-
if (typeof human.detect !== 'function') {
|
|
677
|
-
throw new Error('Human.detect method not available after loading');
|
|
678
|
-
}
|
|
679
|
-
if (!human.version) {
|
|
680
|
-
console.warn('[FaceDetectionEngine] Human.js loaded but version is missing');
|
|
681
|
-
}
|
|
682
|
-
// 关键验证:检查模型是否真的加载了
|
|
683
|
-
if (!human.models || Object.keys(human.models).length === 0) {
|
|
684
|
-
console.error('[FaceDetectionEngine] CRITICAL: human.models is empty after loading!');
|
|
685
|
-
throw new Error('No models were loaded - human.models is empty');
|
|
686
|
-
}
|
|
687
|
-
// 详细检查每个关键模型及其结构
|
|
688
|
-
const criticalModels = ['face', 'antispoof', 'liveness'];
|
|
689
|
-
const missingModels = [];
|
|
690
|
-
for (const modelName of criticalModels) {
|
|
691
|
-
const model = human.models[modelName];
|
|
692
|
-
if (!model) {
|
|
693
|
-
missingModels.push(modelName);
|
|
694
|
-
console.error(`[FaceDetectionEngine] CRITICAL: Model '${modelName}' is missing!`);
|
|
695
|
-
}
|
|
696
|
-
else {
|
|
697
|
-
const isLoaded = model.loaded || model.state === 'loaded' || !!model.model;
|
|
698
|
-
// 检查模型是否有必要的内部结构(防止 "Cannot read properties of undefined (reading 'inputs')" 错误)
|
|
699
|
-
const hasExecutor = !!model['executor'];
|
|
700
|
-
const hasInputs = !!model.inputs && Array.isArray(model.inputs) && model.inputs.length > 0;
|
|
701
|
-
const hasModelUrl = !!model['modelUrl'];
|
|
702
|
-
console.log(`[FaceDetectionEngine] Model '${modelName}':`, {
|
|
703
|
-
loaded: isLoaded,
|
|
704
|
-
state: model.state,
|
|
705
|
-
hasModel: !!model.model,
|
|
706
|
-
hasExecutor,
|
|
707
|
-
hasInputs,
|
|
708
|
-
hasModelUrl,
|
|
709
|
-
inputsType: typeof model.inputs,
|
|
710
|
-
inputsLength: Array.isArray(model.inputs) ? model.inputs.length : 'N/A'
|
|
711
|
-
});
|
|
712
|
-
// 严格检查:模型必须有以下结构才能正常工作
|
|
713
|
-
if (!isLoaded || !hasExecutor || !hasModelUrl) {
|
|
714
|
-
missingModels.push(`${modelName} (incomplete)`);
|
|
715
|
-
console.error(`[FaceDetectionEngine] WARNING: Model '${modelName}' may not be fully loaded - missing structure`);
|
|
716
|
-
}
|
|
717
|
-
// 如果 inputs 未定义会导致 "Cannot read properties of undefined (reading 'inputs')" 错误
|
|
718
|
-
if (!hasInputs && modelName !== 'antispoof') {
|
|
719
|
-
console.warn(`[FaceDetectionEngine] WARNING: Model '${modelName}' has no inputs - may cause errors during detection`);
|
|
720
|
-
missingModels.push(`${modelName} (no inputs)`);
|
|
721
|
-
}
|
|
722
|
-
}
|
|
723
|
-
}
|
|
724
|
-
if (missingModels.length > 0) {
|
|
725
|
-
console.error('[FaceDetectionEngine] Some critical models failed to load:', missingModels);
|
|
726
|
-
throw new Error(`Critical models not loaded: ${missingModels.join(', ')}`);
|
|
727
|
-
}
|
|
728
|
-
// 打印加载的模型信息
|
|
729
|
-
if (human.models) {
|
|
730
|
-
const loadedModels = Object.entries(human.models).map(([name, model]) => ({
|
|
731
|
-
name,
|
|
732
|
-
loaded: model?.loaded || model?.state === 'loaded',
|
|
733
|
-
type: typeof model,
|
|
734
|
-
hasModel: !!model?.model
|
|
735
|
-
}));
|
|
736
|
-
console.log('[FaceDetectionEngine] All loaded models:', {
|
|
737
|
-
totalModels: Object.keys(human.models).length,
|
|
738
|
-
models: loadedModels,
|
|
739
|
-
allModelNames: Object.keys(human.models)
|
|
740
|
-
});
|
|
741
|
-
}
|
|
690
|
+
console.log(`[FaceDetectionEngine] Successfully loaded Human.js with ${backend} backend in ${totalTime.toFixed(2)}ms`);
|
|
742
691
|
return human;
|
|
743
692
|
}
|
|
744
693
|
catch (error) {
|
|
745
694
|
const errorMsg = error instanceof Error ? error.message : 'Unknown error';
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
695
|
+
console.error(`[FaceDetectionEngine] Failed to load models with ${backend} backend:`, errorMsg);
|
|
696
|
+
return null;
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
/**
|
|
700
|
+
* Load Human.js
|
|
701
|
+
* @param modelPath - Path to model files (optional)
|
|
702
|
+
* @param wasmPath - Path to WASM files (optional)
|
|
703
|
+
* @param preferredBackend - Preferred TensorFlow backend: 'auto' | 'webgl' | 'wasm' (default: 'auto')
|
|
704
|
+
* @returns Promise that resolves with Human instance
|
|
705
|
+
*/
|
|
706
|
+
async function loadHuman(modelPath, wasmPath, preferredBackend) {
|
|
707
|
+
const selectedBackend = _detectOptimalBackend(preferredBackend);
|
|
708
|
+
console.log('[FaceDetectionEngine] Starting Human.js initialization:', {
|
|
709
|
+
selectedBackend,
|
|
710
|
+
modelBasePath: modelPath || '(using default)',
|
|
711
|
+
wasmPath: wasmPath || '(using default)',
|
|
712
|
+
userAgent: navigator.userAgent,
|
|
713
|
+
platform: navigator.platform
|
|
714
|
+
});
|
|
715
|
+
// 尝试用主后端加载
|
|
716
|
+
const human = await _tryLoadHumanWithBackend(selectedBackend, modelPath, wasmPath);
|
|
717
|
+
if (human) {
|
|
718
|
+
return human;
|
|
719
|
+
}
|
|
720
|
+
console.log(`[FaceDetectionEngine] Human.js loading failed with ${selectedBackend} backend.`);
|
|
721
|
+
// 尝试用备选后端加载(最多一次降级)
|
|
722
|
+
let fallbackBackend;
|
|
723
|
+
if (selectedBackend === 'wasm' && _isWebGLAvailable()) {
|
|
724
|
+
fallbackBackend = 'webgl';
|
|
725
|
+
}
|
|
726
|
+
else if (selectedBackend === 'webgl') {
|
|
727
|
+
fallbackBackend = 'wasm';
|
|
728
|
+
}
|
|
729
|
+
if (fallbackBackend) {
|
|
730
|
+
console.warn(`[FaceDetectionEngine] Primary backend (${selectedBackend}) failed, attempting fallback to ${fallbackBackend}...`);
|
|
731
|
+
const humanFallback = await _tryLoadHumanWithBackend(fallbackBackend, modelPath, wasmPath);
|
|
732
|
+
if (humanFallback) {
|
|
733
|
+
return humanFallback;
|
|
771
734
|
}
|
|
772
|
-
throw new Error(`Human.js loading failed: ${
|
|
735
|
+
throw new Error(`Human.js loading failed: both ${selectedBackend} and ${fallbackBackend} backends failed`);
|
|
773
736
|
}
|
|
737
|
+
throw new Error(`Human.js loading failed: ${selectedBackend} backend failed (no fallback available)`);
|
|
774
738
|
}
|
|
775
739
|
/**
|
|
776
740
|
* Extract OpenCV version from getBuildInformation
|