Spaces:
No application file
No application file
Commit ·
2f7b723
0
Parent(s):
Initial commit
Browse files- .DS_Store +0 -0
- .ipynb_checkpoints/main-checkpoint.py +12 -0
- app.py +28 -0
- download_model.py +21 -0
- pilates_evaluator.py +226 -0
- project_2 +1 -0
- requirements.txt +11 -0
- test_pilates.py +10 -0
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
.ipynb_checkpoints/main-checkpoint.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
|
| 4 |
+
def main():
|
| 5 |
+
# Load environment variables
|
| 6 |
+
load_dotenv()
|
| 7 |
+
|
| 8 |
+
print("Hello from main.py!")
|
| 9 |
+
# Add your main application logic here
|
| 10 |
+
|
| 11 |
+
if __name__ == "__main__":
|
| 12 |
+
main()
|
app.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import chainlit as cl
|
| 2 |
+
from pilates_evaluator import PilatesVideoEvaluator
|
| 3 |
+
|
| 4 |
+
@cl.on_chat_start
|
| 5 |
+
async def start():
|
| 6 |
+
await cl.Message(content="Welcome! Upload a video to analyze your Pilates exercise.").send()
|
| 7 |
+
|
| 8 |
+
@cl.on_message
|
| 9 |
+
async def main(message: cl.Message):
|
| 10 |
+
if not message.elements:
|
| 11 |
+
await cl.Message(content="Please upload a video file.").send()
|
| 12 |
+
return
|
| 13 |
+
|
| 14 |
+
video_file = message.elements[0]
|
| 15 |
+
if not video_file.name.endswith(('.mp4', '.avi', '.mov')):
|
| 16 |
+
await cl.Message(content="Please upload a valid video file (mp4, avi, mov).").send()
|
| 17 |
+
return
|
| 18 |
+
|
| 19 |
+
await cl.Message(content=f"Analyzing video: {video_file.name}...").send()
|
| 20 |
+
|
| 21 |
+
evaluator = PilatesVideoEvaluator()
|
| 22 |
+
try:
|
| 23 |
+
evaluator.process_video(video_file.path)
|
| 24 |
+
report_path = "pilates_evaluation_report.json"
|
| 25 |
+
evaluator.generate_report(report_path)
|
| 26 |
+
await cl.Message(content=f"Analysis complete! Report saved to {report_path}.").send()
|
| 27 |
+
except Exception as e:
|
| 28 |
+
await cl.Message(content=f"Error analyzing video: {e}").send()
|
download_model.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import urllib.request
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
def download_model():
|
| 5 |
+
model_url = "https://github.com/CMU-Perceptual-Computing-Lab/openpose/raw/master/models/pose/coco/pose_iter_440000.caffemodel"
|
| 6 |
+
proto_url = "https://raw.githubusercontent.com/CMU-Perceptual-Computing-Lab/openpose/master/models/pose/coco/pose_deploy_linevec.prototxt"
|
| 7 |
+
|
| 8 |
+
print("Downloading pose estimation model...")
|
| 9 |
+
|
| 10 |
+
# Download the model file
|
| 11 |
+
urllib.request.urlretrieve(model_url, "pose_model.caffemodel")
|
| 12 |
+
print("Downloaded pose model")
|
| 13 |
+
|
| 14 |
+
# Download the prototxt file
|
| 15 |
+
urllib.request.urlretrieve(proto_url, "pose_deploy_linevec.prototxt")
|
| 16 |
+
print("Downloaded prototxt file")
|
| 17 |
+
|
| 18 |
+
print("Model files downloaded successfully!")
|
| 19 |
+
|
| 20 |
+
if __name__ == "__main__":
|
| 21 |
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import json
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
import os
|
| 8 |
+
import urllib.request
|
| 9 |
+
|
| 10 |
+
class PilatesVideoEvaluator:
|
| 11 |
+
def __init__(self):
|
| 12 |
+
# Initialize OpenCV pose detection
|
| 13 |
+
self.BODY_PARTS = {
|
| 14 |
+
"Neck": 0, "RShoulder": 1, "RElbow": 2, "RWrist": 3,
|
| 15 |
+
"LShoulder": 4, "LElbow": 5, "LWrist": 6, "RHip": 7, "RKnee": 8,
|
| 16 |
+
"RAnkle": 9, "LHip": 10, "LKnee": 11, "LAnkle": 12
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
# Download the model if it doesn't exist
|
| 20 |
+
if not os.path.exists('pose_model.caffemodel') or not os.path.exists('pose_deploy.prototxt'):
|
| 21 |
+
print("Downloading pose estimation model...")
|
| 22 |
+
model_url = "https://github.com/opencv/opencv_3rdparty/raw/dnn_samples_face_detector_20170830/res10_300x300_ssd_iter_140000.caffemodel"
|
| 23 |
+
proto_url = "https://raw.githubusercontent.com/opencv/opencv/master/samples/dnn/face_detector/deploy.prototxt"
|
| 24 |
+
|
| 25 |
+
urllib.request.urlretrieve(model_url, "pose_model.caffemodel")
|
| 26 |
+
urllib.request.urlretrieve(proto_url, "pose_deploy.prototxt")
|
| 27 |
+
print("Model downloaded successfully!")
|
| 28 |
+
|
| 29 |
+
# Load the model
|
| 30 |
+
self.net = cv2.dnn.readNetFromCaffe("pose_deploy.prototxt", "pose_model.caffemodel")
|
| 31 |
+
|
| 32 |
+
# Evaluation metrics
|
| 33 |
+
self.metrics = {
|
| 34 |
+
'total_frames': 0,
|
| 35 |
+
'pose_detected_frames': 0,
|
| 36 |
+
'movement_consistency': [],
|
| 37 |
+
'balance_scores': [],
|
| 38 |
+
'posture_alignment': [],
|
| 39 |
+
'video_quality_score': 0,
|
| 40 |
+
'exercise_duration': 0,
|
| 41 |
+
'detected_exercises': []
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
def analyze_posture(self, frame):
|
| 45 |
+
"""Analyze posture using OpenCV pose estimation"""
|
| 46 |
+
height, width = frame.shape[:2]
|
| 47 |
+
blob = cv2.dnn.blobFromImage(frame, 1.0/255, (368, 368), (0, 0, 0), swapRB=False, crop=False)
|
| 48 |
+
self.net.setInput(blob)
|
| 49 |
+
output = self.net.forward()
|
| 50 |
+
|
| 51 |
+
# Process the output to get keypoints
|
| 52 |
+
points = []
|
| 53 |
+
for i in range(len(self.BODY_PARTS)):
|
| 54 |
+
# Confidence map for the current keypoint
|
| 55 |
+
probMap = output[0, i, :, :]
|
| 56 |
+
probMap = cv2.resize(probMap, (width, height))
|
| 57 |
+
|
| 58 |
+
# Find global maxima of the probMap
|
| 59 |
+
minVal, prob, minLoc, point = cv2.minMaxLoc(probMap)
|
| 60 |
+
|
| 61 |
+
if prob > 0.1: # Confidence threshold
|
| 62 |
+
points.append((int(point[0]), int(point[1])))
|
| 63 |
+
else:
|
| 64 |
+
points.append(None)
|
| 65 |
+
|
| 66 |
+
return points
|
| 67 |
+
|
| 68 |
+
def detect_exercise_type(self, points):
|
| 69 |
+
"""Detect exercise type based on keypoint positions"""
|
| 70 |
+
if not points or len(points) < 18:
|
| 71 |
+
return "Unknown"
|
| 72 |
+
|
| 73 |
+
# Example: Detect plank position
|
| 74 |
+
if (points[self.BODY_PARTS["RShoulder"]] and points[self.BODY_PARTS["RElbow"]] and
|
| 75 |
+
points[self.BODY_PARTS["LShoulder"]] and points[self.BODY_PARTS["LElbow"]]):
|
| 76 |
+
|
| 77 |
+
r_shoulder = points[self.BODY_PARTS["RShoulder"]]
|
| 78 |
+
r_elbow = points[self.BODY_PARTS["RElbow"]]
|
| 79 |
+
l_shoulder = points[self.BODY_PARTS["LShoulder"]]
|
| 80 |
+
l_elbow = points[self.BODY_PARTS["LElbow"]]
|
| 81 |
+
|
| 82 |
+
# Check if arms are straight (plank position)
|
| 83 |
+
r_arm_angle = self.calculate_angle(r_shoulder, r_elbow)
|
| 84 |
+
l_arm_angle = self.calculate_angle(l_shoulder, l_elbow)
|
| 85 |
+
|
| 86 |
+
if 150 < r_arm_angle < 180 and 150 < l_arm_angle < 180:
|
| 87 |
+
return "Plank"
|
| 88 |
+
|
| 89 |
+
return "Unknown"
|
| 90 |
+
|
| 91 |
+
def calculate_angle(self, point1, point2):
|
| 92 |
+
"""Calculate angle between two points"""
|
| 93 |
+
if not point1 or not point2:
|
| 94 |
+
return 0
|
| 95 |
+
return np.degrees(np.arctan2(point2[1] - point1[1], point2[0] - point1[0]))
|
| 96 |
+
|
| 97 |
+
def process_video(self, video_path):
|
| 98 |
+
"""Process video and analyze exercises"""
|
| 99 |
+
cap = cv2.VideoCapture(video_path)
|
| 100 |
+
if not cap.isOpened():
|
| 101 |
+
raise ValueError("Could not open video file")
|
| 102 |
+
|
| 103 |
+
while cap.isOpened():
|
| 104 |
+
ret, frame = cap.read()
|
| 105 |
+
if not ret:
|
| 106 |
+
break
|
| 107 |
+
|
| 108 |
+
self.metrics['total_frames'] += 1
|
| 109 |
+
|
| 110 |
+
# Analyze posture
|
| 111 |
+
points = self.analyze_posture(frame)
|
| 112 |
+
if points:
|
| 113 |
+
self.metrics['pose_detected_frames'] += 1
|
| 114 |
+
|
| 115 |
+
# Detect exercise type
|
| 116 |
+
exercise_type = self.detect_exercise_type(points)
|
| 117 |
+
if exercise_type != "Unknown":
|
| 118 |
+
self.metrics['detected_exercises'].append(exercise_type)
|
| 119 |
+
|
| 120 |
+
# Calculate metrics
|
| 121 |
+
self.metrics['movement_consistency'].append(self.calculate_movement_consistency(points))
|
| 122 |
+
self.metrics['balance_scores'].append(self.calculate_balance_score(points))
|
| 123 |
+
self.metrics['posture_alignment'].append(self.calculate_posture_alignment(points))
|
| 124 |
+
|
| 125 |
+
cap.release()
|
| 126 |
+
self.calculate_final_metrics()
|
| 127 |
+
|
| 128 |
+
def calculate_movement_consistency(self, points):
|
| 129 |
+
"""Calculate movement consistency score"""
|
| 130 |
+
# Implement movement consistency calculation
|
| 131 |
+
return 0.8 # Placeholder
|
| 132 |
+
|
| 133 |
+
def calculate_balance_score(self, points):
|
| 134 |
+
"""Calculate balance score"""
|
| 135 |
+
# Implement balance score calculation
|
| 136 |
+
return 0.7 # Placeholder
|
| 137 |
+
|
| 138 |
+
def calculate_posture_alignment(self, points):
|
| 139 |
+
"""Calculate posture alignment score"""
|
| 140 |
+
# Implement posture alignment calculation
|
| 141 |
+
return 0.9 # Placeholder
|
| 142 |
+
|
| 143 |
+
def calculate_final_metrics(self):
|
| 144 |
+
"""Calculate final metrics"""
|
| 145 |
+
if self.metrics['total_frames'] > 0:
|
| 146 |
+
self.metrics['video_quality_score'] = (
|
| 147 |
+
self.metrics['pose_detected_frames'] / self.metrics['total_frames']
|
| 148 |
+
) * 100
|
| 149 |
+
|
| 150 |
+
def generate_report(self, output_path):
|
| 151 |
+
"""Generate evaluation report"""
|
| 152 |
+
report = {
|
| 153 |
+
'timestamp': datetime.now().isoformat(),
|
| 154 |
+
'metrics': self.metrics,
|
| 155 |
+
'summary': {
|
| 156 |
+
'video_quality': f"{self.metrics['video_quality_score']:.2f}%",
|
| 157 |
+
'detected_exercises': list(set(self.metrics['detected_exercises'])),
|
| 158 |
+
'average_movement_consistency': np.mean(self.metrics['movement_consistency']),
|
| 159 |
+
'average_balance_score': np.mean(self.metrics['balance_scores']),
|
| 160 |
+
'average_posture_alignment': np.mean(self.metrics['posture_alignment'])
|
| 161 |
+
}
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
with open(output_path, 'w') as f:
|
| 165 |
+
json.dump(report, f, indent=4)
|
| 166 |
+
|
| 167 |
+
def visualize_results(self, output_path):
|
| 168 |
+
"""Visualize evaluation results"""
|
| 169 |
+
plt.figure(figsize=(12, 8))
|
| 170 |
+
|
| 171 |
+
# Plot metrics over time
|
| 172 |
+
plt.subplot(2, 2, 1)
|
| 173 |
+
plt.plot(self.metrics['movement_consistency'], label='Movement Consistency')
|
| 174 |
+
plt.title('Movement Consistency Over Time')
|
| 175 |
+
plt.legend()
|
| 176 |
+
|
| 177 |
+
plt.subplot(2, 2, 2)
|
| 178 |
+
plt.plot(self.metrics['balance_scores'], label='Balance Score')
|
| 179 |
+
plt.title('Balance Score Over Time')
|
| 180 |
+
plt.legend()
|
| 181 |
+
|
| 182 |
+
plt.subplot(2, 2, 3)
|
| 183 |
+
plt.plot(self.metrics['posture_alignment'], label='Posture Alignment')
|
| 184 |
+
plt.title('Posture Alignment Over Time')
|
| 185 |
+
plt.legend()
|
| 186 |
+
|
| 187 |
+
plt.tight_layout()
|
| 188 |
+
plt.savefig(output_path)
|
| 189 |
+
plt.close()
|
| 190 |
+
|
| 191 |
+
def main():
|
| 192 |
+
"""Example usage of the Pilates Video Evaluator"""
|
| 193 |
+
evaluator = PilatesVideoEvaluator()
|
| 194 |
+
|
| 195 |
+
# Replace with your video path
|
| 196 |
+
video_path = "pilates_workout.mp4"
|
| 197 |
+
output_video_path = "analyzed_pilates_workout.mp4"
|
| 198 |
+
report_path = "pilates_evaluation_report.json"
|
| 199 |
+
|
| 200 |
+
try:
|
| 201 |
+
# Process the video
|
| 202 |
+
print("Starting video analysis...")
|
| 203 |
+
evaluator.process_video(video_path)
|
| 204 |
+
|
| 205 |
+
# Print report
|
| 206 |
+
print("\n" + "="*50)
|
| 207 |
+
print("PILATES VIDEO EVALUATION REPORT")
|
| 208 |
+
print("="*50)
|
| 209 |
+
|
| 210 |
+
print(f"Video Quality: {evaluator.metrics['video_quality_score']:.2f}%")
|
| 211 |
+
print(f"Detected Exercises: {', '.join(evaluator.metrics['detected_exercises'])}")
|
| 212 |
+
print(f"Average Movement Consistency: {evaluator.metrics['average_movement_consistency']:.2f}")
|
| 213 |
+
print(f"Average Balance Score: {evaluator.metrics['average_balance_score']:.2f}")
|
| 214 |
+
print(f"Average Posture Alignment: {evaluator.metrics['average_posture_alignment']:.2f}")
|
| 215 |
+
|
| 216 |
+
# Save report and visualization
|
| 217 |
+
evaluator.generate_report(report_path)
|
| 218 |
+
evaluator.visualize_results(output_video_path)
|
| 219 |
+
|
| 220 |
+
except Exception as e:
|
| 221 |
+
print(f"Error processing video: {e}")
|
| 222 |
+
print("Make sure you have the required dependencies installed:")
|
| 223 |
+
print("pip install opencv-python numpy matplotlib")
|
| 224 |
+
|
| 225 |
+
if __name__ == "__main__":
|
| 226 |
+
main()
|
project_2
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
Subproject commit b67aa95aed8dcf03101d5647a2f7bfed9bef7c79
|
requirements.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
requests==2.31.0
|
| 2 |
+
python-dotenv==1.0.0
|
| 3 |
+
pytest==7.4.3
|
| 4 |
+
black==23.11.0
|
| 5 |
+
flake8==6.1.0
|
| 6 |
+
opencv-python==4.9.0.80
|
| 7 |
+
tensorflow==2.16.1
|
| 8 |
+
scikit-image==0.22.0
|
| 9 |
+
matplotlib==3.8.4
|
| 10 |
+
Pillow==10.3.0
|
| 11 |
+
numpy==1.26.4
|
test_pilates.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pilates_evaluator import PilatesVideoEvaluator
|
| 2 |
+
|
| 3 |
+
def test_evaluator():
|
| 4 |
+
evaluator = PilatesVideoEvaluator()
|
| 5 |
+
print("PilatesVideoEvaluator initialized successfully!")
|
| 6 |
+
print("Body parts:", evaluator.BODY_PARTS)
|
| 7 |
+
print("Model loaded:", evaluator.net is not None)
|
| 8 |
+
|
| 9 |
+
if __name__ == "__main__":
|
| 10 |
+
test_evaluator()
|