效果如下:这个是图片检测
效果如下:这个是视频检测
效果如下:这个是摄像头检测
1 相关库
除了yolov11所用库之外,本文所用到的额外库为pyqt5,输入指令进行安装
pip install pyqt5
导入所需要的库
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进行界面创作
"""初始化主窗口。这个方法设置了窗口的标题、图标、大小,并初始化了各种组件,如标签、按钮、布局等。还创建了用于摄像头检测的定时器,并设置了初始状态为未暂停。"""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# 水平布局,用于放置左右两个QLabellayout = 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实现
每个部分的按钮实现
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) # 转换为RGBheight1, width1, channel1 = image_rgb.shapebytesPerLine1 = 3 * width1qimage1 = 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) # 转换为RGBheight2, width2, channel2 = annotated_image.shapebytesPerLine2 = 3 * width2qimage2 = 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.shapebytesPerLine1 = 3 * width1qimage = 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 frameannotated_frame = results[0].plot()frame_rgb = cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB)height2, width2, channel2 = frame_rgb.shapebytesPerLine2 = 3 * width2qimage = 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"):breakelse:# Break the loop if the end of the video is reachedbreak# Release the video capture object and close the display windowcap.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.shapebytesPerLine1 = 3 * width1qimage = 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.shapebytesPerLine2 = 3 * width2qimage = 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.clsall_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].namesfor class_id in all_det_info:class_name = class_names_dict[int(class_id)]if class_name in object_dict:object_dict[class_name] += 1else:object_dict[class_name] = 1sorted_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 = Trueelif self.camera_detect_button.isEnabled() and self.camera_detect_button.isChecked():self.is_camera_paused = Truemsg_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 = Falseelif self.is_camera_paused:self.is_camera_paused = Falsedef exit_application(self):# 终止程序运行sys.exit()
4总代码
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 YOLOclass Worker:def __init__(self):self.model = Nonedef load_model(self):model_path, _ = QFileDialog.getOpenFileName(None, "选择模型文件", "", "模型文件 (*.pt)")if model_path:self.model = YOLO(model_path)return self.model is not Nonereturn Falseclass 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# 水平布局,用于放置左右两个QLabellayout = 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 = Nonedef 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) # 转换为RGBheight1, width1, channel1 = image_rgb.shapebytesPerLine1 = 3 * width1qimage1 = 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) # 转换为RGBheight2, width2, channel2 = annotated_image.shapebytesPerLine2 = 3 * width2qimage2 = 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.shapebytesPerLine1 = 3 * width1qimage = 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 frameannotated_frame = results[0].plot()frame_rgb = cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB)height2, width2, channel2 = frame_rgb.shapebytesPerLine2 = 3 * width2qimage = 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"):breakelse:# Break the loop if the end of the video is reachedbreak# Release the video capture object and close the display windowcap.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.shapebytesPerLine1 = 3 * width1qimage = 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.shapebytesPerLine2 = 3 * width2qimage = 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.clsall_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].namesfor class_id in all_det_info:class_name = class_names_dict[int(class_id)]if class_name in object_dict:object_dict[class_name] += 1else:object_dict[class_name] = 1sorted_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 = Trueelif self.camera_detect_button.isEnabled() and self.camera_detect_button.isChecked():self.is_camera_paused = Truemsg_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 = Falseelif self.is_camera_paused:self.is_camera_paused = Falsedef exit_application(self):# 终止程序运行sys.exit()if __name__ == '__main__':app = QApplication(sys.argv)window = MainWindow()window.show()sys.exit(app.exec_())
喜欢的请三连哦