利用openpose进行手势(姿态)识别
在这里插入图片描述
1.编译源项目到windows python
需要注意的是,步骤按博客操作,需要修改地方
- 1.下载最新的源码
- 2.将博客里面的模型下载为代码中对应版本,如图所示
2.进行预测
预测视频中的手势
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | import cv2 import sys import timeit import openpose.pyopenpose as op params = dict() params["model_folder"] = 'openpose\\models' # params["net_resolution"] = '128x192' # params["render_pose"] = '1' params["hand"] = True params["hand_detector"] = 2 params["body"] = 0 opWrapper = op.WrapperPython() opWrapper.configure(params) opWrapper.start() # cap = cv2.VideoCapture(0) videodir = 'D:/py/My_work/video/2-0.mp4' # videodir = 'D:/py/My_work/video/demo1.mp4' cap = cv2.VideoCapture(videodir) print('Video device Initialized.') frame_start_time = timeit.default_timer() frame_cnt = 0 rate = 1 handRectangles = [ # Left/Right hands person 0 # [ # op.Rectangle(320.035889, 377.675049, 69.300949, 69.300949), # op.Rectangle(0., 0., 0., 0.), # ], # Left/Right hands person 1 # [ # op.Rectangle(80.155792, 407.673492, 80.812706, 80.812706), # op.Rectangle(46.449715, 404.559753, 98.898178, 98.898178), # ], [ op.Rectangle(0, 0,0, 0), op.Rectangle(100*rate, 100*rate,300*rate, 300*rate), # op.Rectangle(46.449715, 404.559753, 98.898178, 98.898178), ], # Left/Right hands person 2 # [ # op.Rectangle(185.692673, 303.112244, 157.587555, 157.587555), # op.Rectangle(88.984360, 268.866547, 117.818230, 117.818230), # ] ] while True: # Process Image datum = op.Datum() ret, img = cap.read() # Read camera img = cv2.resize(img,(int(640*rate),int(360*rate))) if ret == False: break imageToProcess = img datum.cvInputData = imageToProcess datum.handRectangles = handRectangles opWrapper.emplaceAndPop([datum]) # print("Body keypoints: \n" + str(datum.poseKeypoints)) m = datum.cvOutputData cv2.rectangle(m, (int(100*rate),int(100*rate)), (int(300*rate),int(300*rate)), (255,0,0), 3) cv2.imshow("Openpose Python - Press Q to Exit", m) frame_end_time = timeit.default_timer() if frame_end_time - frame_start_time > 1: print('fps:{}'.format(frame_cnt)) frame_cnt = 0 frame_start_time = frame_end_time frame_cnt = frame_cnt + 1 k = cv2.waitKey(1) & 0xff if k == 27: break cap.release() cv2.destroyAllWindows() exit(0) |
实时预测body姿态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | # From Python # It requires OpenCV installed for Python import sys import cv2 import os from sys import platform import argparse try: import openpose.pyopenpose as op # Flags parser = argparse.ArgumentParser() parser.add_argument("--image_path", default="1.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"] = "openpose/models/" params["net_resolution"] = '128x192' # 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 # Construct it from system arguments # op.init_argv(args[1]) # oppython = op.OpenposePython() # Starting OpenPose opWrapper = op.WrapperPython(3) opWrapper.configure(params) opWrapper.execute() except Exception as e: print(e) sys.exit(-1) |