| | import cv2 |
| | import mediapipe as mp |
| | import csv |
| | import os |
| | import numpy as np |
| |
|
| | |
| | mp_drawing = mp.solutions.drawing_utils |
| | mp_pose = mp.solutions.pose |
| |
|
| | |
| | cap = cv2.VideoCapture(0) |
| |
|
| | |
| | count = 0 |
| | |
| | if os.path.exists("pose_data.csv"): |
| | os.remove("pose_data.csv") |
| | |
| | with mp_pose.Pose( |
| | min_detection_confidence=0.5, |
| | min_tracking_confidence=0.5) as pose: |
| | |
| | fields = ['frame', 'nose_x', 'nose_y', 'left_shoulder_x', 'left_shoulder_y', 'right_shoulder_x', 'right_shoulder_y', |
| | 'left_elbow_x', 'left_elbow_y', 'right_elbow_x', 'right_elbow_y', 'left_wrist_x', 'left_wrist_y', |
| | 'right_wrist_x', 'right_wrist_y', |
| | 'left_hip_x', 'left_hip_y', 'right_hip_x', 'right_hip_y', 'left_knee_x', 'left_knee_y', 'right_knee_x', |
| | 'right_knee_y', |
| | 'left_ankle_x', 'left_ankle_y', 'right_ankle_x', 'right_ankle_y'] |
| |
|
| | filename = 'pose_data.csv' |
| |
|
| | with open(filename, 'w') as csvfile: |
| | csvwriter = csv.writer(csvfile) |
| | csvwriter.writerow(fields) |
| |
|
| | |
| |
|
| | while cap.isOpened(): |
| | ret, frame = cap.read() |
| | if not ret: |
| | break |
| |
|
| | |
| | image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) |
| | image.flags.writeable = False |
| | |
| | results = pose.process(image) |
| | |
| | image.flags.writeable = True |
| | image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) |
| |
|
| | |
| | if results.pose_landmarks is not None: |
| | if results.pose_landmarks in fields: |
| | mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS) |
| |
|
| | |
| | cv2.imshow('Pose Estimation', image) |
| |
|
| | |
| | if (count % 10 == 0): |
| | num_coords = len(results.pose_landmarks.landmark) |
| | x= [results.pose_landmarks.landmark[i].x |
| | if results.pose_landmarks.landmark[i] |
| | is not None else float('nan') |
| | for i in range(num_coords)] |
| | y = [results.pose_landmarks.landmark[i].y |
| | if results.pose_landmarks.landmark[i] |
| | is not None else float('nan') |
| | for i in range(num_coords)] |
| | row = [count] + x + y |
| | with open(filename, 'a') as csvfile: |
| | csvwriter = csv.writer(csvfile) |
| | csvwriter.writerow(row) |
| |
|
| | count += 1 |
| |
|
| | |
| | if cv2.waitKey(1) & 0xFF == ord('q'): |
| | break |
| |
|
| | |
| | cap.release() |
| | cv2.destroyAllWindows() |
| |
|
| |
|
| | def calculate_angle(a, b, c): |
| | a = np.array(a) |
| | b = np.array(b) |
| | c = np.array(c) |
| |
|
| | radians = np.arctan2(c[1] - b[1], c[0] - b[0]) - np.arctan2(a[1] - b[1], a[0] - b[0]) |
| | angle = np.abs(radians * 180.0 / np.pi) |
| |
|
| | if angle > 180.0: |
| | angle = 360 - angle |
| |
|
| | return angle |
| |
|
| | shoulder = [landmarks[mp_pose.PoseLandmark.right_shoulder.value].x,landmarks[mp_pose.PoseLandmark.right_shoulder.value].y] |
| | elbow = [landmarks[mp_pose.PoseLandmark.right_elbow.value].x,landmarks[mp_pose.PoseLandmark.right_elbow.value].y] |
| | wrist = [landmarks[mp_pose.PoseLandmark.right_wrist.value].x,landmarks[mp_pose.PoseLandmark.right_wrist.value].y] |
| | shoulder = [landmarks[mp_pose.PoseLandmark.left_shoulder.value].x,landmarks[mp_pose.PoseLandmark.left_shoulder.value].y] |
| | elbow = [landmarks[mp_pose.PoseLandmark.left_elbow.value].x,landmarks[mp_pose.PoseLandmark.left_elbow.value].y] |
| | wrist = [landmarks[mp_pose.PoseLandmark.left_wrist.value].x,landmarks[mp_pose.PoseLandmark.left_wrist.value].y] |
| | knee = [landmarks[mp_pose.PoseLandmark.right_knee.value].x,landmarks[mp_pose.PoseLandmark.right_knee.value].y] |
| | hip = [landmarks[mp_pose.PoseLandmark.right_hip.value].x,landmarks[mp_pose.PoseLandmark.right_hip.value].y] |
| | ankle = [landmarks[mp_pose.PoseLandmark.right_ankle.value].x,landmarks[mp_pose.PoseLandmark.right_ankle.value].y] |
| | knee = [landmarks[mp_pose.PoseLandmark.left_knee.value].x,landmarks[mp_pose.PoseLandmark.left_knee.value].y] |
| | hip = [landmarks[mp_pose.PoseLandmark.left_hip.value].x,landmarks[mp_pose.PoseLandmark.left_hip.value].y] |
| | ankle = [landmarks[mp_pose.PoseLandmark.left_ankle.value].x,landmarks[mp_pose.PoseLandmark.left_ankle.value].y] |
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | nose = [landmarks[mp_pose.PoseLandmark.right_nosevalue].x,landmarks[mp_pose.PoseLandmark.left_nose.value].y] |
| | elbow = [landmarks[mp_pose.PoseLandmark.left_elbow.value].x,landmarks[mp_pose.PoseLandmark.left_elbow.value].y] |
| | wrist = [landmarks[mp_pose.PoseLandmark.left_wrist.value].x,landmarks[mp_pose.PoseLandmark.left_wrist.value].y] |
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | |
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|