2 changed files with 322 additions and 432 deletions
+289 -418
View File
@@ -7,10 +7,6 @@ import comfy.model_management as model_management
import folder_paths
from PIL import Image, ImageOps
from transformers import AutoProcessor
from transformers import OneFormerForUniversalSegmentation
from transformers import OneFormerProcessor
def from_torch_image(image):
image = image.squeeze().cpu().numpy() * 255.0
@@ -57,30 +53,6 @@ def do_work_with_mask(image, mask, external_file_name):
return image
def get_segmentation_of_person(image):
NAME_MODEL_ONEFORMER = 'shi-labs/oneformer_ade20k_dinat_large'
processor = OneFormerProcessor.from_pretrained(NAME_MODEL_ONEFORMER)
model = OneFormerForUniversalSegmentation.from_pretrained(
NAME_MODEL_ONEFORMER)
width = image.shape[1]
height = image.shape[0]
inputs = processor(image, ["semantic"], return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
predicted_semantic_map = processor.post_process_semantic_segmentation(
outputs, target_sizes=[(height, width)])[0]
img = predicted_semantic_map.detach().cpu().numpy()
img = (img == 12)
img = img.astype(np.uint8) * 255
return img
class TRI3DATRParseBatch:
def __init__(self):
@@ -1117,7 +1089,8 @@ class TRI3DDWPose_Preprocessor:
include_face=detect_face,
include_body=detect_body)
cur_file_dir = os.path.dirname(os.path.realpath(__file__))
save_file_path = os.path.join(cur_file_dir, filename_path)
save_file_path = os.path.join(cur_file_dir,
filename_path)
json.dump(pose_dict, open(save_file_path, 'w'))
np_result = cv2.resize(np_result, (W, H),
interpolation=cv2.INTER_AREA)
@@ -1158,12 +1131,9 @@ class TRI3DPosetoImage:
canvas = np.zeros(shape=(height, width, 3), dtype=np.uint8)
canvas = comfy_utils.draw_bodypose(canvas, keypoints)
canvas = comfy_utils.draw_handpose(canvas,
keypoints[88:109]) #right hand
canvas = comfy_utils.draw_handpose(canvas, keypoints[88:109]) #right hand
canvas = comfy_utils.draw_handpose(canvas, keypoints[109:]) #left hand
canvas = torch.from_numpy(canvas.astype(np.float32) / 255.0)[
None,
]
canvas = torch.from_numpy(canvas.astype(np.float32)/255.0)[None,]
return (canvas, )
@@ -1185,34 +1155,44 @@ 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")
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))
input_height = input_pose['height']
input_width = input_pose['width']
input_keypoints = input_pose['keypoints']
canvas = np.zeros(shape=(input_height, input_width, 3),
dtype=np.uint8)
canvas = np.zeros(shape=(input_height, input_width, 3), dtype=np.uint8)
ref_pose = json.load(open(ref_pose_json_file))
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
@@ -1226,175 +1206,122 @@ class TRI3DPoseAdaption:
rs_angle_diff = abs(rs_angle_2 - rs_angle_1)
torso_angle_diff = abs(torso_angle_2 - torso_angle_1)
similar_torso = False if (
ls_angle_diff >= rotation_threshold) | (
rs_angle_diff >= rotation_threshold) | (
torso_angle_diff >= rotation_threshold) else True
similar_torso = False if (ls_angle_diff >= rotation_threshold) | (
rs_angle_diff >= rotation_threshold) | (torso_angle_diff >= rotation_threshold) else True
if similar_torso == False:
canvas = torch.from_numpy(
canvas.astype(np.float32) / 255.0)[
None,
]
canvas = torch.from_numpy(canvas.astype(np.float32) / 255.0)[
None,
]
return (canvas, similar_torso)
#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]
if input_keypoints[4] == [-1,-1]: input_keypoints[4] = ref_keypoints[4]
if input_keypoints[7] == [-1,-1]: input_keypoints[7] = ref_keypoints[7]
input_keypoints[88:] = ref_keypoints[88:] #replace hands with reference hands
input_keypoints[88:] = ref_keypoints[
88:] #replace hands with reference hands
if garment_category not in [
"half_sleeve_garment", "full_sleeve_garment"
]:
input_keypoints = comfy_utils.rotate(ref_keypoints,
input_keypoints, 2,
3) # rotate left elbow
input_keypoints = comfy_utils.scale(
ref_keypoints, input_keypoints, 2, 5, 2,
3) #scaling w.r.t to shoulder to elbow ratio of ref pose
if garment_category not in ["half_sleeve_garment", "full_sleeve_garment"]:
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 2, 3) # rotate left elbow
input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 2, 5, 2, 3) #scaling w.r.t to shoulder to elbow ratio of ref pose
prev_lw = input_keypoints[4]
if garment_category != "full_sleeve_garment":
input_keypoints = comfy_utils.rotate(ref_keypoints,
input_keypoints, 3,
4) #rotate left wrist
input_keypoints = comfy_utils.scale(
ref_keypoints, input_keypoints, 2, 3, 3,
4) #scaling w.r.t to elbow to wrist ratio of ref pose
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 3, 4) #rotate left wrist
input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 2, 3, 3, 4) #scaling w.r.t to elbow to wrist ratio of ref pose
#moving to hand ponts to wrist
input_keypoints[109:] = comfy_utils.move(input_keypoints[109],
input_keypoints[4],
input_keypoints[109:])
input_keypoints[109:] = comfy_utils.move(ref_keypoints[4],
ref_keypoints[109],
input_keypoints[109:])
#moving to hand ponts to wrist
input_keypoints[109:] = comfy_utils.move(input_keypoints[109], input_keypoints[4], input_keypoints[109:])
input_keypoints[109:] = comfy_utils.move(ref_keypoints[4], ref_keypoints[109], input_keypoints[109:])
# input_keypoints = comfy_utils.rotate_hand(ref_keypoints, input_keypoints, 109) #rotating left hand
input_keypoints = comfy_utils.scale_hand(
ref_keypoints, input_keypoints, 3, 4,
109) #scaling left hand w.r.t left wrist
input_keypoints = comfy_utils.scale_hand(ref_keypoints, input_keypoints, 3, 4, 109) #scaling left hand w.r.t left wrist
if garment_category not in [
"half_sleeve_garment", "full_sleeve_garment"
]:
input_keypoints = comfy_utils.rotate(ref_keypoints,
input_keypoints, 5,
6) #rotate right elbow
input_keypoints = comfy_utils.scale(
ref_keypoints, input_keypoints, 2, 5, 5,
6) #scaling w.r.t to shoulder to elbow ratio of ref pose
if garment_category not in ["half_sleeve_garment", "full_sleeve_garment"]:
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 5, 6) #rotate right elbow
input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 2, 5, 5, 6) #scaling w.r.t to shoulder to elbow ratio of ref pose
prev_rw = input_keypoints[7]
if garment_category != "full_sleeve_garment":
input_keypoints = comfy_utils.rotate(ref_keypoints,
input_keypoints, 6,
7) #rotate right wrist
input_keypoints = comfy_utils.scale(
ref_keypoints, input_keypoints, 5, 6, 6,
7) #scaling w.r.t to elbow to wrist ratio of ref pose
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 6, 7) #rotate right wrist
input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 5, 6, 6, 7) #scaling w.r.t to elbow to wrist ratio of ref pose
#moving hand points to wrist
input_keypoints[88:109] = comfy_utils.move(input_keypoints[88],
input_keypoints[7],
input_keypoints[88:109])
input_keypoints[88:109] = comfy_utils.move(ref_keypoints[7],
ref_keypoints[88],
input_keypoints[88:109])
input_keypoints[88:109] = comfy_utils.move(input_keypoints[88], input_keypoints[7], input_keypoints[88:109])
input_keypoints[88:109] = comfy_utils.move(ref_keypoints[7], ref_keypoints[88], input_keypoints[88:109])
# 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
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
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
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
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
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[14:18] = comfy_utils.move(prev_nose, input_keypoints[0], input_keypoints[14:18])
input_keypoints = comfy_utils.rotate(ref_keypoints,
input_keypoints, 0,
14) #rotate left eye
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, 14
) #scaling w.r.t to neck len to eye_nose len ratio of ref pose
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,
15) #rotate right eye
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, 15
) #scaling w.r.t to neck len to eye_nose len ratio of ref pose
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, 14,
16) #rotate left ear
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, 16
) #scaling w.r.t to neck len to ear_nose len ratio of ref pose
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, 15,
17) #rotate right ear
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, 17
) #scaling w.r.t to neck len to ear_nose len ratio of ref pose
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,
]
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,]
output_posemap = {"height":input_height, "width":input_width, "keypoints":input_keypoints}
json.dump(output_posemap, open(output_save_path, 'w'))
return (canvas, similar_torso)
if 'back' in image_angle:
input_pose = json.load(open(input_pose_json_file))
@@ -1403,58 +1330,47 @@ class TRI3DPoseAdaption:
input_keypoints = input_pose['keypoints']
input_pose_type = comfy_utils.get_input_pose_type(input_keypoints)
canvas = np.zeros(shape=(input_height, input_width, 3),
dtype=np.uint8)
canvas = np.zeros(shape=(input_height, input_width, 3), dtype=np.uint8)
if 'back_fixed' in image_angle:
back_pose_dir = pathlib.Path().resolve(
) / 'custom_nodes/tri3d-comfyui-nodes/samples/back_poses/'
back_pose_dir = pathlib.Path().resolve() / 'custom_nodes/tri3d-comfyui-nodes/samples/back_poses/'
back_pose_dictionary = {
'back_fixed': 'backpose.json',
'back_fixed_left': 'left_backpose.json',
'back_fixed_right': 'right_backpose.json'
'back_fixed' : 'backpose.json',
'back_fixed_left' : 'left_backpose.json',
'back_fixed_right' : 'right_backpose.json'
}
ref_pose_json_file = back_pose_dir / back_pose_dictionary[
image_angle]
ref_pose_json_file = back_pose_dir / back_pose_dictionary[image_angle]
# /home/ubuntu/GITHUB/comfyanonymous/ComfyUI/custom_nodes/tri3d-comfyui-nodes
print(ref_pose_json_file)
ref_pose = json.load(open(ref_pose_json_file))
ref_keypoints = ref_pose['keypoints']
similar_torso = None
#check torso similarity
if garment_category not in ["shorts", "trouser"]:
ls_angle_1, rs_angle_1, torso_angle_1 = comfy_utils.get_torso_angles(
input_keypoints)
ls_angle_2, rs_angle_2, torso_angle_2 = comfy_utils.get_torso_angles(
ref_keypoints)
ls_angle_1, rs_angle_1, torso_angle_1 = comfy_utils.get_torso_angles(input_keypoints)
ls_angle_2, rs_angle_2, torso_angle_2 = comfy_utils.get_torso_angles(ref_keypoints)
ls_angle_diff = abs(ls_angle_2 - ls_angle_1)
rs_angle_diff = abs(rs_angle_2 - rs_angle_1)
torso_angle_diff = abs(torso_angle_2 - torso_angle_1)
similar_torso = False if (ls_angle_diff >= 5) | (
rs_angle_diff >= 5) | (torso_angle_diff >= 5) else True
similar_torso = False if (ls_angle_diff >= 5) | (rs_angle_diff >= 5) | (torso_angle_diff >= 5) else True
if similar_torso == False:
canvas = torch.from_numpy(
canvas.astype(np.float32) / 255.0)[
None,
]
canvas = torch.from_numpy(canvas.astype(np.float32)/255.0)[None,]
return (canvas, similar_torso)
#Removing the face points if existed
null_indices = [
i for i in range(len(ref_keypoints))
if ref_keypoints[i] == [-1, -1]
]
null_indices = [i for i in range(len(ref_keypoints)) if ref_keypoints[i] == [-1,-1]]
for i in null_indices:
input_keypoints[i] = [-1, -1]
input_keypoints[i] = [-1,-1]
all_x = [i[0] for i in input_keypoints if i[0] != -1]
min_width = min(all_x)
max_width = max(all_x)
@@ -1462,189 +1378,106 @@ class TRI3DPoseAdaption:
if input_pose_type == "front_pose":
#flip horizontally
for i in range(len(input_keypoints)):
x, y = input_keypoints[i]
if input_keypoints[i] == [-1, -1]: continue
input_keypoints[i] = [(max_width - x) + min_width, y]
x,y = input_keypoints[i]
if input_keypoints[i] == [-1,-1]: continue
input_keypoints[i] = [(max_width - x)+min_width, y]
#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]
input_keypoints[88:] = ref_keypoints[
88:] #replace hands with reference hands
if garment_category not in [
"half_sleeve_garment", "full_sleeve_garment"
]:
input_keypoints = comfy_utils.rotate(ref_keypoints,
input_keypoints, 2,
3) # rotate left elbow
input_keypoints = comfy_utils.scale(
ref_keypoints, input_keypoints, 2, 5, 2,
3) #scaling w.r.t to shoulder to elbow ratio of ref pose
if input_keypoints[4] == [-1,-1]: input_keypoints[4] = ref_keypoints[4]
if input_keypoints[7] == [-1,-1]: input_keypoints[7] = ref_keypoints[7]
input_keypoints[88:] = ref_keypoints[88:] #replace hands with reference hands
if garment_category not in ["half_sleeve_garment", "full_sleeve_garment"]:
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 2, 3) # rotate left elbow
input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 2, 5, 2, 3) #scaling w.r.t to shoulder to elbow ratio of ref pose
prev_lw = input_keypoints[4]
if garment_category != "full_sleeve_garment":
input_keypoints = comfy_utils.rotate(ref_keypoints,
input_keypoints, 3,
4) #rotate left wrist
input_keypoints = comfy_utils.scale(
ref_keypoints, input_keypoints, 2, 3, 3,
4) #scaling w.r.t to elbow to wrist ratio of ref pose
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 3, 4) #rotate left wrist
input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 2, 3, 3, 4) #scaling w.r.t to elbow to wrist ratio of ref pose
#moving to hand ponts to wrist
input_keypoints[109:] = comfy_utils.move(input_keypoints[109],
input_keypoints[4],
input_keypoints[109:])
input_keypoints[109:] = comfy_utils.move(ref_keypoints[4],
ref_keypoints[109],
input_keypoints[109:])
#moving to hand ponts to wrist
input_keypoints[109:] = comfy_utils.move(input_keypoints[109], input_keypoints[4], input_keypoints[109:])
input_keypoints[109:] = comfy_utils.move(ref_keypoints[4], ref_keypoints[109], input_keypoints[109:])
# input_keypoints = comfy_utils.rotate_hand(ref_keypoints, input_keypoints, 109) #rotating left hand
input_keypoints = comfy_utils.scale_hand(
ref_keypoints, input_keypoints, 3, 4,
109) #scaling left hand w.r.t left wrist
input_keypoints = comfy_utils.scale_hand(ref_keypoints, input_keypoints, 3, 4, 109) #scaling left hand w.r.t left wrist
if garment_category not in [
"half_sleeve_garment", "full_sleeve_garment"
]:
input_keypoints = comfy_utils.rotate(ref_keypoints,
input_keypoints, 5,
6) #rotate right elbow
input_keypoints = comfy_utils.scale(
ref_keypoints, input_keypoints, 2, 5, 5,
6) #scaling w.r.t to shoulder to elbow ratio of ref pose
if garment_category not in ["half_sleeve_garment", "full_sleeve_garment"]:
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 5, 6) #rotate right elbow
input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 2, 5, 5, 6) #scaling w.r.t to shoulder to elbow ratio of ref pose
prev_rw = input_keypoints[7]
if garment_category != "full_sleeve_garment":
input_keypoints = comfy_utils.rotate(ref_keypoints,
input_keypoints, 6,
7) #rotate right wrist
input_keypoints = comfy_utils.scale(
ref_keypoints, input_keypoints, 5, 6, 6,
7) #scaling w.r.t to elbow to wrist ratio of ref pose
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 6, 7) #rotate right wrist
input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 5, 6, 6, 7) #scaling w.r.t to elbow to wrist ratio of ref pose
#moving hand points to wrist
input_keypoints[88:109] = comfy_utils.move(input_keypoints[88],
input_keypoints[7],
input_keypoints[88:109])
input_keypoints[88:109] = comfy_utils.move(ref_keypoints[7],
ref_keypoints[88],
input_keypoints[88:109])
input_keypoints[88:109] = comfy_utils.move(input_keypoints[88], input_keypoints[7], input_keypoints[88:109])
input_keypoints[88:109] = comfy_utils.move(ref_keypoints[7], ref_keypoints[88], input_keypoints[88:109])
# 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
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
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
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
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 [
"half_sleeve_garment", "full_sleeve_garment"
]:
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 ["half_sleeve_garment", "full_sleeve_garment"]:
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 != "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
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
input_keypoints[14:18] = ref_keypoints[14:18]
input_keypoints[0] = ref_keypoints[0]
if input_keypoints[0] == [-1, -1] and input_keypoints[14] == [
-1, -1
] and input_keypoints[15] == [-1, -1]:
input_keypoints = comfy_utils.rotate(ref_keypoints,
input_keypoints, 1,
16) #rotate left ear
input_keypoints = comfy_utils.scale(
ref_keypoints, input_keypoints, 2, 5, 1, 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, 1,
17) #rotate right ear
input_keypoints = comfy_utils.scale(
ref_keypoints, input_keypoints, 2, 5, 1, 17
) #scaling w.r.t to neck len to ear_nose len ratio of ref pose
if input_keypoints[0] == [-1,-1] and input_keypoints[14] == [-1,-1] and input_keypoints[15] == [-1,-1]:
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 1, 16) #rotate left ear
input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 2, 5, 1, 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, 1,17) #rotate right ear
input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 2, 5, 1, 17) #scaling w.r.t to neck len to ear_nose len ratio of ref pose
else:
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
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
#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[14:18] = comfy_utils.move(prev_nose, input_keypoints[0], input_keypoints[14:18])
if input_keypoints[14] != [-1, -1]:
input_keypoints = comfy_utils.rotate(
ref_keypoints, input_keypoints, 0,
14) #rotate left eye
input_keypoints = comfy_utils.scale(
ref_keypoints, input_keypoints, 1, 0, 0, 14
) #scaling w.r.t to neck len to eye_nose len ratio of ref pose
if input_keypoints[14] != [-1,-1]:
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 0, 14) #rotate left eye
input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 1, 0, 0, 14) #scaling w.r.t to neck len to eye_nose len ratio of ref pose
if input_keypoints[15] != [-1, -1]:
input_keypoints = comfy_utils.rotate(
ref_keypoints, input_keypoints, 0,
15) #rotate right eye
input_keypoints = comfy_utils.scale(
ref_keypoints, input_keypoints, 1, 0, 0, 15
) #scaling w.r.t to neck len to eye_nose len ratio of ref pose
if input_keypoints[15] != [-1,-1]:
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 0, 15) #rotate right eye
input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 1, 0, 0, 15) #scaling w.r.t to neck len to eye_nose len ratio of ref pose
if input_keypoints[16] != [-1, -1]:
input_keypoints = comfy_utils.rotate(
ref_keypoints, input_keypoints, 14,
16) #rotate left ear
input_keypoints = comfy_utils.scale(
ref_keypoints, input_keypoints, 1, 0, 14, 16
) #scaling w.r.t to neck len to ear_nose len ratio of ref pose
if input_keypoints[16] != [-1,-1]:
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 14, 16) #rotate left ear
input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 1, 0, 14, 16) #scaling w.r.t to neck len to ear_nose len ratio of ref pose
if input_keypoints[17] != [-1, -1]:
input_keypoints = comfy_utils.rotate(
ref_keypoints, input_keypoints, 15,
17) #rotate right ear
input_keypoints = comfy_utils.scale(
ref_keypoints, input_keypoints, 1, 0, 15, 17
) #scaling w.r.t to neck len to ear_nose len ratio of ref pose
if input_keypoints[17] != [-1,-1]:
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 15,17) #rotate right ear
input_keypoints = comfy_utils.scale(ref_keypoints, 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,
]
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,]
output_posemap = {"height":input_height, "width":input_width, "keypoints":input_keypoints}
json.dump(output_posemap, open(output_save_path, 'w'))
return (canvas, similar_torso)
@@ -1684,13 +1517,9 @@ class TRI3DLoadPoseJson:
canvas = np.zeros(shape=(height, width, 3), dtype=np.uint8)
canvas = comfy_utils.draw_bodypose(canvas, keypoints)
canvas = comfy_utils.draw_handpose(canvas,
keypoints[88:109]) #right hand
canvas = comfy_utils.draw_handpose(canvas,
keypoints[109:]) #left hand
canvas = torch.from_numpy(canvas.astype(np.float32) / 255.0)[
None,
]
canvas = comfy_utils.draw_handpose(canvas, keypoints[88:109]) #right hand
canvas = comfy_utils.draw_handpose(canvas, keypoints[109:]) #left hand
canvas = torch.from_numpy(canvas.astype(np.float32)/255.0)[None,]
else:
canvas = np.zeros(shape=(height, width, 3), dtype=np.uint8)
# if 'A' in i.getbands():
@@ -1808,7 +1637,6 @@ class FloatToImage:
return image
class TRI3D_recolor_LAB:
@classmethod
@@ -2179,6 +2007,8 @@ class TRI3D_recolor:
return image_output
class TRI3D_image_mask_2_box:
def __init__(self):
@@ -2232,7 +2062,6 @@ class TRI3D_image_mask_box_2_image:
image = to_torch_image(image)
return image
class TRI3D_clipdrop_bgremove_api:
def __init__(self):
@@ -2259,14 +2088,13 @@ class TRI3D_clipdrop_bgremove_api:
CLIPDROP_API_KEY = os.getenv('CLIPDROP_API_KEY')
# CLIPDROP_API_KEY = os.environ.get('CLIPDROP_API_KEY')
r = requests.post('https://clipdrop-api.co/remove-background/v1',
files={
'image_file':
("mannequin.jpg", enc_image.tobytes(),
'image/jpeg'),
},
headers={'x-api-key': CLIPDROP_API_KEY})
files = {
'image_file': ("mannequin.jpg", enc_image.tobytes(), 'image/jpeg'),
},
headers = { 'x-api-key': CLIPDROP_API_KEY}
)
if (r.ok):
pass
else:
@@ -2276,9 +2104,7 @@ class TRI3D_clipdrop_bgremove_api:
# print("decoded output",output.shape)
# mask = output[:,:,3]
# output = output[:,:,0:3]
output = torch.from_numpy(output.astype(np.float32) / 255.0)[
None,
]
output = torch.from_numpy(output.astype(np.float32)/255.0)[None,]
# print("converted image to torch")
# print(output.shape)
# mask = torch.from_numpy(mask.astype(np.float32)/255.0)[None,]
@@ -2286,36 +2112,99 @@ class TRI3D_clipdrop_bgremove_api:
# print(mask.shape)
return output,
class TRI3D_oneformer_person_mask:
class TRI3DPoseProportion:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE", ),
},
}
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 = ("IMAGE", )
CATEGORY = "tri3d"
RETURN_TYPES = ("FLOAT","FLOAT")
RETURN_NAMES = ("pose1_proportion", "pose2_proportion")
CATEGORY = "TRI3D"
def run(self, image):
image = from_torch_image(image)
def run(self, pose1_json_file, pose2_json_file, body_part):
from .dwpose import comfy_utils
image_final = np.zeros((image.shape[0], image.shape[1], 4),
dtype=np.uint8)
pose1 = json.load(open(pose1_json_file,'r'))
pose2 = json.load(open(pose2_json_file,'r'))
pose1_keypoints = pose1['keypoints']
pose2_keypoints = pose2['keypoints']
image_final[:, :, 0:3] = image
image = get_segmentation_of_person(image)
image_final[:, :, 3] = image[:, :]
image = to_torch_image(image_final)
return image
#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
# A dictionary that contains all nodes you want to export with their names
# NOTE: names should be globally unique
@@ -2339,50 +2228,32 @@ NODE_CLASS_MAPPINGS = {
"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-oneformer-person-mask": TRI3D_oneformer_person_mask,
"tri3d-pose-proportion":TRI3DPoseProportion
}
VERSION = "2.3"
# 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,
'tri3d-extract-parts-batch':
'Extract Parts Batch' + " v" + VERSION,
'tri3d-extract-parts-batch2':
'Extract Parts Batch 2' + " v" + VERSION,
"tri3d-position-parts-batch":
"Position Parts Batch" + " v" + VERSION,
"tri3d-swap-pixels":
"Swap Pixels by Mask" + " v" + VERSION,
"tri3d-atr-parse-batch": "ATR Parse Batch" + " v" + VERSION,
'tri3d-extract-parts-batch': 'Extract Parts Batch' + " v" + VERSION,
'tri3d-extract-parts-batch2': 'Extract Parts Batch 2' + " v" + VERSION,
"tri3d-position-parts-batch": "Position Parts Batch" + " v" + VERSION,
"tri3d-swap-pixels": "Swap Pixels by Mask" + " v" + VERSION,
"tri3d-skin-feathered-padded-mask":
"Skin Feathered Padded Mask" + " v" + VERSION,
"tri3d-interaction-canny":
"Garment Skin Interaction Canny" + " v" + VERSION,
"tri3d-dwpose":
"DWPose" + " v" + VERSION,
"tri3d-pose-to-image":
"Pose to Image" + " v" + VERSION,
"tri3d-pose-adaption":
"Pose Adaption" + " v" + VERSION,
"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-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-oneformer-person-mask":
"Get person mask using oneformer" + " v" + VERSION,
"tri3d-dwpose": "DWPose" + " v" + VERSION,
"tri3d-pose-to-image": "Pose to Image" + " v" + VERSION,
"tri3d-pose-adaption": "Pose Adaption" + " v" + VERSION,
"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-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-pose-proportion":"Get pose proportion" + "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]