def pushUpAnalysis(folder_path): '''Open folder and process .json into a dict input: The file path ouput: The times of push-up Judge by tendency! ''' files = os.listdir(folder_path) results = [] tendency = [] result = {} r_elbow_angle_list = [] r_knee_angle_list = [] hip_angle_list = [] # l_elbow_angle_list = [] tick = [] cnt = 0 for i, file in enumerate(files): coor = jp.getJson(folder_path + '\\' + file) if not coor: continue r_elbow_angle = getValue.getElbowAngle(coor, 'R') r_knee_angle = getValue.getKneeAngle(coor, 'R') hip_angle = getValue.getHipAngle(coor, 'R') if hip_angle: hip_angle_list.append(hip_angle) if r_knee_angle: r_knee_angle_list.append(r_knee_angle) if r_elbow_angle: r_elbow_angle_list.append(r_elbow_angle) tick.append(r_elbow_angle) if len(tick) == 5: tend = analysis.getTendency(tick, 20) # One tick tick = [] if tend: tendency.append(tend) if 3 <= len(tendency): if tendency[-1] == 'down' or tendency[-1] == 'stable': if tendency[-2] == 'upper' and tendency[ -3] == 'upper': # a period cnt += 1 result['Num'] = cnt standard = analysis.pushUpPeriodJudge( r_elbow_angle_list, hip_angle_list, r_knee_angle_list) result['IsRElbowStandard'], result[ 'IsHipAngleStandard'], result[ 'IsRKneeStandard'] = standard result['Flag'] = i r_elbow_angle_list = r_knee_angle_list = hip_angle_list = [] results.append(result) result = {} # print(tendency) return results
def getPushUpInfos(folder_path, is_print, region=[]): '''Open folder and process .json into a dict input: The file path ouput: The analysis of some infos of the whole process ''' files = os.listdir(folder_path) max_r_elbow_angle = 0 min_r_elbow_angle = 200 max_knee_angle = 0 min_knee_angle = 200 max_hip_angle = 0 min_hip_angle = 10000 max_hip_distance = 0 min_hip_distance = 10000 cnt = 0 cnt1 = total_elbow_angle = cnt2 = total_hip_distance = 0 cnt3 = total_knee_angle = cnt4 = total_hip_angle = 0 elb_ang = [] hip_dis = [] knee_ang = [] hip_ang = [] for i, file in enumerate(files): coor = jp.getJson(folder_path + '\\' + file) if not coor: continue cnt = i r_elbow_angle = getValue.getElbowAngle(coor, 'R') r_knee_angle = getValue.getKneeAngle(coor, 'R') hip_angle = getValue.getHipAngle(coor, 'R') hip_distance = getValue.getHipDistance(coor, 'R') if r_elbow_angle: elb_ang.append(r_elbow_angle) total_elbow_angle += r_elbow_angle cnt1 += 1 max_r_elbow_angle = max(max_r_elbow_angle, r_elbow_angle) min_r_elbow_angle = min(min_r_elbow_angle, r_elbow_angle) else: elb_ang.append(0) if hip_distance: hip_dis.append(hip_distance) total_hip_distance += hip_distance cnt2 += 1 max_hip_distance = max(max_hip_distance, hip_distance) min_hip_distance = min(min_hip_distance, hip_distance) else: hip_dis.append(0) if r_knee_angle: knee_ang.append(r_knee_angle) total_knee_angle += r_knee_angle cnt3 += 1 max_knee_angle = max(max_knee_angle, r_knee_angle) min_knee_angle = min(min_knee_angle, r_knee_angle) else: knee_ang.append(0) if hip_angle: hip_ang.append(hip_angle) total_hip_angle += hip_angle cnt4 += 1 max_hip_angle = max(max_hip_angle, hip_angle) min_hip_angle = min(min_hip_angle, hip_angle) else: hip_ang.append(0) aver_r_elbow_angle = total_elbow_angle / cnt1 aver_hip_distance = total_hip_distance / cnt2 aver_knee_angle = total_knee_angle / cnt3 aver_hip_angle = total_hip_angle / cnt4 if is_print: print('max elbow angle:', max_r_elbow_angle) print('min elbow angle:', min_r_elbow_angle) print('aver elbow angle:', aver_r_elbow_angle) print('max hip distance:', max_hip_distance) print('min hip distance:', min_hip_distance) print('aver hip distance:', aver_hip_distance) print('max knee angle:', max_knee_angle) print('min knee angle:', min_knee_angle) print('aver knee angle:', aver_knee_angle) print('max hip angle:', max_hip_angle) print('min hip angle:', min_hip_angle) print('aver hip angle:', aver_hip_angle) elb_ang_below_105 = [] for ans in elb_ang: if ans < 105: elb_ang_below_105.append(ans) else: elb_ang_below_105.append(None) cnt += 1 ax1 = plt.subplot(2, 2, 1) # plt.hlines(165, ) # ax1.scatter(range(cnt1), elb_ang, label='elbow angle', s=10) # ax1.scatter(range(cnt1), elb_ang_below_105, label='elbow angle', color='r', s=10) # actu = [89, 149, 238, 328, 418, 597, 686, 746, 836, 955, 985] # region = 0 ax1.scatter(range(cnt), elb_ang, label='elbow angle', s=2) ax1.scatter(range(cnt), elb_ang_below_105, label='elbow angle', color='r', s=2) ax1.hlines(165, 0, cnt, colors='c') ax1.vlines(region, 0, 180) # ax1.vlines(actu, 0, 180, colors='r') ax1.set_title('elbow_angle') ax2 = plt.subplot(2, 2, 2) ax2.scatter(range(cnt), hip_dis, label='hip distance', s=2) ax2.set_title('hip_distance') ax3 = plt.subplot(2, 2, 3) ax3.scatter(range(cnt), knee_ang, label='knee angle', s=2) ax3.vlines(region, 0, 180) # ax3.vlines(actu, 0, 180, colors='r') ax3.set_title('knee_angle') ax4 = plt.subplot(2, 2, 4) ax4.scatter(range(cnt), hip_ang, label='hip', s=2) ax4.vlines(region, 0, 180) # ax4.vlines(actu, 0, 180, colors='r') ax4.set_title('hip_angle') plt.show()
def getSitUpInfos(folder_path, is_print, region=[]): '''Open folder and process .json into a dict input: The file path ouput: The analysis of some infos of the whole process ''' files = os.listdir(folder_path) max_r_waist_angle = 0 min_r_waist_angle = 200 max_r_s_knee_angle = 0 min_r_s_knee_angle = 200 s_knee_ang = [] wai_ang = [] cnt = 0 cnt1 = cnt2 = 0 cnt3 = 0 total_waist_angle = total_s_knee_angle = 0 for i, file in enumerate(files): coor = jp.getJson(folder_path + '\\' + file) if not coor: continue cnt = i r_s_knee_angle = getValue.getKneeAngle(coor, 'R') r_waist_angle = getValue.getWaistAngle(coor, 'R') if r_s_knee_angle: s_knee_ang.append(r_s_knee_angle) total_s_knee_angle += r_s_knee_angle cnt1 += 1 max_r_s_knee_angle = max(max_r_s_knee_angle, r_s_knee_angle) min_r_s_knee_angle = min(min_r_s_knee_angle, r_s_knee_angle) else: s_knee_ang.append(0) if r_waist_angle: wai_ang.append(r_waist_angle) total_waist_angle += r_waist_angle cnt2 += 1 max_r_waist_angle = max(max_r_waist_angle, r_waist_angle) min_r_waist_angle = min(min_r_waist_angle, r_waist_angle) else: wai_ang.append(0) aver_r_s_knee_angle = total_s_knee_angle / cnt1 aver_r_waist_angle = total_waist_angle / cnt2 if is_print: print('max sit-up knee angle:', max_r_s_knee_angle) print('min sit-up knee angle:', min_r_s_knee_angle) print('aver sit-up knee angle:', aver_r_s_knee_angle) print('max sit-up waist angle:', max_r_waist_angle) print('min sit-up waist angle:', min_r_waist_angle) print('aver sit-up waist angle:', aver_r_waist_angle) s_knee_ang_below_60 = [] for ans in s_knee_ang: if ans < 105: s_knee_ang_below_60.append(ans) else: s_knee_ang_below_60.append(None) cnt += 1 wai_ang_above_140 = [] for wans in s_knee_ang: if wans > 140: wai_ang_above_140.append(wans) else: wai_ang_above_140.append(None) cnt3 += 1 cnt = 0 for i in s_knee_ang: if s_knee_ang[i] is not None: cnt += 1 return cnt ax1 = plt.subplot(1, 2, 1) # 第一行的左图 第一个参数代表子图的行数,第二个参数代表该行图像的列数,第三个参数代表每行的第几个图像 # plt.hlines(165, ) # ax1.scatter(range(cnt1), elb_ang, label='elbow angle', s=10) # ax1.scatter(range(cnt1), elb_ang_below_105, label='elbow angle', color='r', s=10) # actu = [89, 149, 238, 328, 418, 597, 686, 746, 836, 955, 985] # region = 0 ax1.scatter(range(cnt), s_knee_ang, label='s_knee angle', s=2) # range(cnt)表示绘制散点图的数据点,s_knee_ang:图名,s:一个实数? ax1.scatter(range(cnt), s_knee_ang_below_60, label='s_knee_angle', color='r', s=2) # color表示颜色 ax1.hlines(70, 0, cnt, colors='c') # 绘制水平辅助线 70:横坐标,0:ymin,cnt: ymax(辅助线纵坐标的最大最小值) colors:颜色 ax1.vlines(region, 0, 180) # 绘制竖直辅助线 # ax1.vlines(actu, 0, 180, colors='r') ax1.set_title('s_knee_angle') ax2 = plt.subplot(1, 2, 2) ax2.scatter(range(cnt), wai_ang, label='waist angle', s=2) ax1.scatter(range(cnt), wai_ang_above_140, label='waist_angle', color='r', s=2) # color表示颜色 ax1.hlines(140, 0, cnt, colors='c') # 绘制水平辅助线 140:横坐标,0:ymin,cnt: ymax(辅助线纵坐标的最大最小值) colors:颜色 ax2.set_title('waist_angle') #fig, ax2 = plt.subplots(figsize=(12, 12)) #ax2.imshow(ax2, aspect='waist_angle') # im是要显示的图案 plt.savefig(r"C:\Users\Lenovo\Desktop\雏燕2019\后端部分所需资料\sit-up.png") plt.show()
def sitUpDetect(length): '''Open folder and process .json into a dict input: The file path ouput: The times of push-up ''' params = dict() params["model_folder"] = r"E:\Programming\Openpose\openpose\openposePython\models" # Starting OpenPose opWrapper = op.WrapperPython() opWrapper.configure(params) opWrapper.start() # get video from webcam or video # start = time.time() cap_side = cv2.VideoCapture(0) cap_front = cv2.VideoCapture(1) # cap2 = cv2.VideoCapture(r'E:\University\科研创新\雏燕计划-体测\体测姿势素材\push-up\push-up-test-1.mp4') # coorList = [] results = [] tendency = [] tick = [] result = {} r_waist_angle_list = [] l_waist_angle_list = [] r_s_knee_angle_list = [] l_s_knee_angle_list = [] r_elbowtoneck_dist_list = [] l_elbowtoneck_dist_list = [] total_r_elbowtoneck_dist = 0 total_l_elbowtoneck_dist = 0 cnt = cnt_1 = cnt_2 = 0 sitUp_cnt = 0 start_time = datetime.now() while True: # Get images from cam ret, imageToProcessFront = cap_front.read() ret, imageToProcessSide = cap_side.read() cv2.imshow('front', imageToProcessFront) cv2.imshow('side', imageToProcessSide) cur_time = datetime.now() delta_time = (cur_time - start_time).seconds print('\r倒计时:' + length - delta_time + '秒', end='') if delta_time == length: break if cnt % 2 == 0: datum1 = op.Datum() datum2 = op.Datum() datum1.cvInputData = imageToProcessFront datum2.cvInputData = imageToProcessSide opWrapper.emplaceAndPop([datum1]) opWrapper.emplaceAndPop([datum2]) if datum1.poseKeypoints.size != 1 and datum2.poseKeypoints.size != 1: coor_front = kpp.getKeyPoints(datum1.poseKeypoints[0]) # 记得改参数 coor_side = kpp.getKeyPoints(datum2.poseKeypoints[0]) r_waist_angle = getValue.getWaistAngle(coor_side, 'R') l_waist_angle = getValue.getWaistAngle(coor_side, 'L') r_s_knee_angle = getValue.getKneeAngle(coor_side, 'R') l_s_knee_angle = getValue.getKneeAngle(coor_side, 'L') r_elbowtoneck_dist = getValue.getElbowToNeckDistance(coor_front, 'R') l_elbowtoneck_dist = getValue.getElbowToNeckDistance(coor_front, 'L') if l_waist_angle: l_waist_angle_list.append(r_waist_angle) tick.append(l_waist_angle) # if l_waist_angle: # l_waist_angle_list.append(l_waist_angle) if r_s_knee_angle: r_s_knee_angle_list.append(r_s_knee_angle) if l_s_knee_angle: l_s_knee_angle_list.append(l_s_knee_angle) if r_elbowtoneck_dist: r_elbowtoneck_dist_list.append(r_elbowtoneck_dist) cnt_1 += 1 total_r_elbowtoneck_dist += r_elbowtoneck_dist aver_r_elbowtoneck_dist = total_r_elbowtoneck_dist / cnt_1 if l_elbowtoneck_dist: l_elbowtoneck_dist_list.append(l_elbowtoneck_dist) cnt_2 += 1 total_l_elbowtoneck_dist += l_elbowtoneck_dist aver_l_elbowtoneck_dist = total_l_elbowtoneck_dist / cnt_2 if len(tick) == 5: tend = analysis.getTendency(tick, 8) # One tick tick = [] if tend: tendency.append(tend) if 3 <= len(tendency): if tendency[-1] == 'down': if tendency[-2] == 'upper': # a period and tendency[-3] == 'upper' sitUp_cnt += 1 result['Num'] = cnt standard = analysis.sitUpPeriodJudge( r_waist_angle_list, l_waist_angle_list, r_s_knee_angle_list, l_s_knee_angle_list, r_elbowtoneck_dist_list, l_elbowtoneck_dist_list, aver_r_elbowtoneck_dist, aver_l_elbowtoneck_dist) result['IsRWaistStandard'], result[ 'IsLWaistStandard'], result[ 'IsRKneeAngle'], result[ 'IsLKneeAngle'], result[ 'IsRElbowtoNeckStandard'], result[ 'IsLElbowtoNeckStandard'], total = standard # result['Flag'] = i r_waist_angle_list = l_waist_angle_list = [] r_s_knee_angle_list = l_s_knee_angle_list = [] r_elbowtoneck_dist_list = l_elbowtoneck_dist_list = [] # 序列置空 cnt_1 = cnt_2 = 0 results.append(result) print(result) db.ret(delta_time, total, sitUp_cnt, 'sitUp', results, True) result = {} cnt += 1 cv2.imshow("OpenPose 1.5.1 - Tutorial Python API", datum1.cvOutputData) cv2.imshow("OpenPose 1.5.1 - Tutorial Python API - front", datum2.cvOutputData) if cv2.waitKey(1) == ord('q'): break db.ret(None, None, None, None, None, False)
def sitUpAnalysis(folder_path): '''Open folder and process .json into a dict input: The file path ouput: The times of push-up ''' files = os.listdir(folder_path) results = [] tendency = [] tick = [] result = {} r_waist_angle_list = [] l_waist_angle_list = [] r_s_knee_angle_list = [] l_s_knee_angle_list = [] r_elbowtoneck_dist_list = [] l_elbowtoneck_dist_list = [] total_r_elbowtoneck_dist = 0 total_l_elbowtoneck_dist = 0 cnt = cnt_1 = cnt_2 = 0 for i, file in enumerate(files): coor = jp.getJson(folder_path + '\\' + file) if not coor: continue r_waist_angle = getValue.getWaistAngle(coor, 'R') l_waist_angle = getValue.getWaistAngle(coor, 'L') r_s_knee_angle = getValue.getKneeAngle(coor, 'R') l_s_knee_angle = getValue.getKneeAngle(coor, 'L') r_elbowtoneck_dist = getValue.getElbowToNeckDistance(coor, 'R') l_elbowtoneck_dist = getValue.getElbowToNeckDistance(coor, 'L') tick.append(r_waist_angle) if r_waist_angle: r_waist_angle_list.append(r_waist_angle) if l_waist_angle: l_waist_angle_list.append(l_waist_angle) if r_s_knee_angle: r_s_knee_angle_list.append(r_s_knee_angle) if l_s_knee_angle: l_s_knee_angle_list.append(l_s_knee_angle) if r_elbowtoneck_dist: r_elbowtoneck_dist_list.append(r_elbowtoneck_dist) cnt_1 += 1 total_r_elbowtoneck_dist += r_elbowtoneck_dist aver_r_elbowtoneck_dist = total_r_elbowtoneck_dist / cnt_1 if l_elbowtoneck_dist: l_elbowtoneck_dist_list.append(l_elbowtoneck_dist) cnt_2 += 1 total_l_elbowtoneck_dist += l_elbowtoneck_dist aver_l_elbowtoneck_dist = total_l_elbowtoneck_dist / cnt_2 if len(tick) == 5: tend = analysis.getTendency(tick, 8) # One tick tick = [] if tend: tendency.append(tend) if 3 <= len(tendency): if tendency[-1] == 'down': if tendency[ -2] == 'upper': # a period and tendency[-3] == 'upper' cnt += 1 result['Num'] = cnt standard = analysis.SitUpPeriodJudge( r_waist_angle_list, l_waist_angle_list, r_s_knee_angle_list, l_s_knee_angle_list, r_elbowtoneck_dist_list, l_elbowtoneck_dist_list, aver_r_elbowtoneck_dist, aver_l_elbowtoneck_dist) result['IsRWaistStandard'], result[ 'IsLWaistStandard'] = standard result['IsRKneeAngle'], result[ 'IsLKneeAngle'] = standard result['IsRElbowtoNeckStandard'], result[ 'IsLElbowtoNeckStandard'] = standard result['Flag'] = i r_waist_angle_list = l_waist_angle_list = [] r_s_knee_angle_list = l_s_knee_angle_list = [] r_elbowtoneck_dist_list = l_elbowtoneck_dist_list = [ ] #序列置空 results.append(result) result = {} print(tendency) return results
def pushUpDetect(length): # Process cnt = 0 # Starting OpenPose opWrapper = op.WrapperPython() opWrapper.configure(params) opWrapper.start() # get video from webcam or video start = time.time() cap_side = cv2.VideoCapture(0) cap_front = cv2.VideoCapture(1) # cap2 = cv2.VideoCapture(r'E:\University\科研创新\雏燕计划-体测\体测姿势素材\push-up\push-up-test-1.mp4') coorList = [] results = [] tendency = [] result = {} r_elbow_angle_list = [] l_elbow_angle_list = [] l_knee_angle_list = [] hip_angle_list = [] hip_distance_list = [] tick = [] pushUpCnt = 0 start_time = datetime.now() while True: # Get images from cam ret, imageToProcessFront = cap_front.read() ret, imageToProcessSide = cap_side.read() cv2.imshow('front', imageToProcessFront) cv2.imshow('side', imageToProcessSide) cnt = 0 cur_time = datetime.now() delta_time = (cur_time - start_time).seconds print('\r倒计时:' + length - delta_time + '秒', end='') if delta_time == length: break if cnt % 2 == 0: datum_front = op.Datum() datum_side = op.Datum() datum_front.cvInputData = imageToProcessFront datum_side.cvInputData = imageToProcessSide opWrapper.emplaceAndPop([datum_front]) opWrapper.emplaceAndPop([datum_side]) # print("Body keypoints:") if datum_front.poseKeypoints.size != 1 and datum_side.poseKeypoints.size != 1: coor_front = kpp.getKeyPoints( datum_front.poseKeypoints[0]) # 记得改参数 coor_side = kpp.getKeyPoints(datum_side.poseKeypoints[0]) r_elbow_angle = getValue.getElbowAngle(coor_front, 'R') l_elbow_angle = getValue.getElbowAngle(coor_front, 'L') l_knee_angle = getValue.getKneeAngle(coor_side, 'L') hip_angle = getValue.getHipAngle(coor_side, 'L') hip_distance = getValue.getHipDistance(coor_side, 'L') if r_elbow_angle: r_elbow_angle_list.append(r_elbow_angle) tick.append(r_elbow_angle) if hip_angle: hip_angle_list.append(hip_angle) if l_knee_angle: l_knee_angle_list.append(l_knee_angle) if l_elbow_angle: l_elbow_angle_list.append(l_elbow_angle) if hip_distance: hip_distance_list.append(hip_distance) if len(tick) == 5: tend = analysis.getTendency(tick, 20) # One tick print(tend) tick = [] if tend: tendency.append(tend) if 3 <= len(tendency): if tendency[-1] == 'down' or tendency[ -1] == 'stable': if tendency[ -2] == 'upper': # a period and tendency[-3] == 'upper' pushUpCnt += 1 result['Num'] = pushUpCnt standard = analysis.pushUpPeriodJudgeTwoSides( r_elbow_angle_list, l_elbow_angle_list, hip_angle_list, l_knee_angle_list, hip_distance_list) result['IsRElbowStandard'], result[ 'IsLElbowStandard'], result[ 'IsHipAngleStandard'], result[ 'IsRKneeStandard'], result[ 'IsHipDistanceStandard'], total = standard r_elbow_angle_list = [] l_elbow_angle_list = [] l_knee_angle_list = [] hip_angle_list = [] hip_distance_list = [] pushUpCnt += 1 results.append(result) print(result) result = {} db.ret(delta_time, total, pushUpCnt, 'pushUp', results, True) cnt += 1 cv2.imshow("front", datum_front.cvOutputData) cv2.imshow("side", datum_side.cvOutputData) if cv2.waitKey(1) == ord('q'): break db.ret(None, None, None, None, None, False)
def pushUpVisualization(coor, i): '''Read coordinates and display it input: List of coordinates ouput: The analysis of some infos of the whole process ''' plt.ion() # for i, coor in enumerate(coorList): # if not coor: # continue cnt = i r_elbow_angle = getValue.getElbowAngle(coor, 'R') r_knee_angle = getValue.getKneeAngle(coor, 'R') hip_angle = getValue.getHipAngle(coor, 'R') hip_distance = getValue.getHipDistance(coor, 'R') # if r_elbow_angle: # elb_ang.append(r_elbow_angle) # else: # elb_ang.append(0) # if hip_distance: # hip_dis.append(hip_distance) # else: # hip_dis.append(0) # if r_knee_angle: # knee_ang.append(r_knee_angle) # else: # knee_ang.append(0) # if hip_angle: # hip_ang.append(hip_angle) # else: # hip_ang.append(0) # elb_ang_below_105 = [] # for ans in elb_ang: # if ans < 105: # elb_ang_below_105.append(ans) # else: # elb_ang_below_105.append(None) # cnt += 1 ax1 = plt.subplot(2, 2, 1) # plt.hlines(165, ) # ax1.scatter(range(cnt1), elb_ang, label='elbow angle', s=10) # ax1.scatter(range(cnt1), elb_ang_below_105, label='elbow angle', color='r', s=10) # actu = [89, 149, 238, 328, 418, 597, 686, 746, 836, 955, 985] # region = 0 ax1.scatter(i, r_elbow_angle, label='elbow angle', s=2) # ax1.scatter(i, elb_ang_below_105, label='elbow angle', color='r', s=2) ax1.hlines(165, 0, cnt, colors='c') # ax1.vlines(actu, 0, 180, colors='r') ax1.set_title('elbow_angle') ax2 = plt.subplot(2, 2, 2) ax2.scatter(i, hip_distance, label='hip distance', s=2) ax2.set_title('hip_distance') ax3 = plt.subplot(2, 2, 3) ax3.scatter(i, r_knee_angle, label='knee angle', s=2) # ax3.vlines(actu, 0, 180, colors='r') ax3.set_title('knee_angle') ax4 = plt.subplot(2, 2, 4) ax4.scatter(i, hip_angle, label='hip', s=2) # ax4.vlines(actu, 0, 180, colors='r') ax4.set_title('hip_angle') # plt.pause(0.01) plt.plot()