yolov5识别系统前端界面
时间: 2025-05-23 12:12:39 浏览: 14
### 构建YOLOv5识别系统的前端界面实现
为了将YOLOv5模型与图形用户界面(GUI)相结合,可以利用PySide6或其他类似的框架来创建一个完整的系统。以下是关于如何实现这一目标的具体方法以及示例代码。
#### 使用PySide6构建YOLOv5的前端界面
PySide6 是 Qt 库的一个 Python 绑定版本,适合用于快速开发跨平台的应用程序。下面展示了一个基本的 GUI 实现方案,该方案允许用户加载图片并调用 YOLOv5 进行目标检测,最后显示结果。
```python
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QLabel, QFileDialog
from PyQt6.QtGui import QPixmap, QImage
import torch
from PIL import Image
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("YOLOv5 Object Detection with PySide6")
# Load the pre-trained model
self.model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # Use a local path or URL as needed [^1]
# Create widgets
self.label_image = QLabel(self)
self.button_load = QPushButton("Load Image", self)
self.button_detect = QPushButton("Detect Objects", self)
# Layout setup
layout = QVBoxLayout()
layout.addWidget(self.label_image)
layout.addWidget(self.button_load)
layout.addWidget(self.button_detect)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
# Connect buttons to functions
self.button_load.clicked.connect(self.open_file_dialog)
self.button_detect.clicked.connect(self.perform_detection)
def open_file_dialog(self):
options = QFileDialog.Options()
file_name, _ = QFileDialog.getOpenFileName(
self,
"QFileDialog.getOpenFileName()",
"",
"Images (*.png *.xpm *.jpg);;All Files (*)",
options=options
)
if file_name:
pixmap = QPixmap(file_name)
self.label_image.setPixmap(pixmap.scaled(640, 480))
def perform_detection(self):
image_path = self.label_image.pixmap().toImage()
if not image_path.isNull():
temp_image = image_path.save("temp.jpg") # Save temporarily for processing
results = self.model(Image.open("temp.jpg")) # Perform detection using YOLOv5 [^1]
detected_image = results.render()[0] # Render bounding boxes on the original image
qimage = QImage(detected_image.data, detected_image.shape[1], detected_image.shape[0], QImage.Format.Format_RGB888).rgbSwapped()
self.label_image.setPixmap(QPixmap.fromImage(qimage))
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
```
此代码片段展示了如何通过 PySide6 创建一个简单的窗口应用程序,并集成了 YOLOv5 模型来进行目标检测[^1]。用户可以选择一张图片并通过按钮触发检测过程,最终结果显示带有标注框的结果图像。
#### 关于保存检测结果至文本文件的功能扩展
如果希望进一步增强功能,比如将检测结果写入文本文件,则可以在 `perform_detection` 方法中加入如下逻辑:
```python
def save_results_to_text(results):
result_str = ""
detections = results.pandas().xyxy[0] # Convert results to pandas DataFrame
for _, row in detections.iterrows():
class_label = row['name']
confidence = row['confidence']
bbox = (row['xmin'], row['ymin'], row['xmax'], row['ymax'])
result_str += f"{class_label}: {bbox} Confidence: {confidence:.2f}\n"
with open("detection_results.txt", "w") as file:
file.write(result_str)
# Modify perform_detection method to include saving logic
detected_objects = results.pandas().xyxy[0]
save_results_to_text(results) # Call function after performing detection [^4]
```
这样就可以把每次检测得到的信息记录下来供后续查阅或与其他系统交互使用[^4]。
---
###
阅读全文
相关推荐


















