|
import cv2 |
|
import mediapipe as mp |
|
import numpy as np |
|
|
|
def extract_keypoints_from_video(video_path, verbose=False): |
|
mp_pose = mp.solutions.pose |
|
mp_hands = mp.solutions.hands |
|
|
|
pose_model = mp_pose.Pose() |
|
hands_model = mp_hands.Hands(static_image_mode=False, max_num_hands=2) |
|
|
|
cap = cv2.VideoCapture(video_path) |
|
keypoints_sequence = [] |
|
|
|
frame_idx = 0 |
|
|
|
while cap.isOpened(): |
|
success, frame = cap.read() |
|
if not success: |
|
break |
|
|
|
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) |
|
h, w, _ = frame.shape |
|
|
|
|
|
pose_results = pose_model.process(frame_rgb) |
|
if not pose_results.pose_landmarks: |
|
frame_idx += 1 |
|
continue |
|
|
|
|
|
pose_landmarks = pose_results.pose_landmarks.landmark |
|
pose = np.array([[lm.x, lm.y] for lm in pose_landmarks]) |
|
|
|
|
|
left_hand = np.zeros((21, 2)) |
|
right_hand = np.zeros((21, 2)) |
|
|
|
hand_results = hands_model.process(frame_rgb) |
|
if hand_results.multi_hand_landmarks and hand_results.multi_handedness: |
|
for hand_landmarks, hand_info in zip(hand_results.multi_hand_landmarks, hand_results.multi_handedness): |
|
label = hand_info.classification[0].label |
|
hand_array = np.array([[lm.x, lm.y] for lm in hand_landmarks.landmark]) |
|
if label == "Left": |
|
left_hand = hand_array |
|
else: |
|
right_hand = hand_array |
|
|
|
keypoints_sequence.append((pose, left_hand, right_hand)) |
|
|
|
if verbose: |
|
print(f"Processed frame {frame_idx}") |
|
frame_idx += 1 |
|
|
|
cap.release() |
|
pose_model.close() |
|
hands_model.close() |
|
|
|
return keypoints_sequence |
|
|
|
def render_person(frame, pose, left_hand, right_hand): |
|
h, w = frame.shape[:2] |
|
|
|
|
|
|
|
NOSE = 0 |
|
LEFT_EYE = 2 |
|
RIGHT_EYE = 5 |
|
LEFT_EAR = 7 |
|
RIGHT_EAR = 8 |
|
|
|
|
|
LEFT_SHOULDER = 11 |
|
RIGHT_SHOULDER = 12 |
|
LEFT_ELBOW = 13 |
|
RIGHT_ELBOW = 14 |
|
LEFT_WRIST = 15 |
|
RIGHT_WRIST = 16 |
|
LEFT_HIP = 23 |
|
RIGHT_HIP = 24 |
|
LEFT_KNEE = 25 |
|
RIGHT_KNEE = 26 |
|
LEFT_ANKLE = 27 |
|
RIGHT_ANKLE = 28 |
|
|
|
|
|
|
|
THUMB_TIP = 4 |
|
INDEX_TIP = 8 |
|
MIDDLE_TIP = 12 |
|
RING_TIP = 16 |
|
PINKY_TIP = 20 |
|
|
|
|
|
finger_connections = [ |
|
|
|
(0, 1), (1, 2), (2, 3), (3, 4), |
|
|
|
(0, 5), (5, 6), (6, 7), (7, 8), |
|
|
|
(0, 9), (9, 10), (10, 11), (11, 12), |
|
|
|
(0, 13), (13, 14), (14, 15), (15, 16), |
|
|
|
(0, 17), (17, 18), (18, 19), (19, 20) |
|
] |
|
|
|
|
|
skin_color = (173, 216, 230) |
|
outline_color = (40, 40, 40) |
|
shirt_color = (205, 170, 125) |
|
|
|
|
|
|
|
|
|
|
|
pants_color = (135, 206, 235) |
|
bow_tie_color = (255, 255, 255) |
|
eye_color = (255, 255, 255) |
|
pupil_color = (0, 0, 0) |
|
|
|
|
|
|
|
|
|
if len(pose) > max(LEFT_EYE, RIGHT_EYE, LEFT_EAR, RIGHT_EAR): |
|
|
|
head_center_x = pose[NOSE][0] * w |
|
head_center_y = pose[NOSE][1] * h |
|
|
|
|
|
if pose[LEFT_EYE][0] > 0 and pose[RIGHT_EYE][0] > 0: |
|
eye_distance = abs(pose[LEFT_EYE][0] - pose[RIGHT_EYE][0]) * w |
|
head_radius = eye_distance * 1.8 |
|
else: |
|
head_radius = 35 |
|
|
|
|
|
ear_radius = int(head_radius * 0.4) |
|
|
|
left_ear_x = int(head_center_x - head_radius * 0.6) |
|
left_ear_y = int(head_center_y - head_radius * 0.8) |
|
cv2.circle(frame, (left_ear_x, left_ear_y), ear_radius, skin_color, -1) |
|
cv2.circle(frame, (left_ear_x, left_ear_y), ear_radius, outline_color, 2) |
|
|
|
cv2.circle(frame, (left_ear_x, left_ear_y), int(ear_radius * 0.6), (120, 160, 180), -1) |
|
|
|
|
|
right_ear_x = int(head_center_x + head_radius * 0.6) |
|
right_ear_y = int(head_center_y - head_radius * 0.8) |
|
cv2.circle(frame, (right_ear_x, right_ear_y), ear_radius, skin_color, -1) |
|
cv2.circle(frame, (right_ear_x, right_ear_y), ear_radius, outline_color, 2) |
|
|
|
cv2.circle(frame, (right_ear_x, right_ear_y), int(ear_radius * 0.6), (120, 160, 180), -1) |
|
|
|
|
|
cv2.circle(frame, (int(head_center_x), int(head_center_y)), int(head_radius), skin_color, -1) |
|
cv2.circle(frame, (int(head_center_x), int(head_center_y)), int(head_radius), outline_color, 2) |
|
|
|
|
|
if pose[LEFT_EYE][0] > 0 and pose[LEFT_EYE][1] > 0: |
|
eye_x, eye_y = int(pose[LEFT_EYE][0] * w), int(pose[LEFT_EYE][1] * h) |
|
|
|
cv2.circle(frame, (eye_x, eye_y), 10, eye_color, -1) |
|
|
|
cv2.circle(frame, (eye_x, eye_y), 6, pupil_color, -1) |
|
|
|
cv2.circle(frame, (eye_x, eye_y), 10, outline_color, 1) |
|
|
|
cv2.circle(frame, (eye_x-3, eye_y-3), 3, (255, 255, 255), -1) |
|
|
|
if pose[RIGHT_EYE][0] > 0 and pose[RIGHT_EYE][1] > 0: |
|
eye_x, eye_y = int(pose[RIGHT_EYE][0] * w), int(pose[RIGHT_EYE][1] * h) |
|
|
|
cv2.circle(frame, (eye_x, eye_y), 10, eye_color, -1) |
|
|
|
cv2.circle(frame, (eye_x, eye_y), 6, pupil_color, -1) |
|
|
|
cv2.circle(frame, (eye_x, eye_y), 10, outline_color, 1) |
|
|
|
cv2.circle(frame, (eye_x-3, eye_y-3), 3, (255, 255, 255), -1) |
|
|
|
|
|
nose_x = int(head_center_x) |
|
nose_y = int(head_center_y + head_radius * 0.1) |
|
|
|
cv2.circle(frame, (nose_x, nose_y), 6, (80, 40, 20), -1) |
|
cv2.circle(frame, (nose_x, nose_y), 6, outline_color, 1) |
|
|
|
|
|
smile_center_x = int(head_center_x) |
|
smile_center_y = int(head_center_y + head_radius * 0.3) |
|
smile_radius = int(head_radius * 0.6) |
|
|
|
cv2.ellipse(frame, (smile_center_x, smile_center_y), (smile_radius, smile_radius//2), |
|
0, 0, 180, outline_color, 3) |
|
|
|
|
|
if len(pose) > max(LEFT_SHOULDER, RIGHT_SHOULDER, LEFT_HIP, RIGHT_HIP): |
|
|
|
left_shoulder = (int(pose[LEFT_SHOULDER][0] * w), int(pose[LEFT_SHOULDER][1] * h)) |
|
right_shoulder = (int(pose[RIGHT_SHOULDER][0] * w), int(pose[RIGHT_SHOULDER][1] * h)) |
|
left_hip = (int(pose[LEFT_HIP][0] * w), int(pose[LEFT_HIP][1] * h)) |
|
right_hip = (int(pose[RIGHT_HIP][0] * w), int(pose[RIGHT_HIP][1] * h)) |
|
|
|
|
|
torso_points = np.array([left_shoulder, right_shoulder, right_hip, left_hip], np.int32) |
|
cv2.fillPoly(frame, [torso_points], shirt_color) |
|
cv2.polylines(frame, [torso_points], True, outline_color, 2) |
|
|
|
|
|
|
|
if len(pose) > max(LEFT_SHOULDER, LEFT_ELBOW, LEFT_WRIST): |
|
if pose[LEFT_SHOULDER][0] > 0 and pose[LEFT_ELBOW][0] > 0: |
|
|
|
cv2.line(frame, |
|
(int(pose[LEFT_SHOULDER][0] * w), int(pose[LEFT_SHOULDER][1] * h)), |
|
(int(pose[LEFT_ELBOW][0] * w), int(pose[LEFT_ELBOW][1] * h)), |
|
skin_color, 36) |
|
cv2.line(frame, |
|
(int(pose[LEFT_SHOULDER][0] * w), int(pose[LEFT_SHOULDER][1] * h)), |
|
(int(pose[LEFT_ELBOW][0] * w), int(pose[LEFT_ELBOW][1] * h)), |
|
outline_color, 2) |
|
|
|
|
|
if pose[LEFT_WRIST][0] > 0: |
|
cv2.line(frame, |
|
(int(pose[LEFT_ELBOW][0] * w), int(pose[LEFT_ELBOW][1] * h)), |
|
(int(pose[LEFT_WRIST][0] * w), int(pose[LEFT_WRIST][1] * h)), |
|
skin_color, 30) |
|
cv2.line(frame, |
|
(int(pose[LEFT_ELBOW][0] * w), int(pose[LEFT_ELBOW][1] * h)), |
|
(int(pose[LEFT_WRIST][0] * w), int(pose[LEFT_WRIST][1] * h)), |
|
outline_color, 2) |
|
|
|
|
|
if len(pose) > max(RIGHT_SHOULDER, RIGHT_ELBOW, RIGHT_WRIST): |
|
if pose[RIGHT_SHOULDER][0] > 0 and pose[RIGHT_ELBOW][0] > 0: |
|
|
|
cv2.line(frame, |
|
(int(pose[RIGHT_SHOULDER][0] * w), int(pose[RIGHT_SHOULDER][1] * h)), |
|
(int(pose[RIGHT_ELBOW][0] * w), int(pose[RIGHT_ELBOW][1] * h)), |
|
skin_color, 36) |
|
cv2.line(frame, |
|
(int(pose[RIGHT_SHOULDER][0] * w), int(pose[RIGHT_SHOULDER][1] * h)), |
|
(int(pose[RIGHT_ELBOW][0] * w), int(pose[RIGHT_ELBOW][1] * h)), |
|
outline_color, 2) |
|
|
|
|
|
if pose[RIGHT_WRIST][0] > 0: |
|
cv2.line(frame, |
|
(int(pose[RIGHT_ELBOW][0] * w), int(pose[RIGHT_ELBOW][1] * h)), |
|
(int(pose[RIGHT_WRIST][0] * w), int(pose[RIGHT_WRIST][1] * h)), |
|
skin_color, 30) |
|
cv2.line(frame, |
|
(int(pose[RIGHT_ELBOW][0] * w), int(pose[RIGHT_ELBOW][1] * h)), |
|
(int(pose[RIGHT_WRIST][0] * w), int(pose[RIGHT_WRIST][1] * h)), |
|
outline_color, 2) |
|
|
|
|
|
|
|
if len(pose) > max(LEFT_HIP, LEFT_KNEE, LEFT_ANKLE): |
|
if pose[LEFT_HIP][0] > 0 and pose[LEFT_KNEE][0] > 0: |
|
|
|
cv2.line(frame, |
|
(int(pose[LEFT_HIP][0] * w), int(pose[LEFT_HIP][1] * h)), |
|
(int(pose[LEFT_KNEE][0] * w), int(pose[LEFT_KNEE][1] * h)), |
|
pants_color, 14) |
|
cv2.line(frame, |
|
(int(pose[LEFT_HIP][0] * w), int(pose[LEFT_HIP][1] * h)), |
|
(int(pose[LEFT_KNEE][0] * w), int(pose[LEFT_KNEE][1] * h)), |
|
outline_color, 2) |
|
|
|
|
|
if pose[LEFT_ANKLE][0] > 0: |
|
cv2.line(frame, |
|
(int(pose[LEFT_KNEE][0] * w), int(pose[LEFT_KNEE][1] * h)), |
|
(int(pose[LEFT_ANKLE][0] * w), int(pose[LEFT_ANKLE][1] * h)), |
|
pants_color, 12) |
|
cv2.line(frame, |
|
(int(pose[LEFT_KNEE][0] * w), int(pose[LEFT_KNEE][1] * h)), |
|
(int(pose[LEFT_ANKLE][0] * w), int(pose[LEFT_ANKLE][1] * h)), |
|
outline_color, 2) |
|
|
|
|
|
if len(pose) > max(RIGHT_HIP, RIGHT_KNEE, RIGHT_ANKLE): |
|
if pose[RIGHT_HIP][0] > 0 and pose[RIGHT_KNEE][0] > 0: |
|
|
|
cv2.line(frame, |
|
(int(pose[RIGHT_HIP][0] * w), int(pose[RIGHT_HIP][1] * h)), |
|
(int(pose[RIGHT_KNEE][0] * w), int(pose[RIGHT_KNEE][1] * h)), |
|
pants_color, 14) |
|
cv2.line(frame, |
|
(int(pose[RIGHT_HIP][0] * w), int(pose[RIGHT_HIP][1] * h)), |
|
(int(pose[RIGHT_KNEE][0] * w), int(pose[RIGHT_KNEE][1] * h)), |
|
outline_color, 2) |
|
|
|
|
|
if pose[RIGHT_ANKLE][0] > 0: |
|
cv2.line(frame, |
|
(int(pose[RIGHT_KNEE][0] * w), int(pose[RIGHT_KNEE][1] * h)), |
|
(int(pose[RIGHT_ANKLE][0] * w), int(pose[RIGHT_ANKLE][1] * h)), |
|
pants_color, 12) |
|
cv2.line(frame, |
|
(int(pose[RIGHT_KNEE][0] * w), int(pose[RIGHT_KNEE][1] * h)), |
|
(int(pose[RIGHT_ANKLE][0] * w), int(pose[RIGHT_ANKLE][1] * h)), |
|
outline_color, 2) |
|
|
|
|
|
if len(pose) > max(LEFT_SHOULDER, RIGHT_SHOULDER): |
|
if pose[LEFT_SHOULDER][0] > 0 and pose[RIGHT_SHOULDER][0] > 0: |
|
|
|
neck_center_x = (pose[LEFT_SHOULDER][0] + pose[RIGHT_SHOULDER][0]) / 2 * w |
|
neck_center_y = (pose[LEFT_SHOULDER][1] + pose[RIGHT_SHOULDER][1]) / 2 * h |
|
|
|
|
|
neck_y = neck_center_y - 15 |
|
|
|
|
|
shoulder_distance = abs(pose[LEFT_SHOULDER][0] - pose[RIGHT_SHOULDER][0]) * w |
|
neck_width = shoulder_distance * 0.3 |
|
neck_height = 25 |
|
|
|
|
|
neck_left = int(neck_center_x - neck_width / 2) |
|
neck_right = int(neck_center_x + neck_width / 2) |
|
neck_top = int(neck_y - neck_height / 2) |
|
neck_bottom = int(neck_y + neck_height / 2) |
|
|
|
|
|
cv2.rectangle(frame, (neck_left, neck_top), (neck_right, neck_bottom), skin_color, -1) |
|
cv2.rectangle(frame, (neck_left, neck_top), (neck_right, neck_bottom), outline_color, 2) |
|
|
|
|
|
bow_center_x = int(neck_center_x) |
|
bow_center_y = int(neck_y + neck_height / 2 + 5) |
|
bow_width = 20 |
|
bow_height = 12 |
|
|
|
|
|
left_bow_points = np.array([ |
|
[bow_center_x - bow_width//2, bow_center_y - bow_height//2], |
|
[bow_center_x - bow_width//2 - 8, bow_center_y], |
|
[bow_center_x - bow_width//2, bow_center_y + bow_height//2], |
|
[bow_center_x - 2, bow_center_y + bow_height//2], |
|
[bow_center_x - 2, bow_center_y - bow_height//2] |
|
], np.int32) |
|
cv2.fillPoly(frame, [left_bow_points], bow_tie_color) |
|
cv2.polylines(frame, [left_bow_points], True, outline_color, 1) |
|
|
|
|
|
right_bow_points = np.array([ |
|
[bow_center_x + bow_width//2, bow_center_y - bow_height//2], |
|
[bow_center_x + bow_width//2 + 8, bow_center_y], |
|
[bow_center_x + bow_width//2, bow_center_y + bow_height//2], |
|
[bow_center_x + 2, bow_center_y + bow_height//2], |
|
[bow_center_x + 2, bow_center_y - bow_height//2] |
|
], np.int32) |
|
cv2.fillPoly(frame, [right_bow_points], bow_tie_color) |
|
cv2.polylines(frame, [right_bow_points], True, outline_color, 1) |
|
|
|
|
|
knot_points = np.array([ |
|
[bow_center_x - 2, bow_center_y - 3], |
|
[bow_center_x + 2, bow_center_y - 3], |
|
[bow_center_x + 2, bow_center_y + 3], |
|
[bow_center_x - 2, bow_center_y + 3] |
|
], np.int32) |
|
cv2.fillPoly(frame, [knot_points], bow_tie_color) |
|
cv2.polylines(frame, [knot_points], True, outline_color, 1) |
|
|
|
|
|
for hand, hand_color in [(left_hand, (255, 0, 0)), (right_hand, (0, 0, 255))]: |
|
if np.any(hand != 0): |
|
|
|
palm_points = [] |
|
|
|
palm_indices = [0, 5, 9, 13, 17] |
|
for idx in palm_indices: |
|
if idx < len(hand) and hand[idx][0] > 0 and hand[idx][1] > 0: |
|
palm_points.append([int(hand[idx][0] * w), int(hand[idx][1] * h)]) |
|
|
|
if len(palm_points) > 3: |
|
palm_points = np.array(palm_points, np.int32) |
|
hull = cv2.convexHull(palm_points) |
|
cv2.fillPoly(frame, [hull], (255, 182, 193)) |
|
cv2.polylines(frame, [hull], True, outline_color, 2) |
|
|
|
|
|
for connection in finger_connections: |
|
start_idx, end_idx = connection |
|
if (start_idx < len(hand) and end_idx < len(hand) and |
|
hand[start_idx][0] > 0 and hand[start_idx][1] > 0 and |
|
hand[end_idx][0] > 0 and hand[end_idx][1] > 0): |
|
|
|
start_point = (int(hand[start_idx][0] * w), int(hand[start_idx][1] * h)) |
|
end_point = (int(hand[end_idx][0] * w), int(hand[end_idx][1] * h)) |
|
|
|
|
|
cv2.line(frame, start_point, end_point, (255, 182, 193), 9) |
|
cv2.line(frame, start_point, end_point, outline_color, 1) |
|
|
|
|
|
finger_tips = [THUMB_TIP, INDEX_TIP, MIDDLE_TIP, RING_TIP, PINKY_TIP] |
|
for tip_idx in finger_tips: |
|
if tip_idx < len(hand) and hand[tip_idx][0] > 0 and hand[tip_idx][1] > 0: |
|
tip_x, tip_y = int(hand[tip_idx][0] * w), int(hand[tip_idx][1] * h) |
|
|
|
cv2.circle(frame, (tip_x, tip_y), 4, (255, 182, 193), -1) |
|
cv2.circle(frame, (tip_x, tip_y), 4, outline_color, 2) |
|
|
|
cv2.circle(frame, (tip_x-1, tip_y-1), 1, (255, 255, 255), -1) |
|
|
|
|
|
for i, (x, y) in enumerate(hand): |
|
if x > 0 and y > 0: |
|
point_x, point_y = int(x * w), int(y * h) |
|
|
|
if i in finger_tips: |
|
cv2.circle(frame, (point_x, point_y), 2, hand_color, -1) |
|
else: |
|
cv2.circle(frame, (point_x, point_y), 1, hand_color, -1) |
|
|
|
return frame |
|
|
|
def interpolate_keypoints(kptsA, kptsB, steps): |
|
poseA, leftA, rightA = kptsA |
|
poseB, leftB, rightB = kptsB |
|
|
|
frames = [] |
|
for t in range(1, steps + 1): |
|
alpha = t / (steps + 1) |
|
interp_pose = (1 - alpha) * poseA + alpha * poseB |
|
|
|
|
|
leftA_detected = np.any(leftA != 0) |
|
rightA_detected = np.any(rightA != 0) |
|
leftB_detected = np.any(leftB != 0) |
|
rightB_detected = np.any(rightB != 0) |
|
|
|
|
|
if leftA_detected and leftB_detected: |
|
print("leftA_detected and leftB_detected") |
|
interp_left = (1 - alpha) * leftA + alpha * leftB |
|
elif leftA_detected: |
|
interp_left = leftA |
|
elif leftB_detected: |
|
interp_left = leftB |
|
else: |
|
interp_left = np.zeros((21, 2)) |
|
|
|
|
|
if rightA_detected and rightB_detected: |
|
print("rightA_detected and rightB_detected") |
|
interp_right = (1 - alpha) * rightA + alpha * rightB |
|
elif rightA_detected: |
|
interp_right = rightA |
|
elif rightB_detected: |
|
interp_right = rightB |
|
else: |
|
interp_right = np.zeros((21, 2)) |
|
|
|
frames.append((interp_pose, interp_left, interp_right)) |
|
return frames |
|
|
|
def get_video_writer(output_path, fps=30.0, width=1280, height=720): |
|
""" |
|
Create a video writer with H.264 codec for better browser compatibility. |
|
Falls back to other codecs if H.264 is not available. |
|
""" |
|
|
|
try: |
|
fourcc = cv2.VideoWriter_fourcc(*'avc1') |
|
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height)) |
|
if out.isOpened(): |
|
print("Using H.264 (avc1) codec for video encoding") |
|
return out |
|
else: |
|
out.release() |
|
except Exception as e: |
|
print(f"H.264 codec not available: {e}") |
|
|
|
|
|
try: |
|
fourcc = cv2.VideoWriter_fourcc(*'mp4v') |
|
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height)) |
|
if out.isOpened(): |
|
print("Using MPEG-4 (mp4v) codec for video encoding") |
|
return out |
|
else: |
|
out.release() |
|
except Exception as e: |
|
print(f"MPEG-4 codec not available: {e}") |
|
|
|
|
|
try: |
|
fourcc = cv2.VideoWriter_fourcc(*'XVID') |
|
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height)) |
|
if out.isOpened(): |
|
print("Using XVID codec for video encoding") |
|
return out |
|
else: |
|
out.release() |
|
except Exception as e: |
|
print(f"XVID codec not available: {e}") |
|
|
|
raise RuntimeError("No suitable video codec found") |
|
|
|
def create_stitched_video(videoA_path, videoB_path, output_path="stitched_output.mp4"): |
|
|
|
videoA_keypoints = extract_keypoints_from_video(videoA_path) |
|
videoB_keypoints = extract_keypoints_from_video(videoB_path) |
|
|
|
|
|
out = get_video_writer(output_path, 30.0, 1280, 720) |
|
|
|
|
|
for pose, l, r in videoA_keypoints: |
|
frame = np.ones((720, 1280, 3), dtype=np.uint8) * 255 |
|
out.write(render_person(frame, pose, l, r)) |
|
|
|
|
|
interp = interpolate_keypoints(videoA_keypoints[-1], videoB_keypoints[0], steps=15) |
|
for pose, l, r in interp: |
|
frame = np.ones((720, 1280, 3), dtype=np.uint8) * 255 |
|
out.write(render_person(frame, pose, l, r)) |
|
|
|
|
|
for pose, l, r in videoB_keypoints: |
|
frame = np.ones((720, 1280, 3), dtype=np.uint8) * 255 |
|
out.write(render_person(frame, pose, l, r)) |
|
|
|
out.release() |
|
print(f"Video saved to {output_path}") |
|
|
|
def create_multi_stitched_video(video_paths, output_path="multi_stitched_output.mp4", transition_steps=15): |
|
""" |
|
Create a stitched video from multiple video files. |
|
|
|
Args: |
|
video_paths (list): List of paths to MP4 video files |
|
output_path (str): Output path for the final video |
|
transition_steps (int): Number of frames for transitions between videos |
|
""" |
|
if len(video_paths) < 2: |
|
print("Need at least 2 videos to stitch together!") |
|
return |
|
|
|
print(f"Processing {len(video_paths)} videos...") |
|
|
|
|
|
all_keypoints = [] |
|
for i, video_path in enumerate(video_paths): |
|
print(f"Extracting keypoints from video {i+1}/{len(video_paths)}: {video_path}") |
|
keypoints = extract_keypoints_from_video(video_path) |
|
all_keypoints.append(keypoints) |
|
print(f" - Extracted {len(keypoints)} frames") |
|
|
|
|
|
out = get_video_writer(output_path, 30.0, 1280, 720) |
|
|
|
total_frames = 0 |
|
|
|
|
|
for i, keypoints in enumerate(all_keypoints): |
|
print(f"Rendering video {i+1}/{len(all_keypoints)}...") |
|
|
|
|
|
for pose, l, r in keypoints: |
|
frame = np.ones((720, 1280, 3), dtype=np.uint8) * 255 |
|
out.write(render_person(frame, pose, l, r)) |
|
total_frames += 1 |
|
|
|
|
|
if i < len(all_keypoints) - 1: |
|
print(f" Adding transition to next video...") |
|
next_keypoints = all_keypoints[i + 1] |
|
|
|
|
|
interp = interpolate_keypoints(keypoints[-1], next_keypoints[0], steps=transition_steps) |
|
for pose, l, r in interp: |
|
frame = np.ones((720, 1280, 3), dtype=np.uint8) * 255 |
|
out.write(render_person(frame, pose, l, r)) |
|
total_frames += 1 |
|
|
|
out.release() |
|
print(f"Multi-stitched video saved to {output_path}") |
|
print(f"Total frames rendered: {total_frames}") |
|
print(f"Video duration: {total_frames/30:.2f} seconds") |
|
|
|
if __name__ == "__main__": |
|
|
|
video_list = [ |
|
"/Users/ethantam/desktop/35304.mp4", |
|
"/Users/ethantam/desktop/23978.mp4", |
|
"/Users/ethantam/desktop/23106.mp4", |
|
|
|
] |
|
|
|
|
|
create_multi_stitched_video(video_list, "multi_stitched_output_1.mp4") |
|
|
|
|
|
|