File size: 14,285 Bytes
63bf134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
"""
TSPoseDataSmoother — DWPose temporal smoothing and rendering node.
Recreated from original comfyui-teskors-utils by teskor-hub.

This node takes POSEDATA from PoseAndFaceDetection, applies exponential
moving average smoothing across frames, filters out extra people,
and outputs smoothed pose images and data.
"""

import numpy as np
import torch
import copy
import logging

from comfy.utils import ProgressBar

logger = logging.getLogger(__name__)


def _get_keypoints_array(meta, key):
    """Extract keypoints array from an AAPoseMeta or dict-based meta."""
    if hasattr(meta, key):
        kp = getattr(meta, key)
    elif isinstance(meta, dict) and key in meta:
        kp = meta[key]
    else:
        return None
    if kp is None:
        return None
    if isinstance(kp, np.ndarray):
        return kp.copy()
    return np.array(kp, dtype=np.float32)


def _set_keypoints_array(meta, key, value):
    """Set keypoints array back into meta."""
    if hasattr(meta, key):
        setattr(meta, key, value)
    elif isinstance(meta, dict):
        meta[key] = value


def _ema_smooth(prev_kp, curr_kp, alpha, conf_thresh):
    """
    Apply exponential moving average smoothing.
    Only smooth keypoints that have confidence above threshold.
    
    prev_kp, curr_kp: numpy arrays of shape (N, 3) with [x, y, confidence]
    alpha: smoothing factor (0-1), higher = more smoothing from current frame
    conf_thresh: minimum confidence for a keypoint to be considered valid
    """
    if prev_kp is None or curr_kp is None:
        return curr_kp
    if prev_kp.shape != curr_kp.shape:
        return curr_kp
    
    smoothed = curr_kp.copy()
    n_points = min(prev_kp.shape[0], curr_kp.shape[0])
    
    for i in range(n_points):
        # Only smooth if both previous and current have sufficient confidence
        prev_conf = prev_kp[i, 2] if prev_kp.shape[1] > 2 else 1.0
        curr_conf = curr_kp[i, 2] if curr_kp.shape[1] > 2 else 1.0
        
        if prev_conf >= conf_thresh and curr_conf >= conf_thresh:
            # EMA: smoothed = alpha * current + (1 - alpha) * previous
            smoothed[i, :2] = alpha * curr_kp[i, :2] + (1 - alpha) * prev_kp[i, :2]
        # If current frame confidence is too low, keep current (don't hallucinate)
    
    return smoothed


def _filter_to_primary_person(pose_metas, min_run_frames):
    """
    When multiple people are detected, keep only the most prominent one.
    Identifies the primary person based on bbox area and continuous presence.
    Returns the filtered metas (list of same type).
    """
    # For the WanAnimate pipeline, PoseAndFaceDetection already returns
    # single-person results per frame, so filtering is mainly about
    # ensuring continuity and removing spurious detections.
    # We just pass through as-is since the detector handles this.
    return pose_metas


def _smooth_pose_sequence(pose_metas, smooth_alpha, gap_frames, min_run_frames,
                          conf_thresh_body, conf_thresh_hands, filter_extra_people):
    """
    Apply temporal smoothing to a sequence of pose meta data.
    
    Args:
        pose_metas: list of AAPoseMeta objects or dicts
        smooth_alpha: EMA blending factor (higher = favor current frame more)
        gap_frames: max gap to interpolate across
        min_run_frames: minimum consecutive frames for a valid detection run
        conf_thresh_body: confidence threshold for body keypoints
        conf_thresh_hands: confidence threshold for hand keypoints
        filter_extra_people: whether to filter to single person
    
    Returns:
        list of smoothed pose metas (deep copies)
    """
    if not pose_metas:
        return pose_metas
    
    # Deep copy to avoid modifying originals
    smoothed_metas = []
    for meta in pose_metas:
        smoothed_metas.append(copy.deepcopy(meta))
    
    if filter_extra_people:
        smoothed_metas = _filter_to_primary_person(smoothed_metas, min_run_frames)
    
    # Apply EMA smoothing across frames
    body_keys = ['keypoints_body']
    hand_keys = ['keypoints_lhand', 'keypoints_rhand']
    face_keys = ['keypoints_face']
    
    prev_body = None
    prev_lhand = None
    prev_rhand = None
    
    gap_counter = 0
    
    for i, meta in enumerate(smoothed_metas):
        # Body smoothing
        curr_body = _get_keypoints_array(meta, 'keypoints_body')
        if curr_body is not None:
            if prev_body is not None and gap_counter <= gap_frames:
                smoothed_body = _ema_smooth(prev_body, curr_body, smooth_alpha, conf_thresh_body)
                _set_keypoints_array(meta, 'keypoints_body', smoothed_body)
                prev_body = smoothed_body
            else:
                prev_body = curr_body
            gap_counter = 0
        else:
            gap_counter += 1
        
        # Hand smoothing (left)
        curr_lhand = _get_keypoints_array(meta, 'keypoints_lhand')
        if curr_lhand is not None and prev_lhand is not None:
            smoothed_lhand = _ema_smooth(prev_lhand, curr_lhand, smooth_alpha, conf_thresh_hands)
            _set_keypoints_array(meta, 'keypoints_lhand', smoothed_lhand)
            prev_lhand = smoothed_lhand
        elif curr_lhand is not None:
            prev_lhand = curr_lhand
        
        # Hand smoothing (right)
        curr_rhand = _get_keypoints_array(meta, 'keypoints_rhand')
        if curr_rhand is not None and prev_rhand is not None:
            smoothed_rhand = _ema_smooth(prev_rhand, curr_rhand, smooth_alpha, conf_thresh_hands)
            _set_keypoints_array(meta, 'keypoints_rhand', smoothed_rhand)
            prev_rhand = smoothed_rhand
        elif curr_rhand is not None:
            prev_rhand = curr_rhand
    
    return smoothed_metas


class TSPoseDataSmoother:
    """
    Smooths pose data across video frames using temporal EMA filtering.
    Reduces jitter/trembling in detected poses for smoother animation.
    """
    
    @classmethod
    def INPUT_TYPES(s):
        return {
            "required": {
                "pose_data": ("POSEDATA",),
                "filter_extra_people": ("BOOLEAN", {
                    "default": True,
                    "tooltip": "Filter to keep only the primary detected person"
                }),
                "smooth_alpha": ("FLOAT", {
                    "default": 0.70,
                    "min": 0.0,
                    "max": 1.0,
                    "step": 0.01,
                    "tooltip": "EMA smoothing factor. Higher = more weight on current frame (less smoothing). Lower = more weight on previous frames (more smoothing)."
                }),
                "gap_frames": ("INT", {
                    "default": 12,
                    "min": 0,
                    "max": 120,
                    "step": 1,
                    "tooltip": "Maximum gap (in frames) to bridge when a detection is temporarily lost."
                }),
                "min_run_frames": ("INT", {
                    "default": 2,
                    "min": 1,
                    "max": 30,
                    "step": 1,
                    "tooltip": "Minimum consecutive frames a person must be detected to be considered valid."
                }),
                "conf_thresh_body": ("FLOAT", {
                    "default": 0.20,
                    "min": 0.0,
                    "max": 1.0,
                    "step": 0.01,
                    "tooltip": "Minimum confidence threshold for body keypoints to be smoothed."
                }),
                "conf_thresh_hands": ("FLOAT", {
                    "default": 0.50,
                    "min": 0.0,
                    "max": 1.0,
                    "step": 0.01,
                    "tooltip": "Minimum confidence threshold for hand keypoints to be smoothed."
                }),
            },
        }
    
    RETURN_TYPES = ("IMAGE", "POSEDATA")
    RETURN_NAMES = ("IMAGE", "pose_data")
    FUNCTION = "smooth"
    CATEGORY = "WanAnimatePreprocess"
    DESCRIPTION = "Smooths pose data across video frames using temporal EMA filtering to reduce jitter in detected poses."
    
    def smooth(self, pose_data, filter_extra_people, smooth_alpha, gap_frames,
               min_run_frames, conf_thresh_body, conf_thresh_hands):
        
        pose_metas = pose_data.get("pose_metas", [])
        pose_metas_original = pose_data.get("pose_metas_original", [])
        
        if not pose_metas:
            logger.warning("TSPoseDataSmoother: No pose_metas found in pose_data")
            return (torch.zeros(1, 64, 64, 3), pose_data)
        
        # Get dimensions from the first meta
        first_meta = pose_metas_original[0] if pose_metas_original else pose_metas[0]
        if hasattr(first_meta, 'width'):
            width = first_meta.width if hasattr(first_meta, 'width') else first_meta.get('width', 512)
            height = first_meta.height if hasattr(first_meta, 'height') else first_meta.get('height', 512)
        elif isinstance(first_meta, dict):
            width = first_meta.get('width', 512)
            height = first_meta.get('height', 512)
        else:
            width = 512
            height = 512
        
        # Apply smoothing to the pose metas
        smoothed_metas = _smooth_pose_sequence(
            pose_metas,
            smooth_alpha=smooth_alpha,
            gap_frames=gap_frames,
            min_run_frames=min_run_frames,
            conf_thresh_body=conf_thresh_body,
            conf_thresh_hands=conf_thresh_hands,
            filter_extra_people=filter_extra_people,
        )
        
        # Render smoothed pose images using the same drawing function
        # as ComfyUI-WanAnimatePreprocess's DrawViTPose
        try:
            from ComfyUI_WanAnimatePreprocess_module import draw_aapose_by_meta_new
        except ImportError:
            pass
        
        # Try to import the drawing function from the WanAnimatePreprocess package
        draw_fn = None
        try:
            import importlib
            import sys
            # Look for the module in custom_nodes
            import os
            custom_nodes_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
            wan_preprocess_dir = os.path.join(custom_nodes_dir, "ComfyUI-WanAnimatePreprocess")
            
            if os.path.exists(wan_preprocess_dir):
                sys.path.insert(0, wan_preprocess_dir)
                from pose_utils.human_visualization import draw_aapose_by_meta_new
                from utils import padding_resize
                draw_fn = draw_aapose_by_meta_new
                sys.path.pop(0)
        except ImportError as e:
            logger.warning(f"TSPoseDataSmoother: Could not import drawing functions: {e}")
        
        comfy_pbar = ProgressBar(len(smoothed_metas))
        pose_images = []
        
        for idx, meta in enumerate(smoothed_metas):
            canvas = np.zeros((height, width, 3), dtype=np.uint8)
            
            if draw_fn is not None:
                try:
                    pose_image = draw_fn(canvas, meta, draw_hand=True, draw_head=True)
                    # Apply padding/resize to match target dimensions
                    try:
                        pose_image = padding_resize(pose_image, height, width)
                    except Exception:
                        pass
                except Exception as e:
                    logger.warning(f"TSPoseDataSmoother: Drawing failed on frame {idx}: {e}")
                    pose_image = canvas
            else:
                # Fallback: simple keypoint rendering
                pose_image = _fallback_draw_pose(canvas, meta, height, width)
            
            pose_images.append(pose_image)
            if (idx + 1) % 10 == 0:
                comfy_pbar.update_absolute(idx + 1)
        
        comfy_pbar.update_absolute(len(smoothed_metas))
        
        pose_images_np = np.stack(pose_images, 0)
        pose_images_tensor = torch.from_numpy(pose_images_np).float() / 255.0
        
        # Build output pose_data with smoothed metas
        smoothed_pose_data = dict(pose_data)
        smoothed_pose_data["pose_metas"] = smoothed_metas
        
        return (pose_images_tensor, smoothed_pose_data)


def _fallback_draw_pose(canvas, meta, height, width):
    """
    Simple fallback pose renderer when ComfyUI-WanAnimatePreprocess
    drawing functions are not available.
    """
    import cv2
    
    kp_body = _get_keypoints_array(meta, 'keypoints_body')
    if kp_body is None:
        return canvas
    
    # COCO-WholeBody skeleton connections for body
    body_connections = [
        (0, 1), (0, 2), (1, 3), (2, 4),  # head
        (5, 6), (5, 7), (7, 9), (6, 8), (8, 10),  # arms
        (5, 11), (6, 12), (11, 12),  # torso
        (11, 13), (13, 15), (12, 14), (14, 16),  # legs
    ]
    
    # Scale keypoints to canvas size
    for conn in body_connections:
        i, j = conn
        if i < len(kp_body) and j < len(kp_body):
            x1 = int(kp_body[i][0] * width) if kp_body[i][0] <= 1.0 else int(kp_body[i][0])
            y1 = int(kp_body[i][1] * height) if kp_body[i][1] <= 1.0 else int(kp_body[i][1])
            x2 = int(kp_body[j][0] * width) if kp_body[j][0] <= 1.0 else int(kp_body[j][0])
            y2 = int(kp_body[j][1] * height) if kp_body[j][1] <= 1.0 else int(kp_body[j][1])
            
            conf1 = kp_body[i][2] if kp_body.shape[1] > 2 else 1.0
            conf2 = kp_body[j][2] if kp_body.shape[1] > 2 else 1.0
            
            if conf1 > 0.1 and conf2 > 0.1:
                cv2.line(canvas, (x1, y1), (x2, y2), (0, 255, 0), 2)
    
    # Draw keypoints as circles
    for i in range(min(len(kp_body), 17)):
        x = int(kp_body[i][0] * width) if kp_body[i][0] <= 1.0 else int(kp_body[i][0])
        y = int(kp_body[i][1] * height) if kp_body[i][1] <= 1.0 else int(kp_body[i][1])
        conf = kp_body[i][2] if kp_body.shape[1] > 2 else 1.0
        if conf > 0.1:
            cv2.circle(canvas, (x, y), 3, (0, 0, 255), -1)
    
    return canvas


# Node registration
NODE_CLASS_MAPPINGS = {
    "TSPoseDataSmoother": TSPoseDataSmoother,
}

NODE_DISPLAY_NAME_MAPPINGS = {
    "TSPoseDataSmoother": "TS Pose Data Smoother",
}