基于YOLO8水稻病虫害检测系统 水稻病虫害检测系统 YOLO目标检测算法 识别图片与视频支持本地摄像头识别,图片识别支持统计检测到的物体数量,UI界面动态调节模型置信度

基于YOLO8的水稻病虫害检测系统 水稻病虫害检测系统 YOLO目标检测算法在这里插入图片描述

技术栈:yolo8+streamlit
[1]可以识别图片与视频,也支持本地摄像头识别,图片识别支持统计检测到的物体数量,并返回到前端页面显示
[2]可以通过UI界面动态调节模型置信度,可以动态选择模型权重
[3]系统目录下附带有非常详细的环境配置运行指导pdf文件(将近10页)。
windows系统下yolo+streamlit项目免环境配置安装包。下载免环境压缩包文件,解压后即可一键启动项目,无需手动配置环境。注意:免环境压缩包,仅限windows系统,其余不支持,下单后可向客服索要。
[4]界面UI优美,检测精度高!包含源码、训练好的权重文件,模型的map50为83.9%在这里插入图片描述

本系统是的web应用服务(有前后端,服务端默认在8501端口启动),可以部署到服务器。不同于基于pyside,pyqt之类的系统,这种系统只能在本地电脑运行,而本系统不单单是可以在本地电脑运行,还可以部署到服务器上
全系统仅200多行代码,代码注释非常非常详细,不用担心看不懂项目逻辑,支持二次开发(如果基于这个系统改为基于YOLO8的安全帽识别,只需将模型放在指定目录下即可)。本系统基于pytorch+streamlit ,只要懂pytorch,对照streamlit官方文档,二次开发轻而易举,在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
好的,下面是一个完整的基于YOLOv8的水稻病虫害检测系统的项目代码和详细配置说明。该系统使用Streamlit作为前端框架,支持图片、视频识别以及本地摄像头识别,并且可以通过UI界面动态调节模型置信度和选择不同的模型权重。

完整项目代码

import os
import sys
import streamlit as st
from ultralytics import YOLO
import cv2
import numpy as np
from PIL import Image
import tempfile

# 加载模型
@st.cache_resource
def load_model(weights_path):
    model = YOLO(weights_path)
    return model

# 图片检测函数
def detect_image(model, image, conf_threshold):
    results = model.predict(image, conf=conf_threshold)[0]
    annotated_frame = annotate_image(image, results)
    return annotated_frame

# 视频检测函数
def detect_video(model, video_path, conf_threshold):
    cap = cv2.VideoCapture(video_path)
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        results = model.predict(frame, conf=conf_threshold)[0]
        annotated_frame = annotate_image(frame, results)
        yield annotated_frame
    cap.release()

# 摄像头检测函数
def detect_camera(model, conf_threshold):
    cap = cv2.VideoCapture(0)
    while True:
        ret, frame = cap.read()
        if not ret:
            break
        results = model.predict(frame, conf=conf_threshold)[0]
        annotated_frame = annotate_image(frame, results)
        yield annotated_frame
    cap.release()

# 标注图像函数
def annotate_image(image, results):
    for result in results.boxes.cpu().numpy():
        r = result.xyxy[0].astype(int)
        cls = int(result.cls[0])
        conf = result.conf[0]
        
        label = f"{model.names[cls]} {conf:.2f}"
        color = (0, 255, 0)
        cv2.rectangle(image, (r[0], r[1]), (r[2], r[3]), color, 2)
        cv2.putText(image, label, (r[0], r[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)
    return image

# Streamlit 主界面
def main():
    st.title("Rice Pest Detection System")
    
    # 动态加载模型
    weights_options = ['best_rice_pest_yolov8.pt']  # 添加更多权重文件路径
    selected_weights = st.sidebar.selectbox("Select Model Weights", weights_options)
    model = load_model(selected_weights)
    
    # 动态调整置信度阈值
    conf_threshold = st.sidebar.slider("Confidence Threshold", min_value=0.0, max_value=1.0, value=0.5, step=0.01)
    
    # 输入方式选择
    input_type = st.sidebar.radio("Input Type", ["Image", "Video", "Camera"])
    
    if input_type == "Image":
        uploaded_file = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"])
        if uploaded_file is not None:
            image = Image.open(uploaded_file)
            image_np = np.array(image)
            annotated_image = detect_image(model, image_np, conf_threshold)
            st.image(annotated_image, channels="BGR", caption="Detected Image")
            
            # 统计检测到的物体数量
            results = model.predict(image_np, conf=conf_threshold)[0]
            class_counts = {}
            for result in results.boxes.cpu().numpy():
                cls = int(result.cls[0])
                class_name = model.names[cls]
                if class_name in class_counts:
                    class_counts[class_name] += 1
                else:
                    class_counts[class_name] = 1
            
            st.subheader("Detection Summary:")
            for class_name, count in class_counts.items():
                st.write(f"{class_name}: {count}")
    
    elif input_type == "Video":
        uploaded_file = st.file_uploader("Upload a video...", type=["mp4", "avi"])
        if uploaded_file is not None:
            tfile = tempfile.NamedTemporaryFile(delete=False) 
            tfile.write(uploaded_file.read())
            tfpath = tfile.name
            cap = cv2.VideoCapture(tfpath)
            frame_placeholder = st.empty()
            for annotated_frame in detect_video(model, tfpath, conf_threshold):
                frame_placeholder.image(annotated_frame, channels="BGR", use_column_width=True)
            cap.release()
            os.remove(tfpath)
    
    elif input_type == "Camera":
        frame_placeholder = st.empty()
        for annotated_frame in detect_camera(model, conf_threshold):
            frame_placeholder.image(annotated_frame, channels="BGR", use_column_width=True)

if __name__ == "__main__":
    main()

文件结构

rice_pest_detection/
├── main.py
├── datasets/
│   └── rice_pests/
│       ├── images/
│       │   ├── image1.jpg
│       │   ├── image2.jpg
│       │   └── ...
│       └── labels/
│           ├── image1.txt
│           ├── image2.txt
│           └── ...
├── best_rice_pest_yolov8.pt
└── requirements.txt

安装依赖项

首先,确保你已经安装了所有必要的依赖项。你可以通过以下命令安装:

pip install -r requirements.txt

requirements.txt 内容如下:

streamlit==1.25.0
opencv-python
torch==2.0
ultralytics

数据集格式

假设你的数据集已经按照YOLOv5/YOLOv8的格式进行了标注。每个图像文件对应一个文本文件,其中包含边界框信息。例如:

image1.jpg

0 0.5 0.5 0.2 0.2
1 0.7 0.7 0.1 0.1

这表示图像中有两个对象,第一个对象的类别是0(背景),第二个对象的类别是1(某种病虫害)。

配置说明

数据集目录结构
  • datasets/rice_pests/images: 存放所有的图片文件。
  • datasets/rice_pests/labels: 存放对应的标注文件(.txt 格式)。
训练好的权重文件 (best_rice_pest_yolov8.pt)

你需要有一个训练好的YOLOv8模型权重文件。这个文件可以从你的训练过程中获得,或者从其他来源获取。

数据集配置文件 (data.yaml)

创建一个 data.yaml 文件来配置数据集路径和类别信息。

运行步骤总结

  1. 克隆项目仓库(如果有的话):

    git clone https://github.com/yourusername/rice_pest_detection.git
    cd rice_pest_detection
    
  2. 安装依赖项

    pip install -r requirements.txt
    
  3. 运行Streamlit应用

    streamlit run main.py
    
  4. 操作界面

    • 选择图片进行检测。
    • 选择视频进行检测。
    • 使用摄像头进行实时检测。
    • 通过侧边栏动态调整模型置信度。
    • 通过侧边栏选择不同的模型权重。

结果展示

你可以通过以下方式查看演示视频:

  • 请求你提供的演示视频链接或文件。

总结

基于YOLOv8的水稻病虫害检测系统的项目介绍和代码实现。该项目支持图片、文件夹、视频等多种输入方式,并且可以实时检测水稻病虫害。希望这些详细的信息和代码能够帮助你顺利实施和优化你的水稻病虫害检测系统。


免环境压缩包

如果你担心环境配置问题,可以直接下载免环境压缩包。以下是免环境压缩包的使用说明:

  1. 解压压缩包

    • 将下载的压缩包解压到任意目录。
  2. 启动项目

    • 打开解压后的目录,找到 start.bat 文件。
    • 双击 start.bat 文件,自动启动项目。
    • 默认情况下,项目会在浏览器中打开,地址为 http://localhost:8501

注意事项

  • 操作系统兼容性:免环境压缩包仅限Windows系统,不支持其他操作系统。
  • 二次开发:系统基于PyTorch和Streamlit,支持二次开发。只需将新的模型权重文件放置在指定目录下即可更换模型。
  • 部署服务器:由于系统是天然的Web应用服务,默认在8501端口启动,可以轻松部署到服务器上。部署后,其他人可以通过提供的地址链接访问和使用系统。

希望这些详细的信息和代码能够帮助你成功实施和优化你的水稻病虫害检测系统!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值