def run(): # Initialize keypoints extractor kp_extractor = KeypointsExtractor() base_path = "/home/kevin/projects/initial-pose-data/images/pos-negs" for exercise_name in os.listdir(base_path): exercise_dir = os.path.join(base_path, exercise_name) for posneg in os.listdir(exercise_dir): posneg_dir = os.path.join(exercise_dir, posneg) for file in os.listdir(posneg_dir): file_path = os.path.join(posneg_dir, file) image = cv2.imread(file_path) try: list_of_pose_and_id, _ = kp_extractor.get_keypoints_and_id_from_img( image) except Exception as e: # cv2.imshow("image yg error", image) # cv2.waitKey(0) print(e) continue keypoints = list_of_pose_and_id[0]['Keypoints'] if validate_keypoints(keypoints): keypoints = keypoints.tolist() is_starting_pose = True if posneg == "pos" else False insert_starting_pose_binary_to_db(keypoints, exercise_name, is_starting_pose) else: cv2.imshow("image yg ga ke validate", image) cv2.waitKey(0)
def run(): # Initialize paths base_path = "/home/kevin/projects/initial-pose-data/train_data" save_path = "/home/kevin/projects/exercise_pose_evaluation_machine/models/initial_pose_model/initial_pose_model.h5" # Get dataset folders dirs = os.listdir(base_path) # Initialize keypoints extractor kp_extractor = KeypointsExtractor() # Logger kp_extraction_logger = create_logger() kp_extraction_logger.write_header() min_max_scaler = MinMaxScaler(feature_range=(0, 1)) for class_name in dirs: class_dir = os.listdir(base_path + '/' + class_name) for file_name in class_dir: file_path = f'{base_path}/{class_name}/{file_name}' image = cv2.imread(file_path) # Get image dimensions for logging height, width, channels = image.shape # record the time to extract keypoints kp_extract_start = time.perf_counter() list_of_pose_and_id = kp_extractor.get_keypoints_and_id_from_img( image) kp_extract_end = time.perf_counter() kp_extraction_log_entry = { "image_res": f"{width}x{height}", "time": kp_extract_end - kp_extract_start, "exercise_type": class_name } kp_extraction_logger.write_row(kp_extraction_log_entry) keypoints = list_of_pose_and_id[0]['Keypoints'] if validate_keypoints(keypoints): normalized_keypoints = min_max_scaler.fit_transform(keypoints) normalized_keypoints = normalized_keypoints.tolist() # print(normalized_keypoints) # print(type(normalized_keypoints)) insert_initial_pose_to_db(normalized_keypoints, class_name)
def run(): # Initialize keypoints extractor kp_extractor = KeypointsExtractor() base_path = "/home/kevin/projects/initial-pose-data/images/v2/pos-negs" for exercise_name in os.listdir(base_path): exercise_dir = os.path.join(base_path, exercise_name) for posneg in os.listdir(exercise_dir): posneg_dir = os.path.join(exercise_dir, posneg) for file in os.listdir(posneg_dir): file_path = os.path.join(posneg_dir, file) image = cv2.imread(file_path) list_of_pose_and_id, _ = kp_extractor.get_keypoints_and_id_from_img(image) keypoints = list_of_pose_and_id[0]['Keypoints'] if validate_keypoints(keypoints): keypoints = keypoints.tolist() is_starting_pose = True if posneg == "pos" else False insert_starting_pose_binary_to_db(keypoints, exercise_name, is_starting_pose)
def test(): pose_detector = PoseDetector("squat") kp_extractor = KeypointsExtractor() file_path = '/home/kevin/projects/initial-pose-data/images/v1/pos-negs/squat/pos/mirrored-VID_20200928_134747.mp4_186.jpg' # Opening OpenCV stream stream = cv2.VideoCapture(file_path) while True: try: ret, imageToProcess = stream.read() except Exception as e: # Break at end of frame break list_of_keypoints = kp_extractor.get_keypoints_and_id_from_img(imageToProcess) x = list_of_keypoints[0] keypoints = np.array(x).flatten() prediction = pose_detector.predict(np.array([keypoints])) print(prediction)
def __init__(self): """ Virtually private constructor. """ if PoseMachine.__instance != None: raise Exception("This class is a singleton!") else: self.target_id = "" self.exercise_type = "" self.pose_predictions = [] self.kp_extractor = KeypointsExtractor() self.init_pose_detector = InitialPoseDetector() self.right_hand_up_detector = RightHandUpDetector() PoseMachine.__instance = self
from sklearn.preprocessing import OneHotEncoder from sklearn.model_selection import train_test_split from keypoints_extractor import KeypointsExtractor def predict_initial_pose(keypoints): # Base paths save_path = "/home/kevin/projects/exercise_pose_evaluation_machine/models/initial_pose_model/initial_pose_model.h5" # Prepare keypoints to feed into network keypoints = np.array(keypoints).flatten().astype(np.float32) # Load model model = load_model(save_path) # Summarize model model.summary() # Get prediction res_lookup = ['plank', 'sit-up', 'dumbell-curl', 'push-up'] return res_lookup[np.argmax(model.predict(np.array([keypoints])))] if __name__ == '__main__': img_test_path = "/home/kevin/projects/Exercise Starting Pose/Pushups/pushupstart9.jpg" image = cv2.imread(img_test_path) kp_extractor = KeypointsExtractor() list_of_pose_and_id = kp_extractor.get_keypoints_and_id_from_img(image) keypoints = list_of_pose_and_id[0]['Keypoints'] print("Result is " + predict_initial_pose(keypoints))
"push-up": "/home/kevin/projects/dataset/push-up-t.mp4", # OK "sit-up": "/home/kevin/projects/dataset/sit-up-t.mp4", # OK "squat": "/home/kevin/projects/dataset/squat-k1.mp4", # OK "plank": "/home/kevin/projects/dataset/plank-k1.mp4", # OK # "test": "/home/kevin/Videos/VID_20210116_152545.mp4" } if __name__ == '__main__': try: # Base paths exercise = "plank" base_path = VIDEO_PATHS[exercise] # base_path = "/home/kevin/projects/dataset/squat-obscured.mp4" # base_path = "/home/kevin/projects/right-hand-up-to-exercise/squat.mp4" kp_extractor = KeypointsExtractor() # Opening OpenCV stream stream = cv2.VideoCapture(base_path) fps = stream.get(cv2.CAP_PROP_FPS) # Define flags init_pose_detected = False target_id = -1 # Define detectors right_hand_up_detector = RightHandUpDetector() initial_pose_detector = InitialPoseDetector() # Define x_min, y_min, x_max, y_max x_min = -1
normalized_keypoints = [x.tolist() for x in keypoints] insert_right_hand_up_pose_to_db(normalized_keypoints, label) if __name__ == "__main__": from multiprocessing import Process # Initialize paths # TODO: Setup the train data in 2 folders true and false folders base_path = "/home/kevin/projects/right_hand_up_pose_data/training_data" # Get dataset folders dirs = os.listdir(base_path) # Initialize keypoints extractor kp_extractor = KeypointsExtractor() min_max_scaler = MinMaxScaler(feature_range=(0, 1)) # Label should be either "true" / "false" (Which are the folder names) for label in dirs: label_dir = os.listdir(base_path + '/' + label) run(base_path, label, label_dir, kp_extractor, min_max_scaler) # label_dir_split = chunks(label_dir, int(len(label_dir) / 3)) # THREADS = [] # for label_dir in label_dir_split: # thread = Process(target=run, args=(base_path, label, label_dir,)) # thread.start() # THREADS.append(thread) # for t in THREADS:
return False return True if __name__ == '__main__': # Keypoint list for each exercise selected_keypoints = [ NECK, R_SHOULDER, R_ELBOW, R_WRIST, L_SHOULDER, L_ELBOW, L_WRIST, MID_HIP, R_HIP, R_KNEE, R_ANKLE, L_HIP, L_KNEE, L_ANKLE ] # Initialize dataset path dataset_path = '/home/kevin/projects/dataset-theo/to-insert-to-db' # Define keypoints kp_extractor = KeypointsExtractor() # Get all keypoints from dataset # and insert into database dirs = os.listdir(dataset_path) for foldername in dirs: # Get all folder list folder = dataset_path + '/' + foldername files = os.listdir(folder) # Repeat for every files for filename in files: try: keypoints = [] class_type = 0 print("Processing file: " + filename)
L_EYE = 16 R_EAR = 17 L_EAR = 18 L_BIG_TOE = 19 L_SMALL_TOE = 20 L_HEEL = 21 R_BIG_TOE = 22 R_SMALL_TOE = 23 R_HEEL = 24 selected_keypoints = [ NECK, R_SHOULDER, R_ELBOW, R_WRIST, L_SHOULDER, L_ELBOW, L_WRIST, MID_HIP, R_HIP, R_KNEE, R_ANKLE, L_HIP, L_KNEE, L_ANKLE ] # Get reps keypoints kp_extractor = KeypointsExtractor() all_exercise_reps, all_exercise_x_low, all_exercise_y_low = kp_extractor.scan_video_without_normalize( base_path, selected_keypoints) scaler = make_min_max_scaler(all_exercise_reps, min(all_exercise_x_low), min(all_exercise_y_low)) normalized_reps = normalize_keypoints_from_external_scaler( all_exercise_reps, scaler) reshaped_normalized_reps = [ np.array(frames).flatten() for frames in normalized_reps ] # Print results print("Result is " + predict_sequence(reshaped_normalized_reps, "sit-up")) # JAPHNE MADAFAKIN DID IT AGAIN! # JAPHNE MADAFAKIN DID IT AGAIN! # JAPHNE MADAFAKIN DID IT AGAIN!