无恙885 2024-03-20 17:13 采纳率: 0%
浏览 446

mg marked as output argument, but provided NumPy array marked as readonly

GUI界面运行报错:
做的是用yolov5做图像识别,因为自己电脑没有显卡跑得慢,后来上网租了显卡跑,但是最后做界面进型图像检测的时候用自己本地跑的模型能正常识别,但是用云端跑的就不行。在csdn上搜这个报错,出来这个结果http://t.csdnimg.cn/NKemg。里面说是opencv-python-headless版本不支持,但是我按他的操作来还是不行。恳请们大家给支点招。
这是代码:

import sys
import torch
from PySide6.QtWidgets import *
from PySide6.QtGui import QPixmap, QImage
from main_windows_ui import Ui_MainWindow
from PIL import Image, ImageTk

def convert2QImage(img):
    height, width, channel = img.shape
    return QImage(img, width, height, width * channel, QImage.Format_RGB888)


class Mainwindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(Mainwindow, self).__init__()
        self.setupUi(self)
        self.bind_slots()
        self.model = torch.hub.load('./', "custom", path = 'runs\\train\\exp_392_Adam_200\\weights\\best.pt', source = 'local')
        self.file_path = None

    def open_img(self):
        self.file_path, _ = QFileDialog.getOpenFileName(self, dir="D:/desktop", filter='*.png;*.jpg;*.tif')
        print(self.file_path)
        if self.file_path:
            pixmap = QPixmap()
            pixmap.load(self.file_path)
            self.input.setPixmap(pixmap)

    def detect_img(self):
        if self.file_path:
            results = self.model(self.file_path)
            print(111)
            image = results.render()
            print(image,type(image[0]))
            qimage = convert2QImage(image[0])
            self.output.setPixmap(QPixmap.fromImage(qimage))

    def bind_slots(self):
        self.input_but.clicked.connect(self.open_img)
        self.detect_but.clicked.connect(self.detect_img)



if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Mainwindow()
    window.show()
    app.exec()


这是报错:

  • img marked as output argument, but provided NumPy array marked as readonly
  • Expected Ptrcv::UMat for argument 'img'
  • argument for rectangle() given by name ('thickness') and position (4)
  • argument for rectangle() given by name ('thickness') and position (4)
  • 写回答

1条回答 默认 最新

  • 码农阿豪@新空间 新星创作者: 前端开发技术领域 2024-03-20 17:13
    关注
    让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
    问题分析:根据报错信息,可能是因为在检测图片时使用了一个 NumPy 数组作为输出参数,但是该数组被标记为只读。此外,还可能是由于没有正确安装 opencv-python-headless 的问题导致的,需要检查安装是否正确并尝试重新安装。 解决方法:可以尝试更改代码中的 detect_img() 方法,用 cv2.imread() 函数加载图片,并将其传递给模型进行识别。 示例代码如下:
    import sys
    import torch
    import cv2
    from PySide6.QtWidgets import *
    from PySide6.QtGui import QPixmap, QImage
    from main_windows_ui import Ui_MainWindow
    from PIL import Image, ImageTk
    def convert2QImage(img):
        height, width, channel = img.shape
        return QImage(img, width, height, width * channel, QImage.Format_RGB888)
    class Mainwindow(QMainWindow, Ui_MainWindow):
        def __init__(self):
            super(Mainwindow, self).__init__()
            self.setupUi(self)
            self.bind_slots()
            self.model = torch.hub.load('./', "custom", path = 'runs/train/exp_392_Adam_200/weights/best.pt', source = 'local')
            self.file_path = None
        
        def open_img(self):
            self.file_path, _ = QFileDialog.getOpenFileName(self, dir="D:/desktop", filter='*.png;*.jpg;*.tif')
            print(self.file_path)
            if self.file_path:
                pixmap = QPixmap()
                pixmap.load(self.file_path)
                self.input.setPixmap(pixmap)
        
        def detect_img(self):
            if self.file_path:
                img = cv2.imread(self.file_path)
                results = self.model(img)
                print(111)
                image = results.render()
                print(image, type(image[0]))
                qimage = convert2QImage(image[0])
                self.output.setPixmap(QPixmap.fromImage(qimage))
        
        def bind_slots(self):
            self.input_but.clicked.connect(self.open_img)
            self.detect_but.clicked.connect(self.detect_img)
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = Mainwindow()
        window.show()
        app.exec()
    

    注意,在这个示例代码中,我们使用了 cv2.imread() 函数来加载图片,并将其传递给模型进行识别。需要先使用 pip 安装 opencv-python 组件。 希望能帮助到你解决问题。

    评论

报告相同问题?

问题事件

  • 创建了问题 3月20日