You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to create a Shiny app using R and Python cause the Yolov8 model was developed in Python. But, I try to use my app calling a *py code (yolov8_loader.py) in my app directory and it doesn't work, without any error.
In my example:
library(shiny)
library(shinydashboard)
library(rsconnect)
library(tidyverse)
library(reticulate)
library(purrr)
library(stringr)
# Create a py_env environment and install: pip install ultralytics
setwd('C:/Users/IFMT/anaconda3/envs/py_env')
renv::init()
Sys.setenv(RENV_PATHS_CACHE = 'C:/Users/IFMT/anaconda3/envs/py_env')
renv::use_python(type = 'conda', name = 'py_env')
#
#Create a new Python file, e.g., yolov8_loader.py, with the following content inside the py_env environment:
# import ultralytics as yolo
#
# def load_model():
# model = yolo.YOLO("https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n.pt")
# return model
# Load the Python module
yolov8_loader <- source_python("yolov8_loader.py")
# Import the load_model function
load_model <- yolov8_loader$load_model
# Load the model
model <- load_model()
# Define the UI
ui <- fluidPage(
# App title ----
titlePanel("Hello YOLOv8!"),
# 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: Histogram ----
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))
# Use the loaded model to make predictions
prediction <- model(img)
paste0("The predicted class is ", prediction)
})
output$image <- renderPlot({
plot(as.raster(image()))
})
}
shinyApp(ui, server)
But the yolov8_loader object is always:
NULL
Please, any help with this bug?
Alexandre
The text was updated successfully, but these errors were encountered:
Hi @Leprechault, does this part of your code work on its own without Shiny?
library(reticulate)
# Create a py_env environment and install: pip install ultralytics
setwd('C:/Users/IFMT/anaconda3/envs/py_env')
renv::init()
Sys.setenv(RENV_PATHS_CACHE='C:/Users/IFMT/anaconda3/envs/py_env')
renv::use_python(type='conda', name='py_env')
##Create a new Python file, e.g., yolov8_loader.py, with the following content inside the py_env environment:# import ultralytics as yolo## def load_model():# model = yolo.YOLO("https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n.pt")# return model# Load the Python moduleyolov8_loader<- source_python("yolov8_loader.py")
# Import the load_model functionload_model<-yolov8_loader$load_model# Load the model
load_model()
I suspect that the errors you're seeing are related to reticulate and the yolo model. If that part of the code works, but starts to fail when you involve Shiny, then we can help. Otherwise, I'd recommend you ask for help on https://forum.posit.co where others who have more experience with reticulate might be able to help you.
Another small note that Shiny for Python might be a great fit for this app, especially if you're using a Python-specific model. As a long-time Shiny for R users, I found it surprisingly easy to start using Shiny for Python and you might have a similar experience.
I want to create a Shiny app using R and Python cause the Yolov8 model was developed in Python. But, I try to use my app calling a *py code (
yolov8_loader.py
) in my app directory and it doesn't work, without any error.In my example:
But the
yolov8_loader
object is always:Please, any help with this bug?
Alexandre
The text was updated successfully, but these errors were encountered: