利用openpose进行手势(姿态)识别
在这里插入图片描述
1.编译源项目到windows python
需要注意的是,步骤按博客操作,需要修改地方
2.将博客里面的模型下载为代码中对应版本,如图所示
2.进行预测
预测视频中的手势
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姿态
# 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)