Download src/core/utils.py from AnhLee0/AI-Coach: direct link, hf CLI and curl.
- Browser
- Download file 1.53 kB
-
https://huggingface.co/spaces/AnhLee0/AI-Coach/resolve/main/src/core/utils.py
- Command line
-
hf download hf://spaces/AnhLee0/AI-Coach/src/core/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/AnhLee0/AI-Coach/resolve/main/src/core/utils.py
1.53 kB
| 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 | |