0

I am trying to use mediapipe pose to export the coordinates of the keypoints of the human body when rowing to a csv file. My main problem is with the .process() function that keeps throwing up an error whenever I execute the cell. I have previously tried doing the pose estimation with out the try-except (the part of the code that exports the keypoint data to a csv file) and it works, but for some reason, this code now kills the kernel every time the cell is run. Each codeblock represents a cell in a jupyter notebook - THE LAST CELL IS THE SAME AS THE 3RD CELL, other than the different video being fed into the mediapipe pose model and the try-except that is supposed to export the data to a csv file, this is the cell where the kernel dies.

import mediapipe as mp
import cv2
import numpy as np
mp_drawing  = mp.solutions.drawing_utils
mp_pose = mp.solutions.pose
cap = cv2.VideoCapture('') <----- THIS IS WHERE THE VIDEO OF SOMEONE ROWING WOULD BE

with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:
    while cap.isOpened():
        ret, image = cap.read()
        
        
        #recolor
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        image.flags.writeable = False
        
        #detections
        results = pose.process(image)
        
        #recolor back to RGB
        image.flags.writeable = True
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS
                                 , mp_drawing.DrawingSpec(color=(245,117,66), thickness=2, circle_radius=4), 
                                 mp_drawing.DrawingSpec(color=(245, 66, 230), thickness=2, circle_radius=2)
                                 )
        
        
        
        
        
        cv2.imshow('RawFeed', image)
        
        if cv2.waitKey(10) & 0xFF == ord('q'):
            break
            
            
cap.release()
cv2.destroyAllWindows()
import csv
import os
import numpy as np
landmarks = ['class']
for val in range (1, num_coords+1):
    landmarks += ['x{}'.format(val), 'y{}'.format(val), 'z{}'.format(val), 'v{}'.format(val)]
with open ('trainData.csv', mode='w', newline ='') as f:
    csv_writer = csv.writer(f, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    csv_writer.writerow(landmarks)
class_name = "Catch"
cap = cv2.VideoCapture('CatchTrain.mp4')

with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:
    while cap.isOpened():
        ret, image = cap.read()
        
        
        #recolor
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        image.flags.writeable = False
        
        #detections
        results = pose.process(image)
        
        #recolor back to RGB
        image.flags.writeable = True
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS
                                 , mp_drawing.DrawingSpec(color=(245,117,66), thickness=2, circle_radius=4), 
                                 mp_drawing.DrawingSpec(color=(245, 66, 230), thickness=2, circle_radius=2)
                                 )
        
        #export coordinates
        try:
            pose = results.pose_landmarks.landmark #gets the pose landmarks
            pose_row = np.array([[landmark.x, landmark.y, landmark.z, landmark.visibility] for landmark in pose]).flatten()
                #gives each landmark component
                # .flatten() collapses an array into a single dimension, [[1,2],[3,4]] --> [1,2,3,4]

            pose_row.insert(0, class_name) #appends the class name to the CSV


                #Exports to CSV

            with open ('trainData.csv', 'a', newline ='') as f:
                csv_writer = csv.writer(f, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
                csv_writer.writerow(pose_row)
               
            
            
            
        except Exception as e:
            pass
        
        
        
        
        
        cv2.imshow('CatchFeed', image)
        
        if cv2.waitKey(10) & 0xFF == ord('q'):
            break
            
            
cap.release()
cv2.destroyAllWindows()

0 Answers0