当前位置: 首页 > 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.gsyq.cn/news/1533630.html

相关文章:

  • Ohook终极指南:5分钟免费解锁Office 365完整功能
  • Monorepo本质:语义一致性治理与规模化协作降熵
  • 实力强的花木枝叶粉碎机生产厂推荐与费用 - mypinpai
  • 5步上手:通达信缠论插件ChanlunX实现智能中枢绘制与笔段识别
  • AWS S3 Sync 生产级同步原理与避坑指南
  • 如何选择最佳句子相似度模型:jeffding/sentence_similarity_semantic_search-openmind vs 传统方法的终极对比指南
  • 靠谱的电力工具检测中心怎么选?弘宇电力检测口碑如何? - mypinpai
  • 电力配电安装步骤?电力配电安装公司
  • 非技术人员如何看懂AI编程全流程:从原型到上线的协作飞轮
  • 探索未来文件管理:ownCloud Infinite Scale
  • SAP Cloud Integration 租户授权设计,从用户、用户组到技术用户的一套治理思路
  • 探讨快递箱批量定制的性价比,哪家更划算? - mypinpai
  • 基于yolov5的森林火灾识别系统,基于深度学习的森林火灾检测系统,森林火灾识别系统。
  • 2026年佛山注册公司服务商怎么选?多维度对比本地外资企业执照注册与电商执照代办机构 - 优质品牌商家
  • 计算机毕业设计之西华花园家教管理系统
  • 中卫市黄金回收白银回收铂金回收彩金回收店铺哪家靠谱?2026实测五家诚信优选实体门店及电话地址推荐 - 盛世金银回收
  • GPT-5.5不存在?揭秘2024真实可用的大模型能力图谱
  • 昭通市黄金回收白银回收铂金回收彩金回收店铺排行榜 2026实测五家诚信优选实体门店及电话地址推荐 - 大熊猫898989
  • Win8.1笔记本详尽装机攻略:驱动注入与BIOS适配实战
  • 2026年市场洞察:浙江熏蒸托盘采购指南,5家供应商深度评测与真实案例参考 - 优质品牌商家
  • 收藏!升学季选专业不踩坑:网络安全等10类长期有前途的专业方向指南
  • MatrixVB:VB6时代的MATLAB式矩阵计算与可视化插件
  • Gemini 3.1 Pro多模态工程落地实战:ROI裁剪与Token精算
  • ROC曲线与AUC深度解析:从阈值扫描到业务决策的工程实践
  • Ubuntu下OBS Studio安装与硬件编码配置实战指南
  • 收藏!想入行金融网络安全?这个专业的培养_课程_就业全梳理
  • Visio 2019合法替代方案与专业绘图技巧全解析
  • 抖音下载神器:如何轻松批量保存你喜欢的短视频内容?
  • 3步掌握Microsoft Foundry Toolkit:在VS Code中构建AI应用的完整指南
  • 跟着 MDN 学 React 框架 Day 3:React 入门——核心概念与第一个应用