13 Commits
2 changed files with 323 additions and 53 deletions
+290 -39
View File
@@ -1155,20 +1155,25 @@ class TRI3DPoseAdaption:
"max": 15.0,
"step": 0.01
}),
"garment_category":([" no_sleeve_garment", "half_sleeve_garment", "full_sleeve_garment", \
"shorts", "trouser"], {"default": " no_sleeve_garment"})
"garment_category":(["no_sleeve_garment", "half_sleeve_garment", "full_sleeve_garment", \
"shorts", "trouser"], {"default": "no_sleeve_garment"}),
"output_save_path": ("STRING",{"default" : "dwpose/keypoints/adapted_pose.json"})
},
"optional": {
"ref_face_pose_json_file": ("STRING",{"default" : ""}),
}
}
RETURN_TYPES = ("IMAGE", "BOOLEAN")
RETURN_TYPES = ("IMAGE", "BOOLEAN","STRING")
FUNCTION = "main"
CATEGORY = "TRI3D"
def main(self, input_pose_json_file, ref_pose_json_file, image_angle, rotation_threshold, garment_category):
def main(self, input_pose_json_file, ref_pose_json_file, image_angle, rotation_threshold, garment_category,output_save_path, ref_face_pose_json_file=""):
from .dwpose import comfy_utils
cur_file_dir = os.path.dirname(os.path.realpath(__file__))
output_save_path = os.path.join(cur_file_dir, output_save_path)
if image_angle == "front":
input_pose = json.load(open(input_pose_json_file))
@@ -1181,6 +1186,13 @@ class TRI3DPoseAdaption:
ref_height = ref_pose['height']
ref_width = ref_pose['width']
ref_keypoints = ref_pose['keypoints']
if ref_face_pose_json_file != "":
ref_face_pose = json.load(open(ref_face_pose_json_file))
ref_face_height = ref_face_pose['height']
ref_face_width = ref_face_pose['width']
ref_face_keypoints = ref_face_pose['keypoints']
similar_torso = None
#check torso similarity
@@ -1202,8 +1214,13 @@ class TRI3DPoseAdaption:
canvas = torch.from_numpy(canvas.astype(np.float32) / 255.0)[
None,
]
return (canvas, similar_torso)
return (canvas, similar_torso, output_save_path)
#Scaling shoulder of input with respect to shoulder-torso ratio of ref-pose
input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 1, 2, 1, 2, ref_torso=True) #left shoulder
input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 1, 5, 1, 5, ref_torso=True) #right shoulder
#Hands
if input_keypoints[4] == [-1,-1]: input_keypoints[4] = ref_keypoints[4]
if input_keypoints[7] == [-1,-1]: input_keypoints[7] = ref_keypoints[7]
@@ -1242,61 +1259,68 @@ class TRI3DPoseAdaption:
# input_keypoints = comfy_utils.rotate_hand(ref_keypoints, input_keypoints, 88) #rotating right hand
input_keypoints = comfy_utils.scale_hand(ref_keypoints, input_keypoints, 6, 7, 88) #scaling right hand w.r.t right wrist
#legs
if garment_category not in ["trouser", "shorts"]:
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 8, 9) #rotate left knee
input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 1, 8, 8, 9) #scale left knee
# #legs
# if garment_category not in ["trouser", "shorts"]:
# input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 8, 9) #rotate left knee
# input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 1, 8, 8, 9, ref_torso=True) #scale left knee
if garment_category != "trouser":
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 9, 10) #rotate left foot
input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 8, 9, 9, 10) #scaling w.r.t to knee to foot ratio of ref pose
# if garment_category != "trouser":
# input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 9, 10) #rotate left foot
# input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 8, 9, 9, 10) #scaling w.r.t to knee to foot ratio of ref pose
if garment_category not in ["trouser", "shorts"]:
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 11, 12) #rotate right knee
input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 1, 11, 11, 12) #scale right knee
# if garment_category not in ["trouser", "shorts"]:
# input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 11, 12) #rotate right knee
# input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 1, 11, 11, 12, ref_torso=True) #scale right knee
if garment_category != "trouser":
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 12, 13) #rotate right foot
input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 11, 12, 12, 13) #scaling w.r.t to knee to foot ratio of ref pose
# if garment_category != "trouser":
# input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 12, 13) #rotate right foot
# input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 11, 12, 12, 13) #scaling w.r.t to knee to foot ratio of ref pose
#face
prev_nose = input_keypoints[0]
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 1, 0) #rotate nose
input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 2, 5, 1, 0) #scale neck to nose w.r.t to shoulder neck-nose len ratio of ref pose
if ref_face_pose_json_file != "":
reference = ref_face_keypoints
else:
reference = ref_keypoints
input_keypoints = comfy_utils.rotate(reference, input_keypoints, 1, 0) #rotate nose
input_keypoints = comfy_utils.scale(reference, input_keypoints, 2, 5, 1, 0) #scale neck to nose w.r.t to shoulder neck-nose len ratio of ref pose
#changing face points to w.r.t to new nose point after rotation
input_keypoints[14:18] = comfy_utils.move(prev_nose, input_keypoints[0], input_keypoints[14:18])
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 0,
input_keypoints = comfy_utils.rotate(reference, input_keypoints, 0,
14) #rotate left eye
input_keypoints = comfy_utils.scale(
ref_keypoints, input_keypoints, 1, 0, 0,
reference, input_keypoints, 1, 0, 0,
14) #scaling w.r.t to neck len to eye_nose len ratio of ref pose
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 0,
input_keypoints = comfy_utils.rotate(reference, input_keypoints, 0,
15) #rotate right eye
input_keypoints = comfy_utils.scale(
ref_keypoints, input_keypoints, 1, 0, 0,
reference, input_keypoints, 1, 0, 0,
15) #scaling w.r.t to neck len to eye_nose len ratio of ref pose
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints,
input_keypoints = comfy_utils.rotate(reference, input_keypoints,
14, 16) #rotate left ear
input_keypoints = comfy_utils.scale(
ref_keypoints, input_keypoints, 1, 0, 14,
reference, input_keypoints, 1, 0, 14,
16) #scaling w.r.t to neck len to ear_nose len ratio of ref pose
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints,
input_keypoints = comfy_utils.rotate(reference, input_keypoints,
15, 17) #rotate right ear
input_keypoints = comfy_utils.scale(
ref_keypoints, input_keypoints, 1, 0, 15,
reference, input_keypoints, 1, 0, 15,
17) #scaling w.r.t to neck len to ear_nose len ratio of ref pose
canvas = comfy_utils.draw_bodypose(canvas, input_keypoints)
canvas = comfy_utils.draw_handpose(canvas, input_keypoints[88:109]) #right hand
canvas = comfy_utils.draw_handpose(canvas, input_keypoints[109:]) #left hand
canvas = torch.from_numpy(canvas.astype(np.float32)/255.0)[None,]
return (canvas, similar_torso)
output_posemap = {"height":input_height, "width":input_width, "keypoints":input_keypoints}
json.dump(output_posemap, open(output_save_path, 'w'))
return (canvas, similar_torso,output_save_path)
if 'back' in image_angle:
@@ -1340,7 +1364,7 @@ class TRI3DPoseAdaption:
if similar_torso == False:
canvas = torch.from_numpy(canvas.astype(np.float32)/255.0)[None,]
return (canvas, similar_torso)
return (canvas, similar_torso, output_save_path)
#Removing the face points if existed
null_indices = [i for i in range(len(ref_keypoints)) if ref_keypoints[i] == [-1,-1]]
@@ -1452,7 +1476,9 @@ class TRI3DPoseAdaption:
canvas = comfy_utils.draw_handpose(canvas, input_keypoints[88:109]) #right hand
canvas = comfy_utils.draw_handpose(canvas, input_keypoints[109:]) #left hand
canvas = torch.from_numpy(canvas.astype(np.float32)/255.0)[None,]
return (canvas, similar_torso)
output_posemap = {"height":input_height, "width":input_width, "keypoints":input_keypoints}
json.dump(output_posemap, open(output_save_path, 'w'))
return (canvas, similar_torso, output_save_path)
class TRI3DLoadPoseJson:
@@ -1611,6 +1637,74 @@ class FloatToImage:
return image
class StringToImage:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"value": ("STRING", {
"default": "Hello World",
})
}
}
CATEGORY = "TRI3D"
RETURN_TYPES = ("IMAGE", )
FUNCTION = "load_image"
def load_image(self, value):
def render_float(float_input):
# latex_expression = '$\\text{' + float_input.replace('_', '\\_') + '}$'
import matplotlib.pyplot as plt
fig = plt.figure(
figsize=(10, 4)) # Dimensions of figsize are in inches
text = fig.text(
x=0.5, # x-coordinate to place the text
y=0.5, # y-coordinate to place the text
s=float_input,
horizontalalignment="center",
verticalalignment="center",
fontsize=32,
)
import tempfile
path_file_image_output = tempfile.NamedTemporaryFile(
).name + '.png'
plt.savefig(path_file_image_output)
import cv2
image = cv2.imread(path_file_image_output, cv2.IMREAD_COLOR)
import os
# os.unlink(path_file_image_output)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
return image
def to_torch_image(image):
import numpy as np
import torch
image = image.astype(dtype=np.float32)
image /= 255.0
image = torch.from_numpy(image)[
None,
]
image = image.unsqueeze(0)
return image
image = render_float(float_input=value)
image = to_torch_image(image)
return image
class TRI3D_recolor_LAB:
@classmethod
@@ -2086,6 +2180,155 @@ class TRI3D_clipdrop_bgremove_api:
# print(mask.shape)
return output,
class TRI3DPoseProportionComparison:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {"required":
{"pose1_json_file":("STRING",{"default":""}), "pose2_json_file":("STRING",{"default":""}), \
"body_part":(["face", "left_elbow", "right_elbow", "left_wrist", "right_wrist", \
"left_knee", "right_knee", "left_ankle", "right_ankle"],{"default":"face"})
}
}
FUNCTION = "run"
RETURN_TYPES = ("FLOAT","FLOAT")
RETURN_NAMES = ("pose1_proportion", "pose2_proportion")
CATEGORY = "TRI3D"
def run(self, pose1_json_file, pose2_json_file, body_part):
from .dwpose import comfy_utils
pose1 = json.load(open(pose1_json_file,'r'))
pose2 = json.load(open(pose2_json_file,'r'))
pose1_keypoints = pose1['keypoints']
pose2_keypoints = pose2['keypoints']
#Calculate torso 2nd coord
pose1_ref_torso_x = int((pose1_keypoints[8][0] + pose1_keypoints[11][0]) / 2)
pose1_ref_torso_y = int((pose1_keypoints[8][1] + pose1_keypoints[11][1]) / 2)
pose2_ref_torso_x = int((pose2_keypoints[8][0] + pose2_keypoints[11][0]) / 2)
pose2_ref_torso_y = int((pose2_keypoints[8][1] + pose2_keypoints[11][1]) / 2)
if body_part == "face":
pose1_ratio = comfy_utils.get_ratio(pose1_keypoints[2][0], pose1_keypoints[2][1], pose1_keypoints[5][0], pose1_keypoints[5][1],
pose1_keypoints[16][0], pose1_keypoints[16][1], pose1_keypoints[17][0], pose1_keypoints[17][1])
pose2_ratio = comfy_utils.get_ratio(pose2_keypoints[2][0], pose2_keypoints[2][1], pose2_keypoints[5][0], pose2_keypoints[5][1],
pose2_keypoints[16][0], pose2_keypoints[16][1], pose2_keypoints[17][0], pose2_keypoints[17][1])
elif body_part == "left_elbow":
pose1_ratio = comfy_utils.get_ratio(pose1_keypoints[2][0], pose1_keypoints[2][1], pose1_keypoints[5][0], pose1_keypoints[5][1],
pose1_keypoints[2][0], pose1_keypoints[2][1], pose1_keypoints[3][0], pose1_keypoints[3][1])
pose2_ratio = comfy_utils.get_ratio(pose2_keypoints[2][0], pose2_keypoints[2][1], pose2_keypoints[5][0], pose2_keypoints[5][1],
pose2_keypoints[2][0], pose2_keypoints[2][1], pose2_keypoints[3][0], pose2_keypoints[3][1])
elif body_part == "right_elbow":
pose1_ratio = comfy_utils.get_ratio(pose1_keypoints[2][0], pose1_keypoints[2][1], pose1_keypoints[5][0], pose1_keypoints[5][1],
pose1_keypoints[5][0], pose1_keypoints[5][1], pose1_keypoints[6][0], pose1_keypoints[6][1])
pose2_ratio = comfy_utils.get_ratio(pose2_keypoints[2][0], pose2_keypoints[2][1], pose2_keypoints[5][0], pose2_keypoints[5][1],
pose2_keypoints[5][0], pose2_keypoints[5][1], pose2_keypoints[6][0], pose2_keypoints[6][1])
elif body_part == "left_wrist":
pose1_ratio = comfy_utils.get_ratio(pose1_keypoints[2][0], pose1_keypoints[2][1], pose1_keypoints[3][0], pose1_keypoints[3][1],
pose1_keypoints[3][0], pose1_keypoints[3][1], pose1_keypoints[4][0], pose1_keypoints[4][1])
pose2_ratio = comfy_utils.get_ratio(pose2_keypoints[2][0], pose2_keypoints[2][1], pose2_keypoints[3][0], pose2_keypoints[3][1],
pose2_keypoints[3][0], pose2_keypoints[3][1], pose2_keypoints[4][0], pose2_keypoints[4][1])
elif body_part == "right_wrist":
pose1_ratio = comfy_utils.get_ratio(pose1_keypoints[5][0], pose1_keypoints[5][1], pose1_keypoints[6][0], pose1_keypoints[6][1],
pose1_keypoints[6][0], pose1_keypoints[6][1], pose1_keypoints[7][0], pose1_keypoints[7][1])
pose2_ratio = comfy_utils.get_ratio(pose2_keypoints[5][0], pose2_keypoints[5][1], pose2_keypoints[6][0], pose2_keypoints[6][1],
pose2_keypoints[6][0], pose2_keypoints[6][1], pose2_keypoints[7][0], pose2_keypoints[7][1])
elif body_part == "left_knee":
pose1_ratio = comfy_utils.get_ratio(pose1_keypoints[1][0], pose1_keypoints[1][1], pose1_ref_torso_x, pose1_ref_torso_y,
pose1_keypoints[8][0], pose1_keypoints[8][1], pose1_keypoints[9][0], pose1_keypoints[9][1])
pose2_ratio = comfy_utils.get_ratio(pose2_keypoints[1][0], pose2_keypoints[1][1], pose2_ref_torso_x, pose2_ref_torso_y,
pose2_keypoints[8][0], pose2_keypoints[8][1], pose2_keypoints[9][0], pose2_keypoints[9][1])
elif body_part == "right_knee":
pose1_ratio = comfy_utils.get_ratio(pose1_keypoints[1][0], pose1_keypoints[1][1], pose1_ref_torso_x, pose1_ref_torso_y,
pose1_keypoints[11][0], pose1_keypoints[11][1], pose1_keypoints[12][0], pose1_keypoints[12][1])
pose2_ratio = comfy_utils.get_ratio(pose2_keypoints[1][0], pose2_keypoints[1][1], pose2_ref_torso_x, pose2_ref_torso_y,
pose2_keypoints[11][0], pose2_keypoints[11][1], pose2_keypoints[12][0], pose2_keypoints[12][1])
elif body_part == "left_ankle":
pose1_ratio = comfy_utils.get_ratio(pose1_keypoints[8][0], pose1_keypoints[8][1], pose1_keypoints[9][0], pose1_keypoints[9][1],
pose1_keypoints[9][0], pose1_keypoints[9][1], pose1_keypoints[10][0], pose1_keypoints[10][1])
pose2_ratio = comfy_utils.get_ratio(pose2_keypoints[8][0], pose2_keypoints[8][1], pose2_keypoints[9][0], pose2_keypoints[9][1],
pose2_keypoints[9][0], pose2_keypoints[9][1], pose2_keypoints[10][0], pose2_keypoints[10][1])
elif body_part == "right_ankle":
pose1_ratio = comfy_utils.get_ratio(pose1_keypoints[11][0], pose1_keypoints[11][1], pose1_keypoints[12][0], pose1_keypoints[12][1],
pose1_keypoints[12][0], pose1_keypoints[12][1], pose1_keypoints[13][0], pose1_keypoints[13][1])
pose2_ratio = comfy_utils.get_ratio(pose2_keypoints[11][0], pose2_keypoints[11][1], pose2_keypoints[12][0], pose2_keypoints[12][1],
pose2_keypoints[12][0], pose2_keypoints[12][1], pose2_keypoints[13][0], pose2_keypoints[13][1])
return pose1_ratio, pose2_ratio
class TRI3DPoseProportions:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {"required":
{"pose1_json_file":("STRING",{"default":""}),
}
}
FUNCTION = "run"
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("proportions")
CATEGORY = "TRI3D"
def run(self, pose1_json_file):
from .dwpose import comfy_utils
pose1 = json.load(open(pose1_json_file,'r'))
pose1_keypoints = pose1['keypoints']
#Calculate torso 2nd coord
pose1_ref_torso_x = int((pose1_keypoints[8][0] + pose1_keypoints[11][0]) / 2)
pose1_ref_torso_y = int((pose1_keypoints[8][1] + pose1_keypoints[11][1]) / 2)
face_ratio = comfy_utils.get_ratio(pose1_keypoints[2][0], pose1_keypoints[2][1], pose1_keypoints[5][0], pose1_keypoints[5][1],
pose1_keypoints[16][0], pose1_keypoints[16][1], pose1_keypoints[17][0], pose1_keypoints[17][1])
left_knee_ratio = comfy_utils.get_ratio(pose1_keypoints[1][0], pose1_keypoints[1][1], pose1_ref_torso_x, pose1_ref_torso_y,
pose1_keypoints[8][0], pose1_keypoints[8][1], pose1_keypoints[9][0], pose1_keypoints[9][1])
right_knee_ratio = comfy_utils.get_ratio(pose1_keypoints[1][0], pose1_keypoints[1][1], pose1_ref_torso_x, pose1_ref_torso_y,
pose1_keypoints[11][0], pose1_keypoints[11][1], pose1_keypoints[12][0], pose1_keypoints[12][1])
knee_ratio = 0.5*(left_knee_ratio + right_knee_ratio)
#ankle ration
left_ankle_ratio = comfy_utils.get_ratio(pose1_keypoints[8][0], pose1_keypoints[8][1], pose1_keypoints[9][0], pose1_keypoints[9][1],
pose1_keypoints[9][0], pose1_keypoints[9][1], pose1_keypoints[10][0], pose1_keypoints[10][1])
right_ankle_ratio = comfy_utils.get_ratio(pose1_keypoints[11][0], pose1_keypoints[11][1], pose1_keypoints[12][0], pose1_keypoints[12][1],
pose1_keypoints[12][0], pose1_keypoints[12][1], pose1_keypoints[13][0], pose1_keypoints[13][1])
ankle_ratio = 0.5*(left_ankle_ratio + right_ankle_ratio)
leg_ratio = 0.5*(knee_ratio + ankle_ratio)
string_output = "Face: " + str(face_ratio) + " Leg: " + str(leg_ratio) + "\nKnee: " + str(knee_ratio) + " Ankle: " + str(ankle_ratio)
print(pose1_json_file.split("/")[-1],string_output)
return string_output
# A dictionary that contains all nodes you want to export with their names
# NOTE: names should be globally unique
NODE_CLASS_MAPPINGS = {
@@ -2102,15 +2345,19 @@ NODE_CLASS_MAPPINGS = {
"tri3d-load-pose-json": TRI3DLoadPoseJson,
"tri3d-face-recognise": TRI3DFaceRecognise,
"tri3d-float-to-image": FloatToImage,
"tri3d-string-to-image": StringToImage,
"tri3d-recolor-mask": TRI3D_recolor,
"tri3d-recolor-mask-LAB_space": TRI3D_recolor_LAB,
"tri3d-recolor-mask-RGB_space": TRI3D_recolor_RGB,
"tri3d-image-mask-2-box": TRI3D_image_mask_2_box,
"tri3d-image-mask-box-2-image": TRI3D_image_mask_box_2_image,
"tri3d-clipdrop-bgremove-api": TRI3D_clipdrop_bgremove_api
"tri3d-clipdrop-bgremove-api": TRI3D_clipdrop_bgremove_api,
"tri3d-pose-proportion-comparision":TRI3DPoseProportionComparison,
"tri3d-pose-proportions":TRI3DPoseProportions
}
VERSION = "2.3"
VERSION = "2.3.x"
# A dictionary that contains the friendly/humanly readable titles for the nodes
NODE_DISPLAY_NAME_MAPPINGS = {
"tri3d-atr-parse-batch": "ATR Parse Batch" + " v" + VERSION,
@@ -2128,10 +2375,14 @@ NODE_DISPLAY_NAME_MAPPINGS = {
"tri3d-load-pose-json": "Load Pose Json" + " v" + VERSION,
"tri3d-face-recognise": "Recognise face" + " v" + VERSION,
"tri3d-float-to-image": "Render float" + " v" + VERSION,
"tri3d-string-to-image": "Render string" + " v" + VERSION,
"tri3d-recolor-mask": "Recolor mask HSV space" + " v" + VERSION,
"tri3d-recolor-mask-LAB_space": "Recolor mask LAB space" + " v" + VERSION,
"tri3d-recolor-mask-RGB_space": "Recolor mask RGB space" + " v" + VERSION,
"tri3d--image-mask-2-box": "Extract box from image" + " v" + VERSION,
"tri3d-image-mask-box-2-image": "Stitch box to image" + " v" + VERSION,
"tri3d-clipdrop-bgremove-api": "RemBG ClipDrop" + " v" + VERSION
"tri3d-clipdrop-bgremove-api": "RemBG ClipDrop" + " v" + VERSION,
"tri3d-pose-proportion-comparision":"Compare pose proportions" + "v" + VERSION,
"tri3d-pose-proportions":"Get pose proportions" + "v" + VERSION,
}
+33 -14
View File
@@ -35,7 +35,7 @@ def draw_bodypose(canvas: np.ndarray, keypoints: list) -> np.ndarray:
keypoint1 = keypoints[k1_index - 1]
keypoint2 = keypoints[k2_index - 1]
if -1 in keypoint1 or -1 in keypoint2:
if any(i < 0 for i in keypoint1) or any(i < 0 for i in keypoint2):
continue
Y = np.array([keypoint1[0], keypoint2[0]])
@@ -46,8 +46,9 @@ def draw_bodypose(canvas: np.ndarray, keypoints: list) -> np.ndarray:
angle = math.degrees(math.atan2(X[0] - X[1], Y[0] - Y[1]))
polygon = cv2.ellipse2Poly((int(mY), int(mX)), (int(length / 2), stickwidth), int(angle), 0, 360, 1)
cv2.fillConvexPoly(canvas, polygon, [int(float(c) * 0.6) for c in color])
for i, (keypoint, color) in enumerate(zip(keypoints, colors)):
if -1 in keypoint:
if any(i < 0 for i in keypoint1):
continue
x, y = keypoint[0], keypoint[1]
@@ -106,25 +107,38 @@ def rotate(src_keypoints, dest_keypoints, point1_idx, point2_idx):
return dest_keypoints
def scale(src_keypoints, dest_keypoints, ref_point1_idx, ref_point2_idx, point1_idx, point2_idx):
ref_x1,ref_y1 = src_keypoints[ref_point1_idx]
ref_x2,ref_y2 = src_keypoints[ref_point2_idx]
ref_x3,ref_y3 = dest_keypoints[ref_point1_idx]
ref_x4,ref_y4 = dest_keypoints[ref_point2_idx]
def scale(src_keypoints, dest_keypoints, ref_point1_idx, ref_point2_idx, point1_idx, point2_idx, ref_torso=False):
if ref_torso == True:
ref_x1,ref_y1 = src_keypoints[1]
ref_x3,ref_y3 = dest_keypoints[1]
ref_x2 = int((src_keypoints[8][0] + src_keypoints[11][0]) / 2)
ref_y2 = int((src_keypoints[8][1] + src_keypoints[11][1]) / 2)
ref_x4 = int((dest_keypoints[8][0] + dest_keypoints[11][0]) / 2)
ref_y4 = int((dest_keypoints[8][1] + dest_keypoints[11][1]) / 2)
else:
ref_x1,ref_y1 = src_keypoints[ref_point1_idx]
ref_x2,ref_y2 = src_keypoints[ref_point2_idx]
ref_x3,ref_y3 = dest_keypoints[ref_point1_idx]
ref_x4,ref_y4 = dest_keypoints[ref_point2_idx]
x1,y1 = src_keypoints[point1_idx]
x2,y2 = src_keypoints[point2_idx]
x3,y3 = dest_keypoints[point1_idx]
x4,y4 = dest_keypoints[point2_idx]
src_ref_len = np.linalg.norm(np.array([ref_x1, ref_y1]) - np.array([ref_x2, ref_y2])) #src ref part distance
dest_ref_len = np.linalg.norm(np.array([ref_x3, ref_y3]) - np.array([ref_x4, ref_y4])) #dest ref part distance
# src_ref_len = np.linalg.norm(np.array([ref_x1, ref_y1]) - np.array([ref_x2, ref_y2])) #src ref part distance
# dest_ref_len = np.linalg.norm(np.array([ref_x3, ref_y3]) - np.array([ref_x4, ref_y4])) #dest ref part distance
src_targ_len = np.linalg.norm(np.array([x1, y1]) - np.array([x2, y2])) #src targ part distance
dest_targ_len = np.linalg.norm(np.array([x3, y3]) - np.array([x4,y4])) #dest targ part distance
# src_targ_len = np.linalg.norm(np.array([x1, y1]) - np.array([x2, y2])) #src targ part distance
# dest_targ_len = np.linalg.norm(np.array([x3, y3]) - np.array([x4,y4])) #dest targ part distance
src_targ_ref_ratio = src_targ_len / src_ref_len #src targ to ref ratio
dest_targ_ref_ratio = dest_targ_len / dest_ref_len #dest targ to ref ratio
# src_targ_ref_ratio = src_targ_len / src_ref_len #src targ to ref ratio
# dest_targ_ref_ratio = dest_targ_len / dest_ref_len #dest targ to ref ratio
src_targ_ref_ratio = get_ratio(ref_x1, ref_y1, ref_x2, ref_y2, x1, y1, x2, y2) #src targ to ref ratio
dest_targ_ref_ratio = get_ratio(ref_x3, ref_y3, ref_x4, ref_y4, x3, y3, x4, y4) #dest targ to ref ratio
scale = src_targ_ref_ratio / dest_targ_ref_ratio
@@ -135,6 +149,11 @@ def scale(src_keypoints, dest_keypoints, ref_point1_idx, ref_point2_idx, point1_
return dest_keypoints
def get_ratio(ref_x1, ref_y1, ref_x2, ref_y2, x1, y1, x2, y2):
ref_len = np.linalg.norm(np.array([ref_x1, ref_y1]) - np.array([ref_x2, ref_y2])) #ref body part length
targ_len = np.linalg.norm(np.array([x1, y1]) - np.array([x2, y2])) #targ body part length
return targ_len / ref_len
def scale_hand(src_keypoints, dest_keypoints, ref_point1_idx, ref_point2_idx, starting_idx):
"""
@@ -239,7 +258,7 @@ def get_torso_angles(keypoints):
a,b = keypoints[5],keypoints[1]
angle_radians = math.tan((a[1]-b[1])/(a[0]-b[0]))
rs_angle = abs(math.degrees(angle_radians))
#getting bisector of torso and getting angle with y_axis
a,b,c = keypoints[8], keypoints[1], keypoints[11]