| 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 |
| |
| |
| pose = landmarks.get("pose") |
| if pose: |
| |
| connections = [ |
| (11, 13), (13, 15), (12, 14), (14, 16), |
| (11, 12), (23, 24), (11, 23), (12, 24), |
| (23, 25), (25, 27), (24, 26), (26, 28) |
| ] |
| 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 |
|
|