I want to create a Shiny Python app without success for image detection. But the functions in shiny
library don't work. My example is:
# Run in Jupyter notebook inside a conda environment where:
# pip install ultralytics
# pip install shiny
import shiny
from shiny import fluidPage, titlePanel, sidebarLayout, sidebarPanel, mainPanel, fileInput, textOutput, plotOutput
import numpy as np
from PIL import Image
import torch
from torchvision.transforms import functional as F
import ultralytics as yolo
# Load the model
def load_model():
model = yolo.YOLO("https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n.pt")
return 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: Text
textOutput(outputId = "prediction"),
# Output: Plot
plotOutput(outputId = "image")
)
)
)
# Define server logic required to make predictions
def server(input, output):
@input.on_change('image_path')
def update_image(change):
if change.new:
# Convert uploaded image to a numpy array
img = Image.open(change.new)
img = np.array(img)
# Preprocess image
img = F.to_tensor(F.resize(img, (416, 416))).unsqueeze(0)
# Use the loaded model to make predictions
prediction = model(img)
# Convert prediction to text
output.prediction.value = "The predicted class is {}".format(prediction)
# Display image
output.image.data = img
# Create Shiny app
app = shiny.Shiny(ui = ui, server = server)
# Run the app
app.run_server()
#ImportError: cannot import name 'fluidPage' from 'shiny'
#(/home/fores/anaconda3/envs/myyolov8/lib/python3.12/site-packages/shiny/__init__.py)
Please, any help with it?