| 40 | |
| 41 | |
| 42 | class ProcessPipeline: |
| 43 | def __init__(self, det_checkpoint_path, pose2d_checkpoint_path, sam_checkpoint_path, flux_kontext_path): |
| 44 | self.pose2d = Pose2d(checkpoint=pose2d_checkpoint_path, detector_checkpoint=det_checkpoint_path) |
| 45 | |
| 46 | model_cfg = "sam2_hiera_l.yaml" |
| 47 | if sam_checkpoint_path is not None: |
| 48 | if not _SAM2_AVAILABLE: |
| 49 | raise RuntimeError("sam2 is required for preprocessing but is not installed. Please install sam2.") |
| 50 | self.predictor = build_sam2_video_predictor(model_cfg, sam_checkpoint_path) |
| 51 | if flux_kontext_path is not None: |
| 52 | from diffusers import FluxKontextPipeline |
| 53 | |
| 54 | self.flux_kontext = FluxKontextPipeline.from_pretrained(flux_kontext_path, torch_dtype=torch.bfloat16).to("cuda") |
| 55 | |
| 56 | @staticmethod |
| 57 | def _get_body_keypoints(pose_meta): |
| 58 | if pose_meta is None: |
| 59 | return None |
| 60 | body_keypoints = pose_meta.get("keypoints_body") |
| 61 | if body_keypoints is None: |
| 62 | return None |
| 63 | try: |
| 64 | body_keypoints = np.asarray(body_keypoints, dtype=np.float32) |
| 65 | except (TypeError, ValueError): |
| 66 | return None |
| 67 | if body_keypoints.ndim != 2 or body_keypoints.shape[1] < 3: |
| 68 | return None |
| 69 | return body_keypoints |
| 70 | |
| 71 | @classmethod |
| 72 | def _get_valid_body_keypoint_mask(cls, pose_meta, keypoint_indices=None): |
| 73 | body_keypoints = cls._get_body_keypoints(pose_meta) |
| 74 | if body_keypoints is None: |
| 75 | return None, None |
| 76 | if keypoint_indices is not None: |
| 77 | valid_indices = [idx for idx in keypoint_indices if idx < body_keypoints.shape[0]] |
| 78 | body_keypoints = body_keypoints[valid_indices] if valid_indices else body_keypoints[:0] |
| 79 | if body_keypoints.shape[0] == 0: |
| 80 | return body_keypoints, np.zeros((0,), dtype=bool) |
| 81 | |
| 82 | coords = body_keypoints[:, :2] |
| 83 | scores = body_keypoints[:, 2] |
| 84 | valid_mask = ( |
| 85 | np.isfinite(coords).all(axis=1) & np.isfinite(scores) & (coords[:, 0] >= 0) & (coords[:, 0] <= 1) & (coords[:, 1] >= 0) & (coords[:, 1] <= 1) & (scores >= BODY_KEYPOINT_CONF_THRESHOLD) |
| 86 | ) |
| 87 | return body_keypoints, valid_mask |
| 88 | |
| 89 | @classmethod |
| 90 | def _count_valid_body_keypoints(cls, pose_meta): |
| 91 | _, valid_mask = cls._get_valid_body_keypoint_mask(pose_meta) |
| 92 | if valid_mask is None: |
| 93 | return 0 |
| 94 | return int(np.count_nonzero(valid_mask)) |
| 95 | |
| 96 | @classmethod |
| 97 | def _is_valid_pose_meta(cls, pose_meta): |
| 98 | return cls._count_valid_body_keypoints(pose_meta) >= MIN_VALID_BODY_KEYPOINTS |
| 99 |
no outgoing calls
no test coverage detected