MCPcopy Create free account
hub / github.com/ModelTC/LightX2V / rescale_skeleton

Function rescale_skeleton

tools/preprocess/retarget_pose.py:300–354  ·  view source on GitHub ↗
(H, W, keypoints, bone_ratio_list)

Source from the content-addressed store, hash-verified

298
299
300def rescale_skeleton(H, W, keypoints, bone_ratio_list):
301 rescale_keypoints = keypoints.copy()
302
303 new_length_list = []
304 angle_list = []
305
306 # keypoints from 0-1 to H/W range
307 for idx in range(len(rescale_keypoints)):
308 if rescale_keypoints[idx] is None or len(rescale_keypoints[idx]) == 0:
309 continue
310
311 rescale_keypoints[idx] = [rescale_keypoints[idx][0] * W, rescale_keypoints[idx][1] * H]
312
313 # first traverse, get new_length_list and angle_list
314 for idx, (k1_index, k2_index) in enumerate(limbSeq):
315 keypoint1 = rescale_keypoints[k1_index - 1]
316 keypoint2 = rescale_keypoints[k2_index - 1]
317
318 if keypoint1 is None or keypoint2 is None or len(keypoint1) == 0 or len(keypoint2) == 0:
319 new_length_list.append(None)
320 angle_list.append(None)
321 continue
322
323 Y = np.array([keypoint1[0], keypoint2[0]]) # * float(W)
324 X = np.array([keypoint1[1], keypoint2[1]]) # * float(H)
325
326 length = ((X[0] - X[1]) ** 2 + (Y[0] - Y[1]) ** 2) ** 0.5
327
328 new_length = length * bone_ratio_list[idx]
329 angle = math.degrees(math.atan2(X[0] - X[1], Y[0] - Y[1]))
330
331 new_length_list.append(new_length)
332 angle_list.append(angle)
333
334 # # second traverse, calculate new keypoints
335 for idx, (k1_index, k2_index) in enumerate(limbSeq):
336 # update dst_keypoints
337 start_keypoint = rescale_keypoints[k1_index - 1]
338 new_length = new_length_list[idx]
339 angle = angle_list[idx]
340
341 if rescale_keypoints[k1_index - 1] is None or rescale_keypoints[k2_index - 1] is None or len(rescale_keypoints[k1_index - 1]) == 0 or len(rescale_keypoints[k2_index - 1]) == 0:
342 continue
343
344 # calculate end_keypoint
345 delta_x = new_length * math.cos(math.radians(angle))
346 delta_y = new_length * math.sin(math.radians(angle))
347
348 end_keypoint_x = start_keypoint[0] - delta_x
349 end_keypoint_y = start_keypoint[1] - delta_y
350
351 # update keypoints
352 rescale_keypoints[k2_index - 1] = [end_keypoint_x, end_keypoint_y]
353
354 return rescale_keypoints
355
356
357def fix_lack_keypoints_use_sym(skeleton):

Callers 1

retarget_poseFunction · 0.85

Calls 2

copyMethod · 0.80
appendMethod · 0.80

Tested by

no test coverage detected