You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:基于YOLOv5构建RStudio中Shiny昆虫图像检测应用

在RStudio中结合Python构建Shiny昆虫检测仪表盘(YOLOv5部署)

我希望在RStudio中结合Python构建Shiny仪表盘应用,目标是部署YOLOv5模型(模型文件为best.pt)实现昆虫图像检测,用户可上传JPG图片并获取带边界框的检测结果。以下是我编写的初步代码,恳请技术协助:

# app.py
from shiny import UI
from yolo_v5_inference import Inference
from PIL import Image as view
from IPython.display import Image
# ui.page

# Load the model
model = r'best.pt' # Better model trains using Yolov5 neural network in Python

# Define the UI
ui <- fluidPage(
  # App title ----
  titlePanel("Hello TensorFlow!"),
  # Sidebar layout with input and output definitions ----
  sidebarLayout(
    # Sidebar panel for inputs ----
    sidebarPanel(
      # Input: File upload
      fileInput("image_path", label = "Input a JPEG image")
    ),
    # Main panel for displaying outputs ----
    mainPanel(
      # Output: 
      textOutput(outputId = "prediction"),
      plotOutput(outputId = "image")
    )
  )
)

# Define server logic required to draw a histogram ----
server <- function(input, output) {
  
  image <- reactive({
    req(input$image_path)
    jpeg::readJPEG(input$image_path$datapath)
  })
  
  output$prediction <- renderText({
    
    img <- image() %>% 
      array_reshape(., dim = c(1, dim(.), 1))
    standart = Inference(crops_path=imgage, yolov5_model_path=model, conf_threshold=0.5, rescale=(1.5, 1.5), save_rescaled=True)
    standart.standart
    
    paste0("The predicted bounding-box ")
  })
  
  output$image <- renderPlot({
    plot(as.raster(image()))
  })
  
}

shinyApp(ui, server)

代码问题分析

  1. R与Python交互缺失:未通过reticulate包建立R调用Python代码的通道
  2. 变量名错误:imgage应为img
  3. 图像预处理逻辑错误:YOLOv5无需将图像reshape为(1, dim, 1)格式,且输入应为文件路径或PIL对象,而非R读取的数组
  4. 检测结果未正确返回:未将带边界框的图像传递到Shiny输出,也未提取有效检测信息
  5. Python代码冗余:shiny.UI导入无实际作用

修正后的完整实现方案

步骤1:配置R与Python交互依赖

install.packages("reticulate")
library(reticulate)
# 确保Python环境已安装YOLOv5依赖
# py_install(c("yolov5", "pillow"))

步骤2:编写Python推理脚本(yolo_v5_inference.py)

# yolo_v5_inference.py
from yolov5 import detect
import os
from PIL import Image

def run_inference(image_path, model_path="best.pt", conf_threshold=0.5):
    # 运行YOLOv5检测
    results = detect.run(
        weights=model_path,
        source=image_path,
        conf_thres=conf_threshold,
        save_txt=False,
        save_conf=True,
        save_crop=False,
        project="temp_detect",
        name="results",
        exist_ok=True
    )
    
    # 获取检测后的图像路径
    result_img_path = os.path.join("temp_detect", "results", os.path.basename(image_path))
    
    # 提取检测详情
    detections = []
    for idx, det in enumerate(results.pred[0]):
        x1, y1, x2, y2, conf, cls = det.tolist()
        class_name = results.names[int(cls)]
        detections.append(f"[{idx+1}] 类别: {class_name}, 置信度: {conf:.2f}, 坐标: ({int(x1)},{int(y1)})-({int(x2)},{int(y2)})")
    
    # 返回带框图像和检测信息
    return Image.open(result_img_path), detections

步骤3:修正Shiny应用代码

library(shiny)
library(reticulate)
library(jpeg)
library(png)

# 加载Python推理模块
source_python("yolo_v5_inference.py")

# 模型路径
model_path <- "best.pt"

# 定义UI
ui <- fluidPage(
    titlePanel("昆虫图像检测仪表盘(YOLOv5)"),
    sidebarLayout(
        sidebarPanel(
            fileInput("image_upload", label = "上传JPEG/PNG图像", accept = c("image/jpeg", "image/png")),
            sliderInput("conf_threshold", label = "置信度阈值", min = 0.1, max = 1.0, value = 0.5, step = 0.1)
        ),
        mainPanel(
            h3("检测结果"),
            verbatimTextOutput("detection_info"),
            plotOutput("detected_image", height = "500px")
        )
    )
)

# 定义Server逻辑
server <- function(input, output) {
    
    # 响应式处理检测结果
    detection_result <- reactive({
        req(input$image_upload)
        img_path <- input$image_upload$datapath
        
        # 调用Python推理函数
        run_inference(img_path, model_path, input$conf_threshold)
    })
    
    # 输出检测文本信息
    output$detection_info <- renderPrint({
        req(detection_result())
        cat("检测到的昆虫信息:\n\n")
        for (det in detection_result()[[2]]) {
            cat(det, "\n")
        }
    })
    
    # 输出带边界框的图像
    output$detected_image <- renderPlot({
        req(detection_result())
        img <- detection_result()[[1]]
        plot(as.raster(img))
    })
    
}

# 启动Shiny应用
shinyApp(ui, server)

内容的提问来源于stack exchange,提问作者Leprechault

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 14:58:22