这个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]
#%%