File size: 1,525 Bytes
1c58706
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import numpy as np

def extract_frame_at_index(video_path, frame_index):
    """
    Trích xuất một khung hình cụ thể từ video bằng index.
    """
    cap = cv2.VideoCapture(video_path)
    cap.set(cv2.CAP_PROP_POS_FRAMES, frame_index)
    ret, frame = cap.read()
    cap.release()
    if ret:
        return frame
    return None

def draw_landmarks_on_image(image, landmarks, color=(0, 255, 0)):
    """
    Vẽ khung xương đơn giản lên một ảnh đơn lẻ (BGR).
    """
    annotated_image = image.copy()
    h, w, _ = annotated_image.shape
    
    # Check if landmarks is in the dict format {"pose": [...], "left_hand": ..., "right_hand": ...}
    pose = landmarks.get("pose")
    if pose:
        # Simple pose connection drawing (subset of joints)
        connections = [
            (11, 13), (13, 15), (12, 14), (14, 16), # Arms
            (11, 12), (23, 24), (11, 23), (12, 24), # Torso
            (23, 25), (25, 27), (24, 26), (26, 28)  # Legs
        ]
        for start_idx, end_idx in connections:
            try:
                p1 = (int(pose[start_idx][0] * w), int(pose[start_idx][1] * h))
                p2 = (int(pose[end_idx][0] * w), int(pose[end_idx][1] * h))
                cv2.line(annotated_image, p1, p2, color, 4)
                cv2.circle(annotated_image, p1, 6, (255, 255, 255), -1)
                cv2.circle(annotated_image, p2, 6, (255, 255, 255), -1)
            except (IndexError, TypeError):
                continue

    return annotated_image