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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from microprograms.temporal_segmentation.entry import entry_microprogram_one_frame
|
| 2 |
+
from microprograms.temporal_segmentation.somersault import somersault_microprogram_one_frame
|
| 3 |
+
from microprograms.temporal_segmentation.twist import twist_microprogram_one_frame
|
| 4 |
+
from microprograms.temporal_segmentation.start_takeoff import takeoff_microprogram_one_frame
|
| 5 |
+
from microprograms.errors.distance_from_springboard_micro_program import board_end
|
| 6 |
+
from microprograms.errors.splash_micro_program import get_splash_from_one_frame
|
| 7 |
+
from microprograms.errors.distance_from_springboard_micro_program import calculate_distance_from_springboard_for_one_frame
|
| 8 |
+
from microprograms.errors.distance_from_springboard_micro_program import calculate_distance_from_platform_for_one_frame
|
| 9 |
+
from microprograms.errors.angles_micro_programs import applyFeetApartError
|
| 10 |
+
from microprograms.errors.angles_micro_programs import applyPositionTightnessError
|
| 11 |
+
from models.detectron2.platform_detector_setup import get_platform_detector
|
| 12 |
+
from models.pose_estimator.pose_estimator_model_setup import get_pose_estimation
|
| 13 |
+
from models.detectron2.diver_detector_setup import get_diver_detector
|
| 14 |
+
from models.pose_estimator.pose_estimator_model_setup import get_pose_model
|
| 15 |
+
from models.detectron2.splash_detector_setup import get_splash_detector
|
| 16 |
+
from somersault_counter import som_counter, twist_counter
|
| 17 |
+
from microprograms.errors.over_rotation import over_rotation
|
| 18 |
+
import pickle
|
| 19 |
+
import os
|
| 20 |
+
import math
|
| 21 |
+
import numpy as np
|
| 22 |
+
import cv2
|
| 23 |
+
|
| 24 |
+
# returns None if either pose_pred or board_end_coord is None
|
| 25 |
+
# returns True if diver is on board, returns False if diver is off board
|
| 26 |
+
def detect_on_board(board_end_coord, board_side, pose_pred, handstand):
|
| 27 |
+
if pose_pred is None:
|
| 28 |
+
print("pose_pred is None")
|
| 29 |
+
return
|
| 30 |
+
if board_end_coord is None:
|
| 31 |
+
print("board end coord is None")
|
| 32 |
+
return
|
| 33 |
+
# side = find_which_side_board_on(outputs)
|
| 34 |
+
if board_side == 'left':
|
| 35 |
+
# if right of board end
|
| 36 |
+
if np.array(pose_pred)[0][2][0] > int(board_end_coord[0]):
|
| 37 |
+
# print("board on left side, we're saying that the diver is on the right")
|
| 38 |
+
# if standing_dive:
|
| 39 |
+
if handstand:
|
| 40 |
+
# print("board end to wrist dist:", math.dist(np.array(pose_pred)[0][15], board_end_coord))
|
| 41 |
+
# print("wrist to elbow dist:", math.dist(np.array(pose_pred)[0][14], np.array(pose_pred)[0][15]))
|
| 42 |
+
distance = math.dist(np.array(pose_pred)[0][15], board_end_coord) < math.dist(np.array(pose_pred)[0][14], np.array(pose_pred)[0][15]) * 1.5
|
| 43 |
+
else:
|
| 44 |
+
# print("board end to ankle dist:", math.dist(np.array(pose_pred)[0][5], board_end_coord))
|
| 45 |
+
# print("ankle to knee dist:", math.dist(np.array(pose_pred)[0][5], np.array(pose_pred)[0][4]))
|
| 46 |
+
distance = math.dist(np.array(pose_pred)[0][5], board_end_coord) < math.dist(np.array(pose_pred)[0][5], np.array(pose_pred)[0][4]) * 1.5
|
| 47 |
+
|
| 48 |
+
# print("distance", distance)
|
| 49 |
+
return distance
|
| 50 |
+
# return False
|
| 51 |
+
# if left of board end
|
| 52 |
+
else:
|
| 53 |
+
# print("board on left side, we're saying that the diver is on the left")
|
| 54 |
+
return True
|
| 55 |
+
else:
|
| 56 |
+
# if right of board end
|
| 57 |
+
if np.array(pose_pred)[0][2][0] > int(board_end_coord[0]):
|
| 58 |
+
# print("board on right side, we're saying that the diver is on the right")
|
| 59 |
+
return True
|
| 60 |
+
# if left of board end
|
| 61 |
+
else:
|
| 62 |
+
# print("board on right side, we're saying that the diver is on the left")
|
| 63 |
+
# if standing_dive:
|
| 64 |
+
if handstand:
|
| 65 |
+
distance = math.dist(np.array(pose_pred)[0][10], board_end_coord) < math.dist(np.array(pose_pred)[0][11], np.array(pose_pred)[0][10]) * 1.5
|
| 66 |
+
else:
|
| 67 |
+
distance = math.dist(np.array(pose_pred)[0][0], board_end_coord) < math.dist(np.array(pose_pred)[0][1], np.array(pose_pred)[0][0]) * 1.5
|
| 68 |
+
# print("distance", distance)
|
| 69 |
+
return distance
|
| 70 |
+
# return False
|
| 71 |
+
|
| 72 |
+
def main():
|
| 73 |
+
first_folder = input("what is the first folder? Ex: 01, FINAWorldChampionships2019_Women10m_final_r1, etc. ")
|
| 74 |
+
second_folder = input("what is the second folder? (dive within the first folder)")
|
| 75 |
+
takeoff = []
|
| 76 |
+
twist = []
|
| 77 |
+
som = []
|
| 78 |
+
entry = []
|
| 79 |
+
distance_from_board = []
|
| 80 |
+
position_tightness = []
|
| 81 |
+
feet_apart = []
|
| 82 |
+
splash = []
|
| 83 |
+
above_board = True
|
| 84 |
+
board_side = input("what side is the board on? type either 'left' or 'right'")
|
| 85 |
+
on_board = True
|
| 86 |
+
handstand = input("is the dive a handstand dive? type either 'True' or 'False' ") == 'True'
|
| 87 |
+
expected_twists = int(input("what are the expected number of twists? type 1 for half a twist, 2 for full twist, etc."))
|
| 88 |
+
expected_som = int(input("what are the expected number of somersaults? type 1 for half a somersault, 2 for full somersault, etc."))
|
| 89 |
+
# expected_twists= 0
|
| 90 |
+
# expected_som = 7
|
| 91 |
+
platform_detector = get_platform_detector()
|
| 92 |
+
splash_detector = get_splash_detector()
|
| 93 |
+
diver_detector = get_diver_detector()
|
| 94 |
+
pose_model = get_pose_model()
|
| 95 |
+
dive_folder_num = "{}_{}".format(first_folder, second_folder)
|
| 96 |
+
directory = './FineDiving/datasets/FINADiving_MTL_256s/{}/{}/'.format(first_folder, second_folder)
|
| 97 |
+
# dive_folder_num = '01_1'
|
| 98 |
+
# directory = './FineDiving/datasets/FINADiving_MTL_256s/01/1/'
|
| 99 |
+
# dive_folder_num = 'FINAWorldChampionships2019_Women10m_final_r1_0'
|
| 100 |
+
# directory = './FineDiving/datasets/FINADiving_MTL_256s/FINAWorldChampionships2019_Women10m_final_r1/0/'
|
| 101 |
+
file_names = os.listdir(directory)
|
| 102 |
+
# with open('./output/joint_plots/{}/pose_preds.pkl'.format(dive_folder_num), 'rb') as pickle_file:
|
| 103 |
+
# pose_preds = pickle.load(pickle_file)
|
| 104 |
+
j = 0
|
| 105 |
+
# if len(pose_preds) > len(file_names):
|
| 106 |
+
# print("WRONG POSE_PREDS")
|
| 107 |
+
prev_pred = None
|
| 108 |
+
som_prev_pred =None
|
| 109 |
+
half_som_count=0
|
| 110 |
+
petal_count = 0
|
| 111 |
+
in_petal = False
|
| 112 |
+
for i in range(len(file_names)):
|
| 113 |
+
# pose_pred = None
|
| 114 |
+
filepath = directory + file_names[i]
|
| 115 |
+
print("filepath:", filepath)
|
| 116 |
+
if file_names[i][-4:] != ".jpg":
|
| 117 |
+
continue
|
| 118 |
+
diver_box, pose_pred = get_pose_estimation(filepath, diver_detector=diver_detector, pose_model=pose_model)
|
| 119 |
+
# if j < len(pose_preds):
|
| 120 |
+
# print("filepath has pose_pred:", filepath)
|
| 121 |
+
# pose_pred = pose_preds[j]
|
| 122 |
+
# j += 1
|
| 123 |
+
calculated_half_som_count, skip = som_counter(pose_pred, som_prev_pred, half_som_count=half_som_count, handstand=handstand)
|
| 124 |
+
if not skip:
|
| 125 |
+
som_prev_pred = pose_pred
|
| 126 |
+
print("calculated_half_som_count:", calculated_half_som_count)
|
| 127 |
+
calculated_petal_count, calculated_in_petal = twist_counter(pose_pred, prev_pose_pred=prev_pred, in_petal=in_petal, petal_count=petal_count)
|
| 128 |
+
print("calculated_petal_count", calculated_petal_count)
|
| 129 |
+
print("calculated_in_petal", calculated_in_petal)
|
| 130 |
+
im = cv2.imread(filepath)
|
| 131 |
+
outputs = platform_detector(im)
|
| 132 |
+
board_end_coord = board_end(outputs, board_side=board_side)
|
| 133 |
+
# if board_end_coord is None:
|
| 134 |
+
# print("NO BOARD NONE CRYING")
|
| 135 |
+
if above_board and not on_board and board_end_coord is not None and pose_pred is not None and np.array(pose_pred)[0][2][1] > int(board_end_coord[1]):
|
| 136 |
+
above_board=False
|
| 137 |
+
if on_board and detect_on_board(board_end_coord, board_side, pose_pred, handstand) is not None and not detect_on_board(board_end_coord, board_side, pose_pred, handstand):
|
| 138 |
+
on_board = False
|
| 139 |
+
print('ON_BOARD:', on_board)
|
| 140 |
+
print('ABOVE_BOARD:', above_board)
|
| 141 |
+
calculated_takeoff = takeoff_microprogram_one_frame(filepath, above_board=above_board, on_board=on_board, pose_pred=pose_pred)
|
| 142 |
+
calculated_twist = twist_microprogram_one_frame(filepath, on_board=on_board, pose_pred=pose_pred, expected_twists=expected_twists, petal_count=petal_count, expected_som=expected_som, half_som_count=half_som_count)
|
| 143 |
+
calculated_som = somersault_microprogram_one_frame(filepath, pose_pred=pose_pred, on_board=on_board, expected_som=expected_som, half_som_count=half_som_count, expected_twists=expected_twists, petal_count=petal_count)
|
| 144 |
+
calculated_entry = entry_microprogram_one_frame(filepath, above_board=above_board, on_board=on_board, pose_pred=pose_pred, expected_twists=expected_twists, petal_count=petal_count, expected_som=expected_som, half_som_count=half_som_count, splash_detector=splash_detector, visualize=True, dive_folder_num=dive_folder_num)
|
| 145 |
+
if calculated_takeoff == 1:
|
| 146 |
+
# distance from board
|
| 147 |
+
dist = calculate_distance_from_platform_for_one_frame(filepath, visualize=False, pose_pred=pose_pred, board_end_coord=board_end_coord, platform_detector=platform_detector) # saves photo to ./output/data/distance_from_board/
|
| 148 |
+
distance_from_board.append(dist)
|
| 149 |
+
# height off board
|
| 150 |
+
# height_off_board.append(dist)
|
| 151 |
+
# proximity to the edge of board
|
| 152 |
+
# proximity_to_end_board.append(dist)
|
| 153 |
+
elif calculated_som == 1:
|
| 154 |
+
half_som_count = calculated_half_som_count
|
| 155 |
+
# petal_count = calculated_petal_count
|
| 156 |
+
# in_petal = calculated_in_petal
|
| 157 |
+
if above_board:
|
| 158 |
+
dist = calculate_distance_from_platform_for_one_frame(filepath, visualize=False, pose_pred=pose_pred, board_end_coord=board_end_coord, platform_detector=platform_detector) # saves photo to ./output/data/distance_from_board/
|
| 159 |
+
# distance from board
|
| 160 |
+
distance_from_board.append(dist)
|
| 161 |
+
# rotation speed
|
| 162 |
+
# position tightness
|
| 163 |
+
position_tightness.append(applyPositionTightnessError(filepath, pose_pred=pose_pred))
|
| 164 |
+
# feet flat
|
| 165 |
+
# feet/legs apart
|
| 166 |
+
feet_apart.append(applyFeetApartError(filepath, pose_pred=pose_pred))
|
| 167 |
+
elif calculated_twist == 1:
|
| 168 |
+
half_som_count = calculated_half_som_count
|
| 169 |
+
petal_count = calculated_petal_count
|
| 170 |
+
in_petal = calculated_in_petal
|
| 171 |
+
if above_board:
|
| 172 |
+
dist = calculate_distance_from_platform_for_one_frame(filepath, visualize=False, pose_pred=pose_pred, board_end_coord=board_end_coord, platform_detector=platform_detector) # saves photo to ./output/data/distance_from_board/
|
| 173 |
+
# distance from board
|
| 174 |
+
distance_from_board.append(dist)
|
| 175 |
+
# twisting speed
|
| 176 |
+
# position tightness
|
| 177 |
+
position_tightness.append(180 - applyPositionTightnessError(filepath, pose_pred=pose_pred))
|
| 178 |
+
# feet flat
|
| 179 |
+
# feet/legs apart
|
| 180 |
+
feet_apart.append(applyFeetApartError(filepath, pose_pred=pose_pred))
|
| 181 |
+
elif calculated_entry == 1:
|
| 182 |
+
# over/under twisting
|
| 183 |
+
# over/under rotating
|
| 184 |
+
print("OVER-ROTATION ERROR:", over_rotation(filepath, pose_pred=pose_pred, diver_detector=diver_detector, pose_model=pose_model))
|
| 185 |
+
# not straight during entry
|
| 186 |
+
# splash
|
| 187 |
+
splash.append(get_splash_from_one_frame(filepath, predictor=splash_detector, visualize=True))
|
| 188 |
+
else:
|
| 189 |
+
print('no phase of dive calculated!')
|
| 190 |
+
takeoff.append(calculated_takeoff)
|
| 191 |
+
twist.append(calculated_twist)
|
| 192 |
+
som.append(calculated_som)
|
| 193 |
+
entry.append(calculated_entry)
|
| 194 |
+
prev_pred = pose_pred
|
| 195 |
+
print("takeoff", takeoff)
|
| 196 |
+
print("twist", twist)
|
| 197 |
+
print("som", som)
|
| 198 |
+
print("entry", entry)
|
| 199 |
+
print("distance_from_board", distance_from_board)
|
| 200 |
+
print("position_tightness", position_tightness)
|
| 201 |
+
print("feet_apart", feet_apart)
|
| 202 |
+
print("splash", splash)
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
if __name__ == "__main__":
|
| 206 |
+
main()
|