pose-gesture-sdk 1.2.4 → 1.2.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pose-gesture-sdk",
3
- "version": "1.2.4",
3
+ "version": "1.2.5",
4
4
  "description": "基于 MediaPipe Tasks(PoseLandmarker)的手语/手势采集状态机 SDK,纯本地运行、零外网依赖。举手保持 startSeconds 秒返回 start,手放下/手消失保持 endSeconds 秒返回 end。",
5
5
  "main": "pose-gesture-sdk.js",
6
6
  "module": "pose-gesture-sdk.js",
@@ -135,6 +135,8 @@
135
135
  handDownBodyRatio: 0.7, // 主判定:手腕低于肩部该倍数的肩髋竖直距离(按人体比例,与机位远近无关);null 关闭
136
136
  handDownHeightRatio: 0.35, // 回退判定(髋不可见/人体非直立时):手腕低于肩部该归一化高度差;null 关闭
137
137
  angleUseZ: false, // 关节角是否启用深度轴 z:默认关闭(MediaPipe z 噪声大,2D 屏幕面角度更稳定)
138
+ /* ---- 裁剪尺寸计算(调 getCropSize() 获取,start 后自动计算) ---- */
139
+ cropMarginRatio: 0.5, // 人体左右边界之外额外预留的间距 = 双臂横向伸展长度 × 该比例(越大越保守,越不会裁到手臂)
138
140
  };
139
141
 
140
142
  // 配置回调字段 -> 事件名 的映射
@@ -225,6 +227,8 @@
225
227
  this._raiseSince = 0; // 举手保持的起始时间戳(ms)
226
228
  this._downSince = 0; // 结束姿态(双手放下/离开画面)保持的起始时间戳(ms)
227
229
  this._metrics = null; // 最近一帧的姿态指标
230
+ this._lastLandmarks = null; // 最近一帧的关键点(供 getCropSize() 计算裁剪尺寸)
231
+ this._cropSize = null; // 居中裁剪尺寸 { width, height }(start 后自动计算,偶数像素)
228
232
  this._listeners = {}; // 事件名 -> 回调集合
229
233
  // 绑定循环回调,确保 requestAnimationFrame 中 this 指向实例
230
234
  this._loop = this._loop.bind(this);
@@ -403,6 +407,81 @@
403
407
  };
404
408
  };
405
409
 
410
+ /** 内部:取偶数(>=2),用于裁剪宽度/高度必须为偶数 */
411
+ PoseGestureSDK.prototype._roundEven = function (v) {
412
+ var r = Math.round(v / 2) * 2;
413
+ return Math.max(2, r);
414
+ };
415
+
416
+ /**
417
+ * 内部:根据视频实际分辨率和当前人体关键点,计算居中裁剪后的宽高(像素)。
418
+ * 裁剪粒度 = 人体外接框,并在左右各预留「双臂横向伸展长度 + 该长度 × cropMarginRatio」的余量,
419
+ * 保证即使双臂张开也不会裁到手臂;高度按人体高度等比例预留余量。
420
+ * 结果宽高均为偶数,且不超过视频实际尺寸。
421
+ */
422
+ PoseGestureSDK.prototype._updateCropSize = function () {
423
+ this._cropSize = null;
424
+ var video = this._videoEl;
425
+ if (!video) return;
426
+ var vw = video.videoWidth, vh = video.videoHeight;
427
+ if (!vw || !vh || !this._lastLandmarks) return;
428
+ var lm = this._lastLandmarks;
429
+ var cfg = this._config;
430
+ var ratio = (cfg && cfg.cropMarginRatio != null) ? cfg.cropMarginRatio : 0.5;
431
+
432
+ // 收集可见关键点,求极大体框(x 方向含手臂,y 方向含头到脚)
433
+ var xs = [], ys = [];
434
+ var lS, rS, lW, rW;
435
+ lm.forEach(function (p, i) {
436
+ if (!p || (p.visibility != null && p.visibility < 0.2)) return; // 低置信度点不参与框定,避免离场噪点撑大画面
437
+ xs.push(p.x); ys.push(p.y);
438
+ if (i === LM.LEFT_SHOULDER) lS = p;
439
+ else if (i === LM.RIGHT_SHOULDER) rS = p;
440
+ else if (i === LM.LEFT_WRIST) lW = p;
441
+ else if (i === LM.RIGHT_WRIST) rW = p;
442
+ });
443
+ if (!xs.length) return;
444
+
445
+ var left = Math.min.apply(null, xs), right = Math.max.apply(null, xs);
446
+ var top = Math.min.apply(null, ys), bottom = Math.max.apply(null, ys);
447
+ var centerX = (left + right) / 2;
448
+ var bodyW = (right - left) * vw; // 人体横向外接宽度
449
+ var bodyH = (bottom - top) * vh; // 人体纵向外接高度
450
+
451
+ // 双臂横向伸展长度:各侧 腕-肩 的 x 方向距离(优先腕,可退回肘),取较大一侧
452
+ var armReachPx = 0;
453
+ [[lS, lW], [rS, rW]].forEach(function (pair) {
454
+ if (pair[0] && pair[1]) {
455
+ var reach = Math.abs(pair[1].x - pair[0].x) * vw;
456
+ if (reach > armReachPx) armReachPx = reach;
457
+ }
458
+ });
459
+ // 每侧预留:双臂伸展长度 + 余量(cropMarginRatio × 伸展长度);未检测到手臂时按体宽的 0.4 兜底
460
+ var armMargin = armReachPx > 0 ? armReachPx * (1 + ratio) : bodyW * 0.4;
461
+ var width = this._roundEven(Math.min(vw, bodyW + armMargin * 2)); // 居中裁剪:左右各留一份余量
462
+
463
+ // 高度:人体高度 + 上下各留 比例 的高度余量(上下对称)
464
+ var height = this._roundEven(Math.min(vh, bodyH * (1 + ratio * 2)));
465
+
466
+ // 中心点不在画面正中时也不超过边界(此时允许略偏,保证完整容纳人体)
467
+ this._cropSize = { width: width, height: height, sourceWidth: vw, sourceHeight: vh };
468
+ };
469
+
470
+ /**
471
+ * 获取居中裁剪后的视频尺寸(宽高均为偶数,单位像素)。
472
+ * 该尺寸在 start 时已自动计算并缓存,据此可生成不裁到人体、且预留双臂伸展余量的裁剪区域。
473
+ * @returns {{source:{width:number,height:number}, crop:{width:number,height:number}}|null}
474
+ * source:裁剪前原始视频尺寸(= 实际视频流分辨率);crop:裁剪后尺寸(偶数像素)。
475
+ * 未初始化或当前帧无人体时为 null
476
+ */
477
+ PoseGestureSDK.prototype.getCropSize = function () {
478
+ if (!this._cropSize) return null;
479
+ return {
480
+ source: { width: this._cropSize.sourceWidth, height: this._cropSize.sourceHeight },
481
+ crop: { width: this._cropSize.width, height: this._cropSize.height },
482
+ };
483
+ };
484
+
406
485
  /**
407
486
  * 手动触发采集开始:不依赖举手手势,直接把状态机置为采集中(collecting)并触发 'start' 事件。
408
487
  * 用于业务侧已确认开始、调用方主动开启采集的场景(程序化触发)。
@@ -415,6 +494,7 @@
415
494
  if (this._state !== STATE.IDLE && this._state !== STATE.PREPARING) return this; // 已在采集中则忽略
416
495
  var now = (typeof performance !== 'undefined' ? performance.now() : Date.now());
417
496
  this._resetHold();
497
+ this._updateCropSize(); // start 后自动计算居中裁剪尺寸(getCropSize() 读取)
418
498
  this._setState(STATE.COLLECTING);
419
499
  this._emit('start', { timestamp: now, metrics: this._metrics, manual: true, meta: opts || null });
420
500
  return this;
@@ -649,6 +729,7 @@
649
729
  try {
650
730
  var result = this._landmarker.detectForVideo(this._videoEl, ts);
651
731
  var landmarks = result.landmarks && result.landmarks[0];
732
+ this._lastLandmarks = landmarks; // 记录最近一帧关键点,供 getCropSize() 计算裁剪尺寸
652
733
  this._evaluate(landmarks);
653
734
  if (this._canvasEl) this._drawSkeleton(landmarks);
654
735
  this._emit('landmarks', { result: result, landmarks: landmarks });
@@ -769,6 +850,7 @@
769
850
  this._emit('progress', { phase: STATE.PREPARING, elapsed: elapsedP, remain: remainP, from: cfg.startSeconds, metrics: this._metrics });
770
851
  if (elapsedP >= cfg.startSeconds) {
771
852
  this._resetHold();
853
+ this._updateCropSize(); // start 后自动计算居中裁剪尺寸(getCropSize() 读取)
772
854
  this._setState(STATE.COLLECTING);
773
855
  this._emit('start', { timestamp: now, metrics: this._metrics });
774
856
  }