MediaPipe is a framework developed by Google that provides solutions for various computer vision tasks such as object detection, facial recognition, and pose estimation. Pose estimation involves identifying the keypoints of a human body in an image or video stream.
For sit and stand detection, we can use MediaPipe's pose estimation model to identify the keypoints of a person's body, and then use the angle between specific keypoints to determine if the person is sitting or standing. Here are the 33 keypoints that MediaPipe identifies for pose estimation:
Nose
Left eye inner
Left eye
Left eye outer
Right eye inner
Right eye
Right eye outer
Left ear
Right ear
Mouth left
Mouth right
Left shoulder
Right shoulder
Left elbow
Right elbow
Left wrist
Right wrist
Left pinky finger
Right pinky finger
Left index finger
Right index finger
Left thumb
Right thumb
Left hip
Right hip
Left knee
Right knee
Left ankle
Right ankle
Left heel
Right heel
Left foot index
Right foot index
To use these keypoints for sit and stand detection, we can focus on the position of the hips and knees. When a person is standing, their hips and knees are generally at a wider angle than when they are sitting. Specifically, we can use the angle between keypoints 11-13-15 (left shoulder, left elbow, left wrist) and keypoints 12-14-16 (right shoulder, right elbow, right wrist) to determine if the person is standing or sitting. If the angle between these keypoints is less than a certain threshold, we can assume that the person is sitting. If the angle is greater than the threshold, we can assume that the person is standing.
The specific threshold may vary depending on the camera angle and other factors, so some experimentation may be necessary to determine the best value for your specific use case.
User
can you please tell how to find angle
Assistant
Sure! To find the angle between two line segments defined by three keypoints in MediaPipe, you can use the dot product and the law of cosines. Here are the steps:
Find the vectors for the two line segments. For example, to find the vector for the left arm (shoulder to wrist), you can subtract the coordinates of the shoulder keypoint from the coordinates of the wrist keypoint:
Calculate the angle in radians using the inverse cosine (arccosine) function:
lua
angle = math.acos(cosine_angle)
Convert the angle to degrees if needed:
lua
angle_degrees = angle * 180 / math.pi
Note that the order of the keypoints matters when calculating the angle. For example, to calculate the angle between the left arm and the torso, you would use keypoints 11 (left shoulder), 13 (left elbow), and 15 (left wrist) in that order. If you use them in a different order, you may get a different angle.
User
please add this to whole code
Assistant
Sure! Here's an example Python code snippet that demonstrates how to find the angle between the left arm and the torso using MediaPipe and OpenCV:
python
import cv2
import mediapipe as mp
import math
mp_drawing = mp.solutions.drawing_utils
mp_pose = mp.solutions.pose
# Initialize MediaPipe Pose model
pose = mp_pose.Pose(static_image_mode=True, min_detection_confidence=0.5)
# Initialize OpenCV VideoCapture object to read from webcam
cap = cv2.VideoCapture(0)
while cap.isOpened():
# Read frame from webcam
success, image = cap.read()
if not success:
break
# Flip the image horizontally for a mirrored view
image = cv2.flip(image, 1)
# Convert image to RGB format and pass to MediaPipe Pose model
results = pose.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# Draw pose landmarks on image
if results.pose_landmarks:
mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
# Extract coordinates of left shoulder, left elbow, and left wrist keypoints
left_shoulder = results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_SHOULDER]
left_elbow = results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_ELBOW]
left_wrist = results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_WRIST]
left_shoulder_x, left_shoulder_y = int(left_shoulder.x * image.shape[1]), int(left_shoulder.y * image.shape[0])
left_elbow_x, left_elbow_y = int(left_elbow.x * image.shape[1]), int(left_elbow.y * image.shape[0])
left_wrist_x, left_wrist_y = int(left_wrist.x * image.shape[1]), int(left_wrist.y * image.shape[0])
# Calculate angle between left arm (shoulder to wrist) and torso (shoulder to hip)
left_arm_vector = (left_wrist_x - left_shoulder_x, left_wrist_y - left_shoulder_y)
left_torso_vector = (results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_HIP].x * image.shape[1] - left_shoulder_x,
results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_HIP].y * image.shape[0] - left_shoulder_y)
dot_product = left_arm_vector[0] * left_torso_vector[0] + left_arm_vector[1] * left_torso_vector[1]
magnitude1 = math.sqrt(left_arm_vector[0]**2 + left_arm_vector[1]**2)
magnitude2 = math.sqrt(left_torso_vector[0]**2 + left_torso_vector[1]**2)
cosine_angle = dot_product / (magnitude1 * magnitude2)
angle = math.acos(cosine_angle)
angle_degrees = angle * 180 / math.pi
# Display angle on image
cv2.putText(image, f"Angle: {angle_degrees:.1f}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# Display image with pose landmarks and angle
cv2.imshow("MediaPipe Pose Estimation", image)
# Press 'q' to exit
if cv2.waitKey(1) == ord('q'):
break
# Release resources
pose.close()
cap
User
please add code for sit and stand detection
Assistant
Sure, here's an example Python code snippet that uses the angle between the left arm and the torso to detect whether a person is sitting or standing:
python
import cv2
import mediapipe as mp
import math
mp_drawing = mp.solutions.drawing_utils
mp_pose = mp.solutions.pose
# Initialize MediaPipe Pose model
pose = mp_pose.Pose(static_image_mode=True, min_detection_confidence=0.5)
# Initialize OpenCV VideoCapture object to read from webcam
cap = cv2.VideoCapture(0)
# Initialize threshold angle to differentiate between sitting and standing
threshold_angle = 120
while cap.isOpened():
# Read frame from webcam
success, image = cap.read()
if not success:
break
# Flip the image horizontally for a mirrored view
image = cv2.flip(image, 1)
# Convert image to RGB format and pass to MediaPipe Pose model
results = pose.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# Draw pose landmarks on image
if results.pose_landmarks:
mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
# Extract coordinates of left shoulder, left elbow, and left wrist keypoints
left_shoulder = results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_SHOULDER]
left_elbow = results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_ELBOW]
left_wrist = results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_WRIST]
left_shoulder_x, left_shoulder_y = int(left_shoulder.x * image.shape[1]), int(left_shoulder.y * image.shape[0])
left_elbow_x, left_elbow_y = int(left_elbow.x * image.shape[1]), int(left_elbow.y * image.shape[0])
left_wrist_x, left_wrist_y = int(left_wrist.x * image.shape[1]), int(left_wrist.y * image.shape[0])
# Calculate angle between left arm (shoulder to wrist) and torso (shoulder to hip)
left_arm_vector = (left_wrist_x - left_shoulder_x, left_wrist_y - left_shoulder_y)
left_torso_vector = (results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_HIP].x * image.shape[1] - left_shoulder_x,
results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_HIP].y * image.shape[0] - left_shoulder_y)
dot_product = left_arm_vector[0] * left_torso_vector[0] + left_arm_vector[1] * left_torso_vector[1]
magnitude1 = math.sqrt(left_arm_vector[0]**2 + left_arm_vector[1]**2)
magnitude2 = math.sqrt(left_torso_vector[0]**2 + left_torso_vector[1]**2)
cosine_angle = dot_product / (magnitude1 * magnitude2)
angle = math.acos(cosine_angle)
angle_degrees = angle * 180 / math.pi
# Display angle on image
cv2.putText(image, f"Angle: {angle_degrees:.1f}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# Determine whether person is sitting or standing based on angle threshold
if angle_degrees > threshold_angle:
cv2.putText(image, "STANDING", (10, 70), cv
User
please complete after # Determine whether person is sitting or standing based on angle threshold
if angle_degrees > threshold_angle:
cv2.putText(image, "STANDING", (10, 70), cv
Assistant
Sure, here's the complete code snippet with the sit/stand detection logic:
python
import cv2
import mediapipe as mp
import math
mp_drawing = mp.solutions.drawing_utils
mp_pose = mp.solutions.pose
# Initialize MediaPipe Pose model
pose = mp_pose.Pose(static_image_mode=True, min_detection_confidence=0.5)
# Initialize OpenCV VideoCapture object to read from webcam
cap = cv2.VideoCapture(0)
# Initialize threshold angle to differentiate between sitting and standing
threshold_angle = 120
while cap.isOpened():
# Read frame from webcam
success, image = cap.read()
if not success:
break
# Flip the image horizontally for a mirrored view
image = cv2.flip(image, 1)
# Convert image to RGB format and pass to MediaPipe Pose model
results = pose.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# Draw pose landmarks on image
if results.pose_landmarks:
mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
# Extract coordinates of left shoulder, left elbow, and left wrist keypoints
left_shoulder = results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_SHOULDER]
left_elbow = results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_ELBOW]
left_wrist = results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_WRIST]
left_shoulder_x, left_shoulder_y = int(left_shoulder.x * image.shape[1]), int(left_shoulder.y * image.shape[0])
left_elbow_x, left_elbow_y = int(left_elbow.x * image.shape[1]), int(left_elbow.y * image.shape[0])
left_wrist_x, left_wrist_y = int(left_wrist.x * image.shape[1]), int(left_wrist.y * image.shape[0])
# Calculate angle between left arm (shoulder to wrist) and torso (shoulder to hip)
left_arm_vector = (left_wrist_x - left_shoulder_x, left_wrist_y - left_shoulder_y)
left_torso_vector = (results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_HIP].x * image.shape[1] - left_shoulder_x,
results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_HIP].y * image.shape[0] - left_shoulder_y)
dot_product = left_arm_vector[0] * left_torso_vector[0] + left_arm_vector[1] * left_torso_vector[1]
magnitude1 = math.sqrt(left_arm_vector[0]**2 + left_arm_vector[1]**2)
magnitude2 = math.sqrt(left_torso_vector[0]**2 + left_torso_vector[1]**2)
cosine_angle = dot_product / (magnitude1 * magnitude2)
angle = math.acos(cosine_angle)
angle_degrees = angle * 180 / math.pi
# Display angle on image
cv2.putText(image, f"Angle: {angle_degrees:.1f}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# Determine whether person is sitting or standing based on angle threshold
if angle_degrees > threshold_angle:
cv2.putText(image, "STANDING", (10, 70), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0
User
Determine whether person is sitting or standing based on angle threshold
Sure, here's the complete code with the sit/stand detection logic implemented after the line you mentioned:
python
import cv2
import mediapipe as mp
import math
mp_drawing = mp.solutions.drawing_utils
mp_pose = mp.solutions.pose
# Initialize MediaPipe Pose model
pose = mp_pose.Pose(static_image_mode=True, min_detection_confidence=0.5)
# Initialize OpenCV VideoCapture object to read from webcam
cap = cv2.VideoCapture(0)
# Initialize threshold angle to differentiate between sitting and standing
threshold_angle = 120
while cap.isOpened():
# Read frame from webcam
success, image = cap.read()
if not success:
break
# Flip the image horizontally for a mirrored view
image = cv2.flip(image, 1)
# Convert image to RGB format and pass to MediaPipe Pose model
results = pose.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# Draw pose landmarks on image
if results.pose_landmarks:
mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
# Extract coordinates of left shoulder, left elbow, and left wrist keypoints
left_shoulder = results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_SHOULDER]
left_elbow = results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_ELBOW]
left_wrist = results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_WRIST]
left_shoulder_x, left_shoulder_y = int(left_shoulder.x * image.shape[1]), int(left_shoulder.y * image.shape[0])
left_elbow_x, left_elbow_y = int(left_elbow.x * image.shape[1]), int(left_elbow.y * image.shape[0])
left_wrist_x, left_wrist_y = int(left_wrist.x * image.shape[1]), int(left_wrist.y * image.shape[0])
# Calculate angle between left arm (shoulder to wrist) and torso (shoulder to hip)
left_arm_vector = (left_wrist_x - left_shoulder_x, left_wrist_y - left_shoulder_y)
left_torso_vector = (results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_HIP].x * image.shape[1] - left_shoulder_x,
results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_HIP].y * image.shape[0] - left_shoulder_y)
dot_product = left_arm_vector[0] * left_torso_vector[0] + left_arm_vector[1] * left_torso_vector[1]
magnitude1 = math.sqrt(left_arm_vector[0]**2 + left_arm_vector[1]**2)
magnitude2 = math.sqrt(left_torso_vector[0]**2 + left_torso_vector[1]**2)
cosine_angle = dot_product / (magnitude1 * magnitude2)
angle = math.acos(cosine_angle)
angle_degrees = angle * 180 / math.pi
# Display angle on image
cv2.putText(image, f"Angle: {angle_degrees:.1f}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# Determine whether person is sitting or standing based on angle threshold
if angle_degrees > threshold_angle:
cv2.putText(image, "STANDING", (10, 70), cv2.FONT_HERSHEY_SIMPLEX, 1,
User
Determine whether person is sitting or standing based on angle threshold
if angle_degrees > threshold_angle:
cv2.putText(image, "STANDING", (10, 70), cv2.FONT_HERSHEY_SIMPLEX, 1,
Assistant
Sure, here's the complete code with the sit/stand detection logic implemented after the line you mentioned:
python
import cv2
import mediapipe as mp
import math
mp_drawing = mp.solutions.drawing_utils
mp_pose = mp.solutions.pose
# Initialize MediaPipe Pose model
pose = mp_pose.Pose(static_image_mode=True, min_detection_confidence=0.5)
# Initialize OpenCV VideoCapture object to read from webcam
cap = cv2.VideoCapture(0)
# Initialize threshold angle to differentiate between sitting and standing
threshold_angle = 120
while cap.isOpened():
# Read frame from webcam
success, image = cap.read()
if not success:
break
# Flip the image horizontally for a mirrored view
image = cv2.flip(image, 1)
# Convert image to RGB format and pass to MediaPipe Pose model
results = pose.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# Draw pose landmarks on image
if results.pose_landmarks:
mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
# Extract coordinates of left shoulder, left elbow, and left wrist keypoints
left_shoulder = results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_SHOULDER]
left_elbow = results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_ELBOW]
left_wrist = results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_WRIST]
left_shoulder_x, left_shoulder_y = int(left_shoulder.x * image.shape[1]), int(left_shoulder.y * image.shape[0])
left_elbow_x, left_elbow_y = int(left_elbow.x * image.shape[1]), int(left_elbow.y * image.shape[0])
left_wrist_x, left_wrist_y = int(left_wrist.x * image.shape[1]), int(left_wrist.y * image.shape[0])
# Calculate angle between left arm (shoulder to wrist) and torso (shoulder to hip)
left_arm_vector = (left_wrist_x - left_shoulder_x, left_wrist_y - left_shoulder_y)
left_torso_vector = (results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_HIP].x * image.shape[1] - left_shoulder_x,
results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_HIP].y * image.shape[0] - left_shoulder_y)
dot_product = left_arm_vector[0] * left_torso_vector[0] + left_arm_vector[1] * left_torso_vector[1]
magnitude1 = math.sqrt(left_arm_vector[0]**2 + left_arm_vector[1]**2)
magnitude2 = math.sqrt(left_torso_vector[0]**2 + left_torso_vector[1]**2)
cosine_angle = dot_product / (magnitude1 * magnitude2)
angle = math.acos(cosine_angle)
angle_degrees = angle * 180 / math.pi
# Display angle on image
cv2.putText(image, f"Angle: {angle_degrees:.1f}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# Determine whether person is sitting or standing based on angle threshold
if angle_degrees > threshold_angle:
cv2.putText(image, "STANDING", (10, 70), cv2.FONT_HERSHEY_SIMPLEX, 1,