Spaces:
Runtime error
Runtime error
Upload 7 files
Browse filesAdded files for deployment
- .gitattributes +3 -0
- Models/left_model.p +3 -0
- Models/pose_model.p +3 -0
- Models/right_model.p +3 -0
- deploy.dockerfile +30 -0
- main.py +262 -0
- requirements.txt +7 -0
- utils.py +178 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
Models/left_model.p filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
Models/pose_model.p filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
Models/right_model.p filter=lfs diff=lfs merge=lfs -text
|
Models/left_model.p
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6a44da6284c1d9758eb640d4d615cba3e20cc79d1490d119bfcc382bc20b7f38
|
| 3 |
+
size 5903929
|
Models/pose_model.p
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1ed16489979ef34ec7500b9b0309e80121542c1592ef02d7b043662885ec6a1e
|
| 3 |
+
size 6322937
|
Models/right_model.p
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ee8d2849f8e1082764f340fa1cea1456c5b780983d937c6f13dbf3f5d4cea180
|
| 3 |
+
size 3584965
|
deploy.dockerfile
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
ENV PYTHONUNBUFFERED=1 \
|
| 4 |
+
PIP_NO_CACHE_DIR=1
|
| 5 |
+
|
| 6 |
+
WORKDIR /app
|
| 7 |
+
|
| 8 |
+
# Install minimal build tools (some packages require compilation)
|
| 9 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 10 |
+
build-essential \
|
| 11 |
+
gcc \
|
| 12 |
+
ca-certificates \
|
| 13 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 14 |
+
|
| 15 |
+
# Install Python deps (cache layer)
|
| 16 |
+
COPY requirements.txt .
|
| 17 |
+
RUN pip install --upgrade pip setuptools wheel && pip install -r requirements.txt
|
| 18 |
+
|
| 19 |
+
# Create a non-root user and set ownership
|
| 20 |
+
RUN useradd -m appuser && mkdir -p /app
|
| 21 |
+
USER appuser
|
| 22 |
+
|
| 23 |
+
# Copy application code
|
| 24 |
+
COPY --chown=appuser:appuser . .
|
| 25 |
+
|
| 26 |
+
# Hugging Face Spaces expects the web process to listen on the provided PORT (defaults to 7860)
|
| 27 |
+
EXPOSE 7860
|
| 28 |
+
|
| 29 |
+
# Use PORT env if set by Spaces, default to 7860; include proxy headers for correct client IPs
|
| 30 |
+
CMD ["sh", "-c", "uvicorn app:app --host 0.0.0.0 --port ${PORT:-7860} --proxy-headers"]
|
main.py
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import warnings
|
| 2 |
+
warnings.filterwarnings("ignore")
|
| 3 |
+
|
| 4 |
+
from flask import Flask, request, jsonify
|
| 5 |
+
import pickle
|
| 6 |
+
import numpy as np
|
| 7 |
+
from utils import Utils
|
| 8 |
+
from matplotlib import pyplot as plt
|
| 9 |
+
import mediapipe as mp
|
| 10 |
+
import cv2
|
| 11 |
+
from queue import Queue
|
| 12 |
+
|
| 13 |
+
import time
|
| 14 |
+
|
| 15 |
+
app = Flask(__name__)
|
| 16 |
+
|
| 17 |
+
# Queue for video frames to be displayed
|
| 18 |
+
frame_queue = Queue(maxsize=10)
|
| 19 |
+
|
| 20 |
+
# Global variables for frame accumulation
|
| 21 |
+
accumulated_probs = None
|
| 22 |
+
frame_count = 0
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def calulating_percentage(avg, all_classes):
|
| 26 |
+
"""
|
| 27 |
+
Given a list of average probabilities (avg) and corresponding class labels (all_classes),
|
| 28 |
+
returns a list of percentage values adjusted by class-specific thresholds.
|
| 29 |
+
"""
|
| 30 |
+
|
| 31 |
+
individual_threshold = {
|
| 32 |
+
"sun": 0.9, "help": 0.9, "teacher": 0.9, "support": 0.9,
|
| 33 |
+
"paper": 0.9, "love": 0.9, "dance": 0.9, "water": 0.9,
|
| 34 |
+
"accident": 0.9, "yes": 0.9, "thick": 0.9, "high": 0.9,
|
| 35 |
+
"poor": 0.9, "i": 0.9, "my": 0.9, "important_1": 0.9,
|
| 36 |
+
"important_2": 0.9, "deaf": 0.9, "winner": 0.9, "eat": 0.9,
|
| 37 |
+
"pizza": 0.9, "go": 0.9, "isl": 0.9, "friend": 0.9,
|
| 38 |
+
"school": 0.9, "deep": 0.9, "loud": 0.9, "flat": 0.9,
|
| 39 |
+
"slow": 0.9, "sad": 0.9, "soft": 0.9, "happy": 0.9,
|
| 40 |
+
"poot": 0.9, "quiet": 0.9, "book": 0.9, "woman": 0.9
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
threshold_percentage = []
|
| 44 |
+
for score, cls in zip(avg, all_classes):
|
| 45 |
+
threshold_val = individual_threshold.get(cls.lower(), 1.0)
|
| 46 |
+
threshold_percentage.append(score * 100 / threshold_val)
|
| 47 |
+
|
| 48 |
+
return threshold_percentage
|
| 49 |
+
|
| 50 |
+
def load_models():
|
| 51 |
+
"""Load all models"""
|
| 52 |
+
left_model_filename = r'Models\left_model.p'
|
| 53 |
+
right_model_filename = r'Models\right_model.p'
|
| 54 |
+
pose_model_filename = r'Models\pose_model.p'
|
| 55 |
+
|
| 56 |
+
def load_model(filename):
|
| 57 |
+
with open(filename, 'rb') as f:
|
| 58 |
+
model_data = pickle.load(f)
|
| 59 |
+
return model_data['model']
|
| 60 |
+
|
| 61 |
+
return (load_model(left_model_filename),
|
| 62 |
+
load_model(right_model_filename),
|
| 63 |
+
load_model(pose_model_filename))
|
| 64 |
+
|
| 65 |
+
# Load models once at startup
|
| 66 |
+
left_model, right_model, pose_model = load_models()
|
| 67 |
+
|
| 68 |
+
# Initialize MediaPipe solutions
|
| 69 |
+
mp_hands = mp.solutions.hands
|
| 70 |
+
mp_pose = mp.solutions.pose
|
| 71 |
+
hands = mp_hands.Hands(max_num_hands=2, min_detection_confidence=0.5, min_tracking_confidence=0.5)
|
| 72 |
+
pose = mp_pose.Pose(min_detection_confidence=0.7, min_tracking_confidence=0.7)
|
| 73 |
+
|
| 74 |
+
# Initialize Utils with axes for palm orientation
|
| 75 |
+
axes = {
|
| 76 |
+
"x": np.array([1, 0, 0]),
|
| 77 |
+
"-x": np.array([-1, 0, 0]),
|
| 78 |
+
"y": np.array([0, 1, 0]),
|
| 79 |
+
"-y": np.array([0, -1, 0]),
|
| 80 |
+
"z": np.array([0, 0, 1]),
|
| 81 |
+
"-z": np.array([0, 0, -1]),
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
utils = Utils(axes)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def process_image(img):
|
| 88 |
+
"""Process a single image (np.array) to get a prediction."""
|
| 89 |
+
global accumulated_probs, frame_count
|
| 90 |
+
|
| 91 |
+
pred = None
|
| 92 |
+
left_probs, right_probs, pose_probs = None, None, None
|
| 93 |
+
|
| 94 |
+
# To improve performance, optionally mark the image as not writeable to
|
| 95 |
+
# pass by reference.
|
| 96 |
+
img.flags.writeable = False
|
| 97 |
+
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 98 |
+
res_pose = pose.process(img_rgb)
|
| 99 |
+
res_hands = hands.process(img_rgb)
|
| 100 |
+
img.flags.writeable = True
|
| 101 |
+
|
| 102 |
+
try:
|
| 103 |
+
# Process hand landmarks (for both left and right hands)
|
| 104 |
+
if res_hands.multi_hand_landmarks:
|
| 105 |
+
for hand_landmarks, handedness in zip(res_hands.multi_hand_landmarks, res_hands.multi_handedness):
|
| 106 |
+
label = handedness.classification[0].label
|
| 107 |
+
features = utils.extract_features(hand_landmarks.landmark, res_pose.pose_landmarks.landmark if res_pose.pose_landmarks else [])
|
| 108 |
+
|
| 109 |
+
if label == 'Left':
|
| 110 |
+
left_prediction = left_model.predict([features])[0]
|
| 111 |
+
left_probs = left_model.predict_proba([features])[0]
|
| 112 |
+
elif label == 'Right':
|
| 113 |
+
right_prediction = right_model.predict([features])[0]
|
| 114 |
+
right_probs = right_model.predict_proba([features])[0]
|
| 115 |
+
|
| 116 |
+
# Process pose landmarks
|
| 117 |
+
if res_pose.pose_landmarks:
|
| 118 |
+
pose_landmarks = res_pose.pose_landmarks
|
| 119 |
+
pose_features = utils.extract_pose_features(pose_landmarks.landmark) # Pass landmark attribute
|
| 120 |
+
pose_prediction = pose_model.predict([pose_features])[0]
|
| 121 |
+
pose_probs = pose_model.predict_proba([pose_features])[0]
|
| 122 |
+
|
| 123 |
+
except Exception as e:
|
| 124 |
+
print(f"Feature extraction error: {e}")
|
| 125 |
+
return {"message": "Feature extraction failed", "frame_count": frame_count}, img
|
| 126 |
+
|
| 127 |
+
# Gathering all class labels used by the three models
|
| 128 |
+
all_classes = sorted(
|
| 129 |
+
set(left_model.classes_).union(
|
| 130 |
+
set(right_model.classes_)).union(
|
| 131 |
+
set(pose_model.classes_))
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
# Aligning probabilities with the master list of classes
|
| 135 |
+
left_probs_aligned = np.zeros(len(all_classes))
|
| 136 |
+
right_probs_aligned = np.zeros(len(all_classes))
|
| 137 |
+
pose_probs_aligned = np.zeros(len(all_classes))
|
| 138 |
+
|
| 139 |
+
if left_probs is not None:
|
| 140 |
+
left_dict = dict(zip(left_model.classes_, left_probs))
|
| 141 |
+
left_probs_aligned = np.array([left_dict.get(cls, 0) for cls in all_classes]) * 100
|
| 142 |
+
|
| 143 |
+
if right_probs is not None:
|
| 144 |
+
right_dict = dict(zip(right_model.classes_, right_probs))
|
| 145 |
+
right_probs_aligned = np.array([right_dict.get(cls, 0) for cls in all_classes]) * 100
|
| 146 |
+
|
| 147 |
+
if pose_probs is not None:
|
| 148 |
+
pose_dict = dict(zip(pose_model.classes_, pose_probs))
|
| 149 |
+
pose_probs_aligned = np.array([pose_dict.get(cls, 0) for cls in all_classes]) * 100
|
| 150 |
+
|
| 151 |
+
# Determine the number of available sources (hand(s)/pose)
|
| 152 |
+
num_sources = sum(prob is not None for prob in [left_probs, right_probs, pose_probs])
|
| 153 |
+
if num_sources == 0:
|
| 154 |
+
return {"message": "No valid data", "frame_count": frame_count}
|
| 155 |
+
|
| 156 |
+
avg = (left_probs_aligned + right_probs_aligned + pose_probs_aligned) / (100 * num_sources)
|
| 157 |
+
|
| 158 |
+
# Convert these averages into final percentages based on custom thresholds
|
| 159 |
+
avg_probs = calulating_percentage(avg, all_classes)
|
| 160 |
+
|
| 161 |
+
if accumulated_probs is None:
|
| 162 |
+
accumulated_probs = np.zeros_like(avg_probs)
|
| 163 |
+
accumulated_probs += avg_probs
|
| 164 |
+
frame_count += 1
|
| 165 |
+
|
| 166 |
+
# Updating the final prediction text after a fixed no. of frames
|
| 167 |
+
if frame_count == 5:
|
| 168 |
+
max_idx = np.argmax(accumulated_probs)
|
| 169 |
+
pred = all_classes[max_idx]
|
| 170 |
+
accumulated_probs = None
|
| 171 |
+
frame_count = 0
|
| 172 |
+
|
| 173 |
+
return {"prediction": pred} if pred else {"message": "Collecting frames", "frame_count": frame_count}, img
|
| 174 |
+
|
| 175 |
+
@app.route('/process_frame', methods=['POST'])
|
| 176 |
+
def process_video_frame():
|
| 177 |
+
"""Receive a video frame, display it, and return a prediction."""
|
| 178 |
+
try:
|
| 179 |
+
if 'frame' not in request.files:
|
| 180 |
+
return jsonify({"status": "error", "message": "No frame file received"}), 400
|
| 181 |
+
|
| 182 |
+
file = request.files['frame']
|
| 183 |
+
img_data = file.read()
|
| 184 |
+
|
| 185 |
+
np_arr = np.frombuffer(img_data, np.uint8)
|
| 186 |
+
img = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
|
| 187 |
+
|
| 188 |
+
if img is None:
|
| 189 |
+
return jsonify({"status": "error", "message": "Failed to decode image"}), 400
|
| 190 |
+
|
| 191 |
+
# Rotate the frame 90 degrees clockwise
|
| 192 |
+
img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
|
| 193 |
+
|
| 194 |
+
if frame_queue.full():
|
| 195 |
+
try:
|
| 196 |
+
# Non-blocking get to remove the oldest frame
|
| 197 |
+
frame_queue.get_nowait()
|
| 198 |
+
except Queue.Empty:
|
| 199 |
+
pass
|
| 200 |
+
|
| 201 |
+
# Process the image to get a prediction
|
| 202 |
+
result, annotated_img = process_image(img)
|
| 203 |
+
|
| 204 |
+
if result.get("prediction"):
|
| 205 |
+
# Clear the queue as we have a final prediction
|
| 206 |
+
while not frame_queue.empty():
|
| 207 |
+
try:
|
| 208 |
+
frame_queue.get_nowait()
|
| 209 |
+
except Queue.Empty:
|
| 210 |
+
break
|
| 211 |
+
|
| 212 |
+
text = f"Prediction: {result['prediction']}"
|
| 213 |
+
cv2.putText(annotated_img, text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
|
| 214 |
+
frame_queue.put(annotated_img)
|
| 215 |
+
|
| 216 |
+
return jsonify({
|
| 217 |
+
"status": "success",
|
| 218 |
+
"prediction": result["prediction"]
|
| 219 |
+
}), 200
|
| 220 |
+
else:
|
| 221 |
+
text = f"{result.get('message', 'Processing')} ({result.get('frame_count', 0)}/5)"
|
| 222 |
+
cv2.putText(annotated_img, text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
|
| 223 |
+
frame_queue.put(annotated_img)
|
| 224 |
+
|
| 225 |
+
return jsonify({
|
| 226 |
+
"status": "collecting",
|
| 227 |
+
"message": result.get("message", "Processing frames"),
|
| 228 |
+
"frame_count": result.get("frame_count", 0)
|
| 229 |
+
}), 200
|
| 230 |
+
|
| 231 |
+
except Exception as e:
|
| 232 |
+
return jsonify({"status": "error", "message": f"Processing error: {str(e)}"}), 500
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
@app.route('/reset', methods=['POST'])
|
| 236 |
+
def reset_accumulation():
|
| 237 |
+
"""Reset the frame accumulation"""
|
| 238 |
+
global accumulated_probs, frame_count
|
| 239 |
+
accumulated_probs = None
|
| 240 |
+
frame_count = 0
|
| 241 |
+
return jsonify({"status": "success", "message": "Accumulation reset"}), 200
|
| 242 |
+
|
| 243 |
+
@app.route('/health', methods=['GET'])
|
| 244 |
+
def health_check():
|
| 245 |
+
"""Health check endpoint"""
|
| 246 |
+
return jsonify({
|
| 247 |
+
"status": "healthy",
|
| 248 |
+
"frame_count": frame_count,
|
| 249 |
+
"models_loaded": True
|
| 250 |
+
}), 200
|
| 251 |
+
|
| 252 |
+
if __name__ == '__main__':
|
| 253 |
+
print("Starting prediction API server...")
|
| 254 |
+
print("Endpoints available:")
|
| 255 |
+
print("- POST /process_frame - Send video frame (multipart) for prediction and display")
|
| 256 |
+
print("- POST /reset - Reset frame accumulation")
|
| 257 |
+
print("- GET /health - Health check")
|
| 258 |
+
print("Press 'q' in the 'Live Stream' window to quit.")
|
| 259 |
+
|
| 260 |
+
# use_reloader=False is important for threads
|
| 261 |
+
app.run(host='0.0.0.0', port=5000, debug=False, threaded=True)
|
| 262 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
numpy
|
| 3 |
+
matplotlib
|
| 4 |
+
mediapipe
|
| 5 |
+
opencv-python-headless
|
| 6 |
+
pickles
|
| 7 |
+
uvicorn
|
utils.py
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
+
def calculate_angle1(self, vec1, vec2):
|
| 11 |
+
dot_product = np.dot(vec1, vec2)
|
| 12 |
+
norm_vec1 = np.linalg.norm(vec1)
|
| 13 |
+
norm_vec2 = np.linalg.norm(vec2)
|
| 14 |
+
self.cosine_angle = dot_product / (norm_vec1 * norm_vec2)
|
| 15 |
+
return self.cosine_angle
|
| 16 |
+
|
| 17 |
+
def get_coordinates_safe(self, landmark, index):
|
| 18 |
+
try:
|
| 19 |
+
return np.array([landmark[index].x, landmark[index].y, landmark[index].z])
|
| 20 |
+
except IndexError:
|
| 21 |
+
return np.array([-1, -1, -1])
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def angle_between_vectors(self, v1, v2):
|
| 25 |
+
dot_product = np.dot(v1, v2)
|
| 26 |
+
magnitude_v1 = np.linalg.norm(v1)
|
| 27 |
+
magnitude_v2 = np.linalg.norm(v2)
|
| 28 |
+
cos_theta = dot_product / (magnitude_v1 * magnitude_v2)
|
| 29 |
+
cos_theta = np.clip(cos_theta, -1.0, 1.0)
|
| 30 |
+
self.theta = np.arccos(cos_theta)
|
| 31 |
+
return np.degrees(self.theta)
|
| 32 |
+
|
| 33 |
+
# Function to classify palm orientation
|
| 34 |
+
def get_palm_orientation(self, normal):
|
| 35 |
+
angles = {axis: self.angle_between_vectors(normal, direction) for axis, direction in self.axes.items()}
|
| 36 |
+
# Find the axis with the smallest angle
|
| 37 |
+
self.best_match_axis = min(angles, key=angles.get)
|
| 38 |
+
return self.best_match_axis
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# Function to extract hand features (angles between vectors and axes)
|
| 42 |
+
def extract_features(self, hand_landmarks, pose_landmarks=None):
|
| 43 |
+
hand_pairs = [
|
| 44 |
+
(1 , 3) , # Thumb
|
| 45 |
+
(6 , 8 ), # Index finger
|
| 46 |
+
(10, 12), # Middle finger
|
| 47 |
+
(14, 16), # Ring finger
|
| 48 |
+
(18, 20), # Pinky finger
|
| 49 |
+
(0 , 9) # Palm direction
|
| 50 |
+
]
|
| 51 |
+
|
| 52 |
+
self.features = []
|
| 53 |
+
for pair in hand_pairs:
|
| 54 |
+
landmark1 = hand_landmarks[pair[0]]
|
| 55 |
+
landmark2 = hand_landmarks[pair[1]]
|
| 56 |
+
|
| 57 |
+
vector = np.array([landmark2.x - landmark1.x, landmark2.y - landmark1.y, landmark2.z - landmark1.z])
|
| 58 |
+
x_axis = np.array([1, 0, 0])
|
| 59 |
+
y_axis = np.array([0, 1, 0])
|
| 60 |
+
z_axis = np.array([0, 0, 1])
|
| 61 |
+
|
| 62 |
+
angle_x = self.calculate_angle1(vector, x_axis)
|
| 63 |
+
angle_y = self.calculate_angle1(vector, y_axis)
|
| 64 |
+
angle_z = self.calculate_angle1(vector, z_axis)
|
| 65 |
+
|
| 66 |
+
self.features.extend([angle_x, angle_y, angle_z])
|
| 67 |
+
|
| 68 |
+
# Safe access to landmarks for 0, 5, and 17
|
| 69 |
+
vector_0_to_5 = self.get_coordinates_safe(hand_landmarks, 5) - self.get_coordinates_safe(hand_landmarks, 0)
|
| 70 |
+
vector_0_to_17 = self.get_coordinates_safe(hand_landmarks, 17) - self.get_coordinates_safe(hand_landmarks, 0)
|
| 71 |
+
|
| 72 |
+
normal_vector = np.cross(vector_0_to_5, vector_0_to_17)
|
| 73 |
+
|
| 74 |
+
normal_angle_x = self.calculate_angle1(normal_vector, x_axis)
|
| 75 |
+
normal_angle_y = self.calculate_angle1(normal_vector, y_axis)
|
| 76 |
+
normal_angle_z = self.calculate_angle1(normal_vector, z_axis)
|
| 77 |
+
|
| 78 |
+
self.features.extend([normal_angle_x, normal_angle_y, normal_angle_z])
|
| 79 |
+
|
| 80 |
+
# If pose landmarks are available, calculate the distance between nose and wrist
|
| 81 |
+
|
| 82 |
+
nose_landmark = self.get_coordinates_safe(pose_landmarks, 0) # Nose is at index 0 in pose landmarks
|
| 83 |
+
wrist_landmark = self.get_coordinates_safe(hand_landmarks, 0) # Wrist is at index 0 in hand landmarks
|
| 84 |
+
|
| 85 |
+
# Calculate the distance in the x and y axes
|
| 86 |
+
distance_x = abs(nose_landmark[0] - wrist_landmark[0])
|
| 87 |
+
distance_y = abs(nose_landmark[1] - wrist_landmark[1])
|
| 88 |
+
|
| 89 |
+
# Append the x and y distances as new features
|
| 90 |
+
self.features.extend([distance_x, distance_y])
|
| 91 |
+
|
| 92 |
+
return self.features
|
| 93 |
+
|
| 94 |
+
#Initialize pose extraction functions
|
| 95 |
+
def calculate_normal_safe(self,p1, p2, p3):
|
| 96 |
+
# Check if any of the points is [-1, -1, -1] (default value for missing landmarks)
|
| 97 |
+
if np.array_equal(p1, [-1, -1, -1]) or np.array_equal(p2, [-1, -1, -1]) or np.array_equal(p3, [-1, -1, -1]):
|
| 98 |
+
return np.array([-1, -1, -1]) # Return [-1, -1, -1] if any point is missing
|
| 99 |
+
else:
|
| 100 |
+
return self.calculate_normal(p1, p2, p3) # Otherwise, calculate the normal as usual
|
| 101 |
+
|
| 102 |
+
# Function to calculate angle between three points
|
| 103 |
+
def calculate_angle2(self,p1, p2, p3):
|
| 104 |
+
# Create vectors from points p1, p2, p3
|
| 105 |
+
v1 = p1 - p2
|
| 106 |
+
v2 = p3 - p2
|
| 107 |
+
|
| 108 |
+
# Calculate the cosine of the angle using dot product
|
| 109 |
+
self.cos_theta = np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))
|
| 110 |
+
|
| 111 |
+
return self.cos_theta
|
| 112 |
+
|
| 113 |
+
# Function to calculate the normal of the plane formed by three points
|
| 114 |
+
def calculate_normal(self, p1, p2, p3):
|
| 115 |
+
# Vectors on the plane
|
| 116 |
+
v1 = p2 - p1
|
| 117 |
+
v2 = p3 - p1
|
| 118 |
+
|
| 119 |
+
# Cross product gives the normal vector
|
| 120 |
+
self.normal = np.cross(v1, v2)
|
| 121 |
+
|
| 122 |
+
# Normalize the normal vector
|
| 123 |
+
self.normal = self.normal / np.linalg.norm(self.normal)
|
| 124 |
+
|
| 125 |
+
return self.normal
|
| 126 |
+
|
| 127 |
+
# Function to calculate the angle between the normal and each of the axes
|
| 128 |
+
def calculate_normal_angles(self,normal):
|
| 129 |
+
# Calculate angles with x, y, z axes
|
| 130 |
+
self.cos_values = []
|
| 131 |
+
for axis in np.eye(3): # x, y, z unit vectors
|
| 132 |
+
cos_value = np.dot(normal, axis)
|
| 133 |
+
self.cos_values.append(cos_value)
|
| 134 |
+
return self.cos_values
|
| 135 |
+
|
| 136 |
+
# Function to calculate the x and y distance between two points
|
| 137 |
+
def calculate_xy_distance(self, p1, p2):
|
| 138 |
+
self.x_distance = abs(p1[0] - p2[0])
|
| 139 |
+
self.y_distance = abs(p1[1] - p2[1])
|
| 140 |
+
return self.x_distance, self.y_distance
|
| 141 |
+
|
| 142 |
+
# Function to extract the pose features
|
| 143 |
+
def extract_pose_features(self, landmarks):
|
| 144 |
+
# Define the landmark indices for the required sets of points (using Pose landmark indices)
|
| 145 |
+
points_sets = {
|
| 146 |
+
"angle_11_12_14": (self.get_coordinates_safe(landmarks, 11), self.get_coordinates_safe(landmarks, 12), self.get_coordinates_safe(landmarks, 14)), # Left shoulder, right shoulder, right elbow
|
| 147 |
+
"angle_12_14_16": (self.get_coordinates_safe(landmarks, 12), self.get_coordinates_safe(landmarks, 11), self.get_coordinates_safe(landmarks, 13)), # Right shoulder, right elbow, right wrist
|
| 148 |
+
"angle_11_13_15": (self.get_coordinates_safe(landmarks, 11), self.get_coordinates_safe(landmarks, 13), self.get_coordinates_safe(landmarks, 15)), # Left shoulder, left elbow, left wrist
|
| 149 |
+
"angle_13_15_17": (self.get_coordinates_safe(landmarks, 12), self.get_coordinates_safe(landmarks, 14), self.get_coordinates_safe(landmarks, 16)), # Left elbow, left wrist, left hand
|
| 150 |
+
"normal_1": (self.get_coordinates_safe(landmarks, 15), self.get_coordinates_safe(landmarks, 17), self.get_coordinates_safe(landmarks, 19)), # Plane formed by left shoulder, left hip, left knee
|
| 151 |
+
"normal_2": (self.get_coordinates_safe(landmarks, 16), self.get_coordinates_safe(landmarks, 18), self.get_coordinates_safe(landmarks, 20)) # Plane formed by right shoulder, right hip, right knee
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
# Calculate the angles between the specific sets of points
|
| 155 |
+
self.angles = []
|
| 156 |
+
for key, (p1, p2, p3) in points_sets.items():
|
| 157 |
+
if key.startswith("angle"):
|
| 158 |
+
angle = self.calculate_angle2(p1, p2, p3)
|
| 159 |
+
self.angles.append(angle)
|
| 160 |
+
|
| 161 |
+
# Calculate normals and angles with axes
|
| 162 |
+
for key, (p1, p2, p3) in points_sets.items():
|
| 163 |
+
if key.startswith("normal"):
|
| 164 |
+
normal = self.calculate_normal_safe(p1, p2, p3) # Safe normal calculation
|
| 165 |
+
if np.array_equal(normal, [-1, -1, -1]):
|
| 166 |
+
# If normal is [-1, -1, -1], it indicates missing points, so append [-1, -1, -1] for each axis angle
|
| 167 |
+
self.angles.extend([-1, -1, -1])
|
| 168 |
+
else:
|
| 169 |
+
normal_angles = self.calculate_normal_angles(normal)
|
| 170 |
+
self.angles.extend(normal_angles) # Append angles with x, y, z axes
|
| 171 |
+
|
| 172 |
+
# Add the distance between points 15 (left wrist) and 16 (right wrist)
|
| 173 |
+
p15 = self.get_coordinates_safe(landmarks, 15) # Left wrist
|
| 174 |
+
p16 = self.get_coordinates_safe(landmarks, 16) # Right wrist
|
| 175 |
+
x_distance, y_distance = self.calculate_xy_distance(p15, p16)
|
| 176 |
+
self.angles.extend([x_distance, y_distance]) # Append x and y distance to the feature list
|
| 177 |
+
|
| 178 |
+
return self.angles
|