Advertisement

混凝土缺陷检测系统yolo 如何训练自己的数据集

阅读量:

ec52516a354b4a128456bbb88b999e6a.png图片数量7353,模型已训练200轮;混凝土缺陷检测 数据集 模型 ui界面

✓类别:exposed reinforcement,rust stain,Crack,Spalling,Efflorescence,delamination(外露钢筋,分层,风化,裂缝,剥落,生锈

数据集+模型+代码
3c3c08577a664a7a9bdfef36c8fea1ed.png

混凝土缺陷检测系统

9d0d437ac5cb4639b73590a535f4b916.png

项目概述

本项目旨在开发一个用于混凝土缺陷检测的系统,该系统基于YOLO(You Only Look Once)目标检测框架。系统包含了一个包含7353张图片的数据集,涵盖六种混凝土缺陷类别,并且已经使用YOLO模型进行了200轮的训练。此外,项目还包括一个可视化的用户界面(UI),使得用户可以轻松上传图片并进行实时缺陷检测。
ee2b0920329342128cc12daf5e06e9d3.png

项目特点

  • 大规模数据集 :包含7353张图片,涵盖了六种常见的混凝土缺陷类别。
  • 预训练模型 :已经训练了200轮,可以直接使用。
  • 兼容多种YOLO版本 :支持YOLOv5、YOLOv6、YOLOv7、YOLOv8、YOLOv9、YOLOv10等多个版本。
  • 用户界面 :提供了一个可视化的用户界面,方便用户上传图片并查看检测结果。

技术栈

  • Python : 编程语言
  • PyTorch : 深度学习框架
  • YOLO系列 : 目标检测框架
  • Flask/Django : Web框架(用于构建UI)
  • HTML/CSS/JavaScript : UI前端开发

数据集介绍

数据集包含六种混凝土缺陷类别:
ae2ba691ecc04dc5b6d9bd815f225812.png

  • Exposed Reinforcement (外露钢筋) :钢筋暴露在混凝土表面。
  • Rust Stain (生锈) :钢筋因腐蚀而在混凝土表面形成的锈迹。
  • Crack (裂缝) :混凝土表面的裂缝。
  • Spalling (剥落) :混凝土表面出现的剥离现象。
  • Efflorescence (风化) :混凝土表面的盐析现象。
  • Delamination (分层) :混凝土内部或表面的分层现象。

数据集结构

复制代码
 ConcreteDefectDetectionDataset/

    
 ├── images/  # 图像文件
    
 │   ├── train/  # 训练集图像
    
 │   │   ├── image_00001.jpg
    
 │   │   ├── image_00002.jpg
    
 │   │   └── ...
    
 │   └── val/  # 验证集图像
    
 │       ├── image_00001.jpg
    
 │       ├── image_00002.jpg
    
 │       └── ...
    
 └── labels/  # YOLO格式标注文件夹
    
     ├── train/  # 训练集标签
    
     │   ├── image_00001.txt
    
     │   ├── image_00002.txt
    
     │   └── ...
    
     └── val/  # 验证集标签
    
     ├── image_00001.txt
    
     ├── image_00002.txt
    
     └── ...
    
    
    
    

模型介绍

模型已经使用YOLO框架进行了200轮的训练,并保存在指定路径中。模型支持多种YOLO版本,包括YOLOv5、YOLOv6、YOLOv7、YOLOv8、YOLOv9、YOLOv10。

用户界面介绍

用户界面使用Flask/Django Web框架开发,提供了一个简单的表单用于上传图片。用户可以选择上传一张图片,并点击提交按钮进行实时检测。检测结果会在页面上显示出来,并附带每个缺陷的边界框和类别标签。

示例代码

1. 主程序入口 (src/main.py)
512814ed18e8445ea1978042f8d23216.png

复制代码
 from flask import Flask, request, render_template

    
 import os
    
 import cv2
    
 import torch
    
 from model import ConcreteDefectDetector
    
  
    
 app = Flask(__name__)
    
  
    
 @app.route('/', methods=['GET', 'POST'])
    
 def index():
    
     if request.method == 'POST':
    
     if 'file' not in request.files:
    
         return "No file part"
    
     file = request.files['file']
    
     if file.filename == '':
    
         return "No selected file"
    
     if file:
    
         filename = 'uploaded_image.jpg'
    
         file.save(filename)
    
         detector = ConcreteDefectDetector()
    
         result_image = detector.detect(filename)
    
         cv2.imwrite('detected_image.jpg', result_image)
    
         return render_template('index.html', result_image='detected_image.jpg')
    
     return render_template('index.html')
    
  
    
 if __name__ == '__main__':
    
     app.run(debug=True)
    
    
    
    

2. 模型定义 (src/model.py)

复制代码
复制代码
 import torch

    
 import torch.nn as nn
    
 from yolov5.models.common import DetectMultiBackend
    
  
    
 class ConcreteDefectDetector:
    
     def __init__(self):
    
     self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    
     self.model = DetectMultiBackend('models/pre_trained.pth', device=self.device)
    
     self.model.eval()
    
  
    
     def detect(self, image_path):
    
     image = cv2.imread(image_path)
    
     results = self.model(image)
    
     results.render()  # updates results.imgs with boxes and labels
    
     return results.imgs[0]
    
  
    
 def load_pretrained_model(path):
    
     model = DetectMultiBackend(path, device=torch.device("cuda" if torch.cuda.is_available() else "cpu"))
    
     model.eval()
    
     return model
    
    
    
    

3. UI前端 (templates/index.html)

复制代码
复制代码
 <!DOCTYPE html>

    
 <html lang="en">
    
 <head>
    
     <meta charset="UTF-8">
    
     <title>混凝土缺陷检测</title>
    
     <style>
    
     body {
    
         font-family: Arial, sans-serif;
    
         text-align: center;
    
         margin-top: 50px;
    
     }
    
     form {
    
         display: inline-block;
    
     }
    
     .result-image {
    
         max-width: 80%;
    
         height: auto;
    
     }
    
     </style>
    
 </head>
    
 <body>
    
     <h1>上传图片进行混凝土缺陷检测</h1>
    
     <form method="post" enctype="multipart/form-data">
    
     <input type="file" name="file" accept="image/*" required>
    
     <br><br>
    
     <button type="submit">上传并检测</button>
    
     </form>
    
     {% if result_image %}
    
     <h2>检测结果</h2>
    
     <img class="result-image" src="{{ result_image }}" alt="Detected Defects">
    
     {% endif %}
    
 </body>
    
 </html>
    
    
    
    

项目目录结构

复制代码
 ConcreteDefectDetectionSystem/

    
 ├── src/
    
 │   ├── main.py  # 主程序入口
    
 │   ├── model.py  # 模型定义
    
 │   ├── templates/  # HTML模板
    
 │   │   ├── index.html  # 用户界面模板
    
 ├── data/
    
 │   ├── ConcreteDefectDetectionDataset/  # 数据集目录
    
 │   │   ├── images/
    
 │   │   └── labels/
    
 ├── models/
    
 │   ├── pre_trained.pth  # 预训练模型
    
 └── README.md  # 项目说明
    
    
    
    

项目运行

确保安装了必要的依赖库:

复制代码
    pip install torch torchvision flask opencv-python
    

然后运行主程序

复制代码
    python src/main.py
    

访问本地服务器:

复制代码
    http://127.0.0.1:5000/
    

学习资源

项目中的代码包含了详细的注释,帮助初学者理解各个部分的功能和作用。同时,提供的数据集和预训练模型可以让用户快速上手,了解如何使用YOLO系列模型进行混凝土缺陷检测。

总结

这个混凝土缺陷检测系统是一个完整的解决方案,它不仅包含了大规模的数据集和预训练模型,还包括了一个可视化的用户界面。系统提供了从数据预处理到模型训练和评估的完整流程,并且易于扩展和修改。对于初学者来说,这是一个很好的学习平台,可以深入了解混凝土缺陷检测技术和深度学习的应用。

全部评论 (0)

还没有任何评论哟~