{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Make Detection with the Trained Model"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"objc[7440]: Class CaptureDelegate is implemented in both /Users/fuixlabsdev1/Programming/PP/graduation-thesis/env/lib/python3.8/site-packages/mediapipe/.dylibs/libopencv_videoio.3.4.16.dylib (0x110e88860) and /Users/fuixlabsdev1/Programming/PP/graduation-thesis/env/lib/python3.8/site-packages/cv2/cv2.abi3.so (0x289efe480). One of the two will be used. Which one is undefined.\n",
"objc[7440]: Class CVWindow is implemented in both /Users/fuixlabsdev1/Programming/PP/graduation-thesis/env/lib/python3.8/site-packages/mediapipe/.dylibs/libopencv_highgui.3.4.16.dylib (0x102fd8a68) and /Users/fuixlabsdev1/Programming/PP/graduation-thesis/env/lib/python3.8/site-packages/cv2/cv2.abi3.so (0x289efe4d0). One of the two will be used. Which one is undefined.\n",
"objc[7440]: Class CVView is implemented in both /Users/fuixlabsdev1/Programming/PP/graduation-thesis/env/lib/python3.8/site-packages/mediapipe/.dylibs/libopencv_highgui.3.4.16.dylib (0x102fd8a90) and /Users/fuixlabsdev1/Programming/PP/graduation-thesis/env/lib/python3.8/site-packages/cv2/cv2.abi3.so (0x289efe4f8). One of the two will be used. Which one is undefined.\n",
"objc[7440]: Class CVSlider is implemented in both /Users/fuixlabsdev1/Programming/PP/graduation-thesis/env/lib/python3.8/site-packages/mediapipe/.dylibs/libopencv_highgui.3.4.16.dylib (0x102fd8ab8) and /Users/fuixlabsdev1/Programming/PP/graduation-thesis/env/lib/python3.8/site-packages/cv2/cv2.abi3.so (0x289efe520). One of the two will be used. Which one is undefined.\n"
]
}
],
"source": [
"import mediapipe as mp\n",
"import cv2\n",
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"import pickle\n",
"\n",
"import warnings\n",
"warnings.filterwarnings('ignore')\n",
"\n",
"# Drawing helpers\n",
"mp_drawing = mp.solutions.drawing_utils\n",
"mp_pose = mp.solutions.pose"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Reconstruct the input structure"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Determine important landmarks for plank\n",
"IMPORTANT_LMS = [\n",
" \"NOSE\",\n",
" \"LEFT_SHOULDER\",\n",
" \"RIGHT_SHOULDER\",\n",
" \"LEFT_ELBOW\",\n",
" \"RIGHT_ELBOW\",\n",
" \"LEFT_WRIST\",\n",
" \"RIGHT_WRIST\",\n",
" \"LEFT_HIP\",\n",
" \"RIGHT_HIP\",\n",
" \"LEFT_KNEE\",\n",
" \"RIGHT_KNEE\",\n",
" \"LEFT_ANKLE\",\n",
" \"RIGHT_ANKLE\",\n",
" \"LEFT_HEEL\",\n",
" \"RIGHT_HEEL\",\n",
" \"LEFT_FOOT_INDEX\",\n",
" \"RIGHT_FOOT_INDEX\",\n",
"]\n",
"\n",
"# Generate all columns of the data frame\n",
"\n",
"HEADERS = [\"label\"] # Label column\n",
"\n",
"for lm in IMPORTANT_LMS:\n",
" HEADERS += [f\"{lm.lower()}_x\", f\"{lm.lower()}_y\", f\"{lm.lower()}_z\", f\"{lm.lower()}_v\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Setup some important functions"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def extract_important_keypoints(results) -> list:\n",
" '''\n",
" Extract important keypoints from mediapipe pose detection\n",
" '''\n",
" landmarks = results.pose_landmarks.landmark\n",
"\n",
" data = []\n",
" for lm in IMPORTANT_LMS:\n",
" keypoint = landmarks[mp_pose.PoseLandmark[lm].value]\n",
" data.append([keypoint.x, keypoint.y, keypoint.z, keypoint.visibility])\n",
" \n",
" return np.array(data).flatten().tolist()\n",
"\n",
"\n",
"def rescale_frame(frame, percent=50):\n",
" '''\n",
" Rescale a frame to a certain percentage compare to its original frame\n",
" '''\n",
" width = int(frame.shape[1] * percent/ 100)\n",
" height = int(frame.shape[0] * percent/ 100)\n",
" dim = (width, height)\n",
" return cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"VIDEO_PATH1 = \"../data/plank/plank_test.mov\"\n",
"VIDEO_PATH2 = \"../data/plank/plank_test_1.mp4\"\n",
"VIDEO_PATH3 = \"../data/plank/plank_test_2.mp4\"\n",
"VIDEO_PATH4 = \"../data/plank/plank_test_3.mp4\"\n",
"VIDEO_PATH5 = \"../data/plank/plank_test_4.mp4\"\n",
"VIDEO_TEST = \"../../demo/plank.mp4\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Make detection with Scikit learn model"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# Load model\n",
"with open(\"./model/LR_model.pkl\", \"rb\") as f:\n",
" sklearn_model = pickle.load(f)\n",
"\n",
"# Dump input scaler\n",
"with open(\"./model/input_scaler.pkl\", \"rb\") as f2:\n",
" input_scaler = pickle.load(f2)"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"ename": "",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31mThe Kernel crashed while executing code in the the current cell or a previous cell. Please review the code in the cell(s) to identify a possible cause of the failure. Click here for more info. View Jupyter log for further details."
]
}
],
"source": [
"cap = cv2.VideoCapture(VIDEO_PATH5)\n",
"current_stage = \"\"\n",
"prediction_probability_threshold = 0.6\n",
"\n",
"with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:\n",
" while cap.isOpened():\n",
" ret, image = cap.read()\n",
"\n",
" if not ret:\n",
" break\n",
"\n",
" # Reduce size of a frame\n",
" image = rescale_frame(image, 50)\n",
" # image = cv2.flip(image, 1)\n",
"\n",
" # Recolor image from BGR to RGB for mediapipe\n",
" image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n",
" image.flags.writeable = False\n",
"\n",
" results = pose.process(image)\n",
"\n",
" if not results.pose_landmarks:\n",
" print(\"No human found\")\n",
" continue\n",
"\n",
" # Recolor image from BGR to RGB for mediapipe\n",
" image.flags.writeable = True\n",
" image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)\n",
"\n",
" # Draw landmarks and connections\n",
" mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS, mp_drawing.DrawingSpec(color=(244, 117, 66), thickness=2, circle_radius=2), mp_drawing.DrawingSpec(color=(245, 66, 230), thickness=2, circle_radius=1))\n",
"\n",
" # Make detection\n",
" try:\n",
" # Extract keypoints from frame for the input\n",
" row = extract_important_keypoints(results)\n",
" X = pd.DataFrame([row], columns=HEADERS[1:])\n",
" X = pd.DataFrame(input_scaler.transform(X))\n",
"\n",
" # Make prediction and its probability\n",
" predicted_class = sklearn_model.predict(X)[0]\n",
" prediction_probability = sklearn_model.predict_proba(X)[0]\n",
" # print(predicted_class, prediction_probability)\n",
"\n",
" # Evaluate model prediction\n",
" if predicted_class == \"C\" and prediction_probability[prediction_probability.argmax()] >= prediction_probability_threshold:\n",
" current_stage = \"Correct\"\n",
" elif predicted_class == \"L\" and prediction_probability[prediction_probability.argmax()] >= prediction_probability_threshold: \n",
" current_stage = \"Low back\"\n",
" elif predicted_class == \"H\" and prediction_probability[prediction_probability.argmax()] >= prediction_probability_threshold: \n",
" current_stage = \"High back\"\n",
" else:\n",
" current_stage = \"unk\"\n",
" \n",
" # Visualization\n",
" # Status box\n",
" cv2.rectangle(image, (0, 0), (250, 60), (245, 117, 16), -1)\n",
"\n",
" # Display class\n",
" cv2.putText(image, \"CLASS\", (95, 12), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA)\n",
" cv2.putText(image, current_stage, (90, 40), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)\n",
"\n",
" # Display probability\n",
" cv2.putText(image, \"PROB\", (15, 12), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA)\n",
" cv2.putText(image, str(round(prediction_probability[np.argmax(prediction_probability)], 2)), (10, 40), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)\n",
"\n",
" except Exception as e:\n",
" print(f\"Error: {e}\")\n",
" \n",
" cv2.imshow(\"CV2\", image)\n",
" \n",
" # Press Q to close cv2 window\n",
" if cv2.waitKey(1) & 0xFF == ord('q'):\n",
" break\n",
"\n",
" cap.release()\n",
" cv2.destroyAllWindows()\n",
"\n",
" # (Optional)Fix bugs cannot close windows in MacOS (https://stackoverflow.com/questions/6116564/destroywindow-does-not-close-window-on-mac-using-python-and-opencv)\n",
" for i in range (1, 5):\n",
" cv2.waitKey(1)\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Make detection with Deep Learning Model"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Metal device set to: Apple M1\n",
"\n",
"systemMemory: 16.00 GB\n",
"maxCacheSize: 5.33 GB\n",
"\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"2022-11-23 00:54:54.408985: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:305] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.\n",
"2022-11-23 00:54:54.409101: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:271] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: )\n"
]
}
],
"source": [
"# Load model\n",
"with open(\"./model/plank_dp.pkl\", \"rb\") as f:\n",
" deep_learning_model = pickle.load(f)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2022-11-23 00:55:24.514124: W tensorflow/core/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz\n",
"2022-11-23 00:55:24.553362: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:113] Plugin optimizer for device_type GPU is enabled.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"1/1 [==============================] - 1s 684ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 11ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 12ms/step\n",
"1/1 [==============================] - 0s 12ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 11ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 15ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 11ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 11ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n",
"1/1 [==============================] - 0s 12ms/step\n",
"1/1 [==============================] - 0s 10ms/step\n",
"1/1 [==============================] - 0s 11ms/step\n",
"1/1 [==============================] - 0s 9ms/step\n"
]
}
],
"source": [
"cap = cv2.VideoCapture(VIDEO_TEST)\n",
"current_stage = \"\"\n",
"prediction_probability_threshold = 0.6\n",
"\n",
"with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:\n",
" while cap.isOpened():\n",
" ret, image = cap.read()\n",
"\n",
" if not ret:\n",
" break\n",
"\n",
" # Reduce size of a frame\n",
" image = rescale_frame(image, 50)\n",
"\n",
" # Recolor image from BGR to RGB for mediapipe\n",
" image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n",
" image.flags.writeable = False\n",
"\n",
" results = pose.process(image)\n",
"\n",
" if not results.pose_landmarks:\n",
" print(\"No human found\")\n",
" continue\n",
"\n",
" # Recolor image from BGR to RGB for mediapipe\n",
" image.flags.writeable = True\n",
" image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)\n",
"\n",
" # Draw landmarks and connections\n",
" mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS, mp_drawing.DrawingSpec(color=(244, 117, 66), thickness=2, circle_radius=2), mp_drawing.DrawingSpec(color=(245, 66, 230), thickness=2, circle_radius=1))\n",
"\n",
" # Make detection\n",
" try:\n",
" # Extract keypoints from frame for the input\n",
" row = extract_important_keypoints(results)\n",
" X = pd.DataFrame([row, ], columns=HEADERS[1:])\n",
" X = pd.DataFrame(input_scaler.transform(X))\n",
" \n",
"\n",
" # Make prediction and its probability\n",
" prediction = deep_learning_model.predict(X)\n",
" predicted_class = np.argmax(prediction, axis=1)[0]\n",
"\n",
" prediction_probability = max(prediction.tolist()[0])\n",
" # print(X)\n",
"\n",
" # Evaluate model prediction\n",
" if predicted_class == 0 and prediction_probability >= prediction_probability_threshold:\n",
" current_stage = \"Correct\"\n",
" elif predicted_class == 2 and prediction_probability >= prediction_probability_threshold: \n",
" current_stage = \"Low back\"\n",
" elif predicted_class == 1 and prediction_probability >= prediction_probability_threshold: \n",
" current_stage = \"High back\"\n",
" else:\n",
" current_stage = \"Unknown\"\n",
"\n",
" # Visualization\n",
" # Status box\n",
" cv2.rectangle(image, (0, 0), (550, 60), (245, 117, 16), -1)\n",
"\n",
" # # Display class\n",
" cv2.putText(image, \"DETECTION\", (95, 12), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA)\n",
" cv2.putText(image, current_stage, (90, 40), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)\n",
"\n",
" # # Display class\n",
" cv2.putText(image, \"CLASS\", (350, 12), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA)\n",
" cv2.putText(image, str(predicted_class), (345, 40), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)\n",
"\n",
" # # Display probability\n",
" cv2.putText(image, \"PROB\", (15, 12), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA)\n",
" cv2.putText(image, str(round(prediction_probability, 2)), (10, 40), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)\n",
"\n",
" except Exception as e:\n",
" print(f\"Error: {e}\")\n",
" \n",
" cv2.imshow(\"CV2\", image)\n",
" \n",
" # Press Q to close cv2 window\n",
" if cv2.waitKey(1) & 0xFF == ord('q'):\n",
" break\n",
"\n",
" cap.release()\n",
" cv2.destroyAllWindows()\n",
"\n",
" # (Optional)Fix bugs cannot close windows in MacOS (https://stackoverflow.com/questions/6116564/destroywindow-does-not-close-window-on-mac-using-python-and-opencv)\n",
" for i in range (1, 5):\n",
" cv2.waitKey(1)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"X"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.8.13 (conda)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.13"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "9260f401923fb5c4108c543a7d176de9733d378b3752e49535ad7c43c2271b65"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}