普通网友 2024-03-23 14:00 采纳率: 18.2%
浏览 21

这个yolov5模型制作ui界面时选中图片后会报错,并不会在界面上输出结果

这个yolov5模型制作ui界面时选中图片后会报错,并不会在界面上输出结果

##base_ui.py
import sys
import torch
from PySide6.QtWidgets import QMainWindow, QApplication, QFileDialog
from un_main_window import Ui_MainWindow
from PySide6.QtGui import QPixmap, QImage


def convert2QImage(img):
    height, width, channels = img.shape
    return QImage(img, width, height, width*channels, QImage.Format_RGB888)
class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setupUi(self)
        self.model = torch.hub.load('.', 'custom', r'E:\yolov5-v5.0\runs\train\exp8\weights\best.pt', source='local')
        self.bind_slots()

    def image_pred(self,flie_path):
        results = self.model(flie_path)
        image = results.render()[0]
        return convert2QImage(image)

    def open_image(self):
        print("点击检测了图片")
        flie_path = QFileDialog.getOpenFileName(self,dir="./VOCdevkit/images",filter="*.jpg;*.png;*.JPEG;")
        if flie_path[0]:
            flie_path = flie_path[0]
            qimage = self.image_pred(flie_path)
            self.input.setPixmap(QPixmap(flie_path))
            self.output.setPixmap(QPixmap.fromImage(qimage))

    def open_video(self):
        print("点击检测了视频")

    def bind_slots(self):
        self.det_image.clicked.connect(self.open_image)
        self.det_video.clicked.connect(self.open_video)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec()
点击检测了图片
Traceback (most recent call last):
  File "E:\yolov5-v5.0\base_ui.py", line 30, in open_image
    qimage = self.image_pred(flie_path)
  File "E:\yolov5-v5.0\base_ui.py", line 22, in image_pred
    image = results.render()[0]
  File "E:\yolov5-v5.0\.\models\common.py", line 370, in render
    self.display(render=True)  # render results
  File "E:\yolov5-v5.0\.\models\common.py", line 344, in display
    plot_one_box(box, img, label=label, color=colors[int(cls) % 10])
  File "E:\yolov5-v5.0\.\utils\plots.py", line 62, in plot_one_box
    cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA)
cv2.error: OpenCV(4.9.0) :-1: error: (-5:Bad argument) in function 'rectangle'
> Overload resolution failed:
>  - img marked as output argument, but provided NumPy array marked as readonly
>  - Expected Ptr<cv::UMat> for argument 'img'
>  - argument for rectangle() given by name ('thickness') and position (4)
>  - argument for rectangle() given by name ('thickness') and position (4)
##hub_detect.ipynb
import torch


model = torch.hub.load('.', 'custom', r'E:\yolov5-v5.0\runs\train\exp8\weights\best.pt', source='local')
img = r"E:\yolov5-v5.0\VOCdevkit\images\train\cat.2.jpg"

results = model(img)
results.show()
results.render()[0]

#%%


  • 写回答

4条回答 默认 最新

  • 码农阿豪 Java领域优质创作者 2024-03-23 14:00
    关注
    让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
    问题描述:yolov5模型制作ui界面时选中图片后会报错,并不会在界面上输出结果。 可能原因:代码中存在OpenCV的报错,具体是因为在cv2.rectangle()函数中输入的img格式不正确,导致了OpenCV的错误。 解决方法:修改cv2.rectangle()函数中输入的img格式,确保其为正确的格式。 示例代码:
    import cv2
    def plot_one_box(x, img, color=None, label=None, line_thickness=None):
        """
        框选目标
        """
        tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1  # line/font thickness
        c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))
        cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA)
        if label:
            tf = max(tl - 1, 1)  # font thickness
            t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]
            c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3
            cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA)  # filled
            cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)
        return img
    

    在原代码中调用此函数即可避免报错。

    评论

报告相同问题?

问题事件

  • 创建了问题 3月23日

悬赏问题

  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现