Last active
November 12, 2023 07:38
-
-
Save mupakoz/c7b3183914b52a08eebbc61599af7e1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
PoseLandmarker, | |
type NormalizedLandmark, | |
type PoseLandmarkerResult | |
} from '@mediapipe/tasks-vision' | |
const poseLandmarker: PoseLandmarker | |
let prevSmoothedLandmarks: NormalizedLandmark[] = [] | |
const smoothingFactor = 0.75 | |
/// initialize pose landmarker | |
poseLandmarker.detectForVideo( | |
videoElement, | |
startTimeMs, | |
(result: PoseLandmarkerResult) => { | |
const smoothedLandmarks = this.smoothLandmarks( | |
result.landmarks[0], | |
prevSmoothedLandmarks, | |
smoothingFactor | |
) | |
prevSmoothedLandmarks = smoothedLandmarks | |
const resultWithSmoothedLandmarks = { | |
...result, | |
landmarks: [smoothedLandmarks] | |
} | |
// use the new result | |
} | |
) | |
smoothLandmarks( | |
currentLandmarks: NormalizedLandmark[], | |
prevSmoothedLandmarks: NormalizedLandmark[], | |
smoothingFactor: number | |
) { | |
if (prevSmoothedLandmarks.length === 0) { | |
// Initial case, no smoothing needed | |
return currentLandmarks | |
} | |
// Smooth each landmark | |
const smoothedLandmarks = currentLandmarks.map((currentLandmark, i) => { | |
const prevSmoothedLandmark = prevSmoothedLandmarks[i] | |
return { | |
x: (1 - smoothingFactor) * currentLandmark.x + smoothingFactor * prevSmoothedLandmark.x, | |
y: (1 - smoothingFactor) * currentLandmark.y + smoothingFactor * prevSmoothedLandmark.y, | |
z: (1 - smoothingFactor) * currentLandmark.z + smoothingFactor * prevSmoothedLandmark.z | |
} | |
}) | |
return smoothedLandmarks | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment