A Python-based motion capture system that uses computer vision to detect human pose and track a ball, generating data for use in Unity animations and visualizations. The project includes both the Python detection scripts and Unity C# scripts for rendering.
This project provides tools to:
- Detect human pose keypoints using OpenCV and cvzone
- Track a ball using YOLOv8 object detection
- Export pose and ball position data to a text file for use in Unity or other 3D applications
- Visualize the detection in real-time
- Render the skeleton and ball animation in Unity
The system supports both real-time preview and offline processing of video files, with options for basic or advanced ball interpolation.
git clone https://github.com/donsolo-khalifa/FootballKeyPointsExtraction.git
cd FootballKeyPointsExtractionFootballKeyPointsExtraction/
├── python # Motion capture & tracking
│ ├── main.py # Real-time visualization
│ ├── motionCapture.py # Basic capture → `AnimationFile.txt`
│ └── ballInterpolationMC.py # Two-pass ball interpolation
├── C# # Unity integration & rendering
│ ├── AnimationCode.cs # Applies data to joints & ball
│ └── SkeletonRenderer.cs # Draws skeleton lines
├── requirements.txt
└── .gitignore
main.py- Real-time visualization of pose estimation and ball trackingmotionCapture.py- Basic motion capture that processes video and saves keypoints to a text fileballInterpolationMC.py- Advanced motion capture with ball position interpolation for smoother animations
AnimationCode.cs- Script for reading animation data and applying it to skeleton and ballSkeletonRenderer.cs- Script for rendering the skeleton by drawing lines between joints
pip install -r requirements.txtopencv-python
cvzone
ultralytics
tqdm
numpy
To preview the pose and ball detection in real-time:
python main.pyPress 'q' to exit the preview.
For simple motion capture without ball interpolation:
python motionCapture.pyThis will process the video file, detect pose keypoints and ball position, and save the data to AnimationFile.txt.
For motion capture with ball interpolation (smoother ball tracking):
python ballInterpolationMC.pyThis uses a two-pass approach:
- First pass detects all ball positions
- Second pass interpolates missing positions and processes pose keypoints
- Outputs combined data to
AnimationFile.txt
The AnimationFile.txt contains comma-separated values for each frame:
- For each keypoint (33 keypoints from PoseDetector):
x,y,z - For the ball (as 34th joint):
x,y,z
Coordinates are normalized:
- x and y values divided by 100
- z values divided by 300
- y-axis is inverted to match Unity coordinate system
The project includes ready-to-use Unity scripts for rendering the captured motion:
-
Create a new Unity project (3D)
-
Import the
AnimationFile.txtinto your Unity project's Assets folder -
Set up the hierarchy:
- Create an empty GameObject as a parent and rename it to
Manager - Create an empty GameObject as a parent and rename it to
Body - Add 33 empty 3D objects (Spheres) as children of
Body(one for each joint) - Create a 3D object (Sphere) as parent for the ball and rename it
Ball - Create an empty GameObject as a parent and rename it to
SkeletonRenderer
- Create an empty GameObject as a parent and rename it to
-
Add the scripts:
-
Add
AnimationCode.csto theManagerGameObject -
Assign all 33 joint
3D objects (Spheres)to theBodyarray in the InspectorNote: Tip for mass assigning joints:
- Lock the Inspector window (click the lock icon in the top-right of the Inspector)
- Select all 33 joints (Spheres) in the Hierarchy by clicking the first one, then Shift+click the last one
- Drag the entire selection into the Body array field in the Inspector
This will automatically populate all array elements in the correct order
-
Assign the ball Sphere to the
Ballreference
-
-
For skeleton visualization:
- Create a LineRenderer prefab with your desired material
- Add
SkeletonRenderer.csto theSkeletonRendererGameObject - Assign the parent GameObject with 33 joints to the
Bodyfield - Assign your LineRenderer prefab to the
linePrefabfield
- Reads the pose and ball data from
AnimationFile.txt - Applies positions to the 33 body joints and the ball
- Plays back the animation with a frame rate of approximately 30 FPS
- Creates line renderers between connected joints
- Defines the skeletal structure using the MediaPipe 33-landmark model
- Updates the lines each frame to follow the joint positions
- Adjust the
Thread.Sleep(30)value inAnimationCode.csto change animation speed - Modify the
_connectionsarray inSkeletonRenderer.csto change which joints are connected - Customize the line appearance by changing the LineRenderer prefab properties
- For real-time webcam use, modify scripts to use
cv2.VideoCapture(0)instead ofcv2.VideoCapture('vida.mp4') - Change
video_pathin the scripts to use a different video file - Adjust
BALL_CLASS_IDif tracking a different object (32 is sports ball in COCO dataset) - Modify scaling factors in the output section if needed for your specific Unity setup
- Set
MAX_FRAMES_TO_KEEPin real-time applications to control how long a ball position is retained when detection fails
ballInterpolationMC.pyprovides smoother ball tracking but is not suitable for real-time applications- For real-time processing (like with a webcam), use
motionCapture.pyor implement simple interpolation with a single-pass approach - YOLOv8 model size can be adjusted for performance vs. accuracy tradeoffs (n, s, m, l, x versions)