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 pullUpDetect(length): # Process cnt = 0 results = [] tendency = [] result = {} r_elbow_angle_list = [] l_elbow_angle_list = [] eye_distance_list = [] tick = [] pullUpCnt = 0 # Starting OpenPose opWrapper = op.WrapperPython() opWrapper.configure(params) opWrapper.start() # get video from webcam or video start = time.time() cap = cv2.VideoCapture(0) cap_t = cv2.VideoCapture(1) # cap2 = cv2.VideoCapture(r'E:\University\科研创新\雏燕计划-体测\体测姿势素材\push-up\push-up-test-1.mp4') coorList = [] start_time = datetime.now() while True: # Get images from cam ret, imageToProcess = cap.read() ret, imageToTest = cap_t.read() cv2.imshow('video', imageToProcess) cv2.imshow('2', imageToTest) 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 = op.Datum() datum.cvInputData = imageToProcess opWrapper.emplaceAndPop([datum]) if datum.poseKeypoints.size != 1: coor = kpp.getKeyPoints(datum.poseKeypoints[0]) if coor: r_elbow_angle = getValue.getElbowAngle(coor, 'R') l_elbow_angle = getValue.getElbowAngle(coor, 'L') eye_distance = getValue.getEyeWristDistance(coor) if l_elbow_angle: l_elbow_angle_list.append(l_elbow_angle) if eye_distance: eye_distance_list.append(eye_distance) 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, 8) # 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 pullUpCnt += 1 result['Num'] = pullUpCnt standard = analysis.pullUpPeriodJudge( r_elbow_angle_list, l_elbow_angle_list, eye_distance_list) result['IsRElbowStandard'], result[ 'IsLElbowStandard'], result[ 'IsHeightStandard'], total = standard r_elbow_angle_list = l_elbow_angle_list = eye_distance_list = [] results.append(result) print(result) result = {} db.ret(delta_time, total, pullUpCnt, 'pullUp', results, True) cv2.imshow("OpenPose 1.5.1 - Tutorial Python API", datum.cvOutputData) if cv2.waitKey(1) == ord('q'): break cnt += 1 db.ret(None, None, None, None, None, False)
cnt = 0 while True: # Get images from cam ret, imageToProcess = cap2.read() # frame = cv2.flip(frame, 1) # cv2.imshow('frame', fr5ame) # print(frame) # print("type:", type(frame)) if cnt % 2 == 0: datum = op.Datum() datum.cvInputData = imageToProcess opWrapper.emplaceAndPop([datum]) # print(type(datum.poseKeypoints), 'Shape', datum.poseKeypoints.shape) print("Body keypoints: \n") # print(datum.poseKeypoints.size) if datum.poseKeypoints.size != 1: coor = kpp.getKeyPoints(datum.poseKeypoints[0]) print(coor) cv2.imshow("OpenPose 1.5.1 - Tutorial Python API", datum.cvOutputData) if cv2.waitKey(1) == ord('q'): break cnt += 1 end = time.time() print("OpenPose demo successfully finished. Total time: " + str(end - start) + " seconds") # except Exception as e: # print(e) # sys.exit(-1)
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)