2021-08-21 02:43:03 +02:00
|
|
|
// based on <https://github.com/andypotato/fingerpose>
|
|
|
|
|
|
|
|
import * as estimator from './estimator';
|
|
|
|
import { Finger, FingerCurl, FingerDirection } from './description';
|
|
|
|
import Gestures from './gestures';
|
|
|
|
|
|
|
|
const minConfidence = 0.7;
|
|
|
|
|
|
|
|
export function analyze(keypoints) { // get estimations of curl / direction for each finger
|
2021-09-19 20:07:53 +02:00
|
|
|
if (!keypoints || keypoints.length === 0) return null;
|
2021-08-21 02:43:03 +02:00
|
|
|
const estimatorRes = estimator.estimate(keypoints);
|
|
|
|
const landmarks = {};
|
|
|
|
for (const fingerIdx of Finger.all) {
|
|
|
|
landmarks[Finger.getName(fingerIdx)] = {
|
|
|
|
curl: FingerCurl.getName(estimatorRes.curls[fingerIdx]),
|
|
|
|
direction: FingerDirection.getName(estimatorRes.directions[fingerIdx]),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// console.log('finger landmarks', landmarks);
|
|
|
|
return landmarks;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function match(keypoints) { // compare gesture description to each known gesture
|
|
|
|
const poses: Array<{ name: string, confidence: number }> = [];
|
2021-09-19 20:07:53 +02:00
|
|
|
if (!keypoints || keypoints.length === 0) return poses;
|
|
|
|
const estimatorRes = estimator.estimate(keypoints);
|
2021-08-21 02:43:03 +02:00
|
|
|
for (const gesture of Gestures) {
|
|
|
|
const confidence = gesture.matchAgainst(estimatorRes.curls, estimatorRes.directions);
|
|
|
|
if (confidence >= minConfidence) poses.push({ name: gesture.name, confidence });
|
|
|
|
}
|
|
|
|
// console.log('finger poses', poses);
|
|
|
|
return poses;
|
|
|
|
}
|