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
}
|
| 15 |
+
},
|
| 16 |
+
"cells": [
|
| 17 |
+
{
|
| 18 |
+
"cell_type": "code",
|
| 19 |
+
"execution_count": null,
|
| 20 |
+
"metadata": {
|
| 21 |
+
"colab": {
|
| 22 |
+
"base_uri": "https://localhost:8080/"
|
| 23 |
+
},
|
| 24 |
+
"id": "YIAgpK3L7Ztm",
|
| 25 |
+
"outputId": "56f931a2-7e43-4cb1-c755-874bb28c8573"
|
| 26 |
+
},
|
| 27 |
+
"outputs": [
|
| 28 |
+
{
|
| 29 |
+
"output_type": "stream",
|
| 30 |
+
"name": "stdout",
|
| 31 |
+
"text": [
|
| 32 |
+
"Mounted at /content/drive\n"
|
| 33 |
+
]
|
| 34 |
+
}
|
| 35 |
+
],
|
| 36 |
+
"source": [
|
| 37 |
+
"from google.colab import drive\n",
|
| 38 |
+
"drive.mount('/content/drive')\n"
|
| 39 |
+
]
|
| 40 |
+
},
|
| 41 |
+
{
|
| 42 |
+
"cell_type": "code",
|
| 43 |
+
"source": [
|
| 44 |
+
"# Install necessary packages\n",
|
| 45 |
+
"!pip install mediapipe tensorflow scikit-learn opencv-python-headless\n",
|
| 46 |
+
"\n",
|
| 47 |
+
"# Import libraries\n",
|
| 48 |
+
"import os\n",
|
| 49 |
+
"import cv2\n",
|
| 50 |
+
"import numpy as np\n",
|
| 51 |
+
"import mediapipe as mp\n",
|
| 52 |
+
"import tensorflow as tf\n",
|
| 53 |
+
"from sklearn.model_selection import train_test_split\n",
|
| 54 |
+
"from tensorflow.keras import Sequential\n",
|
| 55 |
+
"from tensorflow.keras.layers import LSTM, Dense\n"
|
| 56 |
+
],
|
| 57 |
+
"metadata": {
|
| 58 |
+
"colab": {
|
| 59 |
+
"base_uri": "https://localhost:8080/"
|
| 60 |
+
},
|
| 61 |
+
"id": "jInzzU1b-ZIC",
|
| 62 |
+
"outputId": "163cec47-3d6d-435a-9819-fc7755e3ecc0"
|
| 63 |
+
},
|
| 64 |
+
"execution_count": null,
|
| 65 |
+
"outputs": [
|
| 66 |
+
{
|
| 67 |
+
"output_type": "stream",
|
| 68 |
+
"name": "stdout",
|
| 69 |
+
"text": [
|
| 70 |
+
"Requirement already satisfied: mediapipe in /usr/local/lib/python3.10/dist-packages (0.10.15)\n",
|
| 71 |
+
"Requirement already satisfied: tensorflow in /usr/local/lib/python3.10/dist-packages (2.17.0)\n",
|
| 72 |
+
"Requirement already satisfied: scikit-learn in /usr/local/lib/python3.10/dist-packages (1.3.2)\n",
|
| 73 |
+
"Requirement already satisfied: opencv-python-headless in /usr/local/lib/python3.10/dist-packages (4.10.0.84)\n",
|
| 74 |
+
"Requirement already satisfied: absl-py in /usr/local/lib/python3.10/dist-packages (from mediapipe) (1.4.0)\n",
|
| 75 |
+
"Requirement already satisfied: attrs>=19.1.0 in /usr/local/lib/python3.10/dist-packages (from mediapipe) (24.2.0)\n",
|
| 76 |
+
"Requirement already satisfied: flatbuffers>=2.0 in /usr/local/lib/python3.10/dist-packages (from mediapipe) (24.3.25)\n",
|
| 77 |
+
"Requirement already satisfied: jax in /usr/local/lib/python3.10/dist-packages (from mediapipe) (0.4.26)\n",
|
| 78 |
+
"Requirement already satisfied: jaxlib in /usr/local/lib/python3.10/dist-packages (from mediapipe) (0.4.26+cuda12.cudnn89)\n",
|
| 79 |
+
"Requirement already satisfied: matplotlib in /usr/local/lib/python3.10/dist-packages (from mediapipe) (3.7.1)\n",
|
| 80 |
+
"Requirement already satisfied: numpy<2 in /usr/local/lib/python3.10/dist-packages (from mediapipe) (1.26.4)\n",
|
| 81 |
+
"Requirement already satisfied: opencv-contrib-python in /usr/local/lib/python3.10/dist-packages (from mediapipe) (4.10.0.84)\n",
|
| 82 |
+
"Requirement already satisfied: protobuf<5,>=4.25.3 in /usr/local/lib/python3.10/dist-packages (from mediapipe) (4.25.4)\n",
|
| 83 |
+
"Requirement already satisfied: sounddevice>=0.4.4 in /usr/local/lib/python3.10/dist-packages (from mediapipe) (0.5.0)\n",
|
| 84 |
+
"Requirement already satisfied: astunparse>=1.6.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.6.3)\n",
|
| 85 |
+
"Requirement already satisfied: gast!=0.5.0,!=0.5.1,!=0.5.2,>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (0.6.0)\n",
|
| 86 |
+
"Requirement already satisfied: google-pasta>=0.1.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (0.2.0)\n",
|
| 87 |
+
"Requirement already satisfied: h5py>=3.10.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (3.11.0)\n",
|
| 88 |
+
"Requirement already satisfied: libclang>=13.0.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (18.1.1)\n",
|
| 89 |
+
"Requirement already satisfied: ml-dtypes<0.5.0,>=0.3.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (0.4.0)\n",
|
| 90 |
+
"Requirement already satisfied: opt-einsum>=2.3.2 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (3.3.0)\n",
|
| 91 |
+
"Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from tensorflow) (24.1)\n",
|
| 92 |
+
"Requirement already satisfied: requests<3,>=2.21.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (2.32.3)\n",
|
| 93 |
+
"Requirement already satisfied: setuptools in /usr/local/lib/python3.10/dist-packages (from tensorflow) (71.0.4)\n",
|
| 94 |
+
"Requirement already satisfied: six>=1.12.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.16.0)\n",
|
| 95 |
+
"Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (2.4.0)\n",
|
| 96 |
+
"Requirement already satisfied: typing-extensions>=3.6.6 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (4.12.2)\n",
|
| 97 |
+
"Requirement already satisfied: wrapt>=1.11.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.16.0)\n",
|
| 98 |
+
"Requirement already satisfied: grpcio<2.0,>=1.24.3 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.64.1)\n",
|
| 99 |
+
"Requirement already satisfied: tensorboard<2.18,>=2.17 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (2.17.0)\n",
|
| 100 |
+
"Requirement already satisfied: keras>=3.2.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (3.4.1)\n",
|
| 101 |
+
"Requirement already satisfied: tensorflow-io-gcs-filesystem>=0.23.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (0.37.1)\n",
|
| 102 |
+
"Requirement already satisfied: scipy>=1.5.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn) (1.13.1)\n",
|
| 103 |
+
"Requirement already satisfied: joblib>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from scikit-learn) (1.4.2)\n",
|
| 104 |
+
"Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn) (3.5.0)\n",
|
| 105 |
+
"Requirement already satisfied: wheel<1.0,>=0.23.0 in /usr/local/lib/python3.10/dist-packages (from astunparse>=1.6.0->tensorflow) (0.44.0)\n",
|
| 106 |
+
"Requirement already satisfied: rich in /usr/local/lib/python3.10/dist-packages (from keras>=3.2.0->tensorflow) (13.8.0)\n",
|
| 107 |
+
"Requirement already satisfied: namex in /usr/local/lib/python3.10/dist-packages (from keras>=3.2.0->tensorflow) (0.0.8)\n",
|
| 108 |
+
"Requirement already satisfied: optree in /usr/local/lib/python3.10/dist-packages (from keras>=3.2.0->tensorflow) (0.12.1)\n",
|
| 109 |
+
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorflow) (3.3.2)\n",
|
| 110 |
+
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorflow) (3.8)\n",
|
| 111 |
+
"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorflow) (2.0.7)\n",
|
| 112 |
+
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorflow) (2024.7.4)\n",
|
| 113 |
+
"Requirement already satisfied: CFFI>=1.0 in /usr/local/lib/python3.10/dist-packages (from sounddevice>=0.4.4->mediapipe) (1.17.0)\n",
|
| 114 |
+
"Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.18,>=2.17->tensorflow) (3.7)\n",
|
| 115 |
+
"Requirement already satisfied: tensorboard-data-server<0.8.0,>=0.7.0 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.18,>=2.17->tensorflow) (0.7.2)\n",
|
| 116 |
+
"Requirement already satisfied: werkzeug>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.18,>=2.17->tensorflow) (3.0.4)\n",
|
| 117 |
+
"Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->mediapipe) (1.3.0)\n",
|
| 118 |
+
"Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib->mediapipe) (0.12.1)\n",
|
| 119 |
+
"Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib->mediapipe) (4.53.1)\n",
|
| 120 |
+
"Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->mediapipe) (1.4.5)\n",
|
| 121 |
+
"Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib->mediapipe) (9.4.0)\n",
|
| 122 |
+
"Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->mediapipe) (3.1.4)\n",
|
| 123 |
+
"Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib->mediapipe) (2.8.2)\n",
|
| 124 |
+
"Requirement already satisfied: pycparser in /usr/local/lib/python3.10/dist-packages (from CFFI>=1.0->sounddevice>=0.4.4->mediapipe) (2.22)\n",
|
| 125 |
+
"Requirement already satisfied: MarkupSafe>=2.1.1 in /usr/local/lib/python3.10/dist-packages (from werkzeug>=1.0.1->tensorboard<2.18,>=2.17->tensorflow) (2.1.5)\n",
|
| 126 |
+
"Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.10/dist-packages (from rich->keras>=3.2.0->tensorflow) (3.0.0)\n",
|
| 127 |
+
"Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.10/dist-packages (from rich->keras>=3.2.0->tensorflow) (2.16.1)\n",
|
| 128 |
+
"Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.10/dist-packages (from markdown-it-py>=2.2.0->rich->keras>=3.2.0->tensorflow) (0.1.2)\n"
|
| 129 |
+
]
|
| 130 |
+
}
|
| 131 |
+
]
|
| 132 |
+
},
|
| 133 |
+
{
|
| 134 |
+
"cell_type": "code",
|
| 135 |
+
"source": [
|
| 136 |
+
"# Import libraries\n",
|
| 137 |
+
"import os\n",
|
| 138 |
+
"import cv2\n",
|
| 139 |
+
"import numpy as np\n",
|
| 140 |
+
"import mediapipe as mp\n",
|
| 141 |
+
"import tensorflow as tf\n",
|
| 142 |
+
"from sklearn.model_selection import train_test_split\n",
|
| 143 |
+
"from tensorflow.keras import Sequential\n",
|
| 144 |
+
"from tensorflow.keras.layers import LSTM, Dense\n",
|
| 145 |
+
"\n",
|
| 146 |
+
"# Initialize MediaPipe\n",
|
| 147 |
+
"mp_pose = mp.solutions.pose\n",
|
| 148 |
+
"mp_drawing = mp.solutions.drawing_utils\n",
|
| 149 |
+
"\n",
|
| 150 |
+
"def extract_keypoints(video_path):\n",
|
| 151 |
+
" cap = cv2.VideoCapture(video_path)\n",
|
| 152 |
+
" pose = mp_pose.Pose()\n",
|
| 153 |
+
" keypoints = []\n",
|
| 154 |
+
"\n",
|
| 155 |
+
" while cap.isOpened():\n",
|
| 156 |
+
" ret, frame = cap.read()\n",
|
| 157 |
+
" if not ret:\n",
|
| 158 |
+
" break\n",
|
| 159 |
+
"\n",
|
| 160 |
+
" frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n",
|
| 161 |
+
" results = pose.process(frame_rgb)\n",
|
| 162 |
+
"\n",
|
| 163 |
+
" if results.pose_landmarks:\n",
|
| 164 |
+
" landmarks = []\n",
|
| 165 |
+
" for lm in results.pose_landmarks.landmark:\n",
|
| 166 |
+
" landmarks.extend([lm.x, lm.y, lm.z, lm.visibility])\n",
|
| 167 |
+
" keypoints.append(landmarks)\n",
|
| 168 |
+
" else:\n",
|
| 169 |
+
" keypoints.append([0] * 132) # 33 landmarks * 4 values (x, y, z, visibility)\n",
|
| 170 |
+
"\n",
|
| 171 |
+
" cap.release()\n",
|
| 172 |
+
" return np.array(keypoints)\n"
|
| 173 |
+
],
|
| 174 |
+
"metadata": {
|
| 175 |
+
"id": "3eMlBHrR-jEC"
|
| 176 |
+
},
|
| 177 |
+
"execution_count": null,
|
| 178 |
+
"outputs": []
|
| 179 |
+
},
|
| 180 |
+
{
|
| 181 |
+
"cell_type": "code",
|
| 182 |
+
"source": [
|
| 183 |
+
"def process_videos_in_batches(parent_dir, batch_size=10):\n",
|
| 184 |
+
" X = []\n",
|
| 185 |
+
" y = []\n",
|
| 186 |
+
"\n",
|
| 187 |
+
" exercise_folders = [f for f in os.listdir(parent_dir) if os.path.isdir(os.path.join(parent_dir, f))]\n",
|
| 188 |
+
"\n",
|
| 189 |
+
" for exercise_folder in exercise_folders:\n",
|
| 190 |
+
" exercise_path = os.path.join(parent_dir, exercise_folder)\n",
|
| 191 |
+
" video_files = [f for f in os.listdir(exercise_path) if f.endswith('.mp4')]\n",
|
| 192 |
+
"\n",
|
| 193 |
+
" for i, video_file in enumerate(video_files):\n",
|
| 194 |
+
" video_path = os.path.join(exercise_path, video_file)\n",
|
| 195 |
+
" keypoints = extract_keypoints(video_path)\n",
|
| 196 |
+
" # Pad keypoints to ensure consistent length across videos\n",
|
| 197 |
+
" max_length = 100 # Replace with the expected maximum number of frames\n",
|
| 198 |
+
" if keypoints.shape[0] < max_length:\n",
|
| 199 |
+
" padding = np.zeros((max_length - keypoints.shape[0], keypoints.shape[1]))\n",
|
| 200 |
+
" keypoints = np.concatenate((keypoints, padding), axis=0)\n",
|
| 201 |
+
" elif keypoints.shape[0] > max_length: # Trim video if it is longer than max length\n",
|
| 202 |
+
" keypoints = keypoints[:max_length, :]\n",
|
| 203 |
+
" X.append(keypoints)\n",
|
| 204 |
+
" y.append(1) # Assuming all videos are labeled as correct exercise\n",
|
| 205 |
+
"\n",
|
| 206 |
+
" # If batch size is reached or last video in folder, save batch\n",
|
| 207 |
+
" if (i + 1) % batch_size == 0 or (i + 1) == len(video_files):\n",
|
| 208 |
+
" batch_index = i // batch_size\n",
|
| 209 |
+
" np.save(f'/content/drive/MyDrive/keypoints_batch_{exercise_folder}_{batch_index}.npy', np.array(X))\n",
|
| 210 |
+
" np.save(f'/content/drive/MyDrive/labels_batch_{exercise_folder}_{batch_index}.npy', np.array(y))\n",
|
| 211 |
+
" X = [] # Reset lists\n",
|
| 212 |
+
" y = []\n",
|
| 213 |
+
"\n",
|
| 214 |
+
" return True\n",
|
| 215 |
+
"\n",
|
| 216 |
+
"# Define the parent directory containing all exercise folders\n",
|
| 217 |
+
"parent_dir = '/content/drive/MyDrive/correct/correct'\n",
|
| 218 |
+
"process_videos_in_batches(parent_dir, batch_size=10)"
|
| 219 |
+
],
|
| 220 |
+
"metadata": {
|
| 221 |
+
"colab": {
|
| 222 |
+
"base_uri": "https://localhost:8080/"
|
| 223 |
+
},
|
| 224 |
+
"id": "4MUyUyh2-ruX",
|
| 225 |
+
"outputId": "9a1bad49-d50d-44b3-e9e6-d1e311cee30e"
|
| 226 |
+
},
|
| 227 |
+
"execution_count": null,
|
| 228 |
+
"outputs": [
|
| 229 |
+
{
|
| 230 |
+
"metadata": {
|
| 231 |
+
"tags": null
|
| 232 |
+
},
|
| 233 |
+
"name": "stderr",
|
| 234 |
+
"output_type": "stream",
|
| 235 |
+
"text": [
|
| 236 |
+
"/usr/local/lib/python3.10/dist-packages/google/protobuf/symbol_database.py:55: UserWarning: SymbolDatabase.GetPrototype() is deprecated. Please use message_factory.GetMessageClass() instead. SymbolDatabase.GetPrototype() will be removed soon.\n",
|
| 237 |
+
" warnings.warn('SymbolDatabase.GetPrototype() is deprecated. Please '\n"
|
| 238 |
+
]
|
| 239 |
+
}
|
| 240 |
+
]
|
| 241 |
+
},
|
| 242 |
+
{
|
| 243 |
+
"cell_type": "code",
|
| 244 |
+
"source": [
|
| 245 |
+
"import numpy as np\n",
|
| 246 |
+
"import os\n",
|
| 247 |
+
"\n",
|
| 248 |
+
"# Define the directory where your batches are stored\n",
|
| 249 |
+
"batch_dir = '/content/drive/MyDrive/'\n",
|
| 250 |
+
"\n",
|
| 251 |
+
"# Initialize empty lists to store data and labels\n",
|
| 252 |
+
"X = []\n",
|
| 253 |
+
"y = []\n",
|
| 254 |
+
"\n",
|
| 255 |
+
"# Loop through saved batch files\n",
|
| 256 |
+
"for file_name in os.listdir(batch_dir):\n",
|
| 257 |
+
" if file_name.endswith('.npy'):\n",
|
| 258 |
+
" if 'keypoints_batch' in file_name:\n",
|
| 259 |
+
" X.append(np.load(os.path.join(batch_dir, file_name)))\n",
|
| 260 |
+
" elif 'labels_batch' in file_name:\n",
|
| 261 |
+
" y.append(np.load(os.path.join(batch_dir, file_name)))\n",
|
| 262 |
+
"\n",
|
| 263 |
+
"# Combine all batches into a single dataset\n",
|
| 264 |
+
"X = np.concatenate(X, axis=0)\n",
|
| 265 |
+
"y = np.concatenate(y, axis=0)\n",
|
| 266 |
+
"\n",
|
| 267 |
+
"print(f'Loaded {X.shape[0]} samples for training.')\n"
|
| 268 |
+
],
|
| 269 |
+
"metadata": {
|
| 270 |
+
"id": "h79UADwu-9bn",
|
| 271 |
+
"colab": {
|
| 272 |
+
"base_uri": "https://localhost:8080/"
|
| 273 |
+
},
|
| 274 |
+
"outputId": "4cbeaae3-f6e4-4467-86d8-a7d098ca78dd"
|
| 275 |
+
},
|
| 276 |
+
"execution_count": null,
|
| 277 |
+
"outputs": [
|
| 278 |
+
{
|
| 279 |
+
"output_type": "stream",
|
| 280 |
+
"name": "stdout",
|
| 281 |
+
"text": [
|
| 282 |
+
"Loaded 102 samples for training.\n"
|
| 283 |
+
]
|
| 284 |
+
}
|
| 285 |
+
]
|
| 286 |
+
},
|
| 287 |
+
{
|
| 288 |
+
"cell_type": "code",
|
| 289 |
+
"source": [
|
| 290 |
+
"from sklearn.model_selection import train_test_split\n",
|
| 291 |
+
"from tensorflow.keras.models import Sequential\n",
|
| 292 |
+
"from tensorflow.keras.layers import LSTM, Dense\n",
|
| 293 |
+
"\n",
|
| 294 |
+
"# Split data into training and testing sets\n",
|
| 295 |
+
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
|
| 296 |
+
"\n",
|
| 297 |
+
"# Define the LSTM model\n",
|
| 298 |
+
"model = Sequential([\n",
|
| 299 |
+
" LSTM(64, return_sequences=True, input_shape=(X_train.shape[1], X_train.shape[2])),\n",
|
| 300 |
+
" LSTM(64),\n",
|
| 301 |
+
" Dense(32, activation='relu'),\n",
|
| 302 |
+
" Dense(1, activation='sigmoid')\n",
|
| 303 |
+
"])\n",
|
| 304 |
+
"\n",
|
| 305 |
+
"# Compile the model\n",
|
| 306 |
+
"model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n",
|
| 307 |
+
"\n",
|
| 308 |
+
"# Train the model\n",
|
| 309 |
+
"history = model.fit(X_train, y_train, epochs=20, validation_data=(X_test, y_test))\n",
|
| 310 |
+
"\n",
|
| 311 |
+
"# Save the trained model\n",
|
| 312 |
+
"model.save('lstm_model.h5')\n"
|
| 313 |
+
],
|
| 314 |
+
"metadata": {
|
| 315 |
+
"id": "jyr0HzDTBFWQ",
|
| 316 |
+
"colab": {
|
| 317 |
+
"base_uri": "https://localhost:8080/"
|
| 318 |
+
},
|
| 319 |
+
"outputId": "ece2fc87-75aa-4b9a-e40b-f1bbb4d6a7c7"
|
| 320 |
+
},
|
| 321 |
+
"execution_count": null,
|
| 322 |
+
"outputs": [
|
| 323 |
+
{
|
| 324 |
+
"output_type": "stream",
|
| 325 |
+
"name": "stderr",
|
| 326 |
+
"text": [
|
| 327 |
+
"/usr/local/lib/python3.10/dist-packages/keras/src/layers/rnn/rnn.py:204: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
|
| 328 |
+
" super().__init__(**kwargs)\n"
|
| 329 |
+
]
|
| 330 |
+
},
|
| 331 |
+
{
|
| 332 |
+
"output_type": "stream",
|
| 333 |
+
"name": "stdout",
|
| 334 |
+
"text": [
|
| 335 |
+
"Epoch 1/20\n",
|
| 336 |
+
"\u001b[1m3/3\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 307ms/step - accuracy: 0.6601 - loss: 0.6587 - val_accuracy: 1.0000 - val_loss: 0.5224\n",
|
| 337 |
+
"Epoch 2/20\n",
|
| 338 |
+
"\u001b[1m3/3\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 110ms/step - accuracy: 1.0000 - loss: 0.4933 - val_accuracy: 1.0000 - val_loss: 0.3953\n",
|
| 339 |
+
"Epoch 3/20\n",
|
| 340 |
+
"\u001b[1m3/3\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 102ms/step - accuracy: 1.0000 - loss: 0.3809 - val_accuracy: 1.0000 - val_loss: 0.2403\n",
|
| 341 |
+
"Epoch 4/20\n",
|
| 342 |
+
"\u001b[1m3/3\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 106ms/step - accuracy: 1.0000 - loss: 0.2357 - val_accuracy: 1.0000 - val_loss: 0.0733\n",
|
| 343 |
+
"Epoch 5/20\n",
|
| 344 |
+
"\u001b[1m3/3\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 103ms/step - accuracy: 1.0000 - loss: 0.0661 - val_accuracy: 1.0000 - val_loss: 0.0144\n",
|
| 345 |
+
"Epoch 6/20\n",
|
| 346 |
+
"\u001b[1m3/3\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 105ms/step - accuracy: 1.0000 - loss: 0.0128 - val_accuracy: 1.0000 - val_loss: 0.0052\n",
|
| 347 |
+
"Epoch 7/20\n",
|
| 348 |
+
"\u001b[1m3/3\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 108ms/step - accuracy: 1.0000 - loss: 0.0047 - val_accuracy: 1.0000 - val_loss: 0.0024\n",
|
| 349 |
+
"Epoch 8/20\n",
|
| 350 |
+
"\u001b[1m3/3\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 107ms/step - accuracy: 1.0000 - loss: 0.0022 - val_accuracy: 1.0000 - val_loss: 0.0013\n",
|
| 351 |
+
"Epoch 9/20\n",
|
| 352 |
+
"\u001b[1m3/3\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 108ms/step - accuracy: 1.0000 - loss: 0.0012 - val_accuracy: 1.0000 - val_loss: 7.7855e-04\n",
|
| 353 |
+
"Epoch 10/20\n",
|
| 354 |
+
"\u001b[1m3/3\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 101ms/step - accuracy: 1.0000 - loss: 7.1853e-04 - val_accuracy: 1.0000 - val_loss: 5.1570e-04\n",
|
| 355 |
+
"Epoch 11/20\n",
|
| 356 |
+
"\u001b[1m3/3\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 106ms/step - accuracy: 1.0000 - loss: 4.8286e-04 - val_accuracy: 1.0000 - val_loss: 3.7081e-04\n",
|
| 357 |
+
"Epoch 12/20\n",
|
| 358 |
+
"\u001b[1m3/3\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 104ms/step - accuracy: 1.0000 - loss: 3.5438e-04 - val_accuracy: 1.0000 - val_loss: 2.8507e-04\n",
|
| 359 |
+
"Epoch 13/20\n",
|
| 360 |
+
"\u001b[1m3/3\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 103ms/step - accuracy: 1.0000 - loss: 2.7463e-04 - val_accuracy: 1.0000 - val_loss: 2.3120e-04\n",
|
| 361 |
+
"Epoch 14/20\n",
|
| 362 |
+
"\u001b[1m3/3\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 102ms/step - accuracy: 1.0000 - loss: 2.2495e-04 - val_accuracy: 1.0000 - val_loss: 1.9555e-04\n",
|
| 363 |
+
"Epoch 15/20\n",
|
| 364 |
+
"\u001b[1m3/3\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 127ms/step - accuracy: 1.0000 - loss: 1.9260e-04 - val_accuracy: 1.0000 - val_loss: 1.7094e-04\n",
|
| 365 |
+
"Epoch 16/20\n",
|
| 366 |
+
"\u001b[1m3/3\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 105ms/step - accuracy: 1.0000 - loss: 1.6921e-04 - val_accuracy: 1.0000 - val_loss: 1.5331e-04\n",
|
| 367 |
+
"Epoch 17/20\n",
|
| 368 |
+
"\u001b[1m3/3\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 102ms/step - accuracy: 1.0000 - loss: 1.5150e-04 - val_accuracy: 1.0000 - val_loss: 1.4028e-04\n",
|
| 369 |
+
"Epoch 18/20\n",
|
| 370 |
+
"\u001b[1m3/3\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 134ms/step - accuracy: 1.0000 - loss: 1.3873e-04 - val_accuracy: 1.0000 - val_loss: 1.3034e-04\n",
|
| 371 |
+
"Epoch 19/20\n",
|
| 372 |
+
"\u001b[1m3/3\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 163ms/step - accuracy: 1.0000 - loss: 1.2947e-04 - val_accuracy: 1.0000 - val_loss: 1.2256e-04\n",
|
| 373 |
+
"Epoch 20/20\n",
|
| 374 |
+
"\u001b[1m3/3\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 186ms/step - accuracy: 1.0000 - loss: 1.2323e-04 - val_accuracy: 1.0000 - val_loss: 1.1630e-04\n"
|
| 375 |
+
]
|
| 376 |
+
},
|
| 377 |
+
{
|
| 378 |
+
"output_type": "stream",
|
| 379 |
+
"name": "stderr",
|
| 380 |
+
"text": [
|
| 381 |
+
"WARNING:absl:You are saving your model as an HDF5 file via `model.save()` or `keras.saving.save_model(model)`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')` or `keras.saving.save_model(model, 'my_model.keras')`. \n"
|
| 382 |
+
]
|
| 383 |
+
}
|
| 384 |
+
]
|
| 385 |
+
},
|
| 386 |
+
{
|
| 387 |
+
"cell_type": "code",
|
| 388 |
+
"source": [
|
| 389 |
+
"# Import libraries\n",
|
| 390 |
+
"import os\n",
|
| 391 |
+
"import cv2\n",
|
| 392 |
+
"import numpy as np\n",
|
| 393 |
+
"import mediapipe as mp\n",
|
| 394 |
+
"import tensorflow as tf\n",
|
| 395 |
+
"from sklearn.model_selection import train_test_split\n",
|
| 396 |
+
"from tensorflow.keras import Sequential\n",
|
| 397 |
+
"from tensorflow.keras.layers import LSTM, Dense\n",
|
| 398 |
+
"\n",
|
| 399 |
+
"# Initialize MediaPipe\n",
|
| 400 |
+
"mp_pose = mp.solutions.pose\n",
|
| 401 |
+
"mp_drawing = mp.solutions.drawing_utils\n",
|
| 402 |
+
"\n",
|
| 403 |
+
"def extract_keypoints(video_path):\n",
|
| 404 |
+
" cap = cv2.VideoCapture(video_path)\n",
|
| 405 |
+
" pose = mp_pose.Pose()\n",
|
| 406 |
+
" keypoints = []\n",
|
| 407 |
+
"\n",
|
| 408 |
+
" while cap.isOpened():\n",
|
| 409 |
+
" ret, frame = cap.read()\n",
|
| 410 |
+
" if not ret:\n",
|
| 411 |
+
" break\n",
|
| 412 |
+
"\n",
|
| 413 |
+
" frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n",
|
| 414 |
+
" results = pose.process(frame_rgb)\n",
|
| 415 |
+
"\n",
|
| 416 |
+
" if results.pose_landmarks:\n",
|
| 417 |
+
" landmarks = []\n",
|
| 418 |
+
" for lm in results.pose_landmarks.landmark:\n",
|
| 419 |
+
" landmarks.extend([lm.x, lm.y, lm.z, lm.visibility])\n",
|
| 420 |
+
" keypoints.append(landmarks)\n",
|
| 421 |
+
" else:\n",
|
| 422 |
+
" keypoints.append([0] * 132) # 33 landmarks * 4 values (x, y, z, visibility)\n",
|
| 423 |
+
"\n",
|
| 424 |
+
" cap.release()\n",
|
| 425 |
+
" return np.array(keypoints)\n",
|
| 426 |
+
"\n",
|
| 427 |
+
"def predict_exercise(video_path, model):\n",
|
| 428 |
+
" keypoints = extract_keypoints(video_path) # Now extract_keypoints is available\n",
|
| 429 |
+
" keypoints = np.expand_dims(keypoints, axis=0)\n",
|
| 430 |
+
" prediction = model.predict(keypoints)\n",
|
| 431 |
+
"\n",
|
| 432 |
+
" if prediction > 0.5:\n",
|
| 433 |
+
" return 'Correct Exercise'\n",
|
| 434 |
+
" else:\n",
|
| 435 |
+
" return 'Incorrect Exercise'\n",
|
| 436 |
+
"\n",
|
| 437 |
+
"# Example usage:\n",
|
| 438 |
+
"uploaded_video_path = '/content/drive/MyDrive/correct/correct/decline bench press/dbp_1.mp4'\n",
|
| 439 |
+
"result = predict_exercise(uploaded_video_path, model)\n",
|
| 440 |
+
"print(f'Result: {result}')"
|
| 441 |
+
],
|
| 442 |
+
"metadata": {
|
| 443 |
+
"id": "8kcyfLGmBNtZ",
|
| 444 |
+
"colab": {
|
| 445 |
+
"base_uri": "https://localhost:8080/"
|
| 446 |
+
},
|
| 447 |
+
"outputId": "24c01dea-14fc-4467-ec26-d4635ff022ea"
|
| 448 |
+
},
|
| 449 |
+
"execution_count": null,
|
| 450 |
+
"outputs": [
|
| 451 |
+
{
|
| 452 |
+
"output_type": "stream",
|
| 453 |
+
"name": "stdout",
|
| 454 |
+
"text": [
|
| 455 |
+
"\u001b[1m1/1\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 334ms/step\n",
|
| 456 |
+
"Result: Correct Exercise\n"
|
| 457 |
+
]
|
| 458 |
+
}
|
| 459 |
+
]
|
| 460 |
+
}
|
| 461 |
+
]
|
| 462 |
+
}
|