| 335 | |
| 336 | |
| 337 | class Pose2d: |
| 338 | def __init__(self, checkpoint, detector_checkpoint=None, device="cuda", **kwargs): |
| 339 | if detector_checkpoint is not None: |
| 340 | self.detector = Yolo(detector_checkpoint, device) |
| 341 | else: |
| 342 | self.detector = None |
| 343 | |
| 344 | self.model = ViTPose(checkpoint, device) |
| 345 | self.device = device |
| 346 | |
| 347 | def load_images(self, inputs): |
| 348 | """ |
| 349 | Load images from various input types. |
| 350 | |
| 351 | Args: |
| 352 | inputs (Union[str, np.ndarray, List[np.ndarray]]): Input can be file path, |
| 353 | single image array, or list of image arrays |
| 354 | |
| 355 | Returns: |
| 356 | List[np.ndarray]: List of RGB image arrays |
| 357 | |
| 358 | Raises: |
| 359 | ValueError: If file format is unsupported or image cannot be read |
| 360 | """ |
| 361 | if isinstance(inputs, str): |
| 362 | if inputs.lower().endswith((".mp4", ".avi", ".mov", ".mkv")): |
| 363 | cap = cv2.VideoCapture(inputs) |
| 364 | frames = [] |
| 365 | while True: |
| 366 | ret, frame = cap.read() |
| 367 | if not ret: |
| 368 | break |
| 369 | frames.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) |
| 370 | cap.release() |
| 371 | images = frames |
| 372 | elif inputs.lower().endswith((".jpg", ".jpeg", ".png", ".bmp")): |
| 373 | img = cv2.cvtColor(cv2.imread(inputs), cv2.COLOR_BGR2RGB) |
| 374 | if img is None: |
| 375 | raise ValueError(f"Cannot read image: {inputs}") |
| 376 | images = [img] |
| 377 | else: |
| 378 | raise ValueError(f"Unsupported file format: {inputs}") |
| 379 | |
| 380 | elif isinstance(inputs, np.ndarray): |
| 381 | images = [cv2.cvtColor(image, cv2.COLOR_BGR2RGB) for image in inputs] |
| 382 | elif isinstance(inputs, list): |
| 383 | images = [cv2.cvtColor(image, cv2.COLOR_BGR2RGB) for image in inputs] |
| 384 | return images |
| 385 | |
| 386 | def __call__(self, inputs: Union[str, np.ndarray, List[np.ndarray]], return_image: bool = False, **kwargs): |
| 387 | """ |
| 388 | Process input and estimate 2D keypoints. |
| 389 | |
| 390 | Args: |
| 391 | inputs (Union[str, np.ndarray, List[np.ndarray]]): Input can be file path, |
| 392 | single image array, or list of image arrays |
| 393 | **kwargs: Additional arguments for processing |
| 394 | |