Spaces:
Running
Running
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, Response, request, jsonify
|
| 2 |
+
from flask_socketio import SocketIO
|
| 3 |
+
from flask_cors import CORS
|
| 4 |
+
import mediapipe as mp
|
| 5 |
+
import cv2
|
| 6 |
+
import numpy as np
|
| 7 |
+
import time
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
app = Flask(__name__)
|
| 11 |
+
socketio = SocketIO(app)
|
| 12 |
+
CORS(app)
|
| 13 |
+
|
| 14 |
+
mp_pose = mp.solutions.pose
|
| 15 |
+
pose = mp_pose.Pose(static_image_mode=False, min_detection_confidence=0.5, min_tracking_confidence=0.5)
|
| 16 |
+
|
| 17 |
+
previous_keypoints = None
|
| 18 |
+
previous_velocities = None
|
| 19 |
+
previous_time = time.time()
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
@app.route('/')
|
| 23 |
+
def index():
|
| 24 |
+
return render_template('index.html')
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@app.route('/web_app')
|
| 28 |
+
def web_app():
|
| 29 |
+
return render_template('holistic.html')
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
@app.route('/video_app')
|
| 33 |
+
def video_app():
|
| 34 |
+
return render_template('video_app.html')
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def process_frame(frame):
|
| 38 |
+
global previous_keypoints, previous_time, previous_velocities
|
| 39 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 40 |
+
results = pose.process(frame_rgb)
|
| 41 |
+
|
| 42 |
+
if results.pose_landmarks:
|
| 43 |
+
landmarks = results.pose_landmarks.landmark
|
| 44 |
+
keypoints = [(lm.x, lm.y, lm.z) for lm in landmarks]
|
| 45 |
+
current_time = time.time()
|
| 46 |
+
delta_time = current_time - previous_time
|
| 47 |
+
|
| 48 |
+
velocities = []
|
| 49 |
+
accelerations = []
|
| 50 |
+
if previous_keypoints:
|
| 51 |
+
for i, lm in enumerate(landmarks):
|
| 52 |
+
dx = lm.x - previous_keypoints[i][0]
|
| 53 |
+
dy = lm.y - previous_keypoints[i][1]
|
| 54 |
+
dz = lm.z - previous_keypoints[i][2]
|
| 55 |
+
speed = np.sqrt(dx ** 2 + dy ** 2 + dz ** 2) / delta_time
|
| 56 |
+
velocities.append(speed)
|
| 57 |
+
|
| 58 |
+
acceleration = (speed - previous_velocities[i]) / delta_time if previous_velocities else 0
|
| 59 |
+
accelerations.append(acceleration)
|
| 60 |
+
|
| 61 |
+
previous_keypoints = keypoints
|
| 62 |
+
previous_velocities = velocities
|
| 63 |
+
previous_time = current_time
|
| 64 |
+
|
| 65 |
+
return {
|
| 66 |
+
'landmarks': keypoints,
|
| 67 |
+
'velocities': velocities,
|
| 68 |
+
'accelerations': accelerations
|
| 69 |
+
}
|
| 70 |
+
return None
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
@socketio.on('process_frame')
|
| 74 |
+
def handle_process_frame(data):
|
| 75 |
+
frame = cv2.imdecode(np.frombuffer(data['frame'], np.uint8), cv2.IMREAD_COLOR)
|
| 76 |
+
result = process_frame(frame)
|
| 77 |
+
if result:
|
| 78 |
+
socketio.emit('pose_data', result)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
@app.route('/upload_video', methods=['POST'])
|
| 82 |
+
def upload_video():
|
| 83 |
+
file = request.files.get('video')
|
| 84 |
+
if file:
|
| 85 |
+
upload_folder = os.path.join(app.root_path, 'static', 'uploads')
|
| 86 |
+
if not os.path.exists(upload_folder):
|
| 87 |
+
os.makedirs(upload_folder)
|
| 88 |
+
video_path = os.path.join(upload_folder, 'temp_video.mp4')
|
| 89 |
+
file.save(video_path)
|
| 90 |
+
return jsonify(success=True, message='Video uploaded successfully')
|
| 91 |
+
return jsonify(success=False, message='No video file received'), 400
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
@app.route('/video_feed')
|
| 95 |
+
def video_feed():
|
| 96 |
+
def generate_frames():
|
| 97 |
+
video_path = os.path.join(app.root_path, 'static', 'uploads', 'temp_video.mp4')
|
| 98 |
+
if not os.path.exists(video_path):
|
| 99 |
+
return
|
| 100 |
+
|
| 101 |
+
cap = cv2.VideoCapture(video_path)
|
| 102 |
+
while True:
|
| 103 |
+
success, frame = cap.read()
|
| 104 |
+
if not success:
|
| 105 |
+
break
|
| 106 |
+
else:
|
| 107 |
+
result = process_frame(frame)
|
| 108 |
+
if result:
|
| 109 |
+
socketio.emit('pose_data', result)
|
| 110 |
+
ret, buffer = cv2.imencode('.jpg', frame)
|
| 111 |
+
frame = buffer.tobytes()
|
| 112 |
+
yield (b'--frame\r\n'
|
| 113 |
+
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
|
| 114 |
+
cap.release()
|
| 115 |
+
|
| 116 |
+
return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
if __name__ == '__main__':
|
| 120 |
+
os.makedirs(os.path.join(app.root_path, 'static', 'uploads'), exist_ok=True)
|
| 121 |
+
socketio.run(app, host='0.0.0.0', port=5000, debug=True, allow_unsafe_werkzeug=True)
|