基于深度学习卷积网络的YOLOv5检测系统 opyqt界面+coco数据集训练+行人车辆检测计数

张开发
2026/6/10 20:29:00 15 分钟阅读
基于深度学习卷积网络的YOLOv5检测系统 opyqt界面+coco数据集训练+行人车辆检测计数
1.将detect.py运用到界面要将 YOLOv5 的检测结果与 PyQt 界面结合你需要进行一些额外的步骤。以下是一个简单的示例代码展示如何使用 YOLOv5 进行目标检测并在 PyQt 界面中显示结果。首先确保你已经安装了必要的库pip install opencv-python PyQt5 torch然后使用以下代码作为yolov5_detect_pyqt.py假设你要用detect.py进行推理你需要替换下面的detect函数采取 fpython detect.py即可importsysimportcv2importtorchfromPyQt5.QtWidgetsimportQApplication,QWidget,QLabel,QVBoxLayout,QPixmapfromPyQt5.QtGuiimportQImage,QPixmapfromyolov5.detectimportdetect# 导入你的 YOLOv5 检测函数classYOLOv5DetectApp(QWidget):def__init__(self):super().__init__()self.init_ui()definit_ui(self):self.setWindowTitle(YOLOv5 Object Detection with PyQt)self.setGeometry(100,100,800,600)self.labelQLabel(self)self.label.setAlignment(Qt.AlignCenter)vboxQVBoxLayout(self)vbox.addWidget(self.label)self.setLayout(vbox)self.timerQTimer(self)self.timer.timeout.connect(self.update_frame)self.timer.start(1000)# 设置定时器间隔单位为毫秒defupdate_frame(self):# 执行目标检测imagecv2.imread(path/to/your/image.jpg)# 替换成你的图像路径resultsdetect(image)# 使用你的 YOLOv5 检测函数# 在图像上绘制检测结果forresultinresults:labelresult[label]confidenceresult[confidence]boxresult[box]cv2.rectangle(image,(box[0],box[1]),(box[2],box[3]),(0,255,0),2)cv2.putText(image,f{label}{confidence:.2f},(box[0],box[1]-10),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,255,0),2)# 将 OpenCV 图像转换为 PyQt 可显示的格式height,width,channelimage.shape bytes_per_line3*width q_imageQImage(image.data,width,height,bytes_per_line,QImage.Format_RGB888)pixmapQPixmap.fromImage(q_image)# 在 PyQt 界面上显示图像self.label.setPixmap(pixmap)if__name____main__:appQApplication(sys.argv)windowYOLOv5DetectApp()window.show()sys.exit(app.exec_())请注意上述代码中的detect函数是一个示例函数你需要替换它为你的 YOLOv5 目标检测函数。确保你的目标检测函数返回一个包含检测结果的列表每个结果是一个字典包含标签、置信度和边界框信息。这只是一个简单的示例实际上你可能需要对界面进行更多的优化和交互性。此外如果你的检测过程很耗时你可能需要考虑使用多线程来确保界面的响应性。2.将发加载图片或视运用到界面如果你想通过按钮触发加载图片或视频你可以在 PyQt 窗口中添加按钮并通过按钮的点击事件触发加载操作。下面是修改后的代码其中添加了按钮用于加载图片importsysimportcv2fromPyQt5.QtWidgetsimportQApplication,QWidget,QLabel,QVBoxLayout,QPushButton,QFileDialogfromPyQt5.QtGuiimportQImage,QPixmapfromPyQt5.QtCoreimportQt,QTimerfromyolov5.detectimportdetect# 导入你的 YOLOv5 检测函数classYOLOv5DetectApp(QWidget):def__init__(self):super().__init__()self.init_ui()definit_ui(self):self.setWindowTitle(YOLOv5 Object Detection with PyQt)self.setGeometry(100,100,800,600)self.labelQLabel(self)self.label.setAlignment(Qt.AlignCenter)vboxQVBoxLayout(self)vbox.addWidget(self.label)# 添加按钮用于加载图片self.load_image_buttonQPushButton(Load Image,self)self.load_image_button.clicked.connect(self.load_image)vbox.addWidget(self.load_image_button)self.setLayout(vbox)self.timerQTimer(self)self.timer.timeout.connect(self.update_frame)self.timer.start(1000)# 设置定时器间隔单位为毫秒self.image_pathNone# 用于存储当前加载的图像路径defload_image(self):optionsQFileDialog.Options()options|QFileDialog.DontUseNativeDialog file_name,_QFileDialog.getOpenFileName(self,Open Image File,,Image Files (*.png *.jpg *.bmp);;All Files (*),optionsoptions)iffile_name:self.image_pathfile_namedefupdate_frame(self):ifself.image_pathisnotNone:# 执行目标检测imagecv2.imread(self.image_path)resultsdetect(image)# 使用你的 YOLOv5 检测函数# 在图像上绘制检测结果forresultinresults:labelresult[label]confidenceresult[confidence]boxresult[box]cv2.rectangle(image,(box[0],box[1]),(box[2],box[3]),(0,255,0),2)cv2.putText(image,f{label}{confidence:.2f},(box[0],box[1]-10),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,255,0),2)# 将 OpenCV 图像转换为 PyQt 可显示的格式height,width,channelimage.shape bytes_per_line3*width q_imageQImage(image.data,width,height,bytes_per_line,QImage.Format_RGB888)pixmapQPixmap.fromImage(q_image)# 在 PyQt 界面上显示图像self.label.setPixmap(pixmap)if__name____main__:appQApplication(sys.argv)windowYOLOv5DetectApp()window.show()sys.exit(app.exec_())在这个例子中通过添加QPushButton实例load_image_button和连接clicked信号到load_image方法实现了通过按钮加载图像的功能。当按钮被点击时将弹出文件对话框允许用户选择要加载的图像文件。加载的图像路径存储在self.image_path中并在定时器的update_frame方法中使用。

更多文章