HRNet运行python demo/inference.py报错

本文介绍如何在HRNet-Human-Pose-Estimation项目中解决常见错误并成功运行人体姿态估计模型。针对cv2.rectangle函数坐标输入问题进行了详细解析,并提供了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将deep-high-resolution-net.pytorch-master中的demo和visualization文件夹复制到自己的HRNet-Human-Pose-Estimation-master文件夹,运行命令。
在这里插入图片描述在这里插入图片描述
一、虚拟环境中运行命令

python demo/inference.py --cfg demo/inference-config.yaml --videoFile demo/50.mp4 --writeBoxFrames --outputDir output TEST.MODEL_FILE models/pytorch/pose_coco/pose_hrnet_w32_256x192.pth

1.报错:desired inference fps is 10 but video fps is 0.0
报错原因:--videoFile demo/50.mp4,参数有误,正确输入video视频的路径即可运行成功。

2.报错显示cv2.error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function 'rectangle'

(hhr) D:\HRNet-Human-Pose-Estimation-master>python demo/inference.py --cfg demo/inference-config.yaml --videoFile demo/50.mp4 --writeBoxFrames --outputDir output TEST.MODEL_FILE models/pytorch/pose_coco/pose_hrnet_w32_256x192.pth
=> loading model from models/pytorch/pose_coco/pose_hrnet_w32_256x192.pth
Find person bbox in: 4.19201397895813 sec
125.35485 579.5397 (125.35485, 579.5397)
Traceback (most recent call last):
  File "demo/inference.py", line 343, in <module>
    main()
  File "demo/inference.py", line 284, in main
    cv2.rectangle(image_debug, box[0], box[1], color=(0, 255, 0),thickness=3)  # Draw Rectangle with the coordinates
cv2.error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function 'rectangle'
> Overload resolution failed:
>  - Can't parse 'pt1'. Sequence item with index 0 has a wrong type
>  - Can't parse 'pt1'. Sequence item with index 0 has a wrong type
>  - Argument given by name ('color') and position (3)
>  - Argument given by name ('color') and position (3)

在这里插入图片描述
报错原因:输入到cv2.rectangle()这个函数中的坐标不能是浮点数的类型, 要转换成整数才行,使用int()强制类型转换。

二、解决方法:将box[0], box[1]转换为整数类型,但直接改为int(box[0])会报错:

  File "demo/inference.py", line 283, in main
    cv2.rectangle(image_debug, int(box[0]), int(box[1]), color=(0, 255, 0),thickness=3)  # Draw Rectangle with the coordinates
TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'

得知box[0]=(125.35485, 579.5397), box[1]=(794.4199, 1897.901)都是元组 ,因此需要分别将元组的每个元素转换为整形:

        if args.writeBoxFrames:
            for box in pred_boxes:
                cv2.rectangle(image_debug, (int(box[0][0]),int(box[0][1])), (int(box[1][0]),int(box[1][1])), color=(0, 255, 0),thickness=3)  # Draw Rectangle with the coordinates                
#                 cv2.rectangle(image_debug, box[0], box[1], color=(0, 255, 0),thickness=3)  # Draw Rectangle with the coordinates

在这里插入图片描述
三、运行成功
在这里插入图片描述
四、运行demo/demo.py生成的结果如下
生成两个文件:‘output.avi’和’output.jpg’
在这里插入图片描述
运行命令:

python demo/demo.py --video demo/44.mp4

output.avi
若输出视频无法保存,

(hhr) D:\HRNet>python demo/demo.py --video demo/sit9.mp4
=> loading model from models/pytorch/pose_coco/model_best.pth
cannot load the video.

则运行以下命令:

python demo/demo.py --video demo/sit9.mp4 --write

运行结果:

(hhr) D:\HRNet>python demo/demo.py --video demo/sit9.mp4 --write
=> loading model from models/pytorch/pose_coco/model_best.pth
cannot load the video.
video has been saved as output.avi```

python demo/demo.py --image data/mpii/images/000001163.jpg

在这里插入图片描述

### 如何将YOLOPyQt结合使用 为了实现YOLOPyQt的集成,可以按照以下方法构建应用程序: #### 1. 安装依赖库 确保安装了必要的Python包。这通常包括`opencv-python`用于图像处理以及`torch`和`tqdm`等其他可能需要的机器学习框架。 ```bash pip install opencv-python torch tqdm pyqt5 ``` #### 2. 加载并配置YOLO模型 加载预训练好的YOLO权重文件,并设置好检测参数。这部分代码可以从官方GitHub仓库获取或者基于已有的YOLO版本调整[^1]。 ```python import cv2 from ultralytics import YOLO model = YOLO('yolov8n.pt') # Load model ``` #### 3. 创建PyQt界面 设计图形用户界面(GUI),允许用户选择视频源或图片路径作为输入给YOLO进行目标识别。这里展示了一个简单的窗口布局例子[^2]。 ```python from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QLabel, QLineEdit class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("YOLO Object Detection") layout = QVBoxLayout() label = QLabel("Enter image path:") line_edit = QLineEdit() button = QPushButton("Detect Objects!") layout.addWidget(label) layout.addWidget(line_edit) layout.addWidget(button) container = QWidget() container.setLayout(layout) self.setCentralWidget(container) ``` #### 4. 实现对象检测逻辑 当点击按钮时触发事件处理器,在其中调用YOLO来进行预测并将结果显示出来。注意要处理不同类型的媒体数据(如摄像头流、本地文件)[^3]。 ```python def on_button_clicked(self): img_path = self.line_edit.text() # Get input from user results = model(img_path) # Perform inference using loaded model res_plotted = results[0].plot() # Plot bounding boxes over detected objects cv2.imshow("Detected Image", res_plotted) # Show result in a window cv2.waitKey(0) button.clicked.connect(on_button_clicked) ``` 以上就是基本的工作流程;当然实际项目可能会更复杂一些,比如还需要考虑多线程运行以提高性能等问题。对于具体细节上的差异,则取决于所选用的具体YOLO变体及其对应的API接口文档说明[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值