Update utils/editor.py
Browse files28 unmodified lines2930313233343593 unmodified lines12913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217328 unmodified linesdef get_openpose_detector():global _OP_DETECTORif _OP_DETECTOR is None:_OP_DETECTOR = OpenposeDetector()return _OP_DETECTORdef load_pipeline():93 unmodified lineskeypoints úteis (ombros). keypoints dict = {"left_shoulder":(x,y), ...}"""detector = get_openpose_detector()# detect returns a PIL image of the pose map; but also returns 'keypoints' structure if requested# controlnet_aux OpenposeDetector has method detect which returns images; to get keypoints we call detect_and_return_info# We'll attempt to call 'detect' and fallback if not availabletry:detected = detector.detect(model_img)# detector.detect returns a pose image (PIL)pose_image = detected# try to get keypoints via internal method if present (may vary by version)try:info = detector.get_pose(model_img) # some versions provide get_pose# info parsing: try to find shoulders - adapt defensivelykeypoints = {}for person in info:# each person: list of points or dict depending implementation# attempt to parse common formatsif isinstance(person, dict):if "left_shoulder" in person and "right_shoulder" in person:keypoints["left_shoulder"] = tuple(person["left_shoulder"])keypoints["right_shoulder"] = tuple(person["right_shoulder"])breakelif isinstance(person, list) or isinstance(person, tuple):# fallback: OpenPose ordering often uses indices:# 2 = right shoulder, 5 = left shoulder OR vice-versa depending on lib.# We'll try both orders defensivelytry:p2 = person[2]p5 = person[5]# p2/p5 are (x,y,confidence) or similarkeypoints["right_shoulder"] = (int(p2[0]), int(p2[1]))keypoints["left_shoulder"] = (int(p5[0]), int(p5[1]))breakexcept Exception:continuereturn pose_image.convert("RGB"), keypointsexcept Exception:# if we can't get structured keypoints, just return pose image and empty dictreturn pose_image.convert("RGB"), {}except Exception as e:# last fallback: return blank pose (grayscale) and empty keypointsblank = Image.new("RGB", model_img.size, (255,255,255))return blank, {}28 unmodified lines2930313233343593 unmodified lines12913013113213313413513613713813914014114214314414514614714814915015115215328 unmodified linesdef get_openpose_detector():global _OP_DETECTORif _OP_DETECTOR is None:_OP_DETECTOR = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")return _OP_DETECTORdef load_pipeline():93 unmodified lineskeypoints úteis (ombros). keypoints dict = {"left_shoulder":(x,y), ...}"""detector = get_openpose_detector()try:# Gera o mapa de posepose_image = detector(model_img) # Chama diretamente como callable — retorna PIL.Imagepose_image = pose_image.convert("RGB")# Tenta extrair keypoints (depende da versão)keypoints = {}try:# Alguns detectores permitem chamar .to(...) para mover para GPU, mas aqui vamos no básico# Versões recentes do controlnet_aux não expõem facilmente os keypoints# Vamos pular por enquanto — alinhamento será por fallbackpassexcept Exception:passreturn pose_image, keypointsexcept Exception as e:# fallback: return blank pose and empty keypointsblank = Image.new("RGB", model_img.size, (255,255,255))return blank, {}
utils/editor.py+16-36