def generate_output(inputvid, model, orientation, gender, height, weight, outputvid, file_name): try: parser = argparse.ArgumentParser() args = parser.parse_known_args() # Custom Params (refer to include/openpose/flags.hpp for more parameters) params = dict() params["model_folder"] = model #Find a better way to do this. Currently saves each frame as json #params["write_json"] = "json_output" #Configure Openpose Python Wrapper opWrapper = op.WrapperPython() opWrapper.configure(params) opWrapper.start() #Create patient object of person in video patient = Patient(gender, height, weight) body_perc = patient.body_perc() vid_location = inputvid cap = cv2.VideoCapture(vid_location) width = cap.get(3) height = cap.get(4) fps = cap.get(5) font = cv2.FONT_HERSHEY_SIMPLEX total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) #Find Video Format video_type = vid_location.split(".")[-1] if video_type == "mp4": fourcc = cv2.VideoWriter_fourcc(*'mp4v') location = outputvid + '/output.mp4' out = cv2.VideoWriter(location, fourcc, fps, (int(width), int(height))) elif video_type == "avi": fourcc = cv2.VideoWriter_fourcc(*'XVID') location = outputvid + '/output.avi' out = cv2.VideoWriter(location, fourcc, fps, (int(width), int(height))) else: fourcc = cv2.VideoWriter_fourcc(*'mp4v') location = outputvid + '/output.mp4' out = cv2.VideoWriter(location, fourcc, fps, (int(width), int(height))) # print("Video format not supported") # sys.exit(-1) frame_num = 0 #lists to store data per frame com_x_pos = [] com_y_pos = [] com_ang = [] inertias = [] pend_origin = [] print("Calculating") with open(file_name) as f: data = json.load(f) frame_num = 1 for x in data['openpose']: frame_data = (x[str(frame_num)]) #limb positions body = [(frame_data['Neck'][0] + frame_data['MidHip'][0]) / 2, (frame_data['Neck'][1] + frame_data['MidHip'][1]) / 2] pelvis = [frame_data['MidHip'][0], frame_data['MidHip'][1]] R_ear = [frame_data['REar'][0], frame_data['REar'][1]] R_arm = [ (frame_data['RShoulder'][0] + frame_data['RElbow'][0]) / 2, (frame_data['RShoulder'][1] + frame_data['RElbow'][1]) / 2 ] R_forearm = [ (frame_data['RElbow'][0] + frame_data['RWrist'][0]) / 2, (frame_data['RElbow'][1] + frame_data['RWrist'][1]) / 2 ] R_hand = [frame_data['RWrist'][0], frame_data['RWrist'][1]] R_thigh = [(frame_data['RHip'][0] + frame_data['RKnee'][0]) / 2, (frame_data['RHip'][1] + frame_data['RKnee'][1]) / 2] R_shank = [(frame_data['RKnee'][0] + frame_data['RAnkle'][0]) / 2, (frame_data['RKnee'][1] + frame_data['RAnkle'][1]) / 2] R_foot = [(frame_data['RBigToe'][0] + frame_data['RHeel'][0]) / 2, (frame_data['RBigToe'][1] + frame_data['RHeel'][1]) / 2] L_ear = [frame_data['LEar'][0], frame_data['LEar'][1]] L_arm = [ (frame_data['LShoulder'][0] + frame_data['LElbow'][0]) / 2, (frame_data['LShoulder'][1] + frame_data['LElbow'][1]) / 2 ] L_forearm = [ (frame_data['LElbow'][0] + frame_data['LWrist'][0]) / 2, (frame_data['LElbow'][1] + frame_data['LWrist'][1]) / 2 ] L_hand = [frame_data['LWrist'][0], frame_data['LWrist'][1]] L_thigh = [(frame_data['LHip'][0] + frame_data['LKnee'][0]) / 2, (frame_data['LHip'][1] + frame_data['LKnee'][1]) / 2] L_shank = [(frame_data['LKnee'][0] + frame_data['LAnkle'][0]) / 2, (frame_data['LKnee'][1] + frame_data['LAnkle'][1]) / 2] L_foot = [(frame_data['LBigToe'][0] + frame_data['LHeel'][0]) / 2, (frame_data['LBigToe'][1] + frame_data['LHeel'][1]) / 2] # Used for pixel-m conversion nose = [frame_data['Nose'][0], frame_data['Nose'][1]] foot_y = max(R_foot[1], L_foot[1]) if foot_y == 0: print("Error- Foot") nose_y = nose[1] if nose_y == 0: print("Error- Nose") pixel_height = foot_y - nose_y patient.set_pixel_cm(pixel_height) COM_x = 0 COM_y = 0 if orientation == "side": #Calulate x CoM if R_ear[0] == 0 and L_ear[0] == 0: print("Error- head") elif R_ear[0] == 0: COM_x += L_ear[0] * body_perc["head"] else: COM_x += R_ear[0] * body_perc["head"] if body[0] == 0: print("Error- body") else: COM_x += body[0] * body_perc["body"] if pelvis[0] == 0: print("Error- pelvis") else: COM_x += pelvis[0] * body_perc["pelvis"] if R_arm[0] == 0 or L_arm[0] == 0: arms = max(R_arm[0], L_arm[0]) if arms == 0: print("Error- arm") else: COM_x += arms * body_perc["arm"] * 2 else: COM_x += (R_arm[0] + L_arm[0]) * body_perc["arm"] if R_forearm[0] == 0 or (L_forearm[0]) == 0: forearms = max(R_forearm[0], L_forearm[0]) if forearms == 0: print("Error- forearm") else: COM_x += forearms * body_perc["forearm"] * 2 else: COM_x += (R_forearm[0] + L_forearm[0]) * body_perc["forearm"] if R_hand[0] == 0 or L_hand[0] == 0: hands = max(R_hand[0], L_hand[0]) if hands == 0: print("Error- arm") else: COM_x += hands * body_perc["hand"] * 2 else: COM_x += (R_hand[0] + L_hand[0]) * body_perc["hand"] if R_thigh[0] == 0 or L_thigh[0] == 0: thighs = max(R_thigh[0], L_thigh[0]) if thighs == 0: print("Error- thigh") else: COM_x += thighs * body_perc["thigh"] * 2 else: COM_x += (R_thigh[0] + L_thigh[0]) * body_perc["thigh"] if R_shank[0] == 0 or L_shank[0] == 0: shanks = max(R_shank[0], L_shank[0]) if shanks == 0: print("Error- shank") else: COM_x += shanks * body_perc["shank"] * 2 else: COM_x += (R_shank[0] + L_shank[0]) * body_perc["shank"] if R_foot[0] == 0 or L_foot[0] == 0: foots = max(R_foot[0], L_foot[0]) if foots == 0: print("Error- foot") else: COM_x += foots * body_perc["foot"] * 2 else: COM_x += (R_foot[0] + L_foot[0]) * body_perc["foot"] #Calulate y CoM if R_ear[1] == 0 and L_ear[1] == 0: print("Error- head") elif R_ear[1] == 0: COM_x += L_ear[1] * body_perc["head"] else: COM_x += R_ear[1] * body_perc["head"] if body[1] == 0: print("Error- body") else: COM_y += body[1] * body_perc["body"] if pelvis[1] == 0: print("Error- pelvis") else: COM_y += pelvis[1] * body_perc["pelvis"] if R_arm[1] == 0 or L_arm[1] == 0: arms = max(R_arm[1], L_arm[1]) if arms == 0: print("Error- arm") else: COM_y += arms * body_perc["arm"] * 2 else: COM_y += (R_arm[1] + L_arm[1]) * body_perc["arm"] if R_forearm[1] == 0 or L_forearm[1] == 0: forearms = max(R_forearm[1], L_forearm[1]) if forearms == 0: print("Error- forearm") else: COM_y += forearms * body_perc["forearm"] * 2 else: COM_y += (R_forearm[1] + L_forearm[1]) * body_perc["forearm"] if R_hand[1] == 0 or L_hand[1] == 0: hands = max(R_hand[1], L_hand[1]) if hands == 0: print("Error- hand") else: COM_y += hands * body_perc["hand"] * 2 else: COM_y += (R_hand[1] + L_hand[1]) * body_perc["hand"] if R_thigh[1] == 0 or L_thigh[1] == 0: thighs = max(R_thigh[1], L_thigh[1]) if thighs == 0: print("Error- thigh") else: COM_y += thighs * body_perc["thigh"] * 2 else: COM_y += (R_thigh[1] + L_thigh[1]) * body_perc["thigh"] if R_shank[1] == 0 or L_shank[1] == 0: shanks = max(R_shank[1], L_shank[1]) if shanks == 0: print("Error- shank") else: COM_y += shanks * body_perc["shank"] * 2 else: COM_y += (R_shank[1] + L_shank[1]) * body_perc["shank"] if R_foot[1] == 0 or L_foot[1] == 0: foots = max(R_foot[1], L_foot[1]) if foots == 0: print("Error- foot") else: COM_y += foots * body_perc["foot"] * 2 else: COM_y += (R_foot[1] + L_foot[1]) * body_perc["foot"] elif orientation == "front": #Calulate x CoM if nose[0] == 0: print("Error- head") else: COM_x += nose[0] * body_perc["head"] if body[0] == 0: print("Error- body") else: COM_x += body[0] * body_perc["body"] if pelvis[0] == 0: print("Error- pelvis") else: COM_x += pelvis[0] * body_perc["pelvis"] if R_arm[0] == 0 or L_arm[0] == 0: print("Error- arm") COM_x += (R_arm[0] + L_arm[0]) * body_perc["arm"] if R_forearm[0] == 0 or (L_forearm[0]) == 0: print("Error- forearm") COM_x += (R_forearm[0] + L_forearm[0]) * body_perc["forearm"] if R_hand[0] == 0 or L_hand[0] == 0: print("Error- arm") COM_x += (R_hand[0] + L_hand[0]) * body_perc["hand"] if R_thigh[0] == 0 or L_thigh[0] == 0: print("Error- thigh") COM_x += (R_thigh[0] + L_thigh[0]) * body_perc["thigh"] if R_shank[0] == 0 or L_shank[0] == 0: print("Error- shank") COM_x += (R_shank[0] + L_shank[0]) * body_perc["shank"] if R_foot[0] == 0 or L_foot[0] == 0: print("Error- foot") COM_x += (R_foot[0] + L_foot[0]) * body_perc["foot"] #Calulate y CoM if nose[1] == 0: print("Error- head") else: COM_y += nose[1] * body_perc["head"] if body[1] == 0: print("Error- body") else: COM_y += body[1] * body_perc["body"] if pelvis[1] == 0: print("Error- pelvis") else: COM_y += pelvis[1] * body_perc["pelvis"] if R_arm[1] == 0 or L_arm[1] == 0: print("Error- arm") COM_y += (R_arm[1] + L_arm[1]) * body_perc["arm"] if R_forearm[1] == 0 or L_forearm[1] == 0: print("Error- forearm") COM_y += (R_forearm[1] + L_forearm[1]) * body_perc["forearm"] if R_hand[1] == 0 or L_hand[1] == 0: print("Error- hand") COM_y += (R_hand[1] + L_hand[1]) * body_perc["hand"] if R_thigh[1] == 0 or L_thigh[1] == 0: print("Error- thigh") COM_y += (R_thigh[1] + L_thigh[1]) * body_perc["thigh"] if R_shank[1] == 0 or L_shank[1] == 0: print("Error- shank") COM_y += (R_shank[1] + L_shank[1]) * body_perc["shank"] if R_foot[1] == 0 or L_foot[1] == 0: print("Error- foot") COM_y += (R_foot[1] + L_foot[1]) * body_perc["foot"] else: print("Not a valid orientation") com_x_pos.append(int(COM_x)) com_y_pos.append(int(COM_y)) #Mass Moment of Inertia Calc R_ankle = [frame_data['RAnkle'][0], frame_data['RAnkle'][1]] L_ankle = [frame_data['LAnkle'][0], frame_data['LAnkle'][1]] pend = None if orientation == "side": if R_ankle[1] == 0 and L_ankle[1] == 0: print("Error- ankle") else: if R_ankle[1] >= L_ankle[1]: pend = R_ankle else: pend = L_ankle elif orientation == "front": if R_ankle[1] == 0 or L_ankle[1] == 0: print("Error- ankle") #Test- use previous point in list pend = pend_origin[-1] else: pend = [(L_ankle[0] + R_ankle[0]) / 2, (L_ankle[1] + R_ankle[1]) / 2] else: print("Not a valid orientation") CoM = [int(COM_x), int(COM_y)] MMI = calc_inertia(CoM, pend, patient.mass) inertias.append(MMI) #Angle calculation horizontal_axis = [10, 0] #abitary point on the horizontal axis CoM_to_pend_origin = [COM_x - pend[0], pend[1] - COM_y] ang = angle(CoM_to_pend_origin, horizontal_axis) #ang2 = angle2(pend, [COM_x, COM_y]) com_ang.append(ang) pend_origin.append(pend) frame_num += 1 print("Generating Output") cap = cv2.VideoCapture(vid_location) frame_num = 0 max_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) #Apply Savistky-Golay Filter filtered_comx = scipy.signal.savgol_filter(com_x_pos, 21, 3) filtered_com_ang = scipy.signal.savgol_filter(com_ang, 21, 3) #Calculate frame velocities and accelerations # start, stop, velocity = calc_vel(filtered_comx, 5) # start2, stop2, acceleration = calc_acc(velocity, 5, start) #CoG, Force, Angular Acceleration ang_vel_start, _, ang_vel = calc_vel(filtered_com_ang, 5) #Angular vel ang_acc_start, ang_acc_stop, ang_acc = \ calc_acc(ang_vel, 5, ang_vel_start) #Angular acc #Need the updated angular acceleration so that frames match with CoG updated_ang_acc = add_empty_frames(ang_acc, ang_acc_start) force = calc_force(patient, fps) #TODO: Filter pend_origin or just filter CoG and CoP #CoG = CoG_x(com_x_pos, pend_origin) #CoP = CoP_x(CoG, updated_ang_acc, inertias, force) CoG = scipy.signal.savgol_filter(CoG_x(filtered_comx, pend_origin), 11, 3) CoP = scipy.signal.savgol_filter( CoP_x(CoG, updated_ang_acc, inertias, force), 11, 3) CoG = list(CoG) CoP = list(CoP) # #a,b,ma = moving_average(CoG, 5, 5) start, stop, velocity = calc_vel(CoG, 5) start2, stop2, acceleration = calc_acc(velocity, 5, start) # #Graphs # x = np.linspace(1, len(com_x_pos), len(com_x_pos)) # #x_ma = np.linspace(a,b, len(ma)) # plt.subplot(3,1,1) # a=plt.scatter(x,CoG_x(com_x_pos, pend_origin),label="unfiltered", color="green",marker="*",s=30) # b=plt.plot(x, CoG, label="Moving Average", color = 'red') # #b=plt.plot(x_ma, ma, label="Moving Average", color = 'red') # plt.legend() # plt.title("CoM x pos per Frame") # plt.xlabel("Frame") # plt.ylabel("CoM x pos (Pixels)") # plt.subplot(3,1,2) # x2 = np.linspace(start,stop, len(velocity)) # plt.scatter(x2,velocity,label="stars", color="green",marker="*", s=30) # plt.title("Velocity per Frame") # plt.xlabel("Frame") # plt.ylabel("Velocity (Pixels/frame)") # plt.subplot(3,1,3) # x3 = np.linspace(start2,stop2, len(acceleration)) # plt.scatter(x3,acceleration,label="stars", color="green",marker="*", s=30) # plt.title("Accerelation per Frame") # plt.xlabel("Frame") # plt.ylabel("Acceleration (Pixels^2/frame") # # x = np.linspace(1,len(com_x_pos), len(com_x_pos)) # # plt.subplot(2,1,1) # # plt.scatter(x,com_x_pos,label="stars", color="green",marker="*", s=30) # # plt.title("CoM x pos per frame") # # plt.xlabel("Frame") # # plt.ylabel("CoM x pos (Pixels)") # # plt.plot(x, filtered_comx, color = 'red') # # plt.subplot(2,1,2) # # plt.scatter(x,com_ang,label="stars", color="green",marker="*", s=30) # # plt.title("CoM angle pos per frame") # # plt.xlabel("Frame") # # plt.ylabel("CoM ang pos (radians)") # # plt.plot(x, filtered_com_ang, color = 'red') # plt.show() #Store computed data back into json file CoP_cm = [x / patient.pixel_cm for x in CoP] CoG_cm = [x / patient.pixel_cm for x in CoG] data['processed'] = { 'fps': fps, 'CoP_cm': CoP_cm, 'CoG_cm': CoG_cm, 'CoP_frame': CoP, 'CoG_frame': CoG, 'com_x_pos': com_x_pos, 'com_ang': com_ang, 'inertias': inertias, 'force': force, 'pend_origin': pend_origin, 'pixel_cm': patient.pixel_cm } with open(file_name, 'w') as outfile: json.dump(data, outfile, indent=4) #Show each frame and save to output folder print("Generating output video") while (cap.isOpened()): ret, frame = cap.read() if ret == True: # Process Image datum = op.Datum() imageToProcess = frame frame_num += 1 cv2.putText(imageToProcess, str(frame_num), (100, 100), font, 1, (255, 255, 255), 1) datum.cvInputData = imageToProcess opWrapper.emplaceAndPop([datum]) #Add COM circle to image radius = 10 # Blue color in BGR color = (255, 0, 0) thickness = 2 COM_x = int(filtered_comx[frame_num - 1]) COM_y = com_y_pos[frame_num - 1] COM = (COM_x, COM_y) output_frame = datum.cvOutputData cv2.circle(output_frame, COM, radius, color, thickness) #Plot frame velocities if (frame_num >= start and frame_num <= stop): vel = velocity[frame_num - start] point_2 = (int(COM_x + 10 * vel), int(COM_y)) cv2.arrowedLine(output_frame, COM, point_2, (0, 0, 255), 3) #Plot CoP if (frame_num >= ang_acc_start and frame_num <= ang_acc_stop): CoP_frame = CoP[frame_num - ang_acc_start] pend_origin_x = pend_origin[frame_num - 1][0] pend_origin_y = pend_origin[frame_num - 1][1] point_2 = (int(pend_origin_x + CoP_frame), int(pend_origin_y - 20)) point_1 = (int(pend_origin_x + CoP_frame), int(pend_origin_y)) cv2.arrowedLine(output_frame, point_1, point_2, (0, 0, 255), 3) cv2.line(output_frame, (int(pend_origin[frame_num - 1][0]), int(pend_origin[frame_num - 1][1])), (int(pend_origin[frame_num - 1][0]), int(pend_origin[frame_num - 1][1]) - 20), (255, 0, 0), 3) cv2.imshow("Output video", output_frame) out.write(output_frame) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break print("Finished") cap.release() out.release() cv2.destroyAllWindows() return except Exception as e: print(e) sys.exit(-1)
def main(): try: parser = argparse.ArgumentParser() #parser.add_argument("--image_path", default="../../../examples/media/COCO_val2014_000000000192.jpg", help="Process an image. Read all standard formats (jpg, png, bmp, etc.).") #parser.add_argument("--image_dir", default="../openpose/examples/media/COCO_val2014_000000000192.jpg", help="Process an image. Read all standard formats (jpg, png, bmp, etc.).") args = parser.parse_known_args() # Custom Params (refer to include/openpose/flags.hpp for more parameters) params = dict() #params["model_folder"] = "../../../models/" params["model_folder"] = "../openpose/models/" #params["number_people_max"] = 1 #save data as json to folder #Find a better way to do this. Currently saves each frame as json params["write_json"] = "json_output" # Add others in path? for i in range(0, len(args[1])): curr_item = args[1][i] if i != len(args[1]) - 1: next_item = args[1][i + 1] else: next_item = "1" if "--" in curr_item and "--" in next_item: key = curr_item.replace('-', '') if key not in params: params[key] = "1" elif "--" in curr_item and "--" not in next_item: key = curr_item.replace('-', '') if key not in params: params[key] = next_item opWrapper = op.WrapperPython() opWrapper.configure(params) opWrapper.start() patient = Patient("male", 177, 70) #Set gender of patient body_perc = patient.body_perc() #Set orientation of patient movement orientation = "side" #orientation = "front" #Video location as a string vid_location = "media/side_landscape.mp4" #vid_location = "video.avi" cap = cv2.VideoCapture(vid_location) width = cap.get(3) height = cap.get(4) fps = cap.get(5) font = cv2.FONT_HERSHEY_SIMPLEX #Find Video Format video_type = vid_location.split(".")[-1] if video_type == "mp4": fourcc = cv2.VideoWriter_fourcc(*'mp4v') out = cv2.VideoWriter('media/output.mp4', fourcc, fps, (int(width), int(height))) elif video_type == "avi": fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('media/output.avi', fourcc, fps, (int(width), int(height))) else: print("Video format not supported") sys.exit(-1) frame_num = 0 unsuccessful_frames = 0 #lists to store data per frame com_x_pos = [] com_y_pos = [] com_ang = [] inertias = [] pend_origin = [] print("Generating Pose") while (cap.isOpened()): ret, frame = cap.read() #gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) if ret == True: # Process Image datum = op.Datum() imageToProcess = frame frame_num += 1 cv2.putText(imageToProcess, str(frame_num), (100, 100), font, 1, (255, 255, 255), 1) datum.cvInputData = imageToProcess opWrapper.emplaceAndPop([datum]) #Change to tuple body = [(datum.poseKeypoints[0][1][0] + datum.poseKeypoints[0][8][0]) / 2, (datum.poseKeypoints[0][1][1] + datum.poseKeypoints[0][8][1]) / 2] pelvis = [ datum.poseKeypoints[0][8][0], datum.poseKeypoints[0][8][1] ] R_ear = [ datum.poseKeypoints[0][17][0], datum.poseKeypoints[0][17][1] ] R_arm = [(datum.poseKeypoints[0][2][0] + datum.poseKeypoints[0][3][0]) / 2, (datum.poseKeypoints[0][2][1] + datum.poseKeypoints[0][3][1]) / 2] R_forearm = [(datum.poseKeypoints[0][3][0] + datum.poseKeypoints[0][4][0]) / 2, (datum.poseKeypoints[0][3][1] + datum.poseKeypoints[0][4][1]) / 2] R_hand = [ datum.poseKeypoints[0][4][0], datum.poseKeypoints[0][4][1] ] R_thigh = [(datum.poseKeypoints[0][9][0] + datum.poseKeypoints[0][10][0]) / 2, (datum.poseKeypoints[0][9][1] + datum.poseKeypoints[0][10][1]) / 2] R_shank = [(datum.poseKeypoints[0][10][0] + datum.poseKeypoints[0][11][0]) / 2, (datum.poseKeypoints[0][10][1] + datum.poseKeypoints[0][11][1]) / 2] R_foot = [(datum.poseKeypoints[0][22][0] + datum.poseKeypoints[0][24][0]) / 2, (datum.poseKeypoints[0][22][1] + datum.poseKeypoints[0][24][1]) / 2] L_ear = [ datum.poseKeypoints[0][18][0], datum.poseKeypoints[0][18][1] ] L_arm = [(datum.poseKeypoints[0][5][0] + datum.poseKeypoints[0][6][0]) / 2, (datum.poseKeypoints[0][5][1] + datum.poseKeypoints[0][6][1]) / 2] L_forearm = [(datum.poseKeypoints[0][6][0] + datum.poseKeypoints[0][7][0]) / 2, (datum.poseKeypoints[0][6][1] + datum.poseKeypoints[0][7][1]) / 2] L_hand = [ datum.poseKeypoints[0][7][0], datum.poseKeypoints[0][7][1] ] L_thigh = [(datum.poseKeypoints[0][12][0] + datum.poseKeypoints[0][13][0]) / 2, (datum.poseKeypoints[0][12][1] + datum.poseKeypoints[0][13][1]) / 2] L_shank = [(datum.poseKeypoints[0][13][0] + datum.poseKeypoints[0][14][0]) / 2, (datum.poseKeypoints[0][13][1] + datum.poseKeypoints[0][14][1]) / 2] L_foot = [(datum.poseKeypoints[0][21][0] + datum.poseKeypoints[0][19][0]) / 2, (datum.poseKeypoints[0][21][1] + datum.poseKeypoints[0][19][1]) / 2] #Used for pixel-m conversion nose = [ datum.poseKeypoints[0][0][0], datum.poseKeypoints[0][0][1] ] foot_y = max(R_foot[1], L_foot[1]) if foot_y == 0: print("Error- Foot") nose_y = nose[1] if nose_y == 0: print("Error- Nose") pixel_height = foot_y - nose_y patient.set_pixel_cm(pixel_height) COM_x = 0 COM_y = 0 if orientation == "side": #Calulate x CoM if R_ear[0] == 0 and L_ear[0] == 0: print("Error- head") elif R_ear[0] == 0: COM_x += L_ear[0] * body_perc["head"] else: COM_x += R_ear[0] * body_perc["head"] if body[0] == 0: print("Error- body") else: COM_x += body[0] * body_perc["body"] if pelvis[0] == 0: print("Error- pelvis") else: COM_x += pelvis[0] * body_perc["pelvis"] if R_arm[0] == 0 or L_arm[0] == 0: arms = max(R_arm[0], L_arm[0]) if arms == 0: print("Error- arm") else: COM_x += arms * body_perc["arm"] * 2 else: COM_x += (R_arm[0] + L_arm[0]) * body_perc["arm"] if R_forearm[0] == 0 or (L_forearm[0]) == 0: forearms = max(R_forearm[0], L_forearm[0]) if forearms == 0: print("Error- forearm") else: COM_x += forearms * body_perc["forearm"] * 2 else: COM_x += (R_forearm[0] + L_forearm[0]) * body_perc["forearm"] if R_hand[0] == 0 or L_hand[0] == 0: hands = max(R_hand[0], L_hand[0]) if hands == 0: print("Error- arm") else: COM_x += hands * body_perc["hand"] * 2 else: COM_x += (R_hand[0] + L_hand[0]) * body_perc["hand"] if R_thigh[0] == 0 or L_thigh[0] == 0: thighs = max(R_thigh[0], L_thigh[0]) if thighs == 0: print("Error- thigh") else: COM_x += thighs * body_perc["thigh"] * 2 else: COM_x += (R_thigh[0] + L_thigh[0]) * body_perc["thigh"] if R_shank[0] == 0 or L_shank[0] == 0: shanks = max(R_shank[0], L_shank[0]) if shanks == 0: print("Error- shank") else: COM_x += shanks * body_perc["shank"] * 2 else: COM_x += (R_shank[0] + L_shank[0]) * body_perc["shank"] if R_foot[0] == 0 or L_foot[0] == 0: foots = max(R_foot[0], L_foot[0]) if foots == 0: print("Error- foot") else: COM_x += foots * body_perc["foot"] * 2 else: COM_x += (R_foot[0] + L_foot[0]) * body_perc["foot"] #Calulate y CoM if R_ear[1] == 0 and L_ear[1] == 0: print("Error- head") elif R_ear[1] == 0: COM_x += L_ear[1] * body_perc["head"] else: COM_x += R_ear[1] * body_perc["head"] if body[1] == 0: print("Error- body") else: COM_y += body[1] * body_perc["body"] if pelvis[1] == 0: print("Error- pelvis") else: COM_y += pelvis[1] * body_perc["pelvis"] if R_arm[1] == 0 or L_arm[1] == 0: arms = max(R_arm[1], L_arm[1]) if arms == 0: print("Error- arm") else: COM_y += arms * body_perc["arm"] * 2 else: COM_y += (R_arm[1] + L_arm[1]) * body_perc["arm"] if R_forearm[1] == 0 or L_forearm[1] == 0: forearms = max(R_forearm[1], L_forearm[1]) if forearms == 0: print("Error- forearm") else: COM_y += forearms * body_perc["forearm"] * 2 else: COM_y += (R_forearm[1] + L_forearm[1]) * body_perc["forearm"] if R_hand[1] == 0 or L_hand[1] == 0: hands = max(R_hand[1], L_hand[1]) if hands == 0: print("Error- hand") else: COM_y += hands * body_perc["hand"] * 2 else: COM_y += (R_hand[1] + L_hand[1]) * body_perc["hand"] if R_thigh[1] == 0 or L_thigh[1] == 0: thighs = max(R_thigh[1], L_thigh[1]) if thighs == 0: print("Error- thigh") else: COM_y += thighs * body_perc["thigh"] * 2 else: COM_y += (R_thigh[1] + L_thigh[1]) * body_perc["thigh"] if R_shank[1] == 0 or L_shank[1] == 0: shanks = max(R_shank[1], L_shank[1]) if shanks == 0: print("Error- shank") else: COM_y += shanks * body_perc["shank"] * 2 else: COM_y += (R_shank[1] + L_shank[1]) * body_perc["shank"] if R_foot[1] == 0 or L_foot[1] == 0: foots = max(R_foot[1], L_foot[1]) if foots == 0: print("Error- foot") else: COM_y += foots * body_perc["foot"] * 2 else: COM_y += (R_foot[1] + L_foot[1]) * body_perc["foot"] elif orientation == "front": #Calulate x CoM if nose[0] == 0: print("Error- head") else: COM_x += nose[0] * body_perc["head"] if body[0] == 0: print("Error- body") else: COM_x += body[0] * body_perc["body"] if pelvis[0] == 0: print("Error- pelvis") else: COM_x += pelvis[0] * body_perc["pelvis"] if R_arm[0] == 0 or L_arm[0] == 0: print("Error- arm") COM_x += (R_arm[0] + L_arm[0]) * body_perc["arm"] if R_forearm[0] == 0 or (L_forearm[0]) == 0: print("Error- forearm") COM_x += (R_forearm[0] + L_forearm[0]) * body_perc["forearm"] if R_hand[0] == 0 or L_hand[0] == 0: print("Error- arm") COM_x += (R_hand[0] + L_hand[0]) * body_perc["hand"] if R_thigh[0] == 0 or L_thigh[0] == 0: print("Error- thigh") COM_x += (R_thigh[0] + L_thigh[0]) * body_perc["thigh"] if R_shank[0] == 0 or L_shank[0] == 0: print("Error- shank") COM_x += (R_shank[0] + L_shank[0]) * body_perc["shank"] if R_foot[0] == 0 or L_foot[0] == 0: print("Error- foot") COM_x += (R_foot[0] + L_foot[0]) * body_perc["foot"] #Calulate y CoM if nose[1] == 0: print("Error- head") else: COM_y += nose[1] * body_perc["head"] if body[1] == 0: print("Error- body") else: COM_y += body[1] * body_perc["body"] if pelvis[1] == 0: print("Error- pelvis") else: COM_y += pelvis[1] * body_perc["pelvis"] if R_arm[1] == 0 or L_arm[1] == 0: print("Error- arm") COM_y += (R_arm[1] + L_arm[1]) * body_perc["arm"] if R_forearm[1] == 0 or L_forearm[1] == 0: print("Error- forearm") COM_y += (R_forearm[1] + L_forearm[1]) * body_perc["forearm"] if R_hand[1] == 0 or L_hand[1] == 0: print("Error- hand") COM_y += (R_hand[1] + L_hand[1]) * body_perc["hand"] if R_thigh[1] == 0 or L_thigh[1] == 0: print("Error- thigh") COM_y += (R_thigh[1] + L_thigh[1]) * body_perc["thigh"] if R_shank[1] == 0 or L_shank[1] == 0: print("Error- shank") COM_y += (R_shank[1] + L_shank[1]) * body_perc["shank"] if R_foot[1] == 0 or L_foot[1] == 0: print("Error- foot") COM_y += (R_foot[1] + L_foot[1]) * body_perc["foot"] else: print("Not a valid orientation") com_x_pos.append(int(COM_x)) com_y_pos.append(int(COM_y)) #Mass Moment of Inertia Calc R_ankle = [ datum.poseKeypoints[0][11][0], datum.poseKeypoints[0][11][1] ] L_ankle = [ datum.poseKeypoints[0][14][0], datum.poseKeypoints[0][14][1] ] pend = None if orientation == "side": if R_ankle[1] == 0 and L_ankle[1] == 0: print("Error- ankle") else: if R_ankle[1] >= L_ankle[1]: pend = R_ankle else: pend = L_ankle elif orientation == "front": if R_ankle[1] == 0 or L_ankle[1] == 0: print("Error- ankle") #Test- use previous point in list pend = pend_origin[-1] else: pend = [(L_ankle[0] + R_ankle[0]) / 2, (L_ankle[1] + R_ankle[1]) / 2] else: print("Not a valid orientation") CoM = [int(COM_x), int(COM_y)] MMI = calc_inertia(CoM, pend, patient.mass) inertias.append(MMI) #Angle calculation #abitary point on the horizontal axis horizontal_axis = [10, 0] CoM_to_pend_origin = [COM_x - pend[0], pend[1] - COM_y] ang = angle(CoM_to_pend_origin, horizontal_axis) com_ang.append(ang) pend_origin.append(pend) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break cap.release() #out.release() cv2.destroyAllWindows print("Generating Output") cap = cv2.VideoCapture(vid_location) frame_num = 0 max_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) #Apply Savistky-Golay Filter filtered_comx = scipy.signal.savgol_filter(com_x_pos, 51, 3) filtered_com_ang = scipy.signal.savgol_filter(com_ang, 51, 3) #Calculate frame velocities and accelerations start, stop, velocity = calc_vel(filtered_comx, 5) start2, stop2, acceleration = calc_acc(velocity, 5, start) #start, stop, velocity = calc_vel(com_x_pos, 5) #start2, stop2, acceleration = calc_acc(velocity, 5, start) # start, stop, velocity = calc_avg_vel(com_x_pos, 5, 5) # start2, stop2, acceleration = calc_acc(velocity, 5, start) #CoG, Force, Angular Acceleration ang_vel_start, _, ang_vel = calc_vel(filtered_com_ang, 5) #Angular vel ang_acc_start, ang_acc_stop, ang_acc = \ calc_acc(ang_vel, 5, ang_vel_start) #Angular acc #Need the updated angular acceleration so that frames match with CoG updated_ang_acc = add_empty_frames(ang_acc, ang_acc_start) force = calc_force(patient, fps) CoG = CoG_x(filtered_comx, pend_origin) CoP = CoP_x(CoG, updated_ang_acc, inertias, force) # ang_vel_start, _, ang_vel = calc_avg_vel(com_ang, 5,5) #Angular vel # ang_acc_start, ang_acc_stop, ang_acc = \ # calc_acc(ang_vel, 5, ang_vel_start) #Angular acc # #Need the updated angular acceleration so that frames match with CoG # updated_ang_acc = add_empty_frames(ang_acc, ang_acc_start) # force = calc_force(patient, fps) # CoG = CoG_x(com_x_pos, pend_origin) # CoP = CoP_x(CoG, updated_ang_acc, inertias, force) com_diff = [] for i in range(0, len(pend_origin)): com_diff.append(filtered_comx[i] - int(pend_origin[i][0])) #pend_origin_x = pend_origin[:][0] filtered_com_diff = scipy.signal.savgol_filter(com_diff, 21, 3) #print(len(filtered_comx)) #print(len(CoP)) #print(len(acceleration)) keep = CoG[10:] #keep = com_diff[10:] #keep = filtered_com_diff[10:] #print(len(c)) #print(CoP) #print(keep) error = [] for i in range(0, len(keep)): error.append(CoP[i] - keep[i]) filtered_error = scipy.signal.savgol_filter(error, 21, 3) #error = CoP-keep fig, ax1 = plt.subplots() x = np.linspace(start2, stop2, len(acceleration)) color = 'tab:red' ax1.set_xlabel("Frame") ax1.set_ylabel("Accerelation (Pixels/frame^2)", color=color) ax1.tick_params(axis='y', labelcolor=color) ax1.set_xlim(11, 155) ax1.set_ylim((-0.2, 0.2)) a = ax1.plot(x, acceleration, label="COMacc", color=color) #b=plt.plot(x, d, color = 'red', label = 'COP-COM') #plt.legend([a,b], ['COMacc','COP-COM']) plt.title("CoP-CoM and CoM Over Time") #plt.xlabel("Frame") #plt.ylabel("Accerelation (Pixels/frame^2)") ax2 = ax1.twinx( ) # instantiate a second axes that shares the same x-axis color = 'tab:blue' x2 = np.linspace(start2, stop2, len(acceleration)) ax2.set_ylabel("COP-COM (Pixels)", color=color) # we already handled the x-label with ax1 ax2.set_ylim((-20, 20)) ax2.plot(x2, error, color=color) ax2.tick_params(axis='y', labelcolor=color) # ax3 = ax1.twinx() # instantiate a second axes that shares the same x-axis # color = 'tab:black' # ax3.set_ylim((-0.2, 0.2)) plt.axhline(y=0, color='black') plt.subplots_adjust(hspace=0.9, top=0.45) # x = np.linspace(start2,stop2, len(acceleration)) # plt.subplot(2,1,1) # a=plt.plot(x,acceleration,label="COMacc", color="green") # #b=plt.plot(x, d, color = 'red', label = 'COP-COM') # #plt.legend([a,b], ['COMacc','COP-COM']) # plt.title("?") # plt.xlabel("Frame") # plt.ylabel("Accerelation (Pixels/frame^2)") # plt.subplot(2,1,2) # x2 = np.linspace(start2,stop2, len(acceleration)) # b=plt.plot(x2, filtered_error, color = 'red', label = 'COP-COM') # # plt.title("Velocity per Frame") # plt.xlabel("Frame") # plt.ylabel("COP-COM (Pixels)") #Graphs #fig, axes = plt.subplots(nrows=3, ncols=1) #fig.tight_layout(pad = 5) # x = np.linspace(1,len(com_x_pos), len(com_x_pos)) # plt.subplot(3,1,1) # a=plt.scatter(x,com_x_pos,label="unfiltered", color="green",marker="*", s=30,) # b=plt.plot(x, filtered_comx, color = 'red', label = 'filtered') # plt.legend([a,b], ['unfiltered','filtered']) # plt.title("CoM x pos per Frame") # plt.xlabel("Frame") # plt.ylabel("CoM x pos (Pixels)") # plt.subplot(3,1,2) # x2 = np.linspace(start,stop, len(velocity)) # plt.scatter(x2,velocity,label="stars", color="green",marker="*", s=30) # plt.title("Velocity per Frame") # plt.xlabel("Frame") # plt.ylabel("Velocity (Pixels/frame)") # plt.subplot(3,1,3) # x3 = np.linspace(start2,stop2, len(acceleration)) # plt.scatter(x3,acceleration,label="stars", color="green",marker="*", s=30) # plt.title("Accerelation per Frame") # plt.xlabel("Frame") # plt.ylabel("Acceleration (Pixels/frame^2)") #plt.subplots_adjust(hspace = 0.9, top = 0.9) # x = np.linspace(1,len(com_x_pos), len(com_x_pos)) # plt.subplot(2,1,1)front # plt.scatter(x,com_x_pos,label="stars", color="green",marker="*", s=30) # plt.title("CoM x pos per frame") # plt.xlabel("Frame") # plt.ylabel("CoM x pos (Pixels)") # plt.plot(x, filtered_comx, color = 'red') # plt.subplot(2,1,2) # plt.scatter(x,com_ang,label="stars", color="green",marker="*", s=30) # plt.title("CoM angle pos per frame") # plt.xlabel("Frame") # plt.ylabel("CoM ang pos (radians)") # plt.plot(x, filtered_com_ang, color = 'red') plt.show() while (cap.isOpened()): ret, frame = cap.read() if ret == True: # Process Image datum = op.Datum() imageToProcess = frame frame_num += 1 cv2.putText(imageToProcess, str(frame_num), (100, 100), font, 1, (255, 255, 255), 1) datum.cvInputData = imageToProcess opWrapper.emplaceAndPop([datum]) #Add COM circle to image radius = 10 # Blue color in BGR color = (255, 0, 0) thickness = 2 COM_x = int(filtered_comx[frame_num - 1]) COM_y = com_y_pos[frame_num - 1] COM = (COM_x, COM_y) output_frame = datum.cvOutputData cv2.circle(output_frame, COM, radius, color, thickness) #Plot frame velocities if (frame_num >= start and frame_num <= stop): vel = velocity[frame_num - start] point_2 = (int(COM_x + 10 * vel), int(COM_y)) cv2.arrowedLine(output_frame, COM, point_2, (0, 0, 255), 3) #Plot CoP if (frame_num >= ang_acc_start and frame_num <= ang_acc_stop): CoP_frame = CoP[frame_num - ang_acc_start] pend_origin_x = pend_origin[frame_num - 1][0] pend_origin_y = pend_origin[frame_num - 1][1] point_2 = (int(pend_origin_x + CoP_frame), int(pend_origin_y - 20)) point_1 = (int(pend_origin_x + CoP_frame), pend_origin_y) cv2.arrowedLine(output_frame, point_1, point_2, (0, 0, 255), 3) cv2.imshow("OpenPose 1.5.1 - Tutorial Python API", output_frame) out.write(output_frame) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break cap.release() out.release() cv2.destroyAllWindows except Exception as e: print(e) sys.exit(-1)