使用OpenPifPaf调用摄像头检测姿态时视频无输出且报错
OpenPifPaf摄像头人体姿态检测报错修复
问题分析
你遇到的AttributeError: 'NoneType' object has no attribute '__array_interface__'和cv2断言错误,根源有两个:
- 摄像头帧读取可能失败,返回
None的帧传入模型导致报错 AnnotationPainter.annotations()返回的是matplotlib绘图对象,并非OpenCV可直接显示的numpy数组,所以cv2.imshow()无法处理
修复步骤
1. 校验摄像头帧读取结果
在处理帧之前,先判断是否成功读取到帧,避免None传入模型:
_, frame = video.read() if frame is None: break # 读取失败时退出循环
2. 转换matplotlib画布为OpenCV格式
OpenPifPaf的Canvas基于matplotlib,需要把画布内容转换成BGR格式的numpy数组(OpenCV默认格式):
with openpifpaf.show.Canvas.image(frame) as ax: annotation_painter.annotations(ax, predictions) # 从matplotlib画布提取numpy数组 fig = ax.figure fig.canvas.draw() # 转换为numpy数组并调整颜色通道 img_array = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8) img_array = img_array.reshape(fig.canvas.get_width_height()[::-1] + (3,)) img_array = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR) cv2.imshow('frame2', img_array)
3. 优化代码结构
添加摄像头初始化校验,避免循环内不必要的操作,确保资源正常释放。
修改后的完整代码
import openpifpaf import cv2 import numpy as np VIDEO_LINK = 0 # 0对应默认摄像头 video = cv2.VideoCapture(VIDEO_LINK) # 检查摄像头是否成功打开 if not video.isOpened(): print("无法打开摄像头") exit() predictor = openpifpaf.Predictor(checkpoint='shufflenetv2k16') annotation_painter = openpifpaf.show.AnnotationPainter() while True: ret, frame = video.read() if not ret or frame is None: break # 读取失败或视频结束时退出 # 模型预测 predictions, _, _ = predictor.numpy_image(frame) # 可视化处理 with openpifpaf.show.Canvas.image(frame) as ax: annotation_painter.annotations(ax, predictions) # 转换画布为OpenCV可用格式 fig = ax.figure fig.canvas.draw() img_rgb = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8) img_rgb = img_rgb.reshape(fig.canvas.get_width_height()[::-1] + (3,)) img_bgr = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2BGR) cv2.imshow('Pose Detection', img_bgr) # 按q键退出 if cv2.waitKey(33) & 0xFF == ord('q'): break video.release() cv2.destroyAllWindows()
额外注意事项
- 确保摄像头权限正常,没有被其他程序占用
- 如果仍有报错,尝试更新OpenPifPaf和OpenCV到最新版本:
pip install --upgrade openpifpaf opencv-python
内容的提问来源于stack exchange,提问作者JSVJ
相关产品推荐
相关产品推荐

