Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions a0.1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
.venv/
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,297 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "889b03a2",
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import time\n",
"import mediapipe as mp\n",
"import numpy as np\n",
"\n",
"mp_drawing = mp.solutions.drawing_utils\n",
"mp_pose = mp.solutions.pose\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "9ceca6e2",
"metadata": {},
"outputs": [],
"source": [
"def elbow_angle(a, b, c):\n",
" radians = np.arctan2(c.y-b.y, c.x-b.x) - np.arctan2(a.y-b.y, a.x-b.x)\n",
" angle = np.abs(radians*180/np.pi)\n",
"\n",
" if angle > 180:\n",
" angle = 360 - angle\n",
"\n",
" return round(angle, 2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d477d52d",
"metadata": {},
"outputs": [],
"source": [
"def torso_angle(a, b, c, d):\n",
" \"\"\"\n",
" Calculates the angle between torso and the Z-axis.\n",
"\n",
" Args:\n",
" p1 (tuple/list): middle point of shoulders\n",
" p2 (tuple/list): middel point of hips\n",
"\n",
" Returns:\n",
" float: Angle in degrees between the line and the positive Z-axis.\n",
" \"\"\"\n",
" p1 = ((a.x+b.x)/2, (a.y+b.y)/2, (a.z+b.z)/2) # Midpoint of a and b\n",
" p2 = ((c.x+d.x)/2, (c.y+d.y)/2, (c.z+d.z)/2) # Midpoint of c and d\n",
" \n",
" # 1. Define Z-axis unit vector\n",
" z_axis_vector = np.array([0, 0, 1])\n",
"\n",
" # 2. Calculate the line vector (P2 - P1)\n",
" line_vector = np.array(p2) - np.array(p1)\n",
"\n",
" # 3. Calculate the dot product\n",
" dot_product = np.dot(line_vector, z_axis_vector)\n",
"\n",
" # 4. Calculate magnitudes\n",
" magnitude_line = np.linalg.norm(line_vector)\n",
" magnitude_z = np.linalg.norm(z_axis_vector) # This is just 1\n",
"\n",
" # Avoid division by zero if the line is a point\n",
" if magnitude_line == 0:\n",
" return 0.0 # Or handle as error\n",
"\n",
" # 5. Calculate the cosine of the angle\n",
" # Clamp the value to [-1, 1] to avoid floating point errors with acos\n",
" cos_theta = np.clip(dot_product / (magnitude_line * magnitude_z), -1.0, 1.0)\n",
"\n",
" # 6. Calculate the angle in radians and convert to degrees\n",
" angle_radians = np.arccos(cos_theta)\n",
" angle = np.degrees(angle_radians)\n",
"\n",
" if angle > 180:\n",
" angle = 360 - angle\n",
"\n",
"\n",
" return round(angle, 2)\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "d9e1c9d5",
"metadata": {},
"source": [
"1. Torso Angle:\n",
"\n",
" less than 20° relative to the vertical direction (Z axis)\n",
"\n",
"2. Elbow Angle:\n",
"\n",
" At the top:\n",
" greater than 150 \n",
"\n",
" At the bottom:\n",
" less than 90° \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "edc6cf5f",
"metadata": {},
"outputs": [],
"source": [
"cap = cv2.VideoCapture(0)\n",
"\n",
"start_time = time.time()\n",
"counter = 0\n",
"rate = 0\n",
"rate_t = \"0.0\"\n",
"stage = None\n",
"\n",
"with mp_pose.Pose(\n",
" min_detection_confidence=0.5, \n",
" min_tracking_confidence=0.5\n",
") as pose:\n",
" \n",
" while cap.isOpened():\n",
" ret, frame = cap.read()\n",
" \n",
" image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n",
" image.flags.writeable = False\n",
"\n",
" results = pose.process(image)\n",
"\n",
" image.flags.writeable = True\n",
" image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)\n",
"\n",
" try:\n",
" landmarks = results.pose_landmarks.landmark\n",
" l_wrist = landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value]\n",
" l_elbow = landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value]\n",
" l_shoulder = landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value]\n",
" l_hip = landmarks[mp_pose.PoseLandmark.LEFT_HIP.value]\n",
" \n",
" r_wrist = landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value]\n",
" r_elbow = landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value]\n",
" r_shoulder = landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value]\n",
" r_hip = landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value]\n",
" \n",
" lh_angle = elbow_angle(l_wrist, l_elbow, l_shoulder)\n",
" rh_angle = elbow_angle(r_wrist, r_elbow, r_shoulder)\n",
" t_angle = torso_angle(l_shoulder, r_shoulder, l_hip, r_hip)\n",
"\n",
" cv2.putText(image,\n",
" str(lh_angle),\n",
" tuple(np.multiply((l_elbow.x, l_elbow.y), [640, 480]).astype(int)),\n",
" cv2.FONT_HERSHEY_SIMPLEX,\n",
" 0.5,\n",
" (255, 255, 0),\n",
" 2,\n",
" cv2.LINE_AA\n",
" )\n",
" cv2.putText(image,\n",
" str(rh_angle),\n",
" tuple(np.multiply((r_elbow.x, r_elbow.y), [640, 480]).astype(int)),\n",
" cv2.FONT_HERSHEY_SIMPLEX,\n",
" 0.5,\n",
" (255, 255, 0),\n",
" 2,\n",
" cv2.LINE_AA\n",
" )\n",
"\n",
" cv2.putText(image,\n",
" str(t_angle),\n",
" tuple(np.multiply(((l_shoulder.x+r_shoulder.x)/2, (l_shoulder.y+r_shoulder.y)/2), [640, 480]).astype(int)),\n",
" cv2.FONT_HERSHEY_SIMPLEX,\n",
" 0.5,\n",
" (255, 255, 0),\n",
" 2,\n",
" cv2.LINE_AA\n",
" )\n",
" cv2.putText(image,\n",
" timer_text,\n",
" (10, 470),\n",
" cv2.FONT_HERSHEY_SIMPLEX,\n",
" 0.7,\n",
" (0, 255, 0),\n",
" 2,\n",
" cv2.LINE_AA\n",
" )\n",
"\n",
" # Curl counter logic\n",
" if lh_angle > 150 and rh_angle > 150:\n",
" stage = 'up'\n",
" if lh_angle < 90 and rh_angle < 90 and (t_angle - 90) < 20 and stage == 'up':\n",
" stage = 'down'\n",
" counter += 1\n",
"\n",
" except:\n",
" pass\n",
" \n",
" cv2.rectangle(image, (0,0), (150,50), (255,100,0), -1)\n",
" cv2.putText(image, 'REPS: ',\n",
" (0,35),\n",
" cv2.FONT_HERSHEY_COMPLEX,\n",
" 0.7,\n",
" (0,255,255),\n",
" 1,\n",
" cv2.LINE_AA)\n",
" cv2.putText(image,\n",
" str(counter),\n",
" (80,40),\n",
" cv2.FONT_HERSHEY_COMPLEX,\n",
" 1.0,\n",
" (255,255,255),\n",
" 2,\n",
" cv2.LINE_AA)\n",
"\n",
" cv2.rectangle(image, (440,0), (620,50), (255,100,0), -1)\n",
" cv2.putText(image, 'Stage: ',\n",
" (440,35),\n",
" cv2.FONT_HERSHEY_COMPLEX,\n",
" 0.7,\n",
" (0,255,255),\n",
" 1,\n",
" cv2.LINE_AA)\n",
" cv2.putText(image,\n",
" str(stage),\n",
" (530,40),\n",
" cv2.FONT_HERSHEY_COMPLEX,\n",
" 1.0,\n",
" (255,255,255),\n",
" 2,\n",
" cv2.LINE_AA)\n",
"\n",
" cv2.rectangle(image, (220,0), (380,50), (255,100,0), -1)\n",
" elapsed_time = time.time() - start_time\n",
" minutes = int(elapsed_time // 60)\n",
" seconds = int(elapsed_time % 60)\n",
" timer_text = f\"{minutes:02d}:{seconds:02d}\"\n",
" rate = counter / (minutes * 60 + seconds + 0.001) * 60\n",
" rate_t = f\"{rate:.1f}\"\n",
" cv2.putText(image, '#/min: ',\n",
" (220,35),\n",
" cv2.FONT_HERSHEY_COMPLEX,\n",
" 0.7,\n",
" (0,255,255),\n",
" 1,\n",
" cv2.LINE_AA)\n",
" cv2.putText(image,\n",
" str(rate_t),\n",
" (320,40),\n",
" cv2.FONT_HERSHEY_COMPLEX,\n",
" 1.0,\n",
" (255,255,255),\n",
" 2,\n",
" cv2.LINE_AA)\n",
"\n",
" # Draw landmarks\n",
" mp_drawing.draw_landmarks(\n",
" image, \n",
" results.pose_landmarks, \n",
" mp_pose.POSE_CONNECTIONS\n",
" )\n",
" \n",
" cv2.imshow(\"Pose\", image)\n",
" if cv2.waitKey(10) & 0xFF == ord('q'):\n",
" break\n",
"\n",
"cap.release()\n",
"cv2.destroyAllWindows()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "mp_cpu",
"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.10.19"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading