+663 -136
View File
@@ -1086,9 +1086,12 @@ 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, )
@@ -1107,16 +1110,15 @@ class TRI3DPoseAdaption:
"ref_pose_json_file": ("STRING", {
"default": "dwpose/keypoints"
}),
"image_angle": (["front", "side", "back"], {"default": "front"}),
"image_angle": (["front", "side", "back"], {
"default": "front"
}),
"rotation_threshold": ("FLOAT", {
"default": 5.0,
"min": 0.0,
"max": 15.0,
"step": 0.01
})
}
}
@@ -1124,7 +1126,8 @@ class TRI3DPoseAdaption:
FUNCTION = "main"
CATEGORY = "TRI3D"
def main(self, input_pose_json_file, ref_pose_json_file,image_angle,rotation_threshold):
def main(self, input_pose_json_file, ref_pose_json_file, image_angle,
rotation_threshold):
from .dwpose import comfy_utils
if image_angle == "front":
@@ -1133,7 +1136,8 @@ class TRI3DPoseAdaption:
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']
@@ -1150,99 +1154,158 @@ 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
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,
]
return (canvas, similar_torso)
#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
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 2, 3) # rotate left elbow
input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 1, 2, 2, 3) #scaling w.r.t to shoulder to elbow ratio of ref pose
#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
input_keypoints = comfy_utils.rotate(ref_keypoints,
input_keypoints, 2,
3) # rotate left elbow
input_keypoints = comfy_utils.scale(
ref_keypoints, input_keypoints, 1, 2, 2,
3) #scaling w.r.t to shoulder to elbow ratio of ref pose
prev_lw = input_keypoints[4]
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
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 5, 6) #rotate right elbow
input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 1, 5, 5, 6) #scaling w.r.t to shoulder to elbow ratio of ref pose
input_keypoints = comfy_utils.rotate(ref_keypoints,
input_keypoints, 5,
6) #rotate right elbow
input_keypoints = comfy_utils.scale(
ref_keypoints, input_keypoints, 1, 5, 5,
6) #scaling w.r.t to shoulder to elbow ratio of ref pose
prev_rw = input_keypoints[7]
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
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, 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, 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, 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, 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, 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, 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, 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
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])
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 0,
14) #rotate left eye
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
ref_keypoints, 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(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
ref_keypoints, 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(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
ref_keypoints, 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(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
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,
]
return (canvas, similar_torso)
if image_angle == "back":
input_pose = json.load(open(input_pose_json_file))
@@ -1251,121 +1314,209 @@ 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)
ref_pose = json.load(open(ref_pose_json_file))
ref_keypoints = ref_pose['keypoints']
#check torso similarity
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]
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
x, y = input_keypoints[i]
if input_keypoints[i] == [-1, -1]: continue
input_keypoints[i] = [input_width - x, 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
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 2, 3) # rotate left elbow
input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 1, 2, 2, 3) #scaling w.r.t to shoulder to elbow ratio of ref pose
#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
input_keypoints = comfy_utils.rotate(ref_keypoints,
input_keypoints, 2,
3) # rotate left elbow
input_keypoints = comfy_utils.scale(
ref_keypoints, input_keypoints, 1, 2, 2,
3) #scaling w.r.t to shoulder to elbow ratio of ref pose
prev_lw = input_keypoints[4]
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
input_keypoints = comfy_utils.rotate(ref_keypoints, input_keypoints, 5, 6) #rotate right elbow
input_keypoints = comfy_utils.scale(ref_keypoints, input_keypoints, 1, 5, 5, 6) #scaling w.r.t to shoulder to elbow ratio of ref pose
input_keypoints = comfy_utils.rotate(ref_keypoints,
input_keypoints, 5,
6) #rotate right elbow
input_keypoints = comfy_utils.scale(
ref_keypoints, input_keypoints, 1, 5, 5,
6) #scaling w.r.t to shoulder to elbow ratio of ref pose
prev_rw = input_keypoints[7]
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
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, 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, 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, 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, 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, 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, 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, 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
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
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,
]
return (canvas, similar_torso)
@@ -1405,9 +1556,13 @@ 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():
@@ -1456,6 +1611,376 @@ class TRI3DFaceRecognise:
return ({"overlap (float)": s}, )
class TRI3D_recolor_LAB:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image_reference": ("IMAGE", ),
"image_mask_reference": ("IMAGE", ),
"image_recolor": ("IMAGE", ),
"image_mask_recolor": ("IMAGE", ),
}
}
RETURN_TYPES = ("IMAGE", )
FUNCTION = "recolor"
CATEGORY = "TRI3D"
def recolor(self, image_reference, image_mask_reference, image_recolor,
image_mask_recolor):
def get_mu_sigma(array_input, mask_input):
import numpy as np
import math
array_input = array_input.astype(dtype=np.float32).flatten()
mask_input = mask_input.flatten()
sum = np.sum(mask_input)
mean = np.sum(array_input * mask_input) / sum
array_input -= mean
array_input *= mask_input
sigma = math.sqrt(np.sum(np.square(array_input)) / sum)
return mean, sigma
def do_recolor(image_1, mask_1, image_2, mask_2):
import cv2
import numpy as np
import math
image_2_original = image_2.copy()
mask_1 = (mask_1 > 127).astype(dtype=np.uint8)
mask_2 = (mask_2 > 127).astype(dtype=np.uint8)
sum_1 = np.sum(mask_1.flatten())
sum_2 = np.sum(mask_2.flatten())
for i in range(3):
image_1[:, :, i] *= mask_1
image_2[:, :, i] *= mask_2
image_1 = cv2.cvtColor(image_1, cv2.COLOR_BGR2LAB)
image_2 = cv2.cvtColor(image_2, cv2.COLOR_BGR2LAB)
image_1 = image_1.astype(dtype=np.float32)
image_2 = image_2.astype(dtype=np.float32)
for i in range(3):
mu_1, sigma_1 = get_mu_sigma(array_input=image_1[:, :, i],
mask_input=mask_1)
mu_2, sigma_2 = get_mu_sigma(array_input=image_2[:, :, i],
mask_input=mask_2)
image_2[:, :, i] = ((
(image_2[:, :, i] - mu_2) / sigma_2) * sigma_1) + mu_1
image_2 = np.clip(image_2, 0, 255)
image_2 = image_2.astype(dtype=np.uint8)
image_2 = cv2.cvtColor(image_2, cv2.COLOR_LAB2BGR)
for i in range(3):
image_2_original[:, :,
i] = (image_2_original[:, :, i] *
(1 - mask_2)) + (image_2[:, :, i] *
mask_2)
return image_2_original
def from_torch_image(image):
image = image.squeeze().cpu().numpy() * 255.0
image = np.clip(image, 0, 255).astype(np.uint8)
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_reference = from_torch_image(image=image_reference)
image_mask_reference = from_torch_image(
image=image_mask_reference)[:, :, 0]
image_recolor = from_torch_image(image=image_recolor)
image_mask_recolor = from_torch_image(image=image_mask_recolor)[:, :,
0]
image_output = do_recolor(image_1=image_reference,
mask_1=image_mask_reference,
image_2=image_recolor,
mask_2=image_mask_recolor)
image_output = to_torch_image(image=image_output)
return image_output
class TRI3D_recolor_RGB:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image_reference": ("IMAGE", ),
"image_mask_reference": ("IMAGE", ),
"image_recolor": ("IMAGE", ),
"image_mask_recolor": ("IMAGE", ),
}
}
RETURN_TYPES = ("IMAGE", )
FUNCTION = "recolor"
CATEGORY = "TRI3D"
def recolor(self, image_reference, image_mask_reference, image_recolor,
image_mask_recolor):
def get_mu_sigma(array_input, mask_input):
import numpy as np
import math
array_input = array_input.astype(dtype=np.float32).flatten()
mask_input = mask_input.flatten()
sum = np.sum(mask_input)
mean = np.sum(array_input * mask_input) / sum
array_input -= mean
array_input *= mask_input
sigma = math.sqrt(np.sum(np.square(array_input)) / sum)
return mean, sigma
def do_recolor(image_1, mask_1, image_2, mask_2):
import cv2
import numpy as np
import math
image_2_original = image_2.copy()
mask_1 = (mask_1 > 127).astype(dtype=np.uint8)
mask_2 = (mask_2 > 127).astype(dtype=np.uint8)
sum_1 = np.sum(mask_1.flatten())
sum_2 = np.sum(mask_2.flatten())
for i in range(3):
image_1[:, :, i] *= mask_1
image_2[:, :, i] *= mask_2
image_1 = image_1.astype(dtype=np.float32)
image_2 = image_2.astype(dtype=np.float32)
for i in range(3):
mu_1, sigma_1 = get_mu_sigma(array_input=image_1[:, :, i],
mask_input=mask_1)
mu_2, sigma_2 = get_mu_sigma(array_input=image_2[:, :, i],
mask_input=mask_2)
image_2[:, :, i] = ((
(image_2[:, :, i] - mu_2) / sigma_2) * sigma_1) + mu_1
image_2 = np.clip(image_2, 0, 255)
image_2 = image_2.astype(dtype=np.uint8)
for i in range(3):
image_2_original[:, :,
i] = (image_2_original[:, :, i] *
(1 - mask_2)) + (image_2[:, :, i] *
mask_2)
return image_2_original
def from_torch_image(image):
image = image.squeeze().cpu().numpy() * 255.0
image = np.clip(image, 0, 255).astype(np.uint8)
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_reference = from_torch_image(image=image_reference)
image_mask_reference = from_torch_image(
image=image_mask_reference)[:, :, 0]
image_recolor = from_torch_image(image=image_recolor)
image_mask_recolor = from_torch_image(image=image_mask_recolor)[:, :,
0]
image_output = do_recolor(image_1=image_reference,
mask_1=image_mask_reference,
image_2=image_recolor,
mask_2=image_mask_recolor)
image_output = to_torch_image(image=image_output)
return image_output
class TRI3D_recolor:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image_reference": ("IMAGE", ),
"image_mask_reference": ("IMAGE", ),
"image_recolor": ("IMAGE", ),
"image_mask_recolor": ("IMAGE", ),
}
}
RETURN_TYPES = ("IMAGE", )
FUNCTION = "recolor"
CATEGORY = "TRI3D"
def recolor(self, image_reference, image_mask_reference, image_recolor,
image_mask_recolor):
def get_mu_sigma(array_input, mask_input):
import numpy as np
import math
array_input = array_input.astype(dtype=np.float32).flatten()
mask_input = mask_input.flatten()
sum = np.sum(mask_input)
mean = np.sum(array_input * mask_input) / sum
array_input -= mean
array_input *= mask_input
sigma = math.sqrt(np.sum(np.square(array_input)) / sum)
return mean, sigma
def do_recolor(image_1, mask_1, image_2, mask_2):
import cv2
import numpy as np
import math
image_2_original = image_2.copy()
mask_1 = (mask_1 > 127).astype(dtype=np.uint8)
mask_2 = (mask_2 > 127).astype(dtype=np.uint8)
sum_1 = np.sum(mask_1.flatten())
sum_2 = np.sum(mask_2.flatten())
for i in range(3):
image_1[:, :, i] *= mask_1
image_2[:, :, i] *= mask_2
image_1 = cv2.cvtColor(image_1, cv2.COLOR_BGR2HSV_FULL)
image_2 = cv2.cvtColor(image_2, cv2.COLOR_BGR2HSV_FULL)
image_1 = image_1.astype(dtype=np.float32)
image_2 = image_2.astype(dtype=np.float32)
for i in range(3):
mu_1, sigma_1 = get_mu_sigma(array_input=image_1[:, :, i],
mask_input=mask_1)
mu_2, sigma_2 = get_mu_sigma(array_input=image_2[:, :, i],
mask_input=mask_2)
image_2[:, :, i] = ((
(image_2[:, :, i] - mu_2) / sigma_2) * sigma_1) + mu_1
image_2 = image_2.astype(dtype=np.int16)
image_2[:, :, 0] %= 255
image_2 = np.clip(image_2, 0, 255)
image_2 = image_2.astype(dtype=np.uint8)
image_2 = cv2.cvtColor(image_2, cv2.COLOR_HSV2BGR_FULL)
for i in range(3):
image_2_original[:, :,
i] = (image_2_original[:, :, i] *
(1 - mask_2)) + (image_2[:, :, i] *
mask_2)
return image_2_original
def from_torch_image(image):
image = image.squeeze().cpu().numpy() * 255.0
image = np.clip(image, 0, 255).astype(np.uint8)
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_reference = from_torch_image(image=image_reference)
image_mask_reference = from_torch_image(
image=image_mask_reference)[:, :, 0]
image_recolor = from_torch_image(image=image_recolor)
image_mask_recolor = from_torch_image(image=image_mask_recolor)[:, :,
0]
image_output = do_recolor(image_1=image_reference,
mask_1=image_mask_reference,
image_2=image_recolor,
mask_2=image_mask_recolor)
image_output = to_torch_image(image=image_output)
return image_output
class FloatToImage:
@classmethod
@@ -1542,12 +2067,11 @@ NODE_CLASS_MAPPINGS = {
"tri3d-load-pose-json": TRI3DLoadPoseJson,
"tri3d-face-recognise": TRI3DFaceRecognise,
"tri3d-float-to-image": FloatToImage,
"tri3d-recolor-mask": TRI3D_recolor,
"tri3d-recolor-mask-LAB_space": TRI3D_recolor_LAB,
"tri3d-recolor-mask-RGB_space": TRI3D_recolor_RGB,
}
VERSION = "1.7.0"
# A dictionary that contains the friendly/humanly readable titles for the nodes
NODE_DISPLAY_NAME_MAPPINGS = {
@@ -1566,4 +2090,7 @@ 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-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,
}