A Python implementation for real-time human pose detection and analysis using Google's MediaPipe framework.
- Real-time pose detection from webcam or video files
- Landmark extraction with pixel-perfect coordinates
- Angle calculation between any three pose landmarks
- FPS monitoring for performance tracking
- Professional code structure with proper error handling and logging
- Type hints for better code maintainability
- Comprehensive documentation with docstrings
- Python 3.7 or higher
- OpenCV
- MediaPipe
pip install opencv-python mediapipeOr using requirements.txt:
pip install -r requirements.txtfrom pose_detector import demo_webcam
# Start real-time pose detection
demo_webcam()from pose_detector import demo_video_analysis
# Analyze poses in a video file
demo_video_analysis("path/to/your/video.mp4")# Use webcam
python pose_detector.py webcam
# Use video file
python pose_detector.py path/to/video.mp4import cv2
from pose_detector import PoseDetector
# Initialize detector
detector = PoseDetector()
# Load image
image = cv2.imread("person.jpg")
# Detect pose
result_image = detector.detect_pose(image, draw=True)
# Get landmark coordinates
landmarks = detector.get_landmarks(image)
print(f"Detected {len(landmarks)} landmarks")import cv2
from pose_detector import PoseDetector
detector = PoseDetector()
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
# Detect pose
frame = detector.detect_pose(frame)
landmarks = detector.get_landmarks(frame)
# Calculate elbow angle (shoulder-elbow-wrist)
if len(landmarks) > 15:
elbow_angle = detector.calculate_angle(frame, 11, 13, 15)
print(f"Left elbow angle: {elbow_angle:.1f}°")
cv2.imshow("Pose Detection", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()The pose model detects 33 landmarks:
| ID | Landmark Name | ID | Landmark Name |
|---|---|---|---|
| 0 | nose | 17 | left_pinky |
| 1 | left_eye_inner | 18 | right_pinky |
| 2 | left_eye | 19 | left_index |
| 3 | left_eye_outer | 20 | right_index |
| 4 | right_eye_inner | 21 | left_thumb |
| 5 | right_eye | 22 | right_thumb |
| 6 | right_eye_outer | 23 | left_hip |
| 7 | left_ear | 24 | right_hip |
| 8 | right_ear | 25 | left_knee |
| 9 | mouth_left | 26 | right_knee |
| 10 | mouth_right | 27 | left_ankle |
| 11 | left_shoulder | 28 | right_ankle |
| 12 | right_shoulder | 29 | left_heel |
| 13 | left_elbow | 30 | right_heel |
| 14 | right_elbow | 31 | left_foot_index |
| 15 | left_wrist | 32 | right_foot_index |
| 16 | right_wrist |
PoseDetector(
static_image_mode=False,
model_complexity=1,
smooth_landmarks=True,
enable_segmentation=False,
smooth_segmentation=True,
min_detection_confidence=0.5,
min_tracking_confidence=0.5
)Parameters:
static_image_mode: Whether to treat input as static imagesmodel_complexity: Complexity of pose model (0=lite, 1=full, 2=heavy)smooth_landmarks: Whether to smooth landmarks across framesenable_segmentation: Whether to predict segmentation masksmooth_segmentation: Whether to smooth segmentation across framesmin_detection_confidence: Minimum confidence for pose detection (0.0-1.0)min_tracking_confidence: Minimum confidence for pose tracking (0.0-1.0)
Detect pose landmarks in the given image.
Parameters:
image: Input image in BGR formatdraw: Whether to draw pose landmarks on the image
Returns: Image with pose landmarks drawn (if draw=True)
Extract pose landmark positions from the image.
Parameters:
image: Input imagedraw: Whether to draw circles at landmark positions
Returns: List of landmarks in format [id, x, y]
Calculate the angle between three pose landmarks.
Parameters:
image: Input imagepoint1: First landmark IDpoint2: Vertex landmark ID (center point)point3: Third landmark IDdraw: Whether to draw the angle visualization
Returns: Angle in degrees (0-360)
- Reduce model complexity for faster processing on slower devices
- Disable landmark smoothing for static images
- Turn off drawing when only landmark coordinates are needed
- Use appropriate confidence thresholds based on your use case
- Fitness applications: Monitor exercise form and count repetitions
- Physical therapy: Track range of motion and rehabilitation progress
- Sports analysis: Analyze athlete performance and technique
- Gaming: Create motion-controlled games and interfaces
- Security: Monitor for specific poses or gestures
- Fork the repository
- Create a feature branch (
git checkout -b feature/new-feature) - Commit your changes (
git commit -am 'Add new feature') - Push to the branch (
git push origin feature/new-feature) - Create a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Google's MediaPipe team for the pose detection model
- OpenCV community for computer vision tools
- Contributors and users of this project
ImportError: No module named 'mediapipe'
pip install mediapipeCamera not working
- Check if camera is being used by another application
- Try different camera indices (0, 1, 2, etc.)
- Verify camera permissions
Low FPS performance
- Reduce model complexity
- Lower input image resolution
- Disable unnecessary drawing operations
If you encounter any problems or have questions, please:
- Check the troubleshooting section above
- Search existing GitHub issues
- Create a new issue with detailed information about your problem