mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-30 12:01:20 -08:00
refactor(mask): [PKG-MASK-04] migrate strict types and preserve public consumers
This commit is contained in:
1 parent
aba125262a
commit
aca4bc1cd2
30 files changed
+1848
-72
No files matched your search
@@ -0,0 +1,51 @@
|
||||
import type { BodySegmenter } from '@tensorflow-models/body-segmentation'
|
||||
import type { Active, MaskConfig, SegmenterConfig } from './types'
|
||||
import * as bodySegmentation from '@tensorflow-models/body-segmentation'
|
||||
import * as tf from '@tensorflow/tfjs-core'
|
||||
import '@tensorflow/tfjs-backend-webgl'
|
||||
import '@tensorflow/tfjs-backend-cpu'
|
||||
|
||||
export async function loadSegmenter(config: MaskConfig, active: Active): Promise<BodySegmenter | null> {
|
||||
try {
|
||||
await tf.setBackend('webgl')
|
||||
}
|
||||
catch (error) {
|
||||
if (!active())
|
||||
return null
|
||||
// Keep the historical message property read, including non-Error rejection behavior.
|
||||
console.warn('WebGL backend not available, falling back to CPU', (error as { message: unknown }).message)
|
||||
await tf.setBackend('cpu')
|
||||
}
|
||||
if (!active())
|
||||
return null
|
||||
try {
|
||||
// Validate the full legacy object without dropping keys absent from the SDK config union.
|
||||
return await bodySegmentation.createSegmenter(bodySegmentation.SupportedModels.MediaPipeSelfieSegmentation, {
|
||||
runtime: 'mediapipe',
|
||||
modelType: 'general',
|
||||
solutionPath: config.solutionPath,
|
||||
modelSelection: config.modelSelection,
|
||||
smoothSegmentation: config.smoothSegmentation,
|
||||
minDetectionConfidence: config.minDetectionConfidence,
|
||||
minTrackingConfidence: config.minTrackingConfidence,
|
||||
selfieMode: config.selfieMode,
|
||||
} satisfies SegmenterConfig as SegmenterConfig)
|
||||
}
|
||||
catch (error) {
|
||||
if (active())
|
||||
console.error('Error initializing segmenter:', error)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export async function releaseSegmenter(segmenter: BodySegmenter): Promise<void> {
|
||||
try {
|
||||
await segmenter.dispose()
|
||||
}
|
||||
catch (error) {
|
||||
console.warn('Failed to dispose danmuku mask segmenter:', error)
|
||||
}
|
||||
}
|
||||
|
||||
export const toBinaryMask = (...args: Parameters<typeof bodySegmentation.toBinaryMask>) => bodySegmentation.toBinaryMask(...args)
|
||||
export const drawMask = (...args: Parameters<typeof bodySegmentation.drawMask>) => bodySegmentation.drawMask(...args)
|
||||
Reference in new issue
Block a user