File size: 1,579 Bytes
5a45411
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import cv2
import joblib
import mediapipe as mp
import numpy as np
from streamlit_webrtc import webrtc_streamer, VideoTransformerBase

# Load the 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()

class PoseTransformer(VideoTransformerBase):
    def transform(self, frame):
        img = frame.to_ndarray(format="bgr24")  # Convert to BGR format
        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)

            # Predict pose
            y_pred = model.predict(pose_data)
            predicted_label = label_encoder.inverse_transform(y_pred)[0]

            # Display predicted label
            cv2.putText(img, f"Pose: {predicted_label}", (20, 50),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 3)

        return cv2.cvtColor(img, cv2.COLOR_RGB2BGR)

# Streamlit UI
st.title("Live Pose Classification")
st.write("This application detects human poses in real-time.")

# Start WebRTC Stream
webrtc_streamer(key="pose-detection", video_transformer_factory=PoseTransformer)