Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,13 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
import joblib
|
4 |
import mediapipe as mp
|
5 |
import numpy as np
|
6 |
-
|
7 |
|
8 |
-
|
|
|
|
|
9 |
model = joblib.load("pose_classifier.joblib")
|
10 |
label_encoder = joblib.load("label_encoder.joblib")
|
11 |
|
@@ -13,38 +15,47 @@ label_encoder = joblib.load("label_encoder.joblib")
|
|
13 |
mp_pose = mp.solutions.pose
|
14 |
pose = mp_pose.Pose()
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
img = frame.to_ndarray(format="bgr24")
|
23 |
|
24 |
-
|
25 |
-
|
|
|
|
|
26 |
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
|
35 |
-
|
|
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
|
41 |
-
|
42 |
-
cv2.putText(img, f"Pose: {predicted_label}", (20, 50),
|
43 |
-
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 3)
|
44 |
-
except Exception as e:
|
45 |
-
st.warning(f"⚠️ Prediction Error: {e}")
|
46 |
|
47 |
-
|
|
|
48 |
|
49 |
-
#
|
50 |
-
webrtc_streamer(key="pose-classification", video_transformer_factory=PoseClassification)
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
import cv2
|
3 |
import joblib
|
4 |
import mediapipe as mp
|
5 |
import numpy as np
|
6 |
+
import tempfile
|
7 |
|
8 |
+
app = Flask(__name__)
|
9 |
+
|
10 |
+
# Load model and label encoder
|
11 |
model = joblib.load("pose_classifier.joblib")
|
12 |
label_encoder = joblib.load("label_encoder.joblib")
|
13 |
|
|
|
15 |
mp_pose = mp.solutions.pose
|
16 |
pose = mp_pose.Pose()
|
17 |
|
18 |
+
def predict_pose_from_image(image_bytes):
|
19 |
+
# Convert image bytes to numpy array
|
20 |
+
nparr = np.frombuffer(image_bytes, np.uint8)
|
21 |
+
frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
22 |
+
|
23 |
+
if frame is None:
|
24 |
+
return None, "Invalid image"
|
25 |
+
|
26 |
+
# Convert to RGB
|
27 |
+
img_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
28 |
|
29 |
+
# Run MediaPipe Pose
|
30 |
+
results = pose.process(img_rgb)
|
|
|
31 |
|
32 |
+
if results.pose_landmarks:
|
33 |
+
landmarks = results.pose_landmarks.landmark
|
34 |
+
pose_data = [j.x for j in landmarks] + [j.y for j in landmarks] + \
|
35 |
+
[j.z for j in landmarks] + [j.visibility for j in landmarks]
|
36 |
|
37 |
+
pose_data = np.array(pose_data).reshape(1, -1)
|
38 |
+
y_pred = model.predict(pose_data)
|
39 |
+
predicted_label = label_encoder.inverse_transform(y_pred)[0]
|
40 |
+
return predicted_label, None
|
41 |
+
else:
|
42 |
+
return None, "No pose detected"
|
43 |
|
44 |
+
@app.route('/predict-pose', methods=['POST'])
|
45 |
+
def predict_pose():
|
46 |
+
if 'frame' not in request.files:
|
47 |
+
return jsonify({"error": "No image frame uploaded"}), 400
|
48 |
|
49 |
+
file = request.files['frame']
|
50 |
+
img_bytes = file.read()
|
51 |
|
52 |
+
label, error = predict_pose_from_image(img_bytes)
|
53 |
+
if error:
|
54 |
+
return jsonify({"error": error}), 400
|
55 |
|
56 |
+
return jsonify({"predicted_pose": label})
|
|
|
|
|
|
|
|
|
57 |
|
58 |
+
if __name__ == "__main__":
|
59 |
+
app.run(debug=True, port=5007)
|
60 |
|
61 |
+
# curl -X POST -F "frame=@your_image.jpg" http://localhost:5007/predict-pose
|
|