Spaces:
Running
Running
Create app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344from flask import Flask, render_template, Responseimport cv2import mediapipe as mp
app = Flask(__name__)
# Initialize webcam and Mediapipe Pose detectionmp_pose = mp.solutions.posepose = mp_pose.Pose()cap = cv2.VideoCapture(0, cv2.CAP_DSHOW) # Use DirectShow for better camera compatibility
def generate_frames(): while True: success, frame = cap.read() if not success: break else: # Convert BGR to RGB for Mediapipe processing rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) results = pose.process(rgb_frame)
# Draw pose landmarks if results.pose_landmarks: mp.solutions.drawing_utils.draw_landmarks( frame, results.pose_landmarks, mp_pose.POSE_CONNECTIONS )
# Encode frame to JPEG format ret, buffer = cv2.imencode('.jpg', frame) frame = buffer.tobytes()
yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
@app.route('/')def index(): return render_template('index.html') # HTML page to display video
@app.route('/video')def video(): return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == "__main__": app.run(debug=True)app.pyAdded+44