rtmlib

repository·main·Indexed 20 days ago

https://github.com/tau-j/rtmlib

A lightweight library for human and animal pose estimation based on RTMPose and ViTPose models. It provides a dependency-free implementation requiring only numpy, opencv, and onnxruntime, avoiding heavy frameworks like MMCV or MMPose. The library supports various models including RTMPose, DWPose, RTMO, RTMW, and VitPose, with high-level APIs for Wholebody, Body, and Custom configurations, as well as a PoseTracker for video streams.

Tokens
3.2K
Snippets
9
Records
12
Agent score
21%

What's inside rtmlib

  1. Automatic Model Downloading and Mirroring

    main

    By default, rtmlib automatically downloads and applies models with the best performance.

    If the primary OpenMMLab download server (download.openmmlab.com) is unreachable, rtmlib is configured to automatically retry the download from a mirror hosted at huggingface.co/Tau-J/RTMPose. No additional user configuration is required for this failover mechanism.

  2. Install rtmlib via pip or source

    main

    You can install rtmlib directly from PyPI or by cloning the source code.

    PyPI Installation:

    pip install rtmlib -i https://pypi.org/simple

    Source Installation:

    git clone https://github.com/Tau-J/rtmlib.git
    cd rtmlib
    pip install -r requirements.txt
    pip install -e .

    Optional Accelerators: To use GPU or OpenVINO acceleration, you can optionally install:

    • pip install onnxruntime-gpu
    • pip install openvino

    Note for OpenVINO users: You must add the path <your python path>/envs/<your env name>/Lib/site-packages/openvino/libs to your environment path.*

    pip install rtmlib -i https://pypi.org/simple
  3. Track poses in a video stream or webcam

    main

    Use the PoseTracker class to perform pose estimation on a continuous video stream or file. PoseTracker manages detection frequency via det_frequency (detecting every N frames) to optimize performance.

    from rtmlib import Body, Custom, PoseTracker, draw_skeleton
    import cv2
    
    cap = cv2.VideoCapture(0)  # Use cap = cv2.VideoCapture('./demo.mp4') for video files
    
    device = 'cpu'
    backend = 'onnxruntime'
    openpose_skeleton = False
    
    pose_tracker = PoseTracker(Body,
                            mode='balanced',
                            det_frequency=10,  # detect every 10 frames
                            backend=backend, device=device,
                            to_openpose=False)
    
    frame_idx = 0
    while cap.isOpened():
        success, frame = cap.read()
        frame_idx += 1
        if not success:
            break
    
        keypoints, scores = pose_tracker(frame)
    
        img_show = frame.copy()
        img_show = draw_skeleton(img_show,
                                 keypoints,
                                 scores,
                                 openpose_skeleton=openpose_skeleton,
                                 kpt_thr=0.43)
        cv2.imshow('img', img_show)
        cv2.waitKey(10)
  4. Perform pose estimation on a single image

    main

    Use the Wholebody class for high-level pose estimation on a single image. You can specify the device (cpu, cuda, or mps), the backend (opencv, onnxruntime, or openvino), and the mode (performance, lightweight, or balanced). Use draw_skeleton to visualize the results. For animal pose estimation, set to_openpose=True.

    import cv2
    from rtmlib import Wholebody, draw_skeleton
    
    img = cv2.imread('./demo.jpg')
    
    device = 'cpu'  # cpu, cuda, mps
    backend = 'onnxruntime'  # opencv, onnxruntime, openvino
    openpose_skeleton = False  # True for openpose-style (required for animals), False for mmpose-style
    
    wholebody = Wholebody(to_openpose=openpose_skeleton,
                          mode='balanced',  # 'performance', 'lightweight', 'balanced'. Default: 'balanced'
                          backend=backend, device=device)
    keypoints, scores = wholebody(img)
    
    # visualize
    img = draw_skeleton(img, keypoints, scores, kpt_thr=0.5, to_openpose=openpose_skeleton)
    cv2.imshow('img', img)
    cv2.waitKey(0)
  5. Configure Low-level Models (Detectors and Pose Estimators)

    main

    Low-level APIs allow you to instantiate specific model classes by passing an onnx_model argument (a local path, a URL to a .onnx file, or a .zip file).

    # YOLOX human detector
    det_model = YOLOX(onnx_model='https://.../yolox_s_...zip', backend=backend, device=device)
    
    # YOLOX multiclass detector
    det_model = YOLOX('https://.../yolox_s.onnx', det_mode='multiclass', backend=backend, device=device)
    
    # RTMPose pose estimator
    pose_model = RTMPose(onnx_model='https://.../rtmpose-m_...zip', backend=backend, device=device)
    
    # ViTPose pose estimator
    pose_model = ViTPose(onnx_model='https://.../vitpose-b-apt36k.onnx', backend=backend, device=device)
  6. Configure High-level Solutions (Wholebody, Body, Custom)

    main

    High-level APIs (Solutions) allow you to specify models using either a mode string or by providing specific det (detector) and pose (estimator) paths/URLs.

    Using mode:

    from rtmlib import Wholebody
    wholebody = Wholebody(mode='performance', backend=backend, device=device)

    Using specific det and pose paths:

    from rtmlib import Body
    body = Body(det='path/to/detector.zip', 
                det_input_size=(640, 640), 
                pose='path/to/pose.zip', 
                pose_input_size=(288, 384), 
                backend=backend, 
                device=device)

    Using Custom for multiclass or specific configurations:

    from rtmlib import Custom
    custom = Custom(det_class='YOLOX', 
                   det_mode='multiclass', 
                   det='path/to/detector.onnx', 
                   det_input_size=(640, 640), 
                   pose_class='ViTPose', 
                   pose='path/to/pose.onnx', 
                   pose_input_size=(192, 256), 
                   backend=backend, 
                   device=device)
  7. Animal Detection Categories

    main

    When using animal-specific pose estimation models, the supported categories include: gorilla, spider-monkey, howling-monkey, zebra, elephant, hippo, raccon, rhino, giraffe, tiger, deer, lion, panda, cheetah, black-bear, polar-bear, antelope, fox, buffalo, cow, wolf, dog, sheep, cat, horse, rabbit, pig, chimpanzee, monkey, and orangutan.

    CATEGORIES = ['gorilla', 'spider-monkey', 'howling-monkey', 'zebra', 'elephant', 'hippo', 'raccon', 'rhino', 'giraffe', 'tiger', 'deer', 'lion', 'panda', 'cheetah', 'black-bear', 'polar-bear', 'antelope', 'fox', 'buffalo', 'cow', 'wolf', 'dog', 'sheep', 'cat', 'horse', 'rabbit', 'pig', 'chimpanzee', 'monkey', 'orangutan']
  8. Available Detectors in the rtmlib Model Zoo

    main

    The rtmlib Model Zoo provides pre-trained ONNX models for various detection tasks. Models are categorized by the type of object they detect:

    • Person: Includes models trained on COCO (real humans only) and HumanArt (real humans and cartoon characters). Examples include YOLOX-nano, YOLOX-tiny, YOLOX-s, YOLOX-m, YOLOX-l, and YOLOX-x.
    • Hand: Includes RTMDet-nano trained on 5 datasets.
    • Multi-class: Uses YOLOX models (nano, tiny, s, m, l, Darknet53, X) trained on COCO classes, covering 80 categories such as person, bicycle, car, dog, etc.
    COCO_CLASSES = [
        'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
        'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
        'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
        'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard',
        'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
        'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
        'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard',
        'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase',
        'scissors', 'teddy bear', 'hair drier', 'toothbrush']
  9. Available Pose Estimators in the rtmlib Model Zoo

    main

    The library supports several pose estimation models with varying keypoint configurations:

    • Body 17 Keypoints: Includes RTMPose (t, s, m, l, x), RTMO (s, m, l), and ViTPose++ (s, b, l).
    • Body 25 Keypoints: ViTPose (s, b, l) models fine-tuned on COCO+feet.
    • Body 26 Keypoints: RTMPose (t, s, m, l, x) models for Halpe26 format.
    • WholeBody 133 Keypoints: High-fidelity models including ViTPose++ (s, b, l), DWPose (t, s, m, l), RTMW (m, l, x), and RTMW3D-x.
    • Hand: RTMPose-m* for Hand56 keypoints.
    • Face: RTMPose (t, s, m) for Face6 keypoints.
    • Animal: RTMPose-m and ViTPose++ (s, b, l) models trained on AP-10K for various animal categories.
  10. Visualize results with draw_bbox and draw_skeleton

    main

    The rtmlib package exports utility functions for visualizing detection and pose estimation results directly on images.

    • draw_bbox: Draws bounding boxes on the image.
    • draw_skeleton: Draws the skeletal structure (keypoints and connections) on the image.
    from rtmlib import draw_bbox, draw_skeleton
    
    # Assuming 'img' is a numpy array and 'results' contains detection/pose data
    draw_bbox(img, results)
    draw_skeleton(img, results)
  11. Use rtmlib pose estimation models

    main

    The rtmlib package provides high-level interfaces for various pose estimation and object detection models. You can import these models directly from the root package.

    Supported models include:

    • Detection/Pose Models: YOLOX, RTMDet, RFDETR, RTMO, RTMPose, RTMPose3d, ViTPose.
    • Tracking: PoseTracker.
    • Predefined Body/Part Types: Body, Hand, BodyWithFeet, Animal, Wholebody, Wholebody3d, Custom.
    from rtmlib import RTMPose, Body
    
    # Example usage pattern (conceptual)
    model = RTMPose(body_type=Body)
    results = model(image)