Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, Response
|
| 2 |
+
import cv2
|
| 3 |
+
import mediapipe as mp
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
|
| 7 |
+
# Initialize webcam and Mediapipe Pose detection
|
| 8 |
+
mp_pose = mp.solutions.pose
|
| 9 |
+
pose = mp_pose.Pose()
|
| 10 |
+
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW) # Use DirectShow for better camera compatibility
|
| 11 |
+
|
| 12 |
+
def generate_frames():
|
| 13 |
+
while True:
|
| 14 |
+
success, frame = cap.read()
|
| 15 |
+
if not success:
|
| 16 |
+
break
|
| 17 |
+
else:
|
| 18 |
+
# Convert BGR to RGB for Mediapipe processing
|
| 19 |
+
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 20 |
+
results = pose.process(rgb_frame)
|
| 21 |
+
|
| 22 |
+
# Draw pose landmarks
|
| 23 |
+
if results.pose_landmarks:
|
| 24 |
+
mp.solutions.drawing_utils.draw_landmarks(
|
| 25 |
+
frame, results.pose_landmarks, mp_pose.POSE_CONNECTIONS
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Encode frame to JPEG format
|
| 29 |
+
ret, buffer = cv2.imencode('.jpg', frame)
|
| 30 |
+
frame = buffer.tobytes()
|
| 31 |
+
|
| 32 |
+
yield (b'--frame\r\n'
|
| 33 |
+
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
|
| 34 |
+
|
| 35 |
+
@app.route('/')
|
| 36 |
+
def index():
|
| 37 |
+
return render_template('index.html') # HTML page to display video
|
| 38 |
+
|
| 39 |
+
@app.route('/video')
|
| 40 |
+
def video():
|
| 41 |
+
return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
|
| 42 |
+
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
app.run(debug=True)
|