Update utils/editor.py
Browse files- utils/editor.py +16 -36
utils/editor.py
CHANGED
|
@@ -29,7 +29,7 @@ _OP_DETECTOR = None
|
|
| 29 |
def get_openpose_detector():
|
| 30 |
global _OP_DETECTOR
|
| 31 |
if _OP_DETECTOR is None:
|
| 32 |
-
_OP_DETECTOR = OpenposeDetector()
|
| 33 |
return _OP_DETECTOR
|
| 34 |
|
| 35 |
def load_pipeline():
|
|
@@ -129,45 +129,25 @@ def extract_pose_and_keypoints(model_img: Image.Image) -> Tuple[Image.Image, Dic
|
|
| 129 |
keypoints úteis (ombros). keypoints dict = {"left_shoulder":(x,y), ...}
|
| 130 |
"""
|
| 131 |
detector = get_openpose_detector()
|
| 132 |
-
# detect returns a PIL image of the pose map; but also returns 'keypoints' structure if requested
|
| 133 |
-
# controlnet_aux OpenposeDetector has method detect which returns images; to get keypoints we call detect_and_return_info
|
| 134 |
-
# We'll attempt to call 'detect' and fallback if not available
|
| 135 |
try:
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
pose_image =
|
| 139 |
-
|
|
|
|
|
|
|
| 140 |
try:
|
| 141 |
-
|
| 142 |
-
#
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
# each person: list of points or dict depending implementation
|
| 146 |
-
# attempt to parse common formats
|
| 147 |
-
if isinstance(person, dict):
|
| 148 |
-
if "left_shoulder" in person and "right_shoulder" in person:
|
| 149 |
-
keypoints["left_shoulder"] = tuple(person["left_shoulder"])
|
| 150 |
-
keypoints["right_shoulder"] = tuple(person["right_shoulder"])
|
| 151 |
-
break
|
| 152 |
-
elif isinstance(person, list) or isinstance(person, tuple):
|
| 153 |
-
# fallback: OpenPose ordering often uses indices:
|
| 154 |
-
# 2 = right shoulder, 5 = left shoulder OR vice-versa depending on lib.
|
| 155 |
-
# We'll try both orders defensively
|
| 156 |
-
try:
|
| 157 |
-
p2 = person[2]
|
| 158 |
-
p5 = person[5]
|
| 159 |
-
# p2/p5 are (x,y,confidence) or similar
|
| 160 |
-
keypoints["right_shoulder"] = (int(p2[0]), int(p2[1]))
|
| 161 |
-
keypoints["left_shoulder"] = (int(p5[0]), int(p5[1]))
|
| 162 |
-
break
|
| 163 |
-
except Exception:
|
| 164 |
-
continue
|
| 165 |
-
return pose_image.convert("RGB"), keypoints
|
| 166 |
except Exception:
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
| 169 |
except Exception as e:
|
| 170 |
-
#
|
| 171 |
blank = Image.new("RGB", model_img.size, (255,255,255))
|
| 172 |
return blank, {}
|
| 173 |
|
|
|
|
| 29 |
def get_openpose_detector():
|
| 30 |
global _OP_DETECTOR
|
| 31 |
if _OP_DETECTOR is None:
|
| 32 |
+
_OP_DETECTOR = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
|
| 33 |
return _OP_DETECTOR
|
| 34 |
|
| 35 |
def load_pipeline():
|
|
|
|
| 129 |
keypoints úteis (ombros). keypoints dict = {"left_shoulder":(x,y), ...}
|
| 130 |
"""
|
| 131 |
detector = get_openpose_detector()
|
|
|
|
|
|
|
|
|
|
| 132 |
try:
|
| 133 |
+
# Gera o mapa de pose
|
| 134 |
+
pose_image = detector(model_img) # Chama diretamente como callable — retorna PIL.Image
|
| 135 |
+
pose_image = pose_image.convert("RGB")
|
| 136 |
+
|
| 137 |
+
# Tenta extrair keypoints (depende da versão)
|
| 138 |
+
keypoints = {}
|
| 139 |
try:
|
| 140 |
+
# Alguns detectores permitem chamar .to(...) para mover para GPU, mas aqui vamos no básico
|
| 141 |
+
# Versões recentes do controlnet_aux não expõem facilmente os keypoints
|
| 142 |
+
# Vamos pular por enquanto — alinhamento será por fallback
|
| 143 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
except Exception:
|
| 145 |
+
pass
|
| 146 |
+
|
| 147 |
+
return pose_image, keypoints
|
| 148 |
+
|
| 149 |
except Exception as e:
|
| 150 |
+
# fallback: return blank pose and empty keypoints
|
| 151 |
blank = Image.new("RGB", model_img.size, (255,255,255))
|
| 152 |
return blank, {}
|
| 153 |
|