杰瑞科技汇

Python pyopenpose如何安装使用?

Of course! Let's dive deep into using Python with OpenPose.

Python pyopenpose如何安装使用?-图1
(图片来源网络,侵删)

This guide will cover:

  1. What is OpenPose? - A quick overview.
  2. Installation - The most crucial and sometimes tricky part.
  3. A Simple "Hello World" Example - Your first script to detect keypoints.
  4. Understanding the Output - What the data looks like and how to use it.
  5. Advanced Example: Drawing Keypoints - Visualizing the results.
  6. Troubleshooting & Best Practices.

What is OpenPose?

OpenPose is a powerful, real-time, multi-person 2D/3D pose estimation library. It was developed by the Carnegie Mellon University Perceptual Computing Lab. In simple terms, you give it an image or a video, and it returns the coordinates of key body parts (like elbows, knees, wrists) for every person it finds.

It uses a Convolutional Neural Network (CNN) to achieve this. The python-pyopenpose library is a Python wrapper that allows you to easily use the core C++ OpenPose functionality in your Python scripts.


Installation

This is the most challenging part. The standard pip install pyopenpose will not work. You need to build the library from source. It's highly recommended to use a Conda environment to manage dependencies.

Here is a step-by-step guide for Windows (most common) and a brief note for Linux/macOS.

Prerequisites (for all OS)

  • NVIDIA GPU: OpenPose runs significantly faster on an NVIDIA GPU with CUDA support. A CPU-only version is possible but very slow.
  • CUDA Toolkit & cuDNN: You must install versions of CUDA and cuDNN that are compatible with the OpenPose version you are building. Check the OpenPose installation guide for the latest compatibility matrix. For example, OpenPose v1.7.0 works well with CUDA 11.2 and cuDNN 8.1.
  • CMake: A build system used to compile the C++ code.
  • Visual Studio (Windows) / Build Essentials (Linux/macOS): The C++ compiler toolchain.

Step-by-Step Installation (Windows)

  1. Create a Conda Environment: This isolates all the dependencies.

    # Create a new environment named 'openpose_env'
    conda create -n openpose_env python=3.8 -y
    # Activate the environment
    conda activate openpose_env
  2. Install System Dependencies with Conda: This includes CUDA, cuDNN, and other necessary libraries.

    # Install CUDA and cuDNN. Replace '11.2' with your CUDA version.
    conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0 -y
    # Install other required packages
    conda install -c conda-forge cmake git -y
  3. Clone the OpenPose Repository:

    git clone https://github.com/CMU-Perceptual-Computing-Lab/openpose.git
    cd openpose
  4. Create a Build Directory and Configure with CMake:

    # Create a build directory
    mkdir build
    cd build
    # Run CMake to configure the build
    # This is the most important command. It tells CMake where to find CUDA and other dependencies.
    cmake .. -G "Visual Studio 17 2025" -A x64 -DBUILD_PYTHON=ON -DUSE_CUDA=ON -DCUDA_TOOLKIT_ROOT_DIR="C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.2"
    • Note: Adjust the Visual Studio version and CUDA_TOOLKIT_ROOT_DIR to match your system.
  5. Build the Project: This will take a long time (30 minutes to several hours, depending on your CPU).

    # Build the project (using all available CPU cores)
    cmake --build . --config Release -- -j
  6. Set Environment Variables: Python needs to know where to find the compiled OpenPose libraries. You can do this permanently in your system's environment variables or temporarily for your current session.

    For the current session (easiest):

    # In your 'build' directory
    set PYTHONPATH=%PYTHONPATH%;%CD%;%CD%/x64/Release;%CD%/x64/Release/pyopenpose

    Permanently (recommended):

    • Go to "Edit the system environment variables" in Windows.
    • Click "Environment Variables...".
    • Under "System variables", edit the Path variable and add the following paths (adjust openpose path as needed):
      • C:\path\to\your\openpose\build
      • C:\path\to\your\openpose\build\x64\Release
      • C:\path\to\your\openpose\build\x64\Release\pyopenpose
    • Also, create a new system variable named OPENPOSE_DIR with the value C:\path\to\your\openpose\build.
  7. Install Python Dependencies:

    # Back to the root openpose directory
    cd ..
    # Install the Python wrapper
    pip install -e .

    The -e flag installs it in "editable" mode, which is useful for development.

You're now ready to run Python scripts!


A Simple "Hello World" Example

Let's create a Python script to detect keypoints in an image.

Get a Sample Image: Save an image with a clear view of people (e.g., image.jpg). You can find one on the OpenPose GitHub.

Create the Python Script (pose_detection.py):

import cv2
import pyopenpose as op
# --- 1. Initialize OpenPose ---
# This is the most important part. You must tell OpenPose where to find its models.
params = {
    "model_folder": "C:/path/to/your/openpose/models/", # IMPORTANT: Update this path!
    "number_people_max": 1,
    "display": 0, # 0 to disable display, 1 to enable
}
# Starting OpenPose
try:
    opWrapper = op.WrapperPython()
    opWrapper.configure(params)
    opWrapper.start()
except Exception as e:
    print("An error occurred during OpenPose initialization:")
    print(e)
    sys.exit(-1)
# --- 2. Load and Process an Image ---
image_path = "image.jpg" # Path to your image
imageToProcess = cv2.imread(image_path)
if imageToProcess is None:
    print(f"Error: Could not load image at {image_path}")
    sys.exit(-1)
# --- 3. Run Pose Detection ---
datum = op.Datum()
datum.cvInputData = imageToProcess
opWrapper.emplaceAndPop([datum])
# --- 4. Check and Print the Output ---
print("Pose detection complete.")
print(f"Number of people detected: {len(datum.poseKeypoints)}")
if datum.poseKeypoints is not None:
    print("\nKeypoints for the first person:")
    keypoints = datum.poseKeypoints[0] # Get keypoints for the first person
    print(keypoints)
    # Keypoints shape: (25, 3)
    # 25 = number of body parts (COCO format)
    # 3 = (x, y, confidence)
    print(f"\nShape of keypoints array: {keypoints.shape}")
    print(f"Coordinates of the nose (keypoint 0): x={keypoints[0][0]:.2f}, y={keypoints[0][1]:.2f}, conf={keypoints[0][2]:.2f}")
# --- 5. Display the Result ---
# Datum also contains the image with drawn keypoints
if datum.cvOutputData is not None:
    cv2.imshow("OpenPose Result", datum.cvOutputData)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

Before running:

  • CRITICAL: Update "model_folder" in the params dictionary to the absolute path of your OpenPose models directory. This folder should contain pose/coco/ and other model subfolders.
  • Make sure your image.jpg is in the same directory or provide the full path.

Understanding the Output

The most important part of the output is datum.poseKeypoints.

  • datum.poseKeypoints: This is a NumPy array. Its shape depends on the number of people and the model used (COCO is standard).
    • Shape: (Number of People, Number of Body Parts, 3)
    • Number of People: How many individuals OpenPose found.
    • Number of Body Parts: 25 for the COCO model (includes face and hands, but by default, only body is enabled).
    • 3: Represents (x, y, confidence).
      • x, y: The pixel coordinates of the body part.
      • confidence: A float from 0.0 to 1.0 indicating how confident the model is in the detection. You can filter out low-confidence points (e.g., < 0.5).

COCO Body Part Mapping (25 parts): The order of the 25 body parts is fixed. Here are the first 17 (body parts): 0: "Nose", 1: "Neck", 2: "RShoulder", 3: "RElbow", 4: "RWrist", 5: "LShoulder", 6: "LElbow", 7: "LWrist", 8: "MidHip", 9: "RHip", 10: "RKnee", 11: "RAnkle", 12: "LHip", 13: "LKnee", 14: "LAnkle", 15: "REye", 16: "LEye", ... (and more for ears, ears, etc.)


Advanced Example: Drawing Keypoints

Sometimes, you might want to process the keypoints yourself and draw them on a new image instead of using OpenPose's built-in renderer.

import cv2
import pyopenpose as op
import numpy as np
# Initialize OpenPose (same as before)
params = {
    "model_folder": "C:/path/to/your/openpose/models/",
    "number_people_max": 5,
    "display": 0,
}
opWrapper = op.WrapperPython()
opWrapper.configure(params)
opWrapper.start()
# Load image
image_path = "image.jpg"
image = cv2.imread(image_path)
# Run pose detection
datum = op.Datum()
datum.cvInputData = image
opWrapper.emplaceAndPop([datum])
# --- Custom Drawing ---
output_image = image.copy()
# Define connections between body parts (skeleton)
pose_pairs = [
    (0, 1), (1, 2), (2, 3), (3, 4),    # Right arm
    (1, 5), (5, 6), (6, 7),            # Left arm
    (1, 8), (8, 9), (9, 10), (10, 11), # Right leg
    (8, 12), (12, 13), (13, 14),       # Left leg
    (1, 15), (15, 16),                 # Head
]
if datum.poseKeypoints is not None:
    for person in datum.poseKeypoints:
        # Draw keypoints
        for i, (x, y, conf) in enumerate(person):
            if conf > 0.2: # Only draw if confidence is high enough
                cv2.circle(output_image, (int(x), int(y)), 5, (0, 255, 0), -1)
                cv2.putText(output_image, str(i), (int(x) + 10, int(y)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
        # Draw skeleton
        for pair in pose_pairs:
            part_a = pair[0]
            part_b = pair[1]
            # Check if both keypoints are detected
            if person[part_a][2] > 0.2 and person[part_b][2] > 0.2:
                cv2.line(output_image,
                         (int(person[part_a][0]), int(person[part_a][1])),
                         (int(person[part_b][0]), int(person[part_b][1])),
                         (255, 255, 0), 3)
# Display the result
cv2.imshow("Custom OpenPose Result", output_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Troubleshooting & Best Practices

  • ModuleNotFoundError: No module named 'pyopenpose':
    • You are not in the correct Conda environment.
    • You forgot to run pip install -e . in the OpenPose root directory.
    • The PYTHONPATH environment variable is not set correctly.
  • ImportError: DLL load failed:
    • This is a classic C++ library loading error. Almost always caused by a mismatch between the bitness of your Python interpreter (32-bit vs 64-bit) and the OpenPose build. You must use a 64-bit Python interpreter with a 64-bit OpenPose build.
  • Performance is very slow:
    • You are likely running on the CPU. Ensure you have a CUDA-enabled GPU and that OpenPose was compiled with -DUSE_CUDA=ON.
    • Check your GPU's memory. If it's insufficient, OpenPose will fall back to the CPU.
  • No keypoints are detected:
    • Double-check the model_folder path in your params dictionary. It's the most common mistake.
    • The image might be too dark, blurry, or the people are in unusual poses.
  • Best Practice: Always wrap your OpenPose logic in try...except blocks to handle potential errors gracefully, especially during initialization.
分享:
扫描分享到社交APP
上一篇
下一篇