FangSen9000 commited on
Commit ·
46a10a9
1
Parent(s): a36bbc9
Achieve the interpolation effect at the npz level
Browse files- README.md +2 -2
- env/requirements01_dwpose.txt +2 -1
- env/requirements02_controlnext.txt +1 -0
- generated_pose_video/GOOD_MORNING_AFTERNOON_25073_36863_01435_20250803_231751.mp4 +3 -0
- text2pose.py +69 -5
- utils/__pycache__/npz_interpolation.cpython-310.pyc +0 -0
- utils/npz_interpolation.py +236 -0
- {pipeline → utils}/pipeline01_merge_frame_level_npz_from_video_dir.py +0 -0
README.md
CHANGED
|
@@ -31,7 +31,7 @@ The entire project is divided into two parts: `text_or_gloss2pose` and `pose2vid
|
|
| 31 |
## tutorial01 prepare pose dict
|
| 32 |
|
| 33 |
1. cd StableSigner
|
| 34 |
-
2. run `python
|
| 35 |
3. Place the corresponding "gloss-videoID" mapping in the pose dict
|
| 36 |
|
| 37 |
## tutorial02 get pose video from text input
|
|
@@ -39,7 +39,7 @@ The entire project is divided into two parts: `text_or_gloss2pose` and `pose2vid
|
|
| 39 |
```python
|
| 40 |
|
| 41 |
# Default
|
| 42 |
-
python text2pose.py --gloss "good afternoon" --
|
| 43 |
|
| 44 |
# Single gloss
|
| 45 |
python text2pose.py --gloss "hello"
|
|
|
|
| 31 |
## tutorial01 prepare pose dict
|
| 32 |
|
| 33 |
1. cd StableSigner
|
| 34 |
+
2. run `python utils/pipeline01_merge_frame_level_npz_from_video_dir.py tools-new-2025/output/test_dataset_results`
|
| 35 |
3. Place the corresponding "gloss-videoID" mapping in the pose dict
|
| 36 |
|
| 37 |
## tutorial02 get pose video from text input
|
|
|
|
| 39 |
```python
|
| 40 |
|
| 41 |
# Default
|
| 42 |
+
python text2pose.py --gloss "good morning afternoon" --npz-interpolation 20 --width 512 --height 768 --fps 30
|
| 43 |
|
| 44 |
# Single gloss
|
| 45 |
python text2pose.py --gloss "hello"
|
env/requirements01_dwpose.txt
CHANGED
|
@@ -2,4 +2,5 @@ easy_dwpose==1.0.1
|
|
| 2 |
ffmpeg
|
| 3 |
torch
|
| 4 |
torchvision
|
| 5 |
-
einops
|
|
|
|
|
|
| 2 |
ffmpeg
|
| 3 |
torch
|
| 4 |
torchvision
|
| 5 |
+
einops
|
| 6 |
+
scipy
|
env/requirements02_controlnext.txt
CHANGED
|
@@ -5,6 +5,7 @@ opencv-python
|
|
| 5 |
omegaconf
|
| 6 |
onnxruntime
|
| 7 |
pillow
|
|
|
|
| 8 |
matplotlib
|
| 9 |
numpy
|
| 10 |
transformers
|
|
|
|
| 5 |
omegaconf
|
| 6 |
onnxruntime
|
| 7 |
pillow
|
| 8 |
+
scipy
|
| 9 |
matplotlib
|
| 10 |
numpy
|
| 11 |
transformers
|
generated_pose_video/GOOD_MORNING_AFTERNOON_25073_36863_01435_20250803_231751.mp4
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:761d087c399b83e83d0246c6eb84d5cefe7290a01ceb59f1bbab2a29e7a4bf26
|
| 3 |
+
size 1688288
|
text2pose.py
CHANGED
|
@@ -19,6 +19,7 @@ from datetime import datetime
|
|
| 19 |
|
| 20 |
from easy_dwpose import DWposeDetector
|
| 21 |
from easy_dwpose.draw.controlnext import draw_pose, process_pose_data
|
|
|
|
| 22 |
|
| 23 |
# Fixed configuration
|
| 24 |
JSON_FILE = "pose_dict/WLASL_train.json" # JSON mapping file
|
|
@@ -129,13 +130,14 @@ class SignLanguageQA:
|
|
| 129 |
return None
|
| 130 |
|
| 131 |
def generate_video_from_glosses(self, gloss_list, output_path, fps=30, width=480, height=480,
|
| 132 |
-
smoothing_frames=5, smoothing_method='none'):
|
| 133 |
"""Generate video from gloss list"""
|
| 134 |
# Create output directory
|
| 135 |
output_path.parent.mkdir(parents=True, exist_ok=True)
|
| 136 |
|
| 137 |
# Collect all frames from all glosses
|
| 138 |
all_pose_frames = []
|
|
|
|
| 139 |
video_info = []
|
| 140 |
|
| 141 |
for gloss in gloss_list:
|
|
@@ -154,6 +156,9 @@ class SignLanguageQA:
|
|
| 154 |
total_frames = self.get_total_frames(npz_data)
|
| 155 |
print(f" 📹 {video_id}: {total_frames} frames")
|
| 156 |
|
|
|
|
|
|
|
|
|
|
| 157 |
# Extract all frames for this video
|
| 158 |
gloss_frames = []
|
| 159 |
for frame_num in range(1, total_frames + 1):
|
|
@@ -168,7 +173,8 @@ class SignLanguageQA:
|
|
| 168 |
video_info.append({
|
| 169 |
'gloss': gloss,
|
| 170 |
'video_id': video_id,
|
| 171 |
-
'frame_count': len(gloss_frames)
|
|
|
|
| 172 |
})
|
| 173 |
print(f" ✅ Generated {len(gloss_frames)} pose frames")
|
| 174 |
break # Use only first valid video per gloss
|
|
@@ -177,6 +183,53 @@ class SignLanguageQA:
|
|
| 177 |
print("❌ No frames generated")
|
| 178 |
return False
|
| 179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
# Apply smoothing if needed
|
| 181 |
if smoothing_method != 'none' and len(all_pose_frames) > 1:
|
| 182 |
final_frames = self.apply_smoothing(all_pose_frames, smoothing_frames, smoothing_method)
|
|
@@ -339,6 +392,7 @@ class SignLanguageQA:
|
|
| 339 |
# Ask for smoothing options if multiple glosses
|
| 340 |
smoothing_method = 'none'
|
| 341 |
smoothing_frames = 0
|
|
|
|
| 342 |
|
| 343 |
if len(gloss_list) > 1:
|
| 344 |
print(f"\n🌊 Multiple glosses detected, choose smoothing method:")
|
|
@@ -346,8 +400,9 @@ class SignLanguageQA:
|
|
| 346 |
print(f" 2. fade - Fade in/out transition")
|
| 347 |
print(f" 3. blend - Frame blending transition")
|
| 348 |
print(f" 4. pause - Add pause interval")
|
|
|
|
| 349 |
|
| 350 |
-
smooth_choice = input(f"Choose smoothing method [1-
|
| 351 |
|
| 352 |
if smooth_choice == '2':
|
| 353 |
smoothing_method = 'fade'
|
|
@@ -358,16 +413,22 @@ class SignLanguageQA:
|
|
| 358 |
elif smooth_choice == '4':
|
| 359 |
smoothing_method = 'pause'
|
| 360 |
smoothing_frames = int(input("Pause frames [default 10]: ").strip() or "10")
|
|
|
|
|
|
|
|
|
|
| 361 |
|
| 362 |
# Generate output filename
|
| 363 |
suffix = f"_{smoothing_method}" if smoothing_method != 'none' else ""
|
|
|
|
|
|
|
| 364 |
output_file = Path(OUTPUT_DIR) / f"{gloss_str}_{timestamp}{suffix}.mp4"
|
| 365 |
|
| 366 |
# Generate video
|
| 367 |
success = self.generate_video_from_glosses(
|
| 368 |
gloss_list, output_file,
|
| 369 |
smoothing_frames=smoothing_frames,
|
| 370 |
-
smoothing_method=smoothing_method
|
|
|
|
| 371 |
)
|
| 372 |
|
| 373 |
except KeyboardInterrupt:
|
|
@@ -390,6 +451,7 @@ class SignLanguageQA:
|
|
| 390 |
print(" - fade: Fade in/out transition")
|
| 391 |
print(" - blend: Frame blending transition")
|
| 392 |
print(" - pause: Add pause interval")
|
|
|
|
| 393 |
print("=" * 50)
|
| 394 |
|
| 395 |
|
|
@@ -407,6 +469,8 @@ def main():
|
|
| 407 |
default='none', help='Smoothing method (default: none)')
|
| 408 |
parser.add_argument('--smoothing-frames', type=int, default=5,
|
| 409 |
help='Smoothing frames (default: 5)')
|
|
|
|
|
|
|
| 410 |
|
| 411 |
args = parser.parse_args()
|
| 412 |
|
|
@@ -437,7 +501,7 @@ def main():
|
|
| 437 |
|
| 438 |
success = qa_system.generate_video_from_glosses(
|
| 439 |
gloss_list, output_file, args.fps, args.width, args.height,
|
| 440 |
-
args.smoothing_frames, args.smoothing
|
| 441 |
)
|
| 442 |
|
| 443 |
if not success:
|
|
|
|
| 19 |
|
| 20 |
from easy_dwpose import DWposeDetector
|
| 21 |
from easy_dwpose.draw.controlnext import draw_pose, process_pose_data
|
| 22 |
+
from utils.npz_interpolation import interpolate_pose_npz, ease_in_out_cubic
|
| 23 |
|
| 24 |
# Fixed configuration
|
| 25 |
JSON_FILE = "pose_dict/WLASL_train.json" # JSON mapping file
|
|
|
|
| 130 |
return None
|
| 131 |
|
| 132 |
def generate_video_from_glosses(self, gloss_list, output_path, fps=30, width=480, height=480,
|
| 133 |
+
smoothing_frames=5, smoothing_method='none', npz_interpolation_frames=0):
|
| 134 |
"""Generate video from gloss list"""
|
| 135 |
# Create output directory
|
| 136 |
output_path.parent.mkdir(parents=True, exist_ok=True)
|
| 137 |
|
| 138 |
# Collect all frames from all glosses
|
| 139 |
all_pose_frames = []
|
| 140 |
+
all_npz_data = [] # Store NPZ data for interpolation
|
| 141 |
video_info = []
|
| 142 |
|
| 143 |
for gloss in gloss_list:
|
|
|
|
| 156 |
total_frames = self.get_total_frames(npz_data)
|
| 157 |
print(f" 📹 {video_id}: {total_frames} frames")
|
| 158 |
|
| 159 |
+
# Store NPZ data for potential interpolation
|
| 160 |
+
all_npz_data.append(npz_data)
|
| 161 |
+
|
| 162 |
# Extract all frames for this video
|
| 163 |
gloss_frames = []
|
| 164 |
for frame_num in range(1, total_frames + 1):
|
|
|
|
| 173 |
video_info.append({
|
| 174 |
'gloss': gloss,
|
| 175 |
'video_id': video_id,
|
| 176 |
+
'frame_count': len(gloss_frames),
|
| 177 |
+
'npz_data': npz_data
|
| 178 |
})
|
| 179 |
print(f" ✅ Generated {len(gloss_frames)} pose frames")
|
| 180 |
break # Use only first valid video per gloss
|
|
|
|
| 183 |
print("❌ No frames generated")
|
| 184 |
return False
|
| 185 |
|
| 186 |
+
# Apply NPZ interpolation if requested
|
| 187 |
+
if npz_interpolation_frames > 0 and len(all_pose_frames) > 1:
|
| 188 |
+
print(f"🔄 Applying NPZ interpolation with {npz_interpolation_frames} frames...")
|
| 189 |
+
interpolated_pose_frames = []
|
| 190 |
+
|
| 191 |
+
for i in range(len(all_pose_frames)):
|
| 192 |
+
# Add original frames
|
| 193 |
+
interpolated_pose_frames.extend(all_pose_frames[i])
|
| 194 |
+
|
| 195 |
+
# Add interpolated frames between glosses
|
| 196 |
+
if i < len(all_pose_frames) - 1:
|
| 197 |
+
# Get NPZ data for interpolation
|
| 198 |
+
npz1 = video_info[i]['npz_data']
|
| 199 |
+
npz2 = video_info[i + 1]['npz_data']
|
| 200 |
+
|
| 201 |
+
# Interpolate between the two NPZ files
|
| 202 |
+
interpolated_npz_frames = interpolate_pose_npz(
|
| 203 |
+
npz1, npz2,
|
| 204 |
+
num_frames=npz_interpolation_frames,
|
| 205 |
+
method='catmull-rom'
|
| 206 |
+
)
|
| 207 |
+
|
| 208 |
+
# Convert interpolated NPZ frames to visual frames
|
| 209 |
+
for interp_data in interpolated_npz_frames:
|
| 210 |
+
# Extract frame data from interpolated NPZ
|
| 211 |
+
frame_keys = [k for k in interp_data.keys() if k.endswith('_bodies')]
|
| 212 |
+
if frame_keys:
|
| 213 |
+
frame_key = frame_keys[0]
|
| 214 |
+
frame_num = int(frame_key.split('_')[1])
|
| 215 |
+
|
| 216 |
+
# Build frame data dict
|
| 217 |
+
frame_data = {}
|
| 218 |
+
for suffix in ['bodies', 'body_scores', 'hands', 'hands_scores', 'faces', 'faces_scores']:
|
| 219 |
+
key = f"frame_{frame_num:08d}_{suffix}"
|
| 220 |
+
if key in interp_data:
|
| 221 |
+
frame_data[suffix] = interp_data[key]
|
| 222 |
+
|
| 223 |
+
# Create visual frame
|
| 224 |
+
if frame_data:
|
| 225 |
+
pose_frame = self.create_pose_frame(frame_data, width, height)
|
| 226 |
+
if pose_frame is not None:
|
| 227 |
+
interpolated_pose_frames.append(pose_frame)
|
| 228 |
+
|
| 229 |
+
print(f" ✅ Added {npz_interpolation_frames} interpolated frames between '{video_info[i]['gloss']}' and '{video_info[i+1]['gloss']}'")
|
| 230 |
+
|
| 231 |
+
all_pose_frames = [interpolated_pose_frames] # Treat as single sequence
|
| 232 |
+
|
| 233 |
# Apply smoothing if needed
|
| 234 |
if smoothing_method != 'none' and len(all_pose_frames) > 1:
|
| 235 |
final_frames = self.apply_smoothing(all_pose_frames, smoothing_frames, smoothing_method)
|
|
|
|
| 392 |
# Ask for smoothing options if multiple glosses
|
| 393 |
smoothing_method = 'none'
|
| 394 |
smoothing_frames = 0
|
| 395 |
+
npz_interpolation_frames = 0
|
| 396 |
|
| 397 |
if len(gloss_list) > 1:
|
| 398 |
print(f"\n🌊 Multiple glosses detected, choose smoothing method:")
|
|
|
|
| 400 |
print(f" 2. fade - Fade in/out transition")
|
| 401 |
print(f" 3. blend - Frame blending transition")
|
| 402 |
print(f" 4. pause - Add pause interval")
|
| 403 |
+
print(f" 5. npz - NPZ interpolation (smoothest transition)")
|
| 404 |
|
| 405 |
+
smooth_choice = input(f"Choose smoothing method [1-5, default 1]: ").strip()
|
| 406 |
|
| 407 |
if smooth_choice == '2':
|
| 408 |
smoothing_method = 'fade'
|
|
|
|
| 413 |
elif smooth_choice == '4':
|
| 414 |
smoothing_method = 'pause'
|
| 415 |
smoothing_frames = int(input("Pause frames [default 10]: ").strip() or "10")
|
| 416 |
+
elif smooth_choice == '5':
|
| 417 |
+
npz_interpolation_frames = int(input("NPZ interpolation frames [default 10]: ").strip() or "10")
|
| 418 |
+
print(f"📊 Using Catmull-Rom spline interpolation for smooth transitions")
|
| 419 |
|
| 420 |
# Generate output filename
|
| 421 |
suffix = f"_{smoothing_method}" if smoothing_method != 'none' else ""
|
| 422 |
+
if npz_interpolation_frames > 0:
|
| 423 |
+
suffix = f"_npz{npz_interpolation_frames}"
|
| 424 |
output_file = Path(OUTPUT_DIR) / f"{gloss_str}_{timestamp}{suffix}.mp4"
|
| 425 |
|
| 426 |
# Generate video
|
| 427 |
success = self.generate_video_from_glosses(
|
| 428 |
gloss_list, output_file,
|
| 429 |
smoothing_frames=smoothing_frames,
|
| 430 |
+
smoothing_method=smoothing_method,
|
| 431 |
+
npz_interpolation_frames=npz_interpolation_frames
|
| 432 |
)
|
| 433 |
|
| 434 |
except KeyboardInterrupt:
|
|
|
|
| 451 |
print(" - fade: Fade in/out transition")
|
| 452 |
print(" - blend: Frame blending transition")
|
| 453 |
print(" - pause: Add pause interval")
|
| 454 |
+
print(" - npz: NPZ-level interpolation (smoothest, uses Catmull-Rom splines)")
|
| 455 |
print("=" * 50)
|
| 456 |
|
| 457 |
|
|
|
|
| 469 |
default='none', help='Smoothing method (default: none)')
|
| 470 |
parser.add_argument('--smoothing-frames', type=int, default=5,
|
| 471 |
help='Smoothing frames (default: 5)')
|
| 472 |
+
parser.add_argument('--npz-interpolation', type=int, default=0,
|
| 473 |
+
help='Number of NPZ interpolation frames between glosses (default: 0)')
|
| 474 |
|
| 475 |
args = parser.parse_args()
|
| 476 |
|
|
|
|
| 501 |
|
| 502 |
success = qa_system.generate_video_from_glosses(
|
| 503 |
gloss_list, output_file, args.fps, args.width, args.height,
|
| 504 |
+
args.smoothing_frames, args.smoothing, args.npz_interpolation
|
| 505 |
)
|
| 506 |
|
| 507 |
if not success:
|
utils/__pycache__/npz_interpolation.cpython-310.pyc
ADDED
|
Binary file (5.84 kB). View file
|
|
|
utils/npz_interpolation.py
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
NPZ-level interpolation for smooth pose transitions
|
| 4 |
+
Implements various interpolation methods for creating natural transitions between poses
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import numpy as np
|
| 8 |
+
from scipy import interpolate
|
| 9 |
+
from scipy.spatial.transform import Slerp, Rotation
|
| 10 |
+
import cv2
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def ease_in_out_cubic(t):
|
| 14 |
+
"""Cubic easing function for smooth acceleration and deceleration"""
|
| 15 |
+
if t < 0.5:
|
| 16 |
+
return 4 * t * t * t
|
| 17 |
+
else:
|
| 18 |
+
p = 2 * t - 2
|
| 19 |
+
return 1 + p * p * p / 2
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def ease_in_out_sine(t):
|
| 23 |
+
"""Sine easing function for very smooth transitions"""
|
| 24 |
+
return -(np.cos(np.pi * t) - 1) / 2
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def interpolate_keypoints(kp1, kp2, num_frames, easing_func=ease_in_out_cubic):
|
| 28 |
+
"""
|
| 29 |
+
Interpolate between two sets of keypoints using easing functions
|
| 30 |
+
|
| 31 |
+
Args:
|
| 32 |
+
kp1: Starting keypoints (N, 2) or (N, 3)
|
| 33 |
+
kp2: Ending keypoints (N, 2) or (N, 3)
|
| 34 |
+
num_frames: Number of interpolated frames
|
| 35 |
+
easing_func: Easing function to use
|
| 36 |
+
|
| 37 |
+
Returns:
|
| 38 |
+
List of interpolated keypoints
|
| 39 |
+
"""
|
| 40 |
+
interpolated = []
|
| 41 |
+
|
| 42 |
+
for i in range(num_frames):
|
| 43 |
+
t = i / (num_frames - 1) if num_frames > 1 else 0
|
| 44 |
+
t_eased = easing_func(t)
|
| 45 |
+
|
| 46 |
+
# Linear interpolation with easing
|
| 47 |
+
interp_kp = kp1 * (1 - t_eased) + kp2 * t_eased
|
| 48 |
+
interpolated.append(interp_kp)
|
| 49 |
+
|
| 50 |
+
return interpolated
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def catmull_rom_spline(p0, p1, p2, p3, num_points, tension=0.5):
|
| 54 |
+
"""
|
| 55 |
+
Catmull-Rom spline interpolation for smooth curves through control points
|
| 56 |
+
|
| 57 |
+
Args:
|
| 58 |
+
p0, p1, p2, p3: Control points (start from p1 to p2, p0 and p3 for curvature)
|
| 59 |
+
num_points: Number of points to generate
|
| 60 |
+
tension: Tension parameter (0.5 is standard Catmull-Rom)
|
| 61 |
+
|
| 62 |
+
Returns:
|
| 63 |
+
Array of interpolated points
|
| 64 |
+
"""
|
| 65 |
+
points = []
|
| 66 |
+
|
| 67 |
+
for i in range(num_points):
|
| 68 |
+
t = i / (num_points - 1)
|
| 69 |
+
t2 = t * t
|
| 70 |
+
t3 = t2 * t
|
| 71 |
+
|
| 72 |
+
# Catmull-Rom basis functions
|
| 73 |
+
v0 = -tension * t + 2 * tension * t2 - tension * t3
|
| 74 |
+
v1 = 1 + (tension - 3) * t2 + (2 - tension) * t3
|
| 75 |
+
v2 = tension * t + (3 - 2 * tension) * t2 + (tension - 2) * t3
|
| 76 |
+
v3 = -tension * t2 + tension * t3
|
| 77 |
+
|
| 78 |
+
point = v0 * p0 + v1 * p1 + v2 * p2 + v3 * p3
|
| 79 |
+
points.append(point)
|
| 80 |
+
|
| 81 |
+
return np.array(points)
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def interpolate_pose_npz(npz1_data, npz2_data, num_frames=10, method='catmull-rom'):
|
| 85 |
+
"""
|
| 86 |
+
Interpolate between two pose NPZ data structures
|
| 87 |
+
|
| 88 |
+
Args:
|
| 89 |
+
npz1_data: Dictionary containing pose data from first NPZ
|
| 90 |
+
npz2_data: Dictionary containing pose data from second NPZ
|
| 91 |
+
num_frames: Number of transition frames to generate
|
| 92 |
+
method: Interpolation method ('linear', 'cubic', 'catmull-rom')
|
| 93 |
+
|
| 94 |
+
Returns:
|
| 95 |
+
List of interpolated pose data dictionaries
|
| 96 |
+
"""
|
| 97 |
+
interpolated_frames = []
|
| 98 |
+
|
| 99 |
+
# Extract keypoints from the last frame of first video and first frame of second
|
| 100 |
+
# Assume format: frame_XXXXXXXX_bodies, frame_XXXXXXXX_hands, etc.
|
| 101 |
+
|
| 102 |
+
# Get the last frame from npz1
|
| 103 |
+
frame_keys1 = sorted([k for k in npz1_data.keys() if k.endswith('_bodies')])
|
| 104 |
+
if not frame_keys1:
|
| 105 |
+
return []
|
| 106 |
+
last_frame_key1 = frame_keys1[-1]
|
| 107 |
+
frame_num1 = last_frame_key1.split('_')[1]
|
| 108 |
+
|
| 109 |
+
# Get the first frame from npz2
|
| 110 |
+
frame_keys2 = sorted([k for k in npz2_data.keys() if k.endswith('_bodies')])
|
| 111 |
+
if not frame_keys2:
|
| 112 |
+
return []
|
| 113 |
+
first_frame_key2 = frame_keys2[0]
|
| 114 |
+
frame_num2 = first_frame_key2.split('_')[1]
|
| 115 |
+
|
| 116 |
+
# For catmull-rom, we also need one frame before and after
|
| 117 |
+
prev_frame_key1 = frame_keys1[-2] if len(frame_keys1) > 1 else frame_keys1[-1]
|
| 118 |
+
next_frame_key2 = frame_keys2[1] if len(frame_keys2) > 1 else frame_keys2[0]
|
| 119 |
+
|
| 120 |
+
# Extract all components
|
| 121 |
+
components = ['bodies', 'body_scores', 'hands', 'hands_scores', 'faces', 'faces_scores']
|
| 122 |
+
|
| 123 |
+
for i in range(num_frames):
|
| 124 |
+
t = i / (num_frames - 1) if num_frames > 1 else 0
|
| 125 |
+
interpolated_data = {}
|
| 126 |
+
|
| 127 |
+
for component in components:
|
| 128 |
+
key1 = f"frame_{frame_num1}_{component}"
|
| 129 |
+
key2 = f"frame_{frame_num2}_{component}"
|
| 130 |
+
|
| 131 |
+
if key1 in npz1_data and key2 in npz2_data:
|
| 132 |
+
data1 = npz1_data[key1]
|
| 133 |
+
data2 = npz2_data[key2]
|
| 134 |
+
|
| 135 |
+
if method == 'linear':
|
| 136 |
+
# Simple linear interpolation with easing
|
| 137 |
+
t_eased = ease_in_out_cubic(t)
|
| 138 |
+
interpolated = data1 * (1 - t_eased) + data2 * t_eased
|
| 139 |
+
|
| 140 |
+
elif method == 'cubic':
|
| 141 |
+
# Use scipy's cubic interpolation
|
| 142 |
+
if data1.ndim == 3: # Multiple people
|
| 143 |
+
interpolated = np.zeros_like(data1)
|
| 144 |
+
for person_idx in range(data1.shape[0]):
|
| 145 |
+
for joint_idx in range(data1.shape[1]):
|
| 146 |
+
for coord_idx in range(data1.shape[2]):
|
| 147 |
+
y = [data1[person_idx, joint_idx, coord_idx],
|
| 148 |
+
data2[person_idx, joint_idx, coord_idx]]
|
| 149 |
+
f = interpolate.interp1d([0, 1], y, kind='cubic')
|
| 150 |
+
interpolated[person_idx, joint_idx, coord_idx] = f(t)
|
| 151 |
+
else:
|
| 152 |
+
t_eased = ease_in_out_cubic(t)
|
| 153 |
+
interpolated = data1 * (1 - t_eased) + data2 * t_eased
|
| 154 |
+
|
| 155 |
+
elif method == 'catmull-rom':
|
| 156 |
+
# Use Catmull-Rom spline for smoother transitions
|
| 157 |
+
if component.endswith('_scores'):
|
| 158 |
+
# For scores, use simple easing
|
| 159 |
+
t_eased = ease_in_out_sine(t)
|
| 160 |
+
interpolated = data1 * (1 - t_eased) + data2 * t_eased
|
| 161 |
+
else:
|
| 162 |
+
# For keypoints, use spline interpolation
|
| 163 |
+
# Get control points
|
| 164 |
+
prev_key = f"frame_{prev_frame_key1.split('_')[1]}_{component}"
|
| 165 |
+
next_key = f"frame_{next_frame_key2.split('_')[1]}_{component}"
|
| 166 |
+
|
| 167 |
+
p0 = npz1_data.get(prev_key, data1)
|
| 168 |
+
p1 = data1
|
| 169 |
+
p2 = data2
|
| 170 |
+
p3 = npz2_data.get(next_key, data2)
|
| 171 |
+
|
| 172 |
+
if data1.ndim == 3: # Multiple people
|
| 173 |
+
interpolated = np.zeros_like(data1)
|
| 174 |
+
for person_idx in range(data1.shape[0]):
|
| 175 |
+
for joint_idx in range(data1.shape[1]):
|
| 176 |
+
# Interpolate each joint
|
| 177 |
+
points = catmull_rom_spline(
|
| 178 |
+
p0[person_idx, joint_idx],
|
| 179 |
+
p1[person_idx, joint_idx],
|
| 180 |
+
p2[person_idx, joint_idx],
|
| 181 |
+
p3[person_idx, joint_idx],
|
| 182 |
+
num_points=num_frames
|
| 183 |
+
)
|
| 184 |
+
interpolated[person_idx, joint_idx] = points[i]
|
| 185 |
+
else:
|
| 186 |
+
# Single dimension data
|
| 187 |
+
t_eased = ease_in_out_sine(t)
|
| 188 |
+
interpolated = data1 * (1 - t_eased) + data2 * t_eased
|
| 189 |
+
|
| 190 |
+
# Create frame key for interpolated frame
|
| 191 |
+
frame_key = f"frame_{i+1:08d}_{component}"
|
| 192 |
+
interpolated_data[frame_key] = interpolated
|
| 193 |
+
|
| 194 |
+
interpolated_frames.append(interpolated_data)
|
| 195 |
+
|
| 196 |
+
return interpolated_frames
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
def apply_motion_blur(frame, prev_frame, blur_strength=0.3):
|
| 200 |
+
"""Apply motion blur between frames for smoother visual transitions"""
|
| 201 |
+
if prev_frame is None:
|
| 202 |
+
return frame
|
| 203 |
+
|
| 204 |
+
# Weighted average with previous frame
|
| 205 |
+
blurred = cv2.addWeighted(prev_frame, blur_strength, frame, 1 - blur_strength, 0)
|
| 206 |
+
return blurred
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
def smooth_trajectory(keypoints_sequence, window_size=5):
|
| 210 |
+
"""
|
| 211 |
+
Apply trajectory smoothing to a sequence of keypoints
|
| 212 |
+
Uses a moving average filter to smooth the motion
|
| 213 |
+
|
| 214 |
+
Args:
|
| 215 |
+
keypoints_sequence: List of keypoint arrays
|
| 216 |
+
window_size: Size of the smoothing window
|
| 217 |
+
|
| 218 |
+
Returns:
|
| 219 |
+
Smoothed keypoints sequence
|
| 220 |
+
"""
|
| 221 |
+
if len(keypoints_sequence) <= window_size:
|
| 222 |
+
return keypoints_sequence
|
| 223 |
+
|
| 224 |
+
smoothed = []
|
| 225 |
+
half_window = window_size // 2
|
| 226 |
+
|
| 227 |
+
for i in range(len(keypoints_sequence)):
|
| 228 |
+
start_idx = max(0, i - half_window)
|
| 229 |
+
end_idx = min(len(keypoints_sequence), i + half_window + 1)
|
| 230 |
+
|
| 231 |
+
# Average keypoints in the window
|
| 232 |
+
window_kps = keypoints_sequence[start_idx:end_idx]
|
| 233 |
+
avg_kp = np.mean(window_kps, axis=0)
|
| 234 |
+
smoothed.append(avg_kp)
|
| 235 |
+
|
| 236 |
+
return smoothed
|
{pipeline → utils}/pipeline01_merge_frame_level_npz_from_video_dir.py
RENAMED
|
File without changes
|