import streamlit as st import cv2 import joblib import mediapipe as mp import numpy as np # Load trained model and label encoder model = joblib.load("pose_classifier.joblib") label_encoder = joblib.load("label_encoder.joblib") # Initialize MediaPipe Pose mp_pose = mp.solutions.pose pose = mp_pose.Pose() # Streamlit UI st.title("Live Pose Classification") st.write("Real-time pose detection using OpenCV and MediaPipe.") # OpenCV Video Capture cap = cv2.VideoCapture(0) # Streamlit Image Display frame_placeholder = st.empty() while cap.isOpened(): ret, frame = cap.read() if not ret: st.warning("Failed to capture video. Check your camera.") break # Convert frame to RGB img_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Process frame with MediaPipe Pose results = pose.process(img_rgb) if results.pose_landmarks: landmarks = results.pose_landmarks.landmark pose_data = [j.x for j in landmarks] + [j.y for j in landmarks] + \ [j.z for j in landmarks] + [j.visibility for j in landmarks] pose_data = np.array(pose_data).reshape(1, -1) # Predict pose y_pred = model.predict(pose_data) predicted_label = label_encoder.inverse_transform(y_pred)[0] # Display predicted label cv2.putText(frame, f"Pose: {predicted_label}", (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 3) # Display frame in Streamlit frame_placeholder.image(frame, channels="BGR") # Break loop if user stops execution if st.button("Stop Camera"): break cap.release() cv2.destroyAllWindows()