yolo小damo合集

效果如下:这个是图片检测

效果如下:这个是视频检测

效果如下:这个是摄像头检测

1 相关库

除了yolov11所用库之外,本文所用到的额外库为pyqt5,输入指令进行安装

复制代码
pip install pyqt5
导入所需要的库
python 复制代码
import sys
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget, QPushButton, QHBoxLayout, QMessageBox, QFileDialog
from PyQt5.QtGui import QImage, QPixmap, QIcon
import cv2
from ultralytics import YOLO

2 界面

使用pyqt5进行界面创作

python 复制代码
"""
        初始化主窗口。
        这个方法设置了窗口的标题、图标、大小,并初始化了各种组件,如标签、按钮、布局等。
        还创建了用于摄像头检测的定时器,并设置了初始状态为未暂停。
        """
        super().__init__()
        # 设置窗口标题
        self.setWindowTitle("@author:晓晓")
        # self.setWindowIcon(QIcon("icon.png"))
        # 设置窗口大小
        self.setGeometry(300, 150, 800, 400)

        # 创建两个QLabel分别显示左右图像
        # 左侧图像标签,用于显示原始图像
        self.label1 = QLabel()
        # 设置标签内容居中对齐
        self.label1.setAlignment(Qt.AlignCenter)
        # 设置标签最小尺寸
        self.label1.setMinimumSize(580, 450)  
        # 设置标签样式,添加边框并设置背景颜色为黑色
        self.label1.setStyleSheet('border:3px solid #6950a1; background-color: black;')  

        # 右侧图像标签,用于显示检测后的图像
        self.label2 = QLabel()
        # 设置标签内容居中对齐
        self.label2.setAlignment(Qt.AlignCenter)
        # 设置标签最小尺寸
        self.label2.setMinimumSize(580, 450)  
        # 设置标签样式,添加边框并设置背景颜色为黑色
        self.label2.setStyleSheet('border:3px solid #6950a1; background-color: black;')  

        ###摄像头检测
        # 用于存储摄像头对象
        self.camera = None  
        # 定时器,用于定时从摄像头获取帧
        self.camera_timer = QTimer()  
        # 定时器超时连接到更新摄像头帧的方法
        self.camera_timer.timeout.connect(self.update_camera_frame)  

        # 新增属性,用于标记视频检测是否暂停
        self.is_video_paused = False
        # 新增属性,用于标记摄像头检测是否暂停
        self.is_camera_paused = False

        # 水平布局,用于放置左右两个QLabel
        layout = QVBoxLayout()
        # layout.addWidget(self.label1)
        hbox_video = QHBoxLayout()
        # 在水平布局中添加左侧标签,用于显示原始图像
        hbox_video.addWidget(self.label1)  
        # 在水平布局中添加右侧标签,用于显示检测后的图像
        hbox_video.addWidget(self.label2)  
        # 将水平布局添加到垂直布局中
        layout.addLayout(hbox_video)
        # 创建Worker对象
        self.worker = Worker()
        # 创建按钮布局
        hbox_buttons = QHBoxLayout()
        # 添加模型选择按钮
        self.load_model_button = QPushButton("📁模型选择")
        # 点击按钮时连接到load_model方法
        self.load_model_button.clicked.connect(self.load_model)
        # 设置按钮固定大小
        self.load_model_button.setFixedSize(120, 30)
        # 将按钮添加到按钮布局中
        hbox_buttons.addWidget(self.load_model_button)

        # 添加图片检测按钮
        self.image_detect_button = QPushButton("💾图片检测")
        # 点击按钮时连接到detect_image方法
        self.image_detect_button.clicked.connect(self.detect_image)
        # 初始状态下禁用按钮
        self.image_detect_button.setEnabled(False)
        # 设置按钮固定大小
        self.image_detect_button.setFixedSize(120, 30)
        # 将按钮添加到按钮布局中
        hbox_buttons.addWidget(self.image_detect_button)

        # 添加视频检测按钮
        self.video_detect_button = QPushButton("🎬视频检测")
        # 点击按钮时连接到detect_video_start方法
        self.video_detect_button.clicked.connect(self.detect_video_start)
        # 初始状态下禁用按钮
        self.video_detect_button.setEnabled(False)
        # 设置按钮固定大小
        self.video_detect_button.setFixedSize(120, 30)
        # 将按钮添加到按钮布局中
        hbox_buttons.addWidget(self.video_detect_button)

        # 添加摄像头检测按钮
        self.camera_detect_button = QPushButton("📷摄像头检测")
        # 点击按钮时连接到start_camera_detection方法
        self.camera_detect_button.clicked.connect(self.start_camera_detection)
        # 初始状态下禁用按钮
        self.camera_detect_button.setEnabled(False)
        # 设置按钮固定大小
        self.camera_detect_button.setFixedSize(120, 30)
        # 将按钮添加到按钮布局中
        hbox_buttons.addWidget(self.camera_detect_button)

        # 添加显示检测物体按钮
        self.display_objects_button = QPushButton("🔍显示检测物体")
        # 点击按钮时连接到show_detected_objects方法
        self.display_objects_button.clicked.connect(self.show_detected_objects)
        # 初始状态下禁用按钮
        self.display_objects_button.setEnabled(False)
        # 设置按钮固定大小
        self.display_objects_button.setFixedSize(120, 30)
        # 将按钮添加到按钮布局中
        hbox_buttons.addWidget(self.display_objects_button)

        # 添加退出按钮
        self.exit_button = QPushButton("❌退出")
        # 点击按钮时连接到exit_application方法
        self.exit_button.clicked.connect(self.exit_application)
        # 设置按钮固定大小
        self.exit_button.setFixedSize(120, 30)
        # 将按钮添加到按钮布局中
        hbox_buttons.addWidget(self.exit_button)

        # 将按钮布局添加到垂直布局中
        layout.addLayout(hbox_buttons)
        # 创建中心部件
        central_widget = QWidget()
        # 设置中心部件的布局
        central_widget.setLayout(layout)
        # 设置窗口的中心部件
        self.setCentralWidget(central_widget)

        # 用于存储当前检测结果
        self.current_results = None

3实现

每个部分的按钮实现

python 复制代码
    def detect_image(self):
        if self.camera_timer.isActive():  # 判断摄像头检测是否正在运行,如果是则关闭
            self.camera_timer.stop()
            if self.camera is not None:
                self.camera.release()

        image_path, _ = QFileDialog.getOpenFileName(None, "选择图片文件", "", "图片文件 (*.jpg *.jpeg *.png)")
        if image_path:
            image = cv2.imread(image_path)
            if image is not None:
                self.current_results = self.worker.model.predict(image)
                if self.current_results:
                    annotated_image = self.current_results[0].plot()
                    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)  # 转换为RGB
                    height1, width1, channel1 = image_rgb.shape
                    bytesPerLine1 = 3 * width1
                    qimage1 = QImage(image_rgb.data, width1, height1, bytesPerLine1, QImage.Format_RGB888)
                    pixmap1 = QPixmap.fromImage(qimage1)
                    self.label1.setPixmap(pixmap1.scaled(self.label1.size(), Qt.KeepAspectRatio))

                    annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)  # 转换为RGB
                    height2, width2, channel2 = annotated_image.shape
                    bytesPerLine2 = 3 * width2
                    qimage2 = QImage(annotated_image.data, width2, height2, bytesPerLine2, QImage.Format_RGB888)
                    pixmap2 = QPixmap.fromImage(qimage2)
                    self.label2.setPixmap(pixmap2.scaled(self.label2.size(), Qt.KeepAspectRatio))

    def detect_video_start(self):
        if self.camera_timer.isActive():  # 判断摄像头检测是否正在运行,如果是则关闭
            self.camera_timer.stop()
            if self.camera is not None:
                self.camera.release()

        video_path, _ = QFileDialog.getOpenFileName(None, "选择视频文件", "", "视频文件 (*.mp4 *.avi)")
        if video_path:
            cap = cv2.VideoCapture(video_path)
            while cap.isOpened():
                success, frame = cap.read()
                if success:
                    frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                    height1, width1, channel1 = frame_rgb.shape
                    bytesPerLine1 = 3 * width1
                    qimage = QImage(frame_rgb.data, width1, height1, bytesPerLine1, QImage.Format_RGB888)
                    pixmap = QPixmap.fromImage(qimage)
                    self.label1.setPixmap(pixmap.scaled(self.label1.size(), Qt.KeepAspectRatio))

                    results = self.worker.model.predict(frame)
                    # Visualize the results on the frame
                    annotated_frame = results[0].plot()

                    frame_rgb = cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB)
                    height2, width2, channel2 = frame_rgb.shape
                    bytesPerLine2 = 3 * width2
                    qimage = QImage(frame_rgb.data, width2, height2, bytesPerLine2, QImage.Format_RGB888)
                    pixmap = QPixmap.fromImage(qimage)
                    self.label2.setPixmap(pixmap.scaled(self.label2.size(), Qt.KeepAspectRatio))

                    if cv2.waitKey(1) & 0xFF == ord("q"):
                        break
                else:
                    # Break the loop if the end of the video is reached
                    break

            # Release the video capture object and close the display window
            cap.release()
            cv2.destroyAllWindows()

    def start_camera_detection(self):
        self.camera = cv2.VideoCapture(0)  # 打开默认摄像头(索引为0)
        if self.camera.isOpened():
            self.camera_timer.start(30)  # 设置定时器间隔,每30毫秒更新一帧
        else:
            QMessageBox.warning(self, "摄像头错误", "无法打开摄像头,请检查设备是否连接正常。")

    def update_camera_frame(self):
        success, frame = self.camera.read()
        if success:
            # 对获取到的摄像头帧进行水平翻转,实现镜像效果
            frame = cv2.flip(frame, 1)
            frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            height1, width1, channel1 = frame_rgb.shape
            bytesPerLine1 = 3 * width1
            qimage = QImage(frame_rgb.data, width1, height1, bytesPerLine1, QImage.Format_RGB888)
            pixmap = QPixmap.fromImage(qimage)
            self.label1.setPixmap(pixmap.scaled(self.label1.size(), Qt.KeepAspectRatio))

            results = self.worker.model.predict(frame)
            # 此处修改,将每次摄像头检测的结果都更新到self.current_results中(以列表形式存储每次结果)
            if self.current_results is None:
                self.current_results = []
            self.current_results.clear()
            self.current_results.append(results)

            annotated_frame = results[0].plot()
            frame_rgb = cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB)
            height2, width2, channel2 = frame_rgb.shape
            bytesPerLine2 = 3 * width2
            qimage = QImage(frame_rgb.data, width2, height2, bytesPerLine2, QImage.Format_RGB888)
            pixmap = QPixmap.fromImage(qimage)
            self.label2.setPixmap(pixmap.scaled(self.label2.size(), Qt.KeepAspectRatio))
        else:
            self.camera_timer.stop()
            self.camera.release()
            QMessageBox.warning(self, "摄像头错误", "摄像头读取失败,请检查设备是否正常工作。")

    def show_detected_objects(self):
        if self.current_results:
            all_det_info = []
            for result in self.current_results:
                det_info = result[0].boxes.cls
                all_det_info.extend(det_info.tolist())
            object_count = len(all_det_info)
            object_info = f"识别到的物体总个数:{object_count}\n"
            object_dict = {}
            class_names_dict = self.current_results[0][0].names
            for class_id in all_det_info:
                class_name = class_names_dict[int(class_id)]
                if class_name in object_dict:
                    object_dict[class_name] += 1
                else:
                    object_dict[class_name] = 1
            sorted_objects = sorted(object_dict.items(), key=lambda x: x[1], reverse=True)
            for obj_name, obj_count in sorted_objects:
                object_info += f"{obj_name}: {obj_count}\n"
            if self.video_detect_button.isEnabled() and self.video_detect_button.isChecked():
                self.is_video_paused = True
            elif self.camera_detect_button.isEnabled() and self.camera_detect_button.isChecked():
                self.is_camera_paused = True

            msg_box = QMessageBox(self)
            msg_box.setWindowTitle("识别结果")
            msg_box.setText(object_info)
            msg_box.finished.connect(self.resume_detection)
            msg_box.exec_()
        else:
            self.show_message_box("识别结果", "未检测到物体")

    def show_message_box(self, title, message):
        msg_box = QMessageBox(self)
        msg_box.setWindowTitle(title)
        msg_box.setText(message)
        msg_box.exec_()

    def load_model(self):
        if self.worker.load_model():
            self.image_detect_button.setEnabled(True)
            self.display_objects_button.setEnabled(True)
            self.video_detect_button.setEnabled(True)
            self.camera_detect_button.setEnabled(True)

    def resume_detection(self):
        if self.is_video_paused:
            self.is_video_paused = False
        elif self.is_camera_paused:
            self.is_camera_paused = False

    def exit_application(self):
        # 终止程序运行
        sys.exit()

4总代码

python 复制代码
import sys
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget, QPushButton, QHBoxLayout, QMessageBox, QFileDialog
from PyQt5.QtGui import QImage, QPixmap, QIcon
import cv2
from ultralytics import YOLO


class Worker:
    def __init__(self):
        self.model = None

    def load_model(self):
        model_path, _ = QFileDialog.getOpenFileName(None, "选择模型文件", "", "模型文件 (*.pt)")
        if model_path:
            self.model = YOLO(model_path)
            return self.model is not None
        return False


class MainWindow(QMainWindow):
    def __init__(self):
        """
        初始化主窗口。
        这个方法设置了窗口的标题、图标、大小,并初始化了各种组件,如标签、按钮、布局等。
        还创建了用于摄像头检测的定时器,并设置了初始状态为未暂停。
        """
        super().__init__()
        # 设置窗口标题
        self.setWindowTitle("@author:晓晓")
        # self.setWindowIcon(QIcon("icon.png"))
        # 设置窗口大小
        self.setGeometry(300, 150, 800, 400)

        # 创建两个QLabel分别显示左右图像
        # 左侧图像标签,用于显示原始图像
        self.label1 = QLabel()
        # 设置标签内容居中对齐
        self.label1.setAlignment(Qt.AlignCenter)
        # 设置标签最小尺寸
        self.label1.setMinimumSize(580, 450)  
        # 设置标签样式,添加边框并设置背景颜色为黑色
        self.label1.setStyleSheet('border:3px solid #6950a1; background-color: black;')  

        # 右侧图像标签,用于显示检测后的图像
        self.label2 = QLabel()
        # 设置标签内容居中对齐
        self.label2.setAlignment(Qt.AlignCenter)
        # 设置标签最小尺寸
        self.label2.setMinimumSize(580, 450)  
        # 设置标签样式,添加边框并设置背景颜色为黑色
        self.label2.setStyleSheet('border:3px solid #6950a1; background-color: black;')  

        ###摄像头检测
        # 用于存储摄像头对象
        self.camera = None  
        # 定时器,用于定时从摄像头获取帧
        self.camera_timer = QTimer()  
        # 定时器超时连接到更新摄像头帧的方法
        self.camera_timer.timeout.connect(self.update_camera_frame)  

        # 新增属性,用于标记视频检测是否暂停
        self.is_video_paused = False
        # 新增属性,用于标记摄像头检测是否暂停
        self.is_camera_paused = False

        # 水平布局,用于放置左右两个QLabel
        layout = QVBoxLayout()
        # layout.addWidget(self.label1)
        hbox_video = QHBoxLayout()
        # 在水平布局中添加左侧标签,用于显示原始图像
        hbox_video.addWidget(self.label1)  
        # 在水平布局中添加右侧标签,用于显示检测后的图像
        hbox_video.addWidget(self.label2)  
        # 将水平布局添加到垂直布局中
        layout.addLayout(hbox_video)
        # 创建Worker对象
        self.worker = Worker()
        # 创建按钮布局
        hbox_buttons = QHBoxLayout()
        # 添加模型选择按钮
        self.load_model_button = QPushButton("📁模型选择")
        # 点击按钮时连接到load_model方法
        self.load_model_button.clicked.connect(self.load_model)
        # 设置按钮固定大小
        self.load_model_button.setFixedSize(120, 30)
        # 将按钮添加到按钮布局中
        hbox_buttons.addWidget(self.load_model_button)

        # 添加图片检测按钮
        self.image_detect_button = QPushButton("💾图片检测")
        # 点击按钮时连接到detect_image方法
        self.image_detect_button.clicked.connect(self.detect_image)
        # 初始状态下禁用按钮
        self.image_detect_button.setEnabled(False)
        # 设置按钮固定大小
        self.image_detect_button.setFixedSize(120, 30)
        # 将按钮添加到按钮布局中
        hbox_buttons.addWidget(self.image_detect_button)

        # 添加视频检测按钮
        self.video_detect_button = QPushButton("🎬视频检测")
        # 点击按钮时连接到detect_video_start方法
        self.video_detect_button.clicked.connect(self.detect_video_start)
        # 初始状态下禁用按钮
        self.video_detect_button.setEnabled(False)
        # 设置按钮固定大小
        self.video_detect_button.setFixedSize(120, 30)
        # 将按钮添加到按钮布局中
        hbox_buttons.addWidget(self.video_detect_button)

        # 添加摄像头检测按钮
        self.camera_detect_button = QPushButton("📷摄像头检测")
        # 点击按钮时连接到start_camera_detection方法
        self.camera_detect_button.clicked.connect(self.start_camera_detection)
        # 初始状态下禁用按钮
        self.camera_detect_button.setEnabled(False)
        # 设置按钮固定大小
        self.camera_detect_button.setFixedSize(120, 30)
        # 将按钮添加到按钮布局中
        hbox_buttons.addWidget(self.camera_detect_button)

        # 添加显示检测物体按钮
        self.display_objects_button = QPushButton("🔍显示检测物体")
        # 点击按钮时连接到show_detected_objects方法
        self.display_objects_button.clicked.connect(self.show_detected_objects)
        # 初始状态下禁用按钮
        self.display_objects_button.setEnabled(False)
        # 设置按钮固定大小
        self.display_objects_button.setFixedSize(120, 30)
        # 将按钮添加到按钮布局中
        hbox_buttons.addWidget(self.display_objects_button)

        # 添加退出按钮
        self.exit_button = QPushButton("❌退出")
        # 点击按钮时连接到exit_application方法
        self.exit_button.clicked.connect(self.exit_application)
        # 设置按钮固定大小
        self.exit_button.setFixedSize(120, 30)
        # 将按钮添加到按钮布局中
        hbox_buttons.addWidget(self.exit_button)

        # 将按钮布局添加到垂直布局中
        layout.addLayout(hbox_buttons)
        # 创建中心部件
        central_widget = QWidget()
        # 设置中心部件的布局
        central_widget.setLayout(layout)
        # 设置窗口的中心部件
        self.setCentralWidget(central_widget)

        # 用于存储当前检测结果
        self.current_results = None

    def detect_image(self):
        if self.camera_timer.isActive():  # 判断摄像头检测是否正在运行,如果是则关闭
            self.camera_timer.stop()
            if self.camera is not None:
                self.camera.release()

        image_path, _ = QFileDialog.getOpenFileName(None, "选择图片文件", "", "图片文件 (*.jpg *.jpeg *.png)")
        if image_path:
            image = cv2.imread(image_path)
            if image is not None:
                self.current_results = self.worker.model.predict(image)
                if self.current_results:
                    annotated_image = self.current_results[0].plot()
                    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)  # 转换为RGB
                    height1, width1, channel1 = image_rgb.shape
                    bytesPerLine1 = 3 * width1
                    qimage1 = QImage(image_rgb.data, width1, height1, bytesPerLine1, QImage.Format_RGB888)
                    pixmap1 = QPixmap.fromImage(qimage1)
                    self.label1.setPixmap(pixmap1.scaled(self.label1.size(), Qt.KeepAspectRatio))

                    annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)  # 转换为RGB
                    height2, width2, channel2 = annotated_image.shape
                    bytesPerLine2 = 3 * width2
                    qimage2 = QImage(annotated_image.data, width2, height2, bytesPerLine2, QImage.Format_RGB888)
                    pixmap2 = QPixmap.fromImage(qimage2)
                    self.label2.setPixmap(pixmap2.scaled(self.label2.size(), Qt.KeepAspectRatio))

    def detect_video_start(self):
        if self.camera_timer.isActive():  # 判断摄像头检测是否正在运行,如果是则关闭
            self.camera_timer.stop()
            if self.camera is not None:
                self.camera.release()

        video_path, _ = QFileDialog.getOpenFileName(None, "选择视频文件", "", "视频文件 (*.mp4 *.avi)")
        if video_path:
            cap = cv2.VideoCapture(video_path)
            while cap.isOpened():
                success, frame = cap.read()
                if success:
                    frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                    height1, width1, channel1 = frame_rgb.shape
                    bytesPerLine1 = 3 * width1
                    qimage = QImage(frame_rgb.data, width1, height1, bytesPerLine1, QImage.Format_RGB888)
                    pixmap = QPixmap.fromImage(qimage)
                    self.label1.setPixmap(pixmap.scaled(self.label1.size(), Qt.KeepAspectRatio))

                    results = self.worker.model.predict(frame)
                    # Visualize the results on the frame
                    annotated_frame = results[0].plot()

                    frame_rgb = cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB)
                    height2, width2, channel2 = frame_rgb.shape
                    bytesPerLine2 = 3 * width2
                    qimage = QImage(frame_rgb.data, width2, height2, bytesPerLine2, QImage.Format_RGB888)
                    pixmap = QPixmap.fromImage(qimage)
                    self.label2.setPixmap(pixmap.scaled(self.label2.size(), Qt.KeepAspectRatio))

                    if cv2.waitKey(1) & 0xFF == ord("q"):
                        break
                else:
                    # Break the loop if the end of the video is reached
                    break

            # Release the video capture object and close the display window
            cap.release()
            cv2.destroyAllWindows()

    def start_camera_detection(self):
        self.camera = cv2.VideoCapture(0)  # 打开默认摄像头(索引为0)
        if self.camera.isOpened():
            self.camera_timer.start(30)  # 设置定时器间隔,每30毫秒更新一帧
        else:
            QMessageBox.warning(self, "摄像头错误", "无法打开摄像头,请检查设备是否连接正常。")

    def update_camera_frame(self):
        success, frame = self.camera.read()
        if success:
            # 对获取到的摄像头帧进行水平翻转,实现镜像效果
            frame = cv2.flip(frame, 1)
            frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            height1, width1, channel1 = frame_rgb.shape
            bytesPerLine1 = 3 * width1
            qimage = QImage(frame_rgb.data, width1, height1, bytesPerLine1, QImage.Format_RGB888)
            pixmap = QPixmap.fromImage(qimage)
            self.label1.setPixmap(pixmap.scaled(self.label1.size(), Qt.KeepAspectRatio))

            results = self.worker.model.predict(frame)
            # 此处修改,将每次摄像头检测的结果都更新到self.current_results中(以列表形式存储每次结果)
            if self.current_results is None:
                self.current_results = []
            self.current_results.clear()
            self.current_results.append(results)

            annotated_frame = results[0].plot()
            frame_rgb = cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB)
            height2, width2, channel2 = frame_rgb.shape
            bytesPerLine2 = 3 * width2
            qimage = QImage(frame_rgb.data, width2, height2, bytesPerLine2, QImage.Format_RGB888)
            pixmap = QPixmap.fromImage(qimage)
            self.label2.setPixmap(pixmap.scaled(self.label2.size(), Qt.KeepAspectRatio))
        else:
            self.camera_timer.stop()
            self.camera.release()
            QMessageBox.warning(self, "摄像头错误", "摄像头读取失败,请检查设备是否正常工作。")

    def show_detected_objects(self):
        if self.current_results:
            all_det_info = []
            for result in self.current_results:
                det_info = result[0].boxes.cls
                all_det_info.extend(det_info.tolist())
            object_count = len(all_det_info)
            object_info = f"识别到的物体总个数:{object_count}\n"
            object_dict = {}
            class_names_dict = self.current_results[0][0].names
            for class_id in all_det_info:
                class_name = class_names_dict[int(class_id)]
                if class_name in object_dict:
                    object_dict[class_name] += 1
                else:
                    object_dict[class_name] = 1
            sorted_objects = sorted(object_dict.items(), key=lambda x: x[1], reverse=True)
            for obj_name, obj_count in sorted_objects:
                object_info += f"{obj_name}: {obj_count}\n"
            if self.video_detect_button.isEnabled() and self.video_detect_button.isChecked():
                self.is_video_paused = True
            elif self.camera_detect_button.isEnabled() and self.camera_detect_button.isChecked():
                self.is_camera_paused = True

            msg_box = QMessageBox(self)
            msg_box.setWindowTitle("识别结果")
            msg_box.setText(object_info)
            msg_box.finished.connect(self.resume_detection)
            msg_box.exec_()
        else:
            self.show_message_box("识别结果", "未检测到物体")

    def show_message_box(self, title, message):
        msg_box = QMessageBox(self)
        msg_box.setWindowTitle(title)
        msg_box.setText(message)
        msg_box.exec_()

    def load_model(self):
        if self.worker.load_model():
            self.image_detect_button.setEnabled(True)
            self.display_objects_button.setEnabled(True)
            self.video_detect_button.setEnabled(True)
            self.camera_detect_button.setEnabled(True)

    def resume_detection(self):
        if self.is_video_paused:
            self.is_video_paused = False
        elif self.is_camera_paused:
            self.is_camera_paused = False

    def exit_application(self):
        # 终止程序运行
        sys.exit()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

喜欢的请三连哦

相关推荐
zy_destiny8 小时前
【工业场景】用YOLOv12实现饮料类别识别
人工智能·python·深度学习·yolo·机器学习·计算机视觉·目标跟踪
卧式纯绿9 小时前
每日文献(八)——Part one
人工智能·yolo·目标检测·计算机视觉·目标跟踪·cnn
HABuo15 小时前
【YOLOv8】YOLOv8改进系列(12)----替换主干网络之StarNet
人工智能·深度学习·yolo·目标检测·计算机视觉
AdaCoding18 小时前
YOLOv8架构详解
yolo·网络结构图
云卷云舒___________19 小时前
【Ultralytics YOLO COCO 评估脚本 | 获得COCO评价指标】
yolo·coco·ultralytics
plmm烟酒僧2 天前
在 RK3588 多线程推理 YOLO 时,同时开启硬件解码和 RGA 加速的性能分析
yolo·rkmpp·瑞芯微·硬件加速·rga·色彩空间转换
HABuo2 天前
【YOLOv8】YOLOv8改进系列(11)----替换主干网络之MobileNetV4
人工智能·深度学习·yolo·目标检测·计算机视觉
Kai HVZ3 天前
《深度学习》——yolov4详解
人工智能·深度学习·yolo
plmm烟酒僧3 天前
基于 RK3588 的 YOLO 多线程推理多级硬件加速引擎框架设计(代码框架和实现细节)
yolo·rk3588·多线程·rkmpp·硬件加速·视频解码·librga