Custom Human Pose Classification using Mediapipe
Tags: mediapipe, keras, tensorflow, humanpose
Published: 2022-09-26 06:14:14

Creating a Custom pose classification using Mediapipe with help of OpenCV
GitHub: [CustomPose-Classification-Mediapipe](https://github.com/naseemap47/CustomPose-Classification-Mediapipe.git)

## Sample OutPut
### Video

![Video Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ahjjlf48xxq1vbgrxw9q.gif)

### Image

![Image Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jibdk5vmdx7qyk3gbbry.png)

## Demo
I am going to create a Custom Human pose classification using Yoga Pose Dataset.

### 1. Clone the Repository:
```
git clone https://github.com/naseemap47/CustomPose-Classification-Mediapipe.git
cd CustomPose-Classification-Mediapipe
```
### Install Dependency
```
pip3 install -r requirements.txt
```
### Download Dataset:
```
wget -O yoga_poses.zip http://download.tensorflow.org/data/pose_classification/yoga_poses.zip
```
#### About Dataset:
- 5 Classes: Chair, Cobra, Dog, Tree and Warrior
- Contain Train and Test data
- Combine both Train and Test data

```
├── Dataset
│   ├── Chair
│   │   ├── 1.jpg
│   │   ├── 2.jpg
│   │   ├── ...
│   ├── Cobra
│   │   ├── 1.jpg
│   │   ├── 2.jpg
│   │   ├── ...
.   .
.   .
```
### 2. Create Landmark Dataset for each Classes
- `-i` , `--dataset` __ Path to Dataset
- `-o` , `--save` __ Path to save **CSV** file

```
python3 poseLandmark_csv.py -i data/ -o data.csv
```
**CSV** file will be saved in <**path_to_save_csv**>

### 3. Create DeepLearinng Model to predict Human Pose
- `-i` , `--dataset` __ Path to **CSV** Data
- `-o` , `--save` __ Path to save **model.h5** file
```
python3 poseModel.py -i data.csv -o model.h5
```
Model will saved in <**path_to_save_model**> and Model Metrics saved in **metrics.png**

### 4. Inference
Show Predicted Pose Class on Test Image or Video or Web-cam

- `-m` , `--model` __ Path to saved **model.h5**
- `-c` , `--conf` __ Min prediction **conf** to detect pose class (**0<conf<1**)
- `-i` , `--source` __ Path to Image or Video, for webcam use Zero(0)
- `--save` __ It will save Images (on **ImageOutput** Dir) or Videos (“**output.avi**”)

```
# Image
python3 inference.py --model model.h5 --conf 0.75 --source data/test/image.jpg
# Video
python3 inference.py --model model.h5 --conf 0.75 --source data/test/video.mp4
# web-cam
python3 inference.py --model model.h5 --conf 0.75 --source 0
###### to save ######
# Image
python3 inference.py --model model.h5 --conf 0.75 --source data/test/image.jpg --save
# Video
python3 inference.py --model model.h5 --conf 0.75 --source data/test/video.mp4 --save
# web-cam
python3 inference.py --model model.h5 --conf 0.75 --source 0 --save
```
**To Exit Window — Press Q-key**

## Custom Pose Classification
### Clone this Repository:
```
git clone https://github.com/naseemap47/CustomPose-Classification-Mediapipe.git
cd CustomPose-Classification-Mediapipe
git checkout custom
```
### 1. Take your Custom Pose Dataset
**Example**:
```
├── Dataset
│   ├── Pose_1
│   │   ├── 1.jpg
│   │   ├── 2.jpg
│   │   ├── ...
│   ├── Pose_2
│   │   ├── 1.jpg
│   │   ├── 2.jpg
│   │   ├── ...
.   .
.   .
```
### 2. Create Landmark Dataset for each Classes
- `-i` , `--dataset` __ Path to Dataset
- `-o` , `--save` __ Path to save **CSV** file

```
python3 poseLandmark_csv.py -i <path_to_data_dir> -o <path_to_save_csv>
```
CSV file will be saved in <**path_to_save_csv**>

### 3. Create DeepLearinng Model to predict Human Pose
- `-i` , `--dataset` __ Path to **CSV** Data
- `-o` , `--save` __ Path to save **model.h5** file
```
python3 poseModel.py -i <path_to_save_csv> -o <path_to_save_model>
```
Model will saved in <**path_to_save_model**> and Model Metrics saved in **metrics.png**

### 4. Inference
Open **inference.py**
change **Line-43**: According to your Class Names, Write Class Order

Show Predicted Pose Class on Test Image or Video or Web-cam

- `-m` , `--model` __ Path to saved **model.h5**
- `-c` , `--conf` __ Min prediction **conf** to detect pose class (**0<conf<1**)
- `-i` , `--source` __ Path to Image or Video, for webcam use Zero(0)
- `--save` __ It will save Images (on **ImageOutput** Dir) or Videos (“**output.avi**”)

```
python3 inference.py --model <path_to_model> --conf <model_prediction_confidence> --source <image or video or web-cam>
# to save
python3 inference.py --model <path_to_model> --conf <model_prediction_confidence> --source <image or video or web-cam> --save
```

Show Predicted Pose Class on Test Image or Video or Web-cam

**To Exit Window — Press Q-key**

**Thank You…**