This project demonstrates real-time human pose estimation using an ONNX model (likely YOLOv8-Pose or similar) with the Microsoft.ML.OnnxRuntime in C#. It processes an image, detects human keypoints, and visualizes these keypoints and connections on the image.
- Human Pose Estimation: Detects 17 common human body keypoints.
- ONNX Model Inference: Utilizes ONNX Runtime for efficient model execution.
- Keypoint Visualization: Renders detected keypoints and skeletal connections on the input image.
- Pose Classification: Includes basic logic to classify the detected pose as "Standing," "Sitting," "Lying Down," or "Squatting/Crouching".
- Cross-Platform Potential: Built with .NET, allowing for potential use on Windows, Linux, and macOS (ensure ONNX Runtime native binaries are available).
This project is designed to work with ONNX pose estimation models that output heatmaps for keypoints. The example likely uses a model like YOLOv8n-Pose, which outputs a tensor typically of shape [batch_size, num_keypoints*3, height, width]
or a heatmap tensor from which keypoints can be derived. The PostProcessResults
method in PoseHelper.cs
is tailored to process these heatmaps.
The Sample/Media/yolov8n-pose.onnx
file (if present in your repository) would be the model used.
- .NET SDK (e.g., .NET 6, .NET 7, or .NET 8+)
- (Optional, for development) An IDE like Visual Studio or VS Code.
-
Clone the repository:
git clone [https://github.com/DeepLumiere/posedetector.git](https://github.com/DeepLumiere/posedetector.git) cd posedetector
-
Restore .NET dependencies: The project uses NuGet packages like
Microsoft.ML.OnnxRuntime
,System.Drawing.Common
(or alternatives for cross-platform image handling if you've updated it), and potentially an OpenCV wrapper likeOpenCvSharp
. These will be restored automatically when you build the project.dotnet restore
(Or restore via your IDE)
-
Ensure Model Availability: Make sure the ONNX model file (e.g.,
yolov8n-pose.onnx
) is accessible by the application. The current code might expect it in a specific path (e.g., relative to the executable or in aMedia
folder). Adjust the model path inProgram.cs
if necessary.Example
modelPath
inProgram.cs
(verify this in your actual code):// In Program.cs or relevant configuration string modelPath = "path/to/your/model.onnx";
The application is a console application that typically takes an image path as a command-line argument.
-
Build the project:
dotnet build --configuration Release
-
Run the application: Navigate to the build output directory (e.g.,
bin/Release/netX.X/
) and run the executable, providing the path to an image:# Example from within the project's root directory after building # Adjust the target framework (e.g., net7.0) and executable name as needed. cd AIDevGallery.Sample/bin/Release/net7.0/ ./AIDevGallery.Sample.exe "path/to/your/image.jpg" # or on Windows: # AIDevGallery.Sample.exe "path\to\your\image.jpg"
(Adjust
AIDevGallery.Sample
if your project/executable name is different. The path provided in the original code snippetAIDevGallery.Sample.Utils
suggests the project might be namedAIDevGallery.Sample
.)The program will:
- Load the specified image.
- Perform pose detection using the ONNX model.
- Process the results to get keypoint coordinates.
- Classify the detected pose.
- Render the keypoints, connections, and pose label onto the image.
- Save the output image (e.g., as
output.jpg
in the same directory as the input image or a predefined output path - checkProgram.cs
for the exact output behavior).
Program.cs
(or main entry point):- Handles command-line arguments (input image path).
- Loads the ONNX model using
InferenceSession
. - Preprocesses the input image for the model.
- Runs inference.
- Calls
PoseHelper
methods for post-processing and rendering. - Saves the output image.
PoseHelper.cs
:PostProcessResults()
: Converts the raw heatmap output from the ONNX model into a list of (X, Y) keypoint coordinates, scaling them to the original image dimensions.RenderPredictions()
: Draws the detected keypoints, skeletal connections, and the classified pose label onto an image.DetectPose()
: Classifies the overall pose (e.g., "Standing", "Sitting", "Lying Down") based on the relative positions of the detected keypoints. This method uses relative thresholds for better scale invariance.
- Microsoft.ML.OnnxRuntime: For loading and running ONNX models.
- System.Drawing.Common: (Potentially) For
Bitmap
,Graphics
operations. Note: This package has cross-platform limitations on non-Windows systems. Consider alternatives like ImageSharp or SkiaSharp for better cross-platform compatibility if needed. - (If used) OpenCvSharp: Or another OpenCV wrapper for image preprocessing if the model requires specific input formats not easily achieved with
System.Drawing.Common
alone.
(Check your .csproj
file for the exact list of dependencies.)
- Image Loading & Preprocessing: An input image is loaded. It might be resized and normalized to match the input requirements of the ONNX pose estimation model.
- Inference: The preprocessed image tensor is fed into the ONNX model via
OnnxRuntime
. The model outputs raw predictions, often as heatmaps indicating the likelihood of each keypoint at different locations. - Post-processing (
PostProcessResults
):- The heatmaps are parsed to find the location (x, y coordinates) of the highest confidence for each keypoint.
- These coordinates, which are relative to the model's input size (e.g., 64x48 or 640x640 heatmaps), are then scaled back to the original image's dimensions.
- Pose Classification (
DetectPose
):- Using the scaled keypoint coordinates, this function calculates various vertical and horizontal distances between key body parts (shoulders, hips, knees, ankles).
- It applies a set of rules with relative thresholds (e.g., comparing leg segment lengths to torso height) to determine if the person is "Lying Down," "Standing," "Sitting," or "Squatting/Crouching."
- Visualization (
RenderPredictions
):- The scaled keypoints are drawn as circles on a copy of the original image.
- Lines are drawn to connect related keypoints, forming a skeleton.
- The classified pose label is drawn on the image.
- Output: The annotated image is saved to disk.
- Implement batch processing for multiple images.
- Add support for video input (frame-by-frame processing).
- Improve pose classification logic for more nuanced poses or higher accuracy.
- Optimize image preprocessing and post-processing steps.
- Add more robust error handling and logging.
- Provide clear instructions for using different ONNX pose models.
- Enhance cross-platform compatibility for image manipulation (e.g., by replacing
System.Drawing.Common
if targeting non-Windows primarily).
Contributions are welcome! Please feel free to submit a pull request or open an issue for bugs, feature requests, or improvements.
- Fork the repository.
- Create your feature branch (
git checkout -b feature/AmazingFeature
). - Commit your changes (
git commit -m 'Add some AmazingFeature'
). - Push to the branch (
git push origin feature/AmazingFeature
). - Open a Pull Request.
I don't have any, if you can please help me out!