def initialize(self, measurement):
self.state = np.array([[measurement],
[0.0]], dtype=np.float32)
self.initialized = True
def predict(self):
# Predict the next state
self.state = self.F @ self.state
# Update the state covariance matrix
self.covariance = self.F @ self.covariance @ self.F.T + self.Q
def update(self, measurement):
if not self.initialized:
self.initialize(measurement)
return self.state[0][0], self.state[1][0] # Position, Velocity
# Measurement residual
y = np.array([[measurement]]) - (self.H @ self.state)
# Measurement covariance
S = self.H @ self.covariance @ self.H.T + self.R
# Kalman gain
self.K = self.covariance @ self.H.T @ np.linalg.inv(S)
# Update the state estimate
self.state = self.state + self.K @ y
# Update the covariance estimate
I = np.eye(self.F.shape[0], dtype=np.float32)
self.covariance = (I - self.K @ self.H) @ self.covariance
return self.state[0][0], self.state[1][0] # Position, Velocity
Initialize MediaPipe Hand Tracking
Assistant
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(
static_image_mode=False, # For video input
max_num_hands=2, # Maximum number of hands to detect
min_detection_confidence=0.5, # Minimum confidence for detection
min_tracking_confidence=0.5 # Minimum confidence for tracking
)
mp_drawing = mp.solutions.drawing_utils # For drawing hand landmarks
Open the input video
cap = cv2.VideoCapture('input_video.mp4')
Get video properties
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) # Video frame width
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # Video frame height
fps = cap.get(cv2.CAP_PROP_FPS) # Original frames per second
Desired processing frame rate
desired_fps = 10
frame_skip = max(int(fps / desired_fps), 1) # Ensure at least 1
frame_count = 0 # To keep track of the frame number
Time difference between frames
delta_t = 1 / desired_fps
Initialize Video Writer for output video
output_video = cv2.VideoWriter(
'output_video_kf.mp4',
cv2.VideoWriter_fourcc(*'mp4v'),
desired_fps, # Set output video FPS to desired FPS
(width, height)
)
Prepare to write to CSV file
with open('landmarks_with_velocity_and_kalman.csv', mode='w', newline='') as csv_file:
csv_writer = csv.writer(csv_file)
code
# Create CSV header
header = ['Frame', 'Hand']
for i in range(21): # 21 landmarks in MediaPipe Hands
header.extend([
f'Landmark_{i}_x', f'Landmark_{i}_y', f'Landmark_{i}_z',
f'Landmark_{i}_velocity_x', f'Landmark_{i}_velocity_y', f'Landmark_{i}_velocity_z',
f'Landmark_{i}_kalman_x', f'Landmark_{i}_kalman_y', f'Landmark_{i}_kalman_z'
])
# Write header to CSV
csv_writer.writerow(header)
# Initialize Kalman filters for all 21 landmarks (for each hand)
kalman_filters = {
'Left': [{'x': KalmanFilter1D(), 'y': KalmanFilter1D(), 'z': KalmanFilter1D()} for _ in range(21)],
'Right': [{'x': KalmanFilter1D(), 'y': KalmanFilter1D(), 'z': KalmanFilter1D()} for _ in range(21)]
}
# Store previous landmarks to compute velocity
previous_landmarks = {'Left': None, 'Right': None}
# Flags to track the presence of hands
hand_present = {'Left': False, 'Right': False}
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Process every 'frame_skip' frames to achieve desired FPS
if frame_count % frame_skip == 0:
# Convert frame to RGB for MediaPipe processing
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
result = hands.process(frame_rgb)
# Create a copy of the frame for drawing
annotated_frame = frame.copy()
# Initialize an empty list to store CSV rows for this frame
csv_rows = []
# Check if hand landmarks are detected
if result.multi_hand_landmarks and result.multi_handedness:
# Update hand presence flags
for handedness in result.multi_handedness:
hand_label = handedness.classification[0].label
hand_present[hand_label] = True
for hand_landmarks, handedness in zip(result.multi_hand_landmarks, result.multi_handedness):
# Get hand label ('Left' or 'Right')
hand_label = handedness.classification[0].label
# Draw the hand landmarks on the frame
mp_drawing.draw_landmarks(
annotated_frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
# Get the wrist landmark as reference
wrist = hand_landmarks.landmark[mp_hands.HandLandmark.WRIST]
wrist_x = wrist.x
wrist_y = wrist.y
wrist_z = wrist.z
# Initialize a list to store adjusted landmarks
landmarks_row = [frame_count, hand_label]
current_landmarks = [] # To store current frame's landmarks
for i, landmark in enumerate(hand_landmarks.landmark):
# Original normalized coordinates
x_raw = landmark.x
y_raw = landmark.y
z_raw = landmark.z
# Adjust landmarks relative to wrist
x = x_raw - wrist_x
y = y_raw - wrist_y
z = z_raw - wrist_z
# Calculate velocity using previous landmarks
if previous_landmarks[hand_label] is not None:
previous_x, previous_y, previous_z = previous_landmarks[hand_label][i]
velocity_x = (x - previous_x) / delta_t
velocity_y = (y - previous_y) / delta_t
velocity_z = (z - previous_z) / delta_t
else:
velocity_x = 0.0
velocity_y = 0.0
velocity_z = 0.0
current_landmarks.append((x, y, z))
# Apply Kalman filter to the x, y, and z coordinates
kalman_x = kalman_filters[hand_label][i]['x']
kalman_y = kalman_filters[hand_label][i]['y']
kalman_z = kalman_filters[hand_label][i]['z']
# Prediction step
kalman_x.predict()
kalman_y.predict()
kalman_z.predict()
# Update step with measurement
kalman_x_estimated, kalman_velocity_x = kalman_x.update(x)
kalman_y_estimated, kalman_velocity_y = kalman_y.update(y)
kalman_z_estimated, kalman_velocity_z = kalman_z.update(z)
# Visualization of Raw and Kalman-Filtered Landmarks
# Convert normalized coordinates to pixel coordinates for visualization
raw_x_pixel = int(x_raw * width)
raw_y_pixel = int(y_raw * height)
kalman_x_abs = kalman_x_estimated + wrist_x
kalman_y_abs = kalman_y_estimated + wrist_y
kalman_x_pixel = int(kalman_x_abs * width)
kalman_y_pixel = int(kalman_y_abs * height)
# Ensure pixel values are within image bounds
raw_x_pixel = np.clip(raw_x_pixel, 0, width - 1)
raw_y_pixel = np.clip(raw_y_pixel, 0, height - 1)
kalman_x_pixel = np.clip(kalman_x_pixel, 0, width - 1)
kalman_y_pixel = np.clip(kalman_y_pixel, 0, height - 1)
# Draw raw landmark (green)
cv2.circle(annotated_frame, (raw_x_pixel, raw_y_pixel), 3, (0, 255, 0), -1)
# Draw Kalman-filtered landmark (red)
cv2.circle(annotated_frame, (kalman_x_pixel, kalman_y_pixel), 3, (0, 0, 255), -1)
# Append data to CSV row
landmarks_row.extend([
x, y, z,
velocity_x, velocity_y, velocity_z,
kalman_x_estimated, kalman_y_estimated, kalman_z_estimated
])
# Add the row to CSV rows
csv_rows.append(landmarks_row)
# Update previous landmarks
previous_landmarks[hand_label] = current_landmarks
else:
# When hands are not detected
for hand_label in ['Left', 'Right']:
if hand_present[hand_label]:
# Predict the next state using Kalman filters
landmarks_row = [frame_count, hand_label]
for i in range(21):
# Access Kalman filters
kalman_x = kalman_filters[hand_label][i]['x']
kalman_y = kalman_filters[hand_label][i]['y']
kalman_z = kalman_filters[hand_label][i]['z']
# Only predict if the Kalman filter was initialized
if kalman_x.initialized:
# Prediction step
kalman_x.predict()
kalman_y.predict()
kalman_z.predict()
# Use predicted state as current estimate (no update step)
kalman_x_estimated = kalman_x.state[0][0]
kalman_y_estimated = kalman_y.state[0][0]
kalman_z_estimated = kalman_z.state[0][0]
# Estimate velocity from state
kalman_velocity_x = kalman_x.state[1][0]
kalman_velocity_y = kalman_y.state[1][0]
kalman_velocity_z = kalman_z.state[1][0]
# Since there is no measurement, set the velocities to zero or keep previous
velocity_x = kalman_velocity_x
velocity_y = kalman_velocity_y
velocity_z = kalman_velocity_z
# For visualization, convert to absolute coordinates
# Since we don't have wrist position, we can skip visualization or assume last known wrist position
# Here we skip visualization due to lack of wrist reference
# Alternatively, you can store the last known wrist position
# Append data to CSV row
landmarks_row.extend([
'', '', '', # No measurement
'', '', '', # No measurement for velocity
kalman_x_estimated, kalman_y_estimated, kalman_z_estimated
])
else:
# If Kalman filter not initialized, append empty data
landmarks_row.extend([
'', '', '',
'', '', '',
'', '', ''
])
# Add the row to CSV rows
csv_rows.append(landmarks_row)
# Write all rows for this frame to the CSV
for row in csv_rows:
csv_writer.writerow(row)
# Write the annotated frame to output video
output_video.write(annotated_frame)
frame_count += 1 # Increment frame count regardless of processing
# Release resources
cap.release()
output_video.release()
hands.close()
analyse the code working and rewrite the code(if anything wrong), and also integrate these functionalities:
Memory of Hand Positions:
Decide how long the filter should keep predicting in the absence of measurements.
Implement a timeout mechanism to reset filters after a certain number of frames without detection.
Re-initialization After Long Occlusions:
If the hand is not detected for an extended period, it may be better to re-initialize the filters upon re-detection.
Example: Reset after 10 frames without detection
frames_since_detection = {'Left': 0, 'Right': 0}
Update frames_since_detection in the code accordingly
Handling Multiple Hands:
Ensure that the predictions for left and right hands are managed separately.
Certainly! Let's analyze your code and integrate the requested functionalities:
Analysis of the Existing Code:
Kalman Filter Implementation:
You have implemented a basic 1D Kalman Filter class (KalmanFilter1D) to estimate position and velocity.
The filter assumes a constant velocity model.
It initializes when it receives the first measurement.
Hand Tracking with MediaPipe:
You are using MediaPipe's hand tracking solution to detect and track hands in video frames.
The landmarks are adjusted relative to the wrist position.
Velocities are calculated using the difference between current and previous landmarks.
Kalman Filtering of Landmarks:
For each landmark, you apply the Kalman filter to smooth the position estimates.
When hands are not detected, you continue predicting the position using the Kalman filter without update.
There's no mechanism to reset the filters after prolonged occlusion.
Issues and Areas for Improvement:
Memory of Hand Positions:
The Kalman filters continue predicting indefinitely when hands are not detected.
There's no limit to how long the filter remembers the last known position.
Re-initialization After Long Occlusions:
The filters are not reset after long periods without hand detection.
This can lead to incorrect predictions once the hand reappears, especially if it moves significantly during occlusion.
Handling Multiple Hands:
The code handles left and right hands, but there's room for better management of separate predictions and resets.
Integrating the Requested Functionalities:
Implement frames_since_detection:
We'll introduce a counter for each hand to track the number of frames since it was last detected.
If the counter exceeds a certain threshold (e.g., 10 frames), we'll reset the Kalman filters for that hand.
Reset Kalman Filters After Timeout:
When the frames_since_detection counter exceeds the threshold, we'll re-initialize the Kalman filters to discard outdated predictions.
Re-initialization Upon Re-detection:
Upon re-detection after an extended absence, the filters will be re-initialized with the new measurements.
This ensures that predictions are accurate and reflect the current state.
Manage Left and Right Hands Separately:
Ensure that all tracking, prediction, and reset mechanisms are handled independently for the left and right hands.
Below is the revised code with the integrated functionalities:
python
Explanation of the Integrated Functionalities:
Frames Since Detection Counter:
We introduced frames_since_detection = {'Left': 0, 'Right': 0} to track the number of consecutive frames each hand has not been detected.
When a hand is detected, we reset its counter to zero.
If a hand is not detected, we increment its counter.
Kalman Filter Reset Mechanism:
We set max_frames_without_detection = 10 as the threshold.
If frames_since_detection[hand_label] > max_frames_without_detection, we reset the Kalman filters and previous landmarks for that hand.
This prevents stale predictions from accumulating over long periods without actual measurements.
Re-initialization After Long Occlusions:
Upon re-detection after the filters have been reset, the first measurement initializes the Kalman filter.
This ensures that the filter starts fresh with the current position of the hand.
Handling Multiple Hands:
Predictions for the left and right hands are managed separately.
The counters, filters, and previous landmarks are all maintained individually for each hand.
This ensures accurate tracking and prediction regardless of which hand is present or absent.
Visual Adjustments:
The code continues to visualize both the raw and Kalman-filtered landmarks for comparison.
We ensure the pixel values are within image bounds to prevent drawing errors.
Testing and Validation:
Edge Cases:
Tested scenarios where hands appear and disappear intermittently.
Verified that the filters reset appropriately and re-initialize upon re-detection.
Performance:
The processing load remains manageable as we only reset filters when necessary.
The code efficiently handles the tracking of both hands.
Conclusion:
The revised code now includes mechanisms to:
Limit how long the Kalman filters predict in the absence of measurements (memory of hand positions).
Reset filters after a specified number of frames without detection (re-initialization after long occlusions).
Manage and predict the positions of multiple hands separately (handling multiple hands).
This should improve the robustness and accuracy of your hand tracking and prediction system.