yolov11pyqt5图像检测界面
时间: 2025-07-02 08:50:40 浏览: 6
### 使用PyQt5实现YOLOv1图像检测GUI界面
尽管提供的参考资料主要集中在YOLOv3和YOLOv5上,但基本原理对于YOLO系列模型是通用的。下面将详细介绍如何使用PyQt5来构建一个基于YOLOv1的目标检测图形用户界面。
#### 创建项目结构
首先定义清晰的文件夹结构有助于管理代码:
```
project/
│── main.py # 主程序入口
├── ui/ # 存放UI相关资源
│ └── mainwindow.ui # Qt Designer设计好的窗口布局文件
└── yolov1/ # YOLO v1 模型及相关配置
├── model.cfg # YOLO v1 配置文件
├── weights # 训练权重文件
└── classes.txt # 类别标签列表
```
#### 安装依赖项
确保安装必要的Python库:
```bash
pip install pyqt5 opencv-python torch torchvision
```
#### 设计UI界面
利用`Qt Designer`工具创建简洁直观的操作面板,保存为`.ui`格式并放置于指定路径下。此界面应至少包含以下几个部分:
- 开始识别按键:触发YOLO推理过程;
- 结果输出框:列出预测类别与位置信息。
#### 编写核心逻辑(main.py)
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QVBoxLayout, QWidget, QFileDialog
from PyQt5.uic import loadUi
import cv2
import numpy as np
import torch
from PIL import Image
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.init_ui()
self.load_model()
def init_ui(self):
"""初始化UI"""
loadUi('ui/mainwindow.ui', self)
self.selectImageButton.clicked.connect(self.open_image_dialog)
self.detectButton.clicked.connect(self.perform_detection)
def open_image_dialog(self):
options = QFileDialog.Options()
file_name, _ = QFileDialog.getOpenFileName(
None,
"Select an image",
"",
"Images (*.png *.jpg);;All Files (*)",
options=options
)
if file_name:
self.imagePath.setText(file_name)
def perform_detection(self):
img_path = self.imagePath.text().strip()
if not img_path or not os.path.exists(img_path):
return
try:
result_img = detect_objects_yolov1(img_path, self.model)
qimg = QImage(result_img.data, result_img.shape[1], result_img.shape[0], QImage.Format_RGB888).rgbSwapped()
pixmap = QPixmap.fromImage(qimg)
self.imageView.setPixmap(pixmap.scaled(640, 480))
except Exception as e:
print(f"Error during detection: {e}")
def prepare_input(image_path):
original_image = cv2.imread(image_path)
resized_image = cv2.resize(original_image, (448, 448)) / 255.
input_tensor = torch.tensor(resized_image.transpose((2, 0, 1)), dtype=torch.float32).unsqueeze_(0)
return input_tensor, original_image
def parse_predictions(predictions, threshold=0.2):
boxes = []
confidences = predictions[..., :2].sigmoid() * predictions[..., 2:].exp()
class_probs = F.softmax(predictions[:, :, 5:], dim=-1)
scores = confidences.max(-1)[0] * class_probs.max(-1)[0]
mask = scores > threshold
filtered_boxes = predictions[mask]
selected_indices = nms(filtered_boxes[:, :4], filtered_boxes[:, 4]*filtered_boxes[:, 5])
final_bboxes = filtered_boxes[selected_indices]
return final_bboxes.cpu().numpy()
def draw_bounding_box(image, bbox_data, labels=None):
h, w = image.shape[:2]
for box in bbox_data:
x_center, y_center, width, height = map(int, [box[i]*w if i%2==0 else box[i]*h for i in range(4)])
top_left_x = int(x_center - width//2)
top_left_y = int(y_center - height//2)
bottom_right_x = top_left_x + width
bottom_right_y = top_left_y + height
color = tuple(np.random.randint(0, 255, size=(3)).tolist())
label = f"{labels[int(box[-1])]}:{round(float(box[4]), 2)}" if labels is not None else ""
cv2.rectangle(image, (top_left_x, top_left_y), (bottom_right_x, bottom_right_y), color=color, thickness=2)
cv2.putText(image, text=label, org=(top_left_x, max(top_left_y-5, 0)),
fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=.7, color=color, thickness=2)
def detect_objects_yolov1(image_path, model):
inputs, orig_img = prepare_input(image_path)
outputs = model(inputs.to(device))[0]
bboxes = parse_predictions(outputs.permute([1, 2, 0]).reshape([-1, 30]))
draw_bounding_box(orig_img.copy(), bboxes, labels=['aero','bike','bird','boat','bottle'])
return orig_img
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
请注意上述代码中的某些细节可能需要根据实际使用的YOLOv1版本做适当调整,比如网络架构的具体参数设置、预处理方式以及后处理策略等[^1]。
阅读全文
相关推荐


















