yolo11-目标检测&官方模型测试

这里实现了一个python程序,用于测试模型的目标检测功能

1.测试代码

复制代码
import os
import cv2
from PyQt5.QtWidgets import (QApplication, QMainWindow, QVBoxLayout, QHBoxLayout, QLabel, 
                            QComboBox, QPushButton, QFileDialog, QWidget, QGroupBox)
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtCore import Qt
from ultralytics import YOLO

class YOLOViewer(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("YOLO 图像识别工具")
        self.setGeometry(100, 100, 1200, 800)
        
        # 初始化变量
        self.image_dir = ""
        self.model_dir = ""
        self.yaml_path = ""
        self.current_image = None
        self.current_model = None
        self.classes = []
        
        # 创建主界面
        self.init_ui()
        
    def init_ui(self):
        # 主布局
        main_widget = QWidget()
        main_layout = QHBoxLayout()
        
        # 左侧控制面板
        control_panel = QGroupBox("控制面板")
        control_layout = QVBoxLayout()
        
        # 图片目录选择
        self.btn_image_dir = QPushButton("选择图片目录")
        self.btn_image_dir.clicked.connect(self.select_image_dir)
        self.combo_images = QComboBox()
        self.combo_images.currentIndexChanged.connect(self.load_image)
        
        # 模型选择
        self.btn_model_dir = QPushButton("选择模型目录")
        self.btn_model_dir.clicked.connect(self.select_model_dir)
        self.combo_models = QComboBox()
        self.combo_models.currentIndexChanged.connect(self.load_model)
        
        # YAML文件选择
        self.btn_yaml = QPushButton("选择YAML文件")
        self.btn_yaml.clicked.connect(self.select_yaml_file)
        self.combo_classes = QComboBox()
        self.combo_classes.currentIndexChanged.connect(self.update_detection)
        
        # 识别按钮
        self.btn_detect = QPushButton("执行识别")
        self.btn_detect.clicked.connect(self.detect_objects)
        
        # 添加到控制面板
        control_layout.addWidget(QLabel("图片目录:"))
        control_layout.addWidget(self.btn_image_dir)
        control_layout.addWidget(self.combo_images)
        
        control_layout.addWidget(QLabel("模型选择:"))
        control_layout.addWidget(self.btn_model_dir)
        control_layout.addWidget(self.combo_models)
        
        control_layout.addWidget(QLabel("类别选择:"))
        control_layout.addWidget(self.btn_yaml)
        control_layout.addWidget(self.combo_classes)
        
        control_layout.addWidget(self.btn_detect)
        control_layout.addStretch()
        
        control_panel.setLayout(control_layout)
        
        # 右侧图像显示
        self.image_label = QLabel()
        self.image_label.setAlignment(Qt.AlignCenter)
        self.image_label.setStyleSheet("border: 1px solid black;")
        
        # 添加布局
        main_layout.addWidget(control_panel, stretch=1)
        main_layout.addWidget(self.image_label, stretch=3)
        
        main_widget.setLayout(main_layout)
        self.setCentralWidget(main_widget)
    
    def select_image_dir(self):
        """选择图片目录"""
        self.image_dir = QFileDialog.getExistingDirectory(self, "选择图片目录")
        if self.image_dir:
            self.load_image_list()
    
    def load_image_list(self):
        """加载图片列表到下拉框"""
        self.combo_images.clear()
        image_files = [f for f in os.listdir(self.image_dir) 
                      if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp'))]
        self.combo_images.addItems(image_files)
    
    def select_model_dir(self):
        """选择模型目录"""
        self.model_dir = QFileDialog.getExistingDirectory(self, "选择模型目录")
        if self.model_dir:
            self.load_model_list()
    
    def load_model_list(self):
        """加载模型列表到下拉框"""
        self.combo_models.clear()
        model_files = [f for f in os.listdir(self.model_dir) 
                      if f.lower().endswith('.pt')]
        self.combo_models.addItems(model_files)
    
    def select_yaml_file(self):
        """选择YAML文件"""
        self.yaml_path, _ = QFileDialog.getOpenFileName(self, "选择YAML文件", "", "YAML Files (*.yaml *.yml)")
        if self.yaml_path:
            self.load_classes()
    
    def load_classes(self):
        try:
            import yaml
            with open(self.yaml_path, 'r', encoding='utf-8') as f:
                data = yaml.safe_load(f)
                self.classes = data.get('names', {})  # 确保是字典格式
                
                self.combo_classes.clear()
                self.combo_classes.addItem("所有类别", "all")
                
                # 直接遍历字典的键值对
                for idx, name in self.classes.items():
                    self.combo_classes.addItem(f"{idx}: {name}", idx)
        except Exception as e:
            print(f"加载YAML文件出错: {e}")
    
    def load_image(self):
        """加载选中的图片"""
        if self.combo_images.currentIndex() >= 0:
            image_file = os.path.join(self.image_dir, self.combo_images.currentText())
            self.current_image = cv2.imread(image_file)
            self.display_image(self.current_image)
    
    def load_model(self):
        """加载选中的模型"""
        if self.combo_models.currentIndex() >= 0:
            model_file = os.path.join(self.model_dir, self.combo_models.currentText())
            self.current_model = YOLO(model_file)
    
    def detect_objects(self):
        """执行目标检测,正确处理类别选择"""
        if self.current_image is None or self.current_model is None:
            return
            
        selected_text = self.combo_classes.currentText()  # 如 "0: person"
        if selected_text == "所有类别":
            classes = None  # 检测所有类别
        else:
            class_idx = int(selected_text.split(":")[0])  # 提取数字部分
            classes = [class_idx]
        
        results = self.current_model.predict(self.current_image, classes=classes)
        annotated_image = results[0].plot()
        self.display_image(annotated_image)
    
    def update_detection(self):
        """当类别改变时自动更新检测结果"""
        if self.current_image is not None and self.current_model is not None:
            self.detect_objects()
    
    def display_image(self, image):
        """显示图片"""
        if image is not None:
            # 转换颜色空间 BGR -> RGB
            rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            h, w, ch = rgb_image.shape
            bytes_per_line = ch * w
            qt_image = QImage(rgb_image.data, w, h, bytes_per_line, QImage.Format_RGB888)
            pixmap = QPixmap.fromImage(qt_image)
            
            # 缩放图片以适应标签大小
            scaled_pixmap = pixmap.scaled(
                self.image_label.size(), 
                Qt.KeepAspectRatio, 
                Qt.SmoothTransformation
            )
            self.image_label.setPixmap(scaled_pixmap)

if __name__ == "__main__":
    app = QApplication([])
    window = YOLOViewer()
    window.show()
    app.exec_()

需要导入模型的对应yaml文件,用于类型区分

2.官方模型测试

人类

相关推荐
X54先生(人文科技)25 分钟前
《元创力》纪实录·实战篇先卷后观:碳硅对位范式的首次实战归档
人工智能·ai写作·开源协议
scan72427 分钟前
智能体多个工具调用
python
Risk Actuary29 分钟前
快速傅里叶变换与聚合风险精算模型
人工智能·深度学习·机器学习
2401_8676239834 分钟前
CSS Flex布局中如何设置子元素间距_掌握gap属性的现代用法
jvm·数据库·python
莱歌数字41 分钟前
ANSYS模拟仿真不锈钢件激光焊接变形量
人工智能·科技·电脑·制造·散热
即使再小的船也能远航41 分钟前
【Python】安装
开发语言·python
weixin_4217252642 分钟前
Linux 编程语言全解析:C、C++、Python、Go、Rust 谁更强?
linux·python·go·c·编程语言
冬奇Lab1 小时前
理发师会被 AI 取代吗?这可能是 AI 时代最有意思的一个社会学问题
人工智能·aigc
没有梦想的咸鱼185-1037-16631 小时前
AI-Python机器学习、深度学习核心技术与前沿应用及OpenClaw、Hermes自动化编程
人工智能·python·深度学习·机器学习·chatgpt·数据挖掘·数据分析
渣渣苏1 小时前
怎么量化一个Agent的性能?
人工智能·ai·agent·智能体