lightweight openpose多线程实现人体姿态估计,python代码实现
时间: 2023-08-23 10:06:43 浏览: 177
使用python语言的超轻量openpose源码,能实时对人体进行姿态检测,下载可直接运行
5星 · 资源好评率100%
以下是使用`OpenCV`和`OpenPose`库实现多线程人体姿态估计的Python代码示例:
```python
import cv2
import threading
import numpy as np
from queue import Queue
from openpose import pyopenpose as op
# OpenPose参数设置
params = dict()
params["model_folder"] = "models/"
params["net_resolution"] = "-1x368"
params["model_pose"] = "BODY_25"
# 初始化OpenPose
opWrapper = op.WrapperPython()
opWrapper.configure(params)
opWrapper.start()
# 用于存储预测结果的队列
result_queue = Queue()
# 处理图像的线程
def process_image(image):
# 运行OpenPose
datum = op.Datum()
datum.cvInputData = image
opWrapper.emplaceAndPop([datum])
# 将预测结果存入队列
result_queue.put(datum)
# 显示结果的线程
def show_result():
while True:
if not result_queue.empty():
datum = result_queue.get()
# 绘制骨架
image = datum.cvOutputData
pose_keypoints = datum.poseKeypoints
for i in range(pose_keypoints.shape[0]):
for j in range(pose_keypoints.shape[1]):
cv2.circle(image, tuple(pose_keypoints[i][j][:2]), 5, (0, 255, 0), -1)
cv2.imshow("OpenPose result", image)
# 按下ESC键退出程序
if cv2.waitKey(1) == 27:
break
# 读取视频并进行处理
cap = cv2.VideoCapture("test.mp4")
while True:
ret, frame = cap.read()
if not ret:
break
# 开启新线程处理图像
t = threading.Thread(target=process_image, args=(frame,))
t.start()
# 开启新线程显示结果
t = threading.Thread(target=show_result)
t.start()
# 等待所有线程结束
t.join()
cap.release()
cv2.destroyAllWindows()
```
在代码中,`process_image`函数用于处理图像,运行OpenPose并将预测结果存入队列中;`show_result`函数用于显示结果,从队列中获取预测结果并绘制骨架;`cap.read()`读取视频帧,每读取一帧就开启一个新线程处理该帧图像。最后,开启新线程显示结果,并等待所有线程结束。
阅读全文