基于深度学习的苹果损坏检测系统(YOLOv8+YOLO数据集+UI界面+Python项目+模型)

一、项目介绍

摘要

本项目基于YOLOv8目标检测算法,开发了一套苹果损坏智能检测系统,专注于识别苹果是否受损。系统采用单类别检测('damaged_apple'),通过深度学习技术自动分析苹果表面是否存在损伤、腐烂或机械伤害,从而快速判断其品质。数据集包含356张苹果图像(训练集253张,验证集103张),经过数据增强和模型优化,系统能够高效、准确地检测苹果的受损情况,适用于水果分拣、仓储管理和零售质检等场景。

相较于传统人工检测方法,本系统具有自动化、高效率、高一致性等优势,可大幅降低人工成本,提高水果分拣的准确性和速度。同时,该系统可集成到工业分拣设备或移动端应用,为水果供应链提供智能化的质量控制解决方案。

项目意义

1. 提升水果分拣效率,降低人工成本

传统的水果分拣依赖人工目视检查,不仅效率低(每小时仅能检测几百个水果),而且容易因疲劳或主观判断导致误差。本系统基于YOLOv8深度学习模型,可实现毫秒级单张图像检测,适用于高速分拣流水线,显著提升分拣效率(预计提升5-10倍),同时减少人工干预,降低企业运营成本。

2. 减少食品浪费,优化供应链管理

全球每年约有30%的水果因储存和运输过程中的损伤而被丢弃。本系统可早期识别受损苹果,帮助果农、批发商和零售商及时筛选出即将变质的水果,优先处理或调整销售策略,从而减少食品浪费和经济损失。此外,系统可结合仓储管理,优化库存周转,确保优质苹果优先进入市场。

3. 标准化水果质检,提高市场竞争力

人工质检标准难以统一,不同质检员可能对“轻微损伤”或“可接受瑕疵”的判断存在差异。本系统通过AI模型实现客观、一致的检测标准,确保所有苹果按照相同质量标准分类,提高产品信誉,增强市场竞争力。

4. 适用于多种应用场景

  • 果园与包装厂:在采摘后立即进行初筛,剔除受损苹果,减少后续运输损耗。

  • 批发市场与仓储中心:自动化分拣,提高物流效率,降低人工误判率。

  • 超市与零售店:实时检测库存水果新鲜度,优化货架管理,减少消费者投诉。

5. 技术可扩展性强

尽管当前模型仅针对苹果检测,但算法框架可迁移至其他水果(如梨、桃、柑橘等)的损伤检测,只需调整数据集即可适配不同需求。此外,YOLOv8的轻量化版本(如YOLOv8s或YOLOv8n)可部署在边缘设备(如分拣机器人、智能手机等),实现实时检测。

6. 推动农业智能化发展

本项目是计算机视觉+农业的典型应用,符合智慧农业的发展趋势。通过AI技术赋能传统农业,可推动水果产业的自动化、数字化升级,为未来农业4.0(如无人农场、AI质检机器人)奠定技术基础。


总结

YOLOv8苹果损坏检测系统不仅能够提升水果行业的质检效率,减少食品浪费,还能帮助企业优化供应链管理,提高市场竞争力。其技术方案具有高精度、易部署、可扩展等特点,适用于水果产业的多个环节,具备广阔的商业应用前景和社会经济价值。未来,可进一步结合多光谱成像近红外检测技术,提升对轻微损伤或内部变质的识别能力,打造更完善的智能水果分拣体系。


基于深度学习的苹果损坏检测系统(YOLOv8+YOLO数据集+UI界面+Python项目源码+模型)_哔哩哔哩_bilibili

基于深度学习的苹果损坏检测系统(YOLOv8+YOLO数据集+UI界面+Python项目源码+模型)

二、项目功能展示

系统功能

图片检测:可对单张图片进行检测,返回检测框及类别信息。
批量图片检测:支持文件夹输入,一次性检测多张图片,生成批量检测结果。
视频检测:支持视频文件输入,检测视频中每一帧的情况。
摄像头实时检测:连接USB 摄像头,实现实时监测,

  • 图片检测

        该功能允许用户通过单张图片进行目标检测。输入一张图片后,YOLO模型会实时分析图像,识别出其中的目标,并在图像中框出检测到的目标,输出带有目标框的图像。批量图片检测

        用户可以一次性上传多个图片进行批量处理。该功能支持对多个图像文件进行并行处理,并返回每张图像的目标检测结果,适用于需要大规模处理图像数据的应用场景。

  • 视频检测

        视频检测功能允许用户将视频文件作为输入。YOLO模型将逐帧分析视频,并在每一帧中标记出检测到的目标。最终结果可以是带有目标框的视频文件或实时展示,适用于视频监控和分析等场景。

  • 摄像头实时检测

        该功能支持通过连接摄像头进行实时目标检测。YOLO模型能够在摄像头拍摄的实时视频流中进行目标检测,实时识别并显示检测结果。此功能非常适用于安防监控、无人驾驶、智能交通等应用,提供即时反馈。

核心特点:

  • 高精度:基于YOLO模型,提供精确的目标检测能力,适用于不同类型的图像和视频。
  • 实时性:特别优化的算法使得实时目标检测成为可能,无论是在视频还是摄像头实时检测中,响应速度都非常快。
  • 批量处理:支持高效的批量图像和视频处理,适合大规模数据分析。

三、数据集介绍

数据集特点:

  • 高质量标注: 每张图像都经过精确的标注,标注信息包括腐烂苹果的边界框位置,确保模型能够准确学习目标特征。

  • 多样性: 数据集中的图像涵盖了不同腐烂程度(如轻微腐烂、严重腐烂)、光照条件、背景环境以及苹果的不同摆放姿态,确保模型能够适应各种实际场景。

  • 针对性: 数据集专注于腐烂苹果的检测,适合单一类别的高精度检测任务。

应用场景:

  1. 果园管理:
    在果园中实时检测腐烂苹果,帮助果农及时清理腐烂果实,防止病害传播,提高果园管理效率。

  2. 水果分拣:
    在水果加工厂中,系统可以自动分拣腐烂苹果,确保只有高质量的苹果进入市场,减少人工分拣成本。

  3. 食品质量检测:
    在食品供应链中,系统可以用于检测苹果的质量状态,确保食品安全和质量控制。


技术优势

  • 高精度检测: 基于深度学习的目标检测算法(如YOLOv8),能够实现高精度的腐烂苹果检测。

  • 实时性: 系统支持实时检测,能够快速处理图像并输出检测结果。

  • 易用性: 系统可部署于多种硬件平台(如嵌入式设备、服务器等),满足不同场景的需求。

通过该数据集和深度学习模型的结合,苹果腐烂检测系统能够为用户提供高效、精准的腐烂苹果检测解决方案,提升生产效率并降低人工成本。

数据集配置文件data.yaml

train: .\datasets\images\train
val: .\datasets\images\val
test: .\datasets\images\test


nc: 1
names: ['damaged_apple']

数据集制作流程

  • 标注数据:使用标注工具(如LabelImg、CVAT等)对图像中的目标进行标注。每个目标需要标出边界框,并且标注类别。

  • 转换格式:将标注的数据转换为YOLO格式。YOLO标注格式为每行:<object-class> <x_center> <y_center> <width> <height>,这些坐标是相对于图像尺寸的比例。

  • 分割数据集:将数据集分为训练集、验证集和测试集,通常的比例是80%训练集、10%验证集和10%测试集。

  • 准备标签文件:为每张图片生成一个对应的标签文件,确保标签文件与图片的命名一致。

  • 调整图像尺寸:根据YOLO网络要求,统一调整所有图像的尺寸(如416x416或608x608)。

四、项目环境配置

创建虚拟环境

首先新建一个Anaconda环境,每个项目用不同的环境,这样项目中所用的依赖包互不干扰。

终端输入

conda create -n yolov8 python==3.9

激活虚拟环境

conda activate yolov8
 

安装cpu版本pytorch

pip install torch torchvision torchaudio

pycharm中配置anaconda

安装所需要库

pip install -r requirements.txt

五、模型训练

训练代码

from ultralytics import YOLO

model_path = 'yolov8s.pt'
data_path = 'datasets/data.yaml'

if __name__ == '__main__':
    model = YOLO(model_path)
    results = model.train(data=data_path,
                          epochs=500,
                          batch=64,
                          device='0',
                          workers=0,
                          project='runs/detect',
                          name='exp',
                          )
根据实际情况更换模型
yolov8n.yaml (nano):轻量化模型,适合嵌入式设备,速度快但精度略低。
yolov8s.yaml (small):小模型,适合实时任务。
yolov8m.yaml (medium):中等大小模型,兼顾速度和精度。
yolov8b.yaml (base):基本版模型,适合大部分应用场景。
yolov8l.yaml (large):大型模型,适合对精度要求高的任务。
  • --batch 64:每批次64张图像。
  • --epochs 500:训练500轮。
  • --datasets/data.yaml:数据集配置文件。
  • --weights yolov8s.pt:初始化模型权重,yolov8s.pt 是预训练的轻量级YOLO模型。

训练结果

六、核心代码

# -*- coding: utf-8 -*-
import os
import sys
import time
import cv2
import numpy as np
from PIL import ImageFont
from PyQt5.QtCore import Qt, QTimer, QThread, pyqtSignal, QCoreApplication
from PyQt5.QtWidgets import (QApplication, QMainWindow, QFileDialog,
                             QMessageBox, QWidget, QHeaderView,
                             QTableWidgetItem, QAbstractItemView)
from ultralytics import YOLO

# 自定义模块导入
sys.path.append('UIProgram')
from UIProgram.UiMain import Ui_MainWindow
from UIProgram.QssLoader import QSSLoader
from UIProgram.precess_bar import ProgressBar
import detect_tools as tools
import Config


class DetectionApp(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        # 初始化应用
        self._setup_ui()
        self._connect_signals()
        self._load_stylesheet()

        # 模型和资源初始化
        self._init_detection_resources()

    def _setup_ui(self):
        """初始化UI界面设置"""
        self.display_width = 700
        self.display_height = 500
        self.source_path = None
        self.camera_active = False
        self.video_capture = None

        # 配置表格控件
        table = self.ui.tableWidget
        table.verticalHeader().setSectionResizeMode(QHeaderView.Fixed)
        table.verticalHeader().setDefaultSectionSize(40)
        table.setColumnWidth(0, 80)  # ID列
        table.setColumnWidth(1, 200)  # 路径列
        table.setColumnWidth(2, 150)  # 类别列
        table.setColumnWidth(3, 90)  # 置信度列
        table.setColumnWidth(4, 230)  # 位置列
        table.setSelectionBehavior(QAbstractItemView.SelectRows)
        table.verticalHeader().setVisible(False)
        table.setAlternatingRowColors(True)

    def _connect_signals(self):
        """连接按钮信号与槽函数"""
        self.ui.PicBtn.clicked.connect(self._handle_image_input)
        self.ui.comboBox.activated.connect(self._update_selection)
        self.ui.VideoBtn.clicked.connect(self._handle_video_input)
        self.ui.CapBtn.clicked.connect(self._toggle_camera)
        self.ui.SaveBtn.clicked.connect(self._save_results)
        self.ui.ExitBtn.clicked.connect(QCoreApplication.quit)
        self.ui.FilesBtn.clicked.connect(self._process_image_batch)

    def _load_stylesheet(self):
        """加载CSS样式表"""
        style_file = 'UIProgram/style.css'
        qss = QSSLoader.read_qss_file(style_file)
        self.setStyleSheet(qss)

    def _init_detection_resources(self):
        """初始化检测相关资源"""
        # 加载YOLOv8模型
        self.detector = YOLO('runs/detect/exp/weights/best.pt', task='detect')
        self.detector(np.zeros((48, 48, 3)))  # 预热模型

        # 初始化字体和颜色
        self.detection_font = ImageFont.truetype("Font/platech.ttf", 25, 0)
        self.color_palette = tools.Colors()

        # 初始化定时器
        self.frame_timer = QTimer()
        self.save_timer = QTimer()

    def _handle_image_input(self):
        """处理单张图片输入"""
        self._stop_video_capture()

        file_path, _ = QFileDialog.getOpenFileName(
            self, '选择图片', './', "图片文件 (*.jpg *.jpeg *.png)")
        if not file_path:
            return

        self._process_single_image(file_path)

    def _process_single_image(self, image_path):
        """处理并显示单张图片的检测结果"""
        self.source_path = image_path
        self.ui.comboBox.setEnabled(True)

        # 读取并检测图片
        start_time = time.time()
        detection_results = self.detector(image_path)[0]
        processing_time = time.time() - start_time

        # 解析检测结果
        boxes = detection_results.boxes.xyxy.tolist()
        self.detection_boxes = [list(map(int, box)) for box in boxes]
        self.detection_classes = detection_results.boxes.cls.int().tolist()
        confidences = detection_results.boxes.conf.tolist()
        self.confidence_scores = [f'{score * 100:.2f}%' for score in confidences]

        # 更新UI显示
        self._update_detection_display(detection_results, processing_time)
        self._update_object_selection()
        self._show_detection_details()
        self._display_results_table(image_path)

    def _update_detection_display(self, results, process_time):
        """更新检测结果显示"""
        # 显示处理时间
        self.ui.time_lb.setText(f'{process_time:.3f} s')

        # 获取带标注的图像
        annotated_img = results.plot()
        self.current_result = annotated_img

        # 调整并显示图像
        width, height = self._calculate_display_size(annotated_img)
        resized_img = cv2.resize(annotated_img, (width, height))
        qimage = tools.cvimg_to_qpiximg(resized_img)

        self.ui.label_show.setPixmap(qimage)
        self.ui.label_show.setAlignment(Qt.AlignCenter)
        self.ui.PiclineEdit.setText(self.source_path)

        # 更新检测数量
        self.ui.label_nums.setText(str(len(self.detection_classes)))

    def _calculate_display_size(self, image):
        """计算适合显示的图像尺寸"""
        img_height, img_width = image.shape[:2]
        aspect_ratio = img_width / img_height

        if aspect_ratio >= self.display_width / self.display_height:
            width = self.display_width
            height = int(width / aspect_ratio)
        else:
            height = self.display_height
            width = int(height * aspect_ratio)

        return width, height

    def _update_object_selection(self):
        """更新目标选择下拉框"""
        options = ['全部']
        target_labels = [
            f'{Config.names[cls_id]}_{idx}'
            for idx, cls_id in enumerate(self.detection_classes)
        ]
        options.extend(target_labels)

        self.ui.comboBox.clear()
        self.ui.comboBox.addItems(options)

    def _show_detection_details(self, index=0):
        """显示检测目标的详细信息"""
        if not self.detection_boxes:
            self._clear_detection_details()
            return

        box = self.detection_boxes[index]
        self.ui.type_lb.setText(Config.CH_names[self.detection_classes[index]])
        self.ui.label_conf.setText(self.confidence_scores[index])
        self.ui.label_xmin.setText(str(box[0]))
        self.ui.label_ymin.setText(str(box[1]))
        self.ui.label_xmax.setText(str(box[2]))
        self.ui.label_ymax.setText(str(box[3]))

    def _clear_detection_details(self):
        """清空检测详情显示"""
        self.ui.type_lb.setText('')
        self.ui.label_conf.setText('')
        self.ui.label_xmin.setText('')
        self.ui.label_ymin.setText('')
        self.ui.label_xmax.setText('')
        self.ui.label_ymax.setText('')

    def _display_results_table(self, source_path):
        """在表格中显示检测结果"""
        table = self.ui.tableWidget
        table.setRowCount(0)
        table.clearContents()

        for idx, (box, cls_id, conf) in enumerate(zip(
                self.detection_boxes, self.detection_classes, self.confidence_scores)):

            row = table.rowCount()
            table.insertRow(row)

            # 添加表格项
            items = [
                QTableWidgetItem(str(row + 1)),  # ID
                QTableWidgetItem(source_path),  # 路径
                QTableWidgetItem(Config.CH_names[cls_id]),  # 类别
                QTableWidgetItem(conf),  # 置信度
                QTableWidgetItem(str(box))  # 位置坐标
            ]

            # 设置文本居中
            for item in [items[0], items[2], items[3]]:
                item.setTextAlignment(Qt.AlignCenter)

            # 添加到表格
            for col, item in enumerate(items):
                table.setItem(row, col, item)

        table.scrollToBottom()

    def _process_image_batch(self):
        """批量处理图片"""
        self._stop_video_capture()

        folder = QFileDialog.getExistingDirectory(self, "选择图片文件夹", "./")
        if not folder:
            return

        self.source_path = folder
        valid_extensions = {'jpg', 'png', 'jpeg', 'bmp'}

        for filename in os.listdir(folder):
            filepath = os.path.join(folder, filename)
            if (os.path.isfile(filepath) and
                    filename.split('.')[-1].lower() in valid_extensions):
                self._process_single_image(filepath)
                QApplication.processEvents()  # 保持UI响应

    def _update_selection(self):
        """更新用户选择的检测目标显示"""
        selection = self.ui.comboBox.currentText()

        if selection == '全部':
            boxes = self.detection_boxes
            display_img = self.current_result
            self._show_detection_details(0)
        else:
            idx = int(selection.split('_')[-1])
            boxes = [self.detection_boxes[idx]]
            display_img = self.detector(self.source_path)[0][idx].plot()
            self._show_detection_details(idx)

        # 更新显示
        width, height = self._calculate_display_size(display_img)
        resized_img = cv2.resize(display_img, (width, height))
        qimage = tools.cvimg_to_qpiximg(resized_img)

        self.ui.label_show.clear()
        self.ui.label_show.setPixmap(qimage)
        self.ui.label_show.setAlignment(Qt.AlignCenter)

    def _handle_video_input(self):
        """处理视频输入"""
        if self.camera_active:
            self._toggle_camera()

        video_path = self._get_video_path()
        if not video_path:
            return

        self._start_video_processing(video_path)
        self.ui.comboBox.setEnabled(False)

    def _get_video_path(self):
        """获取视频文件路径"""
        path, _ = QFileDialog.getOpenFileName(
            self, '选择视频', './', "视频文件 (*.avi *.mp4)")

        if path:
            self.source_path = path
            self.ui.VideolineEdit.setText(path)
            return path
        return None

    def _start_video_processing(self, video_path):
        """开始处理视频流"""
        self.video_capture = cv2.VideoCapture(video_path)
        self.frame_timer.start(1)
        self.frame_timer.timeout.connect(self._process_video_frame)

    def _stop_video_capture(self):
        """停止视频捕获"""
        if self.video_capture:
            self.video_capture.release()
            self.frame_timer.stop()
            self.camera_active = False
            self.ui.CaplineEdit.setText('摄像头未开启')
            self.video_capture = None

    def _process_video_frame(self):
        """处理视频帧"""
        ret, frame = self.video_capture.read()
        if not ret:
            self._stop_video_capture()
            return

        # 执行目标检测
        start_time = time.time()
        results = self.detector(frame)[0]
        processing_time = time.time() - start_time

        # 解析结果
        self.detection_boxes = results.boxes.xyxy.int().tolist()
        self.detection_classes = results.boxes.cls.int().tolist()
        self.confidence_scores = [f'{conf * 100:.2f}%' for conf in results.boxes.conf.tolist()]

        # 更新显示
        self._update_detection_display(results, processing_time)
        self._update_object_selection()
        self._show_detection_details()
        self._display_results_table(self.source_path)

    def _toggle_camera(self):
        """切换摄像头状态"""
        self.camera_active = not self.camera_active

        if self.camera_active:
            self.ui.CaplineEdit.setText('摄像头开启')
            self.video_capture = cv2.VideoCapture(0)
            self._start_video_processing(0)
            self.ui.comboBox.setEnabled(False)
        else:
            self.ui.CaplineEdit.setText('摄像头未开启')
            self.ui.label_show.clear()
            self._stop_video_capture()

    def _save_results(self):
        """保存检测结果"""
        if not self.video_capture and not self.source_path:
            QMessageBox.information(self, '提示', '没有可保存的内容,请先打开图片或视频!')
            return

        if self.camera_active:
            QMessageBox.information(self, '提示', '无法保存摄像头实时视频!')
            return

        if self.video_capture:
            self._save_video_result()
        else:
            self._save_image_result()

    def _save_video_result(self):
        """保存视频检测结果"""
        confirm = QMessageBox.question(
            self, '确认',
            '保存视频可能需要较长时间,确定继续吗?',
            QMessageBox.Yes | QMessageBox.No)

        if confirm == QMessageBox.No:
            return

        self._stop_video_capture()
        saver = VideoSaverThread(
            self.source_path, self.detector,
            self.ui.comboBox.currentText())
        saver.start()
        saver.update_ui_signal.connect(self._update_progress)

    def _save_image_result(self):
        """保存图片检测结果"""
        if os.path.isfile(self.source_path):
            # 处理单张图片
            filename = os.path.basename(self.source_path)
            name, ext = filename.rsplit(".", 1)
            save_name = f"{name}_detect_result.{ext}"
            save_path = os.path.join(Config.save_path, save_name)

            cv2.imwrite(save_path, self.current_result)
            QMessageBox.information(
                self, '完成',
                f'图片已保存至: {save_path}')
        else:
            # 处理文件夹中的图片
            valid_exts = {'jpg', 'png', 'jpeg', 'bmp'}
            for filename in os.listdir(self.source_path):
                if filename.split('.')[-1].lower() in valid_exts:
                    filepath = os.path.join(self.source_path, filename)
                    name, ext = filename.rsplit(".", 1)
                    save_name = f"{name}_detect_result.{ext}"
                    save_path = os.path.join(Config.save_path, save_name)

                    results = self.detector(filepath)[0]
                    cv2.imwrite(save_path, results.plot())

            QMessageBox.information(
                self, '完成',
                f'所有图片已保存至: {Config.save_path}')

    def _update_progress(self, current, total):
        """更新保存进度"""
        if current == 1:
            self.progress_dialog = ProgressBar(self)
            self.progress_dialog.show()

        if current >= total:
            self.progress_dialog.close()
            QMessageBox.information(
                self, '完成',
                f'视频已保存至: {Config.save_path}')
            return

        if not self.progress_dialog.isVisible():
            return

        percent = int(current / total * 100)
        self.progress_dialog.setValue(current, total, percent)
        QApplication.processEvents()


class VideoSaverThread(QThread):
    """视频保存线程"""
    update_ui_signal = pyqtSignal(int, int)

    def __init__(self, video_path, model, selection):
        super().__init__()
        self.video_path = video_path
        self.detector = model
        self.selection = selection
        self.active = True
        self.colors = tools.Colors()

    def run(self):
        """执行视频保存"""
        cap = cv2.VideoCapture(self.video_path)
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        fps = cap.get(cv2.CAP_PROP_FPS)
        size = (
            int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
            int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))

        filename = os.path.basename(self.video_path)
        name, _ = filename.split('.')
        save_path = os.path.join(
            Config.save_path,
            f"{name}_detect_result.avi")

        writer = cv2.VideoWriter(save_path, fourcc, fps, size)
        total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
        current_frame = 0

        while cap.isOpened() and self.active:
            current_frame += 1
            ret, frame = cap.read()

            if not ret:
                break

            # 执行检测
            results = self.detector(frame)[0]
            frame = results.plot()
            writer.write(frame)
            self.update_ui_signal.emit(current_frame, total_frames)

        # 释放资源
        cap.release()
        writer.release()

    def stop(self):
        """停止保存过程"""
        self.active = False


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

七、项目

演示与介绍视频:

基于深度学习的苹果损坏检测系统(YOLOv8+YOLO数据集+UI界面+Python项目源码+模型)_哔哩哔哩_bilibili

基于深度学习的苹果损坏检测系统(YOLOv8+YOLO数据集+UI界面+Python项目源码+模型)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值