digitalai commited on
Commit
c3f2605
·
1 Parent(s): 40706af

Create gym.py

Browse files
Files changed (1) hide show
  1. gym.py +54 -0
gym.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import mediapipe as mp
3
+ import csv
4
+ import os
5
+
6
+ mp_drawing = mp.solutions.drawing_utils
7
+ mp_pose = mp.solutions.pose
8
+
9
+ cap = cv2.VideoCapture(0)
10
+ count = 0
11
+
12
+ with mp_pose.Pose(
13
+ min_detection_confidence=0.5,
14
+ min_tracking_confidence=0.5) as pose:
15
+ fields = ['frame', 'nose_x', 'nose_y', 'left_shoulder_x', 'left_shoulder_y', 'right_shoulder_x', 'right_shoulder_y']
16
+ filename = 'pose_data.csv'
17
+ with open(filename, 'w') as csvfile:
18
+ csvwriter = csv.writer(csvfile)
19
+ csvwriter.writerow(fields)
20
+ while cap.isOpened():
21
+ ret, frame = cap.read()
22
+ if not ret:
23
+ break
24
+ image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
25
+ image.flags.writeable = False
26
+ results = pose.process(image)
27
+ image.flags.writeable = True
28
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
29
+
30
+ if results.pose_landmarks is not None:
31
+ mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
32
+
33
+ cv2.imshow('Pose Estimation', image)
34
+ for result.pose.landmarks in fields:
35
+ print(results.pose.landmarks.landmark.x,results.pose.landmarks.landmark.y )
36
+
37
+ if (count % 10 == 0):
38
+ num_coords = len(results.pose_landmarks.landmark)
39
+ print(results.pose_landmarks.landmark)
40
+ row = [count] + [
41
+ results.pose_landmarks.landmark[i].x if results.pose_landmarks.landmark[i] is not None else float('nan')
42
+ for i in range(num_coords)] + \
43
+ [results.pose_landmarks.landmark[i].y if results.pose_landmarks.landmark[i] is not None else float(
44
+ 'nan') for i in range(num_coords)]
45
+ with open(filename, 'a') as csvfile:
46
+ csvwriter = csv.writer(csvfile)
47
+ csvwriter.writerow(row)
48
+ count += 1
49
+
50
+ if cv2.waitKey(1) & 0xFF == ord('q'):
51
+ break
52
+
53
+ cap.release()
54
+ cv2.destroyAllWindows()