Spaces:
Sleeping
Sleeping
File size: 1,725 Bytes
5a45411 662b753 5a45411 662b753 5a45411 d5b1e39 5a45411 d5b1e39 662b753 5a45411 662b753 5a45411 662b753 5a45411 662b753 5a45411 662b753 5a45411 662b753 5a45411 662b753 5a45411 662b753 d5b1e39 662b753 d5b1e39 662b753 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import streamlit as st
import av
import joblib
import mediapipe as mp
import numpy as np
from streamlit_webrtc import webrtc_streamer, VideoTransformerBase
# 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 on Hugging Face Spaces")
st.write("Using Streamlit WebRTC, OpenCV, and MediaPipe.")
class PoseClassification(VideoTransformerBase):
def transform(self, frame):
img = frame.to_ndarray(format="bgr24")
# Convert frame to RGB
img_rgb = cv2.cvtColor(img, 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)
try:
y_pred = model.predict(pose_data)
predicted_label = label_encoder.inverse_transform(y_pred)[0]
# Draw label on frame
cv2.putText(img, f"Pose: {predicted_label}", (20, 50),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 3)
except Exception as e:
st.warning(f"⚠️ Prediction Error: {e}")
return av.VideoFrame.from_ndarray(img, format="bgr24")
# Start WebRTC streamer
webrtc_streamer(key="pose-classification", video_transformer_factory=PoseClassification) |