Spaces:
Sleeping
Sleeping
File size: 7,962 Bytes
b808ec1 f7d725a e7a4186 b808ec1 f7d725a b808ec1 f7d725a b808ec1 f7d725a b808ec1 f7d725a b808ec1 f7d725a b808ec1 f7d725a b808ec1 f7d725a b808ec1 f7d725a b808ec1 f7d725a b808ec1 e7a4186 b808ec1 f7d725a b808ec1 e7a4186 b808ec1 291adba b808ec1 e7a4186 b808ec1 f7d725a b808ec1 4703605 b808ec1 f7d725a b808ec1 e7a4186 b808ec1 |
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
import cv2
import numpy as np
import onnxruntime as ort
import torch
from mediapipe.python.solutions import (drawing_styles, drawing_utils,
holistic, pose)
from torchvision.transforms.v2 import Compose, UniformTemporalSubsample
def draw_skeleton_on_image(
image: np.ndarray,
detection_results,
resize_to: tuple[int, int] = None,
) -> np.ndarray:
'''
Draw skeleton on the image.
Parameters
----------
image : np.ndarray
Image to draw skeleton on.
detection_results
Detection results.
resize_to : tuple[int, int], optional
Resize the image to the specified size.
Returns
-------
np.ndarray
Annotated image with skeleton.
'''
annotated_image = np.copy(image)
# Draw pose connections
drawing_utils.draw_landmarks(
annotated_image,
detection_results.pose_landmarks,
holistic.POSE_CONNECTIONS,
landmark_drawing_spec=drawing_styles.get_default_pose_landmarks_style(),
)
# Draw left hand connections
drawing_utils.draw_landmarks(
annotated_image,
detection_results.left_hand_landmarks,
holistic.HAND_CONNECTIONS,
drawing_utils.DrawingSpec(color=(121, 22, 76), thickness=2, circle_radius=4),
drawing_utils.DrawingSpec(color=(121, 44, 250), thickness=2, circle_radius=2),
)
# Draw right hand connections
drawing_utils.draw_landmarks(
annotated_image,
detection_results.right_hand_landmarks,
holistic.HAND_CONNECTIONS,
drawing_utils.DrawingSpec(color=(245, 117, 66), thickness=2, circle_radius=4),
drawing_utils.DrawingSpec(color=(245, 66, 230), thickness=2, circle_radius=2),
)
if resize_to is not None:
annotated_image = cv2.resize(
annotated_image,
resize_to,
interpolation=cv2.INTER_AREA,
)
return annotated_image
def calculate_angle(
shoulder: list,
elbow: list,
wrist: list,
) -> float:
'''
Calculate the angle between the shoulder, elbow, and wrist.
Parameters
----------
shoulder : list
Shoulder coordinates.
elbow : list
Elbow coordinates.
wrist : list
Wrist coordinates.
Returns
-------
float
Angle in degree between the shoulder, elbow, and wrist.
'''
shoulder = np.array(shoulder)
elbow = np.array(elbow)
wrist = np.array(wrist)
radians = np.arctan2(wrist[1] - elbow[1], wrist[0] - elbow[0]) \
- np.arctan2(shoulder[1] - elbow[1], shoulder[0] - elbow[0])
angle = np.abs(radians * 180.0 / np.pi)
if angle > 180.0:
angle = 360 - angle
return angle
def do_hands_relax(
pose_landmarks: list,
angle_threshold: float = 160.0,
) -> bool:
'''
Check if the hand is down.
Parameters
----------
hand_landmarks : list
Hand landmarks.
angle_threshold : float, optional
Angle threshold, by default 160.0.
Returns
-------
bool
True if the hand is down, False otherwise.
'''
if pose_landmarks is None:
return True
landmarks = pose_landmarks.landmark
left_shoulder = [
landmarks[pose.PoseLandmark.LEFT_SHOULDER.value].x,
landmarks[pose.PoseLandmark.LEFT_SHOULDER.value].y,
landmarks[pose.PoseLandmark.LEFT_SHOULDER.value].visibility,
]
left_elbow = [
landmarks[pose.PoseLandmark.LEFT_ELBOW.value].x,
landmarks[pose.PoseLandmark.LEFT_ELBOW.value].y,
landmarks[pose.PoseLandmark.LEFT_SHOULDER.value].visibility,
]
left_wrist = [
landmarks[pose.PoseLandmark.LEFT_WRIST.value].x,
landmarks[pose.PoseLandmark.LEFT_WRIST.value].y,
landmarks[pose.PoseLandmark.LEFT_SHOULDER.value].visibility,
]
left_angle = calculate_angle(left_shoulder, left_elbow, left_wrist)
right_shoulder = [
landmarks[pose.PoseLandmark.RIGHT_SHOULDER.value].x,
landmarks[pose.PoseLandmark.RIGHT_SHOULDER.value].y,
landmarks[pose.PoseLandmark.RIGHT_SHOULDER.value].visibility,
]
right_elbow = [
landmarks[pose.PoseLandmark.RIGHT_ELBOW.value].x,
landmarks[pose.PoseLandmark.RIGHT_ELBOW.value].y,
landmarks[pose.PoseLandmark.RIGHT_SHOULDER.value].visibility,
]
right_wrist = [
landmarks[pose.PoseLandmark.RIGHT_WRIST.value].x,
landmarks[pose.PoseLandmark.RIGHT_WRIST.value].y,
landmarks[pose.PoseLandmark.RIGHT_SHOULDER.value].visibility,
]
right_angle = calculate_angle(right_shoulder, right_elbow, right_wrist)
is_visible = all(
[
left_shoulder[2] > 0,
left_elbow[2] > 0,
left_wrist[2] > 0,
right_shoulder[2] > 0,
right_elbow[2] > 0,
right_wrist[2] > 0,
]
)
return all(
[
is_visible,
left_angle < angle_threshold,
right_angle < angle_threshold,
]
)
def get_predictions(
inputs: dict,
ort_session: ort.InferenceSession,
id2gloss: dict,
k: int = 3,
) -> list:
'''
Get the top-k predictions.
Parameters
----------
inputs : dict
Model inputs.
model : VideoMAEForVideoClassification
Model to get predictions from.
k : int, optional
Number of predictions to return, by default 3.
Returns
-------
list
Top-k predictions.
'''
if inputs is None:
return []
logits = torch.from_numpy(ort_session.run(None, inputs)[0])
# Get top-3 predictions
topk_scores, topk_indices = torch.topk(logits, k, dim=1)
topk_scores = torch.nn.functional.softmax(topk_scores, dim=1).squeeze().detach().numpy()
topk_indices = topk_indices.squeeze().detach().numpy()
return [
{
'label': id2gloss[str(topk_indices[i])],
'score': topk_scores[i],
}
for i in range(k)
]
def preprocess(
model_num_frames: int,
keypoints_detector,
source: str,
model_input_height: int,
model_input_width: int,
transform: Compose,
) -> dict:
'''
Preprocess the video.
Parameters
----------
model_num_frames : int
Number of frames in the model.
keypoints_detector
Keypoints detector.
source : str
Video source.
model_input_height : int
Model input height.
model_input_width : int
Model input width.
transform : Compose
Transform to apply.
Returns
-------
dict
Model inputs.
'''
skeleton_video = []
did_sample_start = False
cap = cv2.VideoCapture(source)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Detect keypoints.
detection_results = keypoints_detector.process(frame)
skeleton_frame = draw_skeleton_on_image(
image=np.zeros((1080, 1080, 3), dtype=np.uint8),
detection_results=detection_results,
resize_to=(model_input_height, model_input_width),
)
# (height, width, channels) -> (channels, height, width)
skeleton_frame = transform(torch.tensor(skeleton_frame).permute(2, 0, 1))
# Extract sign video.
if not do_hands_relax(detection_results.pose_landmarks):
if not did_sample_start:
did_sample_start = True
elif did_sample_start:
break
if did_sample_start:
skeleton_video.append(skeleton_frame)
cap.release()
if len(skeleton_video) < model_num_frames:
return None
skeleton_video = torch.stack(skeleton_video)
skeleton_video = UniformTemporalSubsample(model_num_frames)(skeleton_video)
inputs = {
'pixel_values': skeleton_video.unsqueeze(0).numpy(),
}
return inputs
|