Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
vjshankar4204 committed Dec 5, 2023
2 parents e1e2464 + 92d5754 commit df5e2cb
Show file tree
Hide file tree
Showing 9 changed files with 4,189 additions and 1,521 deletions.
Binary file modified SquatMaster/Data/videos/exercise_stock_video3_wRepCount.mp4
Binary file not shown.
2,613 changes: 1,110 additions & 1,503 deletions SquatMaster/ML_training/classification.ipynb

Large diffs are not rendered by default.

108 changes: 93 additions & 15 deletions SquatMaster/ML_training/test1/ML_training_test1.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,102 @@
import matplotlib.pyplot as plt
import cv2
import mediapipe as mp
import numpy as np
import PIL
import tensorflow as tf
import sys
sys.path.insert(0,'../functions')
mp_drawing = mp.solutions.drawing_utils
mp_pose = mp.solutions.pose
import os

from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
# Specify the location of the file with the video to be read
# Capture frames from a saved video
filename_r = '../../Data/videos/exercise_stock_video3.mp4'
# filename_r = 4
# Specify the location where the new video witwwh detections will be written
# filename_w = '../Data/videos/exercise_stock_video3_wRepCount.mp4'

import pathlib
cap = cv2.VideoCapture(filename_r)

dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos.tar', origin=dataset_url, extract=True)
data_dir = pathlib.Path(data_dir).with_suffix('')
# Get video frame dimensions and fps
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
frame_size = (frame_width,frame_height)
fps = cap.get(5)

image_count = len(list(data_dir.glob('*/*.jpg')))
print(image_count)
# # Create a video writer object
# output = cv2.VideoWriter(filename_w, cv2.VideoWriter_fourcc(*'XVID'), fps, frame_size)

frame_count = 0

## Setup mediapipe instance
with mp_pose.Pose(min_detection_confidence=0.9, min_tracking_confidence=0.5, enable_segmentation=True) as pose:

fps = cap.get(5)
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))

while cap.isOpened():
ret, frame = cap.read()

# Stop the code when the video is over
if not ret:
print('Failed to grab a frame')
break

# Count frames and calculate time in video
frame_count += 1
time = frame_count / fps #[s]

# Recolor image to RGB
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image.flags.writeable = False

# Make detection
results = pose.process(image)

# Recolor back to BGR
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

# Extract landmarks
try:
landmarks = results.pose_landmarks.landmark

# Get coordinates
hip = [landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].x,landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].y]
knee = [landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value].x,landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value].y]
ankle = [landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].x,landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].y]



except:
pass



# Render detections
mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS,
mp_drawing.DrawingSpec(color=(245,117,66), thickness=2, circle_radius=2),
mp_drawing.DrawingSpec(color=(245,66,230), thickness=2, circle_radius=2)
)


# Display and write the video
if ret == True:
cv2.imshow('Mediapipe Feed', image)
# output.write(image)

# Pause or stop the video when instructed
key = cv2.waitKey(5)
# Stop by pressing 'q'
if key == ord('q'):
break
# Pause by pressing 'w', resume by pressing any other key
if key == ord('w'):
cv2.waitKey(-1)


cap.release()
cv2.destroyAllWindows()


roses = list(data_dir.glob('roses/*'))
im = PIL.Image.open(str(roses[1]))

im.show()
194 changes: 194 additions & 0 deletions SquatMaster/ML_training/test1/ML_training_test2.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import mediapipe as mp\n",
"import numpy as np\n",
"import sys\n",
"sys.path.insert(0,'../../functions')\n",
"mp_drawing = mp.solutions.drawing_utils\n",
"mp_pose = mp.solutions.pose\n",
"import os\n",
"from heel_crop1 import heel_crop\n",
"\n",
"# from tensorflow import keras\n",
"# from tensorflow.keras import layers\n",
"# from tensorflow.keras.models import Sequential"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"I0000 00:00:1701556954.369788 158802 gl_context_egl.cc:85] Successfully initialized EGL. Major : 1 Minor: 5\n",
"I0000 00:00:1701556954.371567 192045 gl_context.cc:344] GL version: 3.2 (OpenGL ES 3.2 Mesa 23.2.1-1ubuntu3), renderer: AMD Radeon Graphics (renoir, LLVM 15.0.7, DRM 3.54, 6.5.0-13-generic)\n",
"qt.qpa.xcb: QXcbConnection: XCB error: 146 (Unknown), sequence: 38249, resource id: 0, major code: 139 (Unknown), minor code: 20\n",
"qt.qpa.xcb: QXcbConnection: XCB error: 146 (Unknown), sequence: 38254, resource id: 1329, major code: 139 (Unknown), minor code: 20\n"
]
}
],
"source": [
"\n",
"# Specify the location of the file with the video to be read\n",
"# Capture frames from a saved video\n",
"filename_r = '../../Data/videos/exercise_stock_video3.mp4'\n",
"# filename_r = 4\n",
"filename_r = '../../../../Material/hill_raise/IMG_3719.MOV'\n",
"# Specify the location where the new video with detections will be written\n",
"# filename_w = '../Data/videos/exercise_stock_video3_wRepCount.mp4'\n",
"\n",
"cap = cv2.VideoCapture(filename_r)\n",
"\n",
"# Get video frame dimensions and fps\n",
"frame_width = int(cap.get(3))\n",
"frame_height = int(cap.get(4))\n",
"frame_size = (frame_width,frame_height)\n",
"fps = cap.get(5)\n",
"\n",
"# # Create a video writer object\n",
"# output = cv2.VideoWriter(filename_w, cv2.VideoWriter_fourcc(*'XVID'), fps, frame_size)\n",
"\n",
"frame_count = 0\n",
"frame_count_trim = 0\n",
"\n",
"## Setup mediapipe instance\n",
"with mp_pose.Pose(min_detection_confidence=0.9, min_tracking_confidence=0.5, enable_segmentation=True) as pose:\n",
"\n",
" fps = cap.get(5)\n",
" frame_width = int(cap.get(3))\n",
" frame_height = int(cap.get(4))\n",
"\n",
" while cap.isOpened():\n",
" ret, frame = cap.read()\n",
"\n",
" # Rotate the video feed if its coming from the phone webcam\n",
" if filename_r == 4:\n",
" frame=cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)\n",
"\n",
"\n",
" # Stop the code when the video is over\n",
" if not ret:\n",
" print('Failed to grab a frame')\n",
" break\n",
"\n",
" # Count frames and calculate time in video\n",
" frame_count += 1\n",
" time = frame_count / fps #[s]\n",
"\n",
" # Recolor image to RGB\n",
" image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n",
" image.flags.writeable = False\n",
" \n",
" # Make detection\n",
" results = pose.process(image)\n",
" \n",
" # Recolor back to BGR\n",
" image.flags.writeable = True\n",
" image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)\n",
"\n",
" image_trimmed = frame\n",
"\n",
" # Extract landmarks\n",
" try:\n",
" landmarks = results.pose_landmarks.landmark\n",
" \n",
" # Get coordinates\n",
" right_ear = [landmarks[mp_pose.PoseLandmark.RIGHT_EAR.value].x,landmarks[mp_pose.PoseLandmark.RIGHT_EAR.value].y]\n",
" left_ankle = [landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].x,landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].y]\n",
" right_ankle = [landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].x,landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].y]\n",
" left_heel = [landmarks[mp_pose.PoseLandmark.LEFT_HEEL.value].x,landmarks[mp_pose.PoseLandmark.LEFT_HEEL.value].y]\n",
" right_heel = [landmarks[mp_pose.PoseLandmark.RIGHT_HEEL.value].x,landmarks[mp_pose.PoseLandmark.RIGHT_HEEL.value].y]\n",
" left_footIndex = [landmarks[mp_pose.PoseLandmark.LEFT_FOOT_INDEX.value].x,landmarks[mp_pose.PoseLandmark.LEFT_FOOT_INDEX.value].y]\n",
" right_footIndex = [landmarks[mp_pose.PoseLandmark.RIGHT_FOOT_INDEX.value].x,landmarks[mp_pose.PoseLandmark.RIGHT_FOOT_INDEX.value].y]\n",
" \n",
" \n",
" points = [left_ankle,left_heel,left_footIndex]\n",
" [image_trimmed, frame_count_trim] = heel_crop(points, frame, frame_count_trim)\n",
" \n",
" except:\n",
" pass\n",
" \n",
"\n",
" # Render detections\n",
" mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS,\n",
" mp_drawing.DrawingSpec(color=(245,117,66), thickness=2, circle_radius=2), \n",
" mp_drawing.DrawingSpec(color=(245,66,230), thickness=2, circle_radius=2) \n",
" )\n",
"\n",
"\n",
" # Display and write the video\n",
" if ret == True:\n",
" cv2.imshow('Mediapipe Feed', image)\n",
" # output.write(image)\n",
"\n",
" try:\n",
" cv2.imshow('Right foot', image_trimmed)\n",
" # if frame_count_trim % 5 == 0:\n",
" # cv2.imwrite(f'/home/rafik/PROJECTS/pose1/Material/hill_raise/cropped_feet/left_foot/left_foot_{frame_count_trim}.jpg', image_trimmed)\n",
" except:\n",
" print('No foot in sight')\n",
"\n",
" # Pause or stop the video when instructed\n",
" key = cv2.waitKey(5)\n",
" # Stop by pressing 'q'\n",
" if key == ord('q'):\n",
" break\n",
" # Pause by pressing 'w', resume by pressing any other key\n",
" if key == ord('w'):\n",
" cv2.waitKey(-1)\n",
"\n",
"\n",
" cap.release()\n",
" cv2.destroyAllWindows()\n"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/home/rafik/PROJECTS/pose1/MediaPipePoseEstimation-eschar/SquatMaster/ML_training/test1\n"
]
}
],
"source": [
"print(os.getcwd())"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "venv_pose1",
"language": "python",
"name": "venv_pose1"
},
"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.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit df5e2cb

Please sign in to comment.