如何使用深度学习框架目标检测算法Yolo训练水果蔬菜检测数据集 建立基于YOLOv8的水果蔬菜识别系统,并使用PyQt5作为前端界面,多种水果蔬菜的识别检测功能

如何使用深度学习框架目标检测算法Yolo训练水果蔬菜检测数据集 建立基于YOLOv8的水果蔬菜识别系统,并使用PyQt5作为前端界面,多种水果蔬菜的识别检测功能


以下文字及代码仅供参考

基于深度学习的水果蔬菜识别系统

在这里插入图片描述
1
在这里插入图片描述

项目预实现功能及相关软件配置:
软件:Pycharm+Anaconda
环境:python=3.9 opencv_python PyQt5
文件:
1.完整程序文件(.py等)
2.UI界面源文件、图标(.ui、.qrc、.py等)
3.测试图片、视频文件(.jpeg、.mp4、.avi等)
在这里插入图片描述

预实现功能: 系统实现了对于多种水果蔬菜的识别检测功能:包括通过选择图片、视频、摄像头进行实时识别;检测速度快、识别精度较高。
①选择图片识别水果蔬菜。
②选择视频识别水果蔬菜。
③摄像头检测识别水果蔬菜。

构建一个基于YOLOv8的水果蔬菜识别系统,并使用PyQt5作为前端界面,我们可以按照以下步骤进行:

一、环境搭建

确保你的环境中已安装Python和pip。然后安装以下库和工具:

conda create -n fruit_vegetable_detection python=3.9
conda activate fruit_vegetable_detection
pip install opencv-python PyQt5 ultralytics

二、数据集准备

你需要一个包含多种水果和蔬菜的数据集,并标注好类别。假设你已经有了这样的数据集,结构如下:

dataset/
├── images/
│   ├── train/
│   └── val/
└── labels/
    ├── train/
    └── val/

三、模型训练

使用YOLOv8进行模型训练。首先,克隆Ultralytics仓库并下载预训练模型:

git clone https://github.com/ultralytics/ultralytics.git
cd ultralytics
pip install -r requirements.txt

然后,使用以下命令进行训练:

yolo task=detect mode=train model=yolov8n.yaml data=your_dataset.yaml epochs=100 imgsz=640

其中,your_dataset.yaml 是你的数据集配置文件,内容如下:

train: dataset/images/train
val: dataset/images/val

nc: 20  # 假设你有20种水果和蔬菜
names: ['apple', 'banana', 'grape', ...]

四、推理代码

编写推理代码,支持图片、视频和摄像头流的识别。

图片推理
from ultralytics import YOLO
import cv2

def detect_image(model, image_path):
    results = model(image_path)
    return results[0].plot()
视频推理
def detect_video(model, video_path):
    cap = cv2.VideoCapture(video_path)
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        results = model(frame)
        annotated_frame = results[0].plot()
        cv2.imshow('YOLOv8 Detection', annotated_frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows()
摄像头推理
def detect_camera(model):
    cap = cv2.VideoCapture(0)
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        results = model(frame)
        annotated_frame = results[0].plot()
        cv2.imshow('YOLOv8 Detection', annotated_frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows()

五、UI界面设计

使用PyQt5设计用户界面。

UI文件(.ui)

创建一个名为main_window.ui的文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>基于YOLOv8的水果蔬菜检测识别系统</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="QLabel" name="label">
      <property name="text">
       <string>基于YOLOv8的水果蔬菜检测识别系统</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QLabel" name="image_label">
      <property name="text">
       <string/>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QPushButton" name="browse_button">
      <property name="text">
       <string>选择图片</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QPushButton" name="video_button">
      <property name="text">
       <string>选择视频</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QPushButton" name="camera_button">
      <property name="text">
       <string>摄像头检测</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>
转换UI文件为Python代码

使用pyuic5.ui文件转换为.py文件:

pyuic5 main_window.ui -o main_window.py

六、完整程序文件

编写主程序文件main.py

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog
from PyQt5.QtGui import QPixmap
from main_window import Ui_MainWindow
from ultralytics import YOLO
import cv2

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.model = YOLO("best.pt")
        self.browse_button.clicked.connect(self.load_image)
        self.video_button.clicked.connect(self.load_video)
        self.camera_button.clicked.connect(self.start_camera)

    def load_image(self):
        file_name, _ = QFileDialog.getOpenFileName(self, "选择图片", "", "Images (*.png *.xpm *.jpg)")
        if file_name:
            results = self.model(file_name)
            annotated_image = results[0].plot()
            cv2.imwrite("annotated_image.jpg", annotated_image)
            pixmap = QPixmap("annotated_image.jpg")
            self.image_label.setPixmap(pixmap)

    def load_video(self):
        file_name, _ = QFileDialog.getOpenFileName(self, "选择视频", "", "Videos (*.mp4 *.avi)")
        if file_name:
            detect_video(self.model, file_name)

    def start_camera(self):
        detect_camera(self.model)

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

七、测试与部署

将上述代码保存为main.py,运行程序:

python main.py

基于YOLOv8和PyQt5的水果蔬菜识别系统,可以识别图片、视频和摄像头流,并支持动态调节模型置信度和选择模型权重。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值