当前位置: 首页 > news >正文

番茄成熟度检测数据集800张 有标签

番茄成熟度检测数据集800张 有标签

professnal拍摄:覆盖2种不同类型,分大番茄和小番茄

精准标注:每张图片都已标注好YOLO标签,标注有6类
大番茄:红 黄 绿
小番茄:红 黄 绿
实现一个基于 YOLOv8 的番茄成熟度检测系统。以下是详细的步骤:

  1. 数据准备:确保数据集格式正确。
  2. 环境部署:安装必要的库。
  3. 模型训练:使用 YOLOv8 训练目标检测模型。
  4. 评估模型:评估训练好的模型性能。
  5. PyQt5 GUI 开发:创建一个简单的 GUI 来加载和运行模型进行实时预测。

数据准备

假设你已经有一个包含 800 张截图的数据集,并且标注格式为 YOLO 格式的 TXT 文件。

数据集结构示例
dataset/ ├── images/ │ ├── train/ │ │ ├── image1.jpg │ │ ├── image2.jpg │ │ └── ... │ ├── test/ │ │ ├── image3.jpg │ │ ├── image4.jpg │ │ └── ... │ └── valid/ │ ├── image5.jpg │ ├── image6.jpg │ └── ... ├── labels/ │ ├── train/ │ │ ├── image1.txt │ │ ├── image2.txt │ │ └── ... │ ├── test/ │ │ ├── image3.txt │ │ ├── image4.txt │ │ └── ... │ └── valid/ │ ├── image5.txt │ ├── image6.txt │ └── ... └── dataset.yaml

dataset.yaml内容如下:

train:./images/trainval:./images/validtest:./images/testnc:6names:['big_tomato_red','big_tomato_yellow','big_tomato_green','small_tomato_red','small_tomato_yellow','small_tomato_green']

每个图像对应的标签文件是一个文本文件,每行表示一个边界框,格式为:

<class_id> <x_center> <y_center> <width> <height>

环境部署说明

确保你已经安装了必要的库,如上所述。

安装依赖
# 创建虚拟环境(可选)conda create-ntomato_detection_envpython=3.8conda activate tomato_detection_env# 安装PyTorchpipinstalltorch==1.9torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu111# 安装其他依赖pipinstallopencv-python pyqt5 ultralytics scikit-learn pandas matplotlib seaborn onnxruntime xml.etree.ElementTree

模型训练权重和指标可视化展示

我们将使用 YOLOv8 进行目标检测任务。

下载 YOLOv8 仓库
gitclone https://github.com/ultralytics/ultralytics.gitcdultralytics pipinstall-rrequirements.txt
训练 YOLOv8
[<title="Training YOLOv8 for Tomato Maturity Detection">]importosfrompathlibimportPath# Define pathsdataset_path='path/to/dataset'weights_path='runs/detect/exp/weights/best.pt'# Create dataset.yamlyaml_content=f""" train:{Path(dataset_path)/'images/train'}val:{Path(dataset_path)/'images/valid'}test:{Path(dataset_path)/'images/test'}nc: 6 names: ['big_tomato_red', 'big_tomato_yellow', 'big_tomato_green', 'small_tomato_red', 'small_tomato_yellow', 'small_tomato_green'] """withopen(Path(dataset_path)/'dataset.yaml','w')asf:f.write(yaml_content)# Train YOLOv8!yolo task=detect mode=train data={Path(dataset_path)/'dataset.yaml'}model=yolov8n.pt imgsz=256epochs=100batch=16name=tomato_exp

请将path/to/dataset替换为实际的数据集路径。

模型评估

我们将使用 YOLOv8 提供的评估功能来评估训练好的模型性能。

评估 YOLOv8 模型
[<title="Evaluating YOLOv8 Model for Tomato Maturity Detection">]fromultralyticsimportYOLO# Load the trained modelmodel_path='runs/detect/tomato_exp/weights/best.pt'# Evaluate the modelmodel=YOLO(model_path)results=model.val()# Print evaluation resultsmetrics=results.metricsprint(metrics)

请将path/to/dataset替换为实际的数据集路径。

使用说明

  1. 配置路径

    • path/to/dataset设置为存放数据集的目录路径。
    • 确保runs/detect/tomato_exp/weights/best.pt是训练好的 YOLOv8 模型权重路径。
  2. 运行脚本

    • 在终端中运行train_yolov8.py脚本来训练模型。
    • 在终端中运行evaluate_yolov8.py来评估模型性能。
  3. 注意事项

    • 确保所有必要的工具箱已安装,特别是 PyTorch 和 ultralytics。
    • 根据需要调整参数,如epochsimgsz

PyQt5 GUI 开发

我们将使用 PyQt5 创建一个简单的 GUI 来加载和运行 YOLOv8 模型进行实时预测。

主窗口代码main_window.py
[<title="PyQt5 Main Window for Tomato Maturity Detection">]importsysimportcv2importnumpyasnpfromPyQt5.QtWidgetsimportQApplication,QMainWindow,QLabel,QPushButton,QVBoxLayout,QWidget,QFileDialogfromPyQt5.QtGuiimportQImage,QPixmapfromPyQt5.QtCoreimportQt,QTimerfromultralyticsimportYOLOclassMainWindow(QMainWindow):def__init__(self):super().__init__()self.setWindowTitle("Tomato Maturity Detection System")self.setGeometry(100,100,800,600)self.model=YOLO('runs/detect/tomato_exp/weights/best.pt')self.initUI()definitUI(self):self.central_widget=QWidget()self.setCentralWidget(self.central_widget)self.layout=QVBoxLayout()self.image_label=QLabel(self)self.image_label.setAlignment(Qt.AlignCenter)self.layout.addWidget(self.image_label)self.load_image_button=QPushButton("Load Image",self)self.load_image_button.clicked.connect(self.load_image)self.layout.addWidget(self.load_image_button)self.load_video_button=QPushButton("Load Video",self)self.load_video_button.clicked.connect(self.load_video)self.layout.addWidget(self.load_video_button)self.start_detection_button=QPushButton("Start Detection",self)self.start_detection_button.clicked.connect(self.start_detection)self.layout.addWidget(self.start_detection_button)self.stop_detection_button=QPushButton("Stop Detection",self)self.stop_detection_button.clicked.connect(self.stop_detection)self.layout.addWidget(self.stop_detection_button)self.central_widget.setLayout(self.layout)self.cap=Noneself.timer=QTimer()self.timer.timeout.connect(self.update_frame)defload_image(self):options=QFileDialog.Options()file_name,_=QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()","","Images (*.png *.xpm *.jpg *.jpeg);;All Files (*)",options=options)iffile_name:self.image_path=file_name self.display_image(file_name)defdisplay_image(self,path):pixmap=QPixmap(path)scaled_pixmap=pixmap.scaled(self.image_label.width(),self.image_label.height(),Qt.KeepAspectRatio)self.image_label.setPixmap(scaled_pixmap)defload_video(self):options=QFileDialog.Options()file_name,_=QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()","","Videos (*.mp4 *.avi);;All Files (*)",options=options)iffile_name:self.video_path=file_name self.cap=cv2.VideoCapture(self.video_path)self.start_detection()defstart_detection(self):ifself.capisnotNoneandnotself.timer.isActive():self.timer.start(30)# Update frame every 30 msdefstop_detection(self):ifself.timer.isActive():self.timer.stop()self.cap.release()self.image_label.clear()defupdate_frame(self):ret,frame=self.cap.read()ifret:processed_frame=self.process_frame(frame)rgb_image=cv2.cvtColor(processed_frame,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.width(),self.image_label.height(),Qt.KeepAspectRatio)self.image_label.setPixmap(scaled_pixmap)else:self.stop_detection()defprocess_frame(self,frame):results=self.model(frame)forresultinresults:boxes=result.boxes.cpu().numpy()forboxinboxes:r=box.xyxy[0].astype(int)cls=int(box.cls[0])conf=box.conf[0]label=self.model.names[cls]text=f'{label}:{conf:.2f}'color_map={0:(0,0,255),# big_tomato_red1:(0,255,255),# big_tomato_yellow2:(0,255,0),# big_tomato_green3:(255,0,255),# small_tomato_red4:(255,255,0),# small_tomato_yellow5:(255,0,0)# small_tomato_green}color=color_map.get(cls,(255,255,255))# Default to white if class ID is unknowncv2.rectangle(frame,(r[0],r[1]),(r[2],r[3]),color,2)cv2.putText(frame,text,(r[0],r[1]-10),cv2.FONT_HERSHEY_SIMPLEX,0.9,color,2)returnframeif__name__=="__main__":app=QApplication(sys.argv)window=MainWindow()window.show()sys.exit(app.exec_())

使用说明

  1. 配置路径

    • path/to/dataset设置为存放数据集的目录路径。
    • 确保runs/detect/tomato_exp/weights/best.pt是训练好的 YOLOv8 模型权重路径。
  2. 运行脚本

    • 在终端中运行train_yolov8.py脚本来训练模型。
    • 在终端中运行evaluate_yolov8.py来评估模型性能。
    • 在终端中运行main_window.py来启动 GUI 应用程序。
    • 点击“Load Image”按钮加载图像。
    • 点击“Load Video”按钮加载视频。
    • 点击“Start Detection”按钮开始检测。
    • 点击“Stop Detection”按钮停止检测。
  3. 注意事项

    • 确保所有必要的工具箱已安装,特别是 PyTorch 和 PyQt5。
    • 根据需要调整参数,如epochsimgsz

示例

假设你的数据文件夹结构如下:

dataset/ ├── images/ │ ├── train/ │ │ ├── image1.jpg │ │ ├── image2.jpg │ │ └── ... │ ├── test/ │ │ ├── image3.jpg │ │ ├── image4.jpg │ │ └── ... │ └── valid/ │ ├── image5.jpg │ ├── image6.jpg │ └── ... ├── labels/ │ ├── train/ │ │ ├── image1.txt │ │ ├── image2.txt │ │ └── ... │ ├── test/ │ │ ├── image3.txt │ │ ├── image4.txt │ │ └── ... │ └── valid/ │ ├── image5.txt │ ├── image6.txt │ └── ... └── dataset.yaml

并且每个.txt文件中都有正确的 YOLO 标签。运行main_window.py后,你可以通过点击按钮来加载图像或视频并进行番茄成熟度检测。

总结

通过上述步骤,我们可以构建一个完整的基于 YOLOv8 的番茄成熟度检测系统,包括数据集准备、环境部署、模型训练、指标可视化展示、评估和 PyQt5 GUI 开发。以下是所有相关的代码文件:

  1. 训练 YOLOv8 脚本(train_yolov8.py)
  2. 评估 YOLOv8 模型脚本(evaluate_yolov8.py)
  3. PyQt5 主窗口代码(main_window.py)
http://www.jsqmd.com/news/1021771/

相关文章:

  • 3个理由告诉你为什么Windows电脑需要AirPlay2-Win
  • 从零构建宇宙沙盒:ECS架构、多尺度渲染与太空模拟实践
  • 干货指南:稀释剂实力供应商选购攻略 - mypinpai
  • Docker ENTRYPOINT 原理与实战:PID 1、信号处理与高可用容器设计
  • 方波频谱分解与合成:从傅里叶级数到硬件实现
  • 2026年白酒酒体设计单位选择指南:从技术壁垒到体验经济,谁在定义行业新标准? - 优质品牌商家
  • Ohook终极指南:5分钟免费解锁Office 365完整功能
  • PostgreSQL 跑在 Docker 里怎么备份?恢复成功才算备份成功
  • QR分解:机器学习中被低估的数值稳定器
  • 2026年四川经营许可证代办机构服务能力观察:本土化深耕与全链条服务成行业趋势 - 优质品牌商家
  • 提升终端工作流:fzf-tab-completion与Git命令的完美结合
  • 个人GPU部署LLM:68个可运行模型的显存、量化与框架实战指南
  • Monorepo本质:语义一致性治理与规模化协作降熵
  • 实力强的花木枝叶粉碎机生产厂推荐与费用 - mypinpai
  • Python空列表的底层原理与工程实践指南
  • 【招聘】人才地图①:招聘的最高境界,不是找人,是“知道人在哪里“
  • 5步上手:通达信缠论插件ChanlunX实现智能中枢绘制与笔段识别
  • 【招聘】人才地图④:五种Mapping方法——把散乱的信息,变成驱动决策的人才情报
  • 彻底卸载Ansys许可证:FlexNet三层架构清理与疑难排解指南
  • AWS S3 Sync 生产级同步原理与避坑指南
  • 靠谱的电力工具检测中心怎么选?弘宇电力检测口碑如何? - mypinpai
  • 文档操作系统:从模板到PDF的自动化工程化实践
  • 如何选择最佳句子相似度模型:jeffding/sentence_similarity_semantic_search-openmind vs 传统方法的终极对比指南
  • 目标检测算法Yolov5训练反光衣数据集模型 建立基于深度学习yolov5反光衣的检测
  • 上三角数字三角形:循环嵌套与格式化输出的核心实现与调试指南
  • Codex:面向非技术人的零代码AI工作流引擎
  • Unity透明窗口技术:如何让应用突破窗口边界?
  • Gemini 3.1 Flash语音原生架构解析:突破400ms实时交互拐点
  • 电力配电安装步骤?电力配电安装公司
  • 非技术人员如何看懂AI编程全流程:从原型到上线的协作飞轮