提问人:flow44 提问时间:11/18/2023 最后编辑:flow44 更新时间:11/20/2023 访问量:32
按下按钮后,如何在破折号中更新 vtk 图像的显示?
How to update the display of a vtk image in dash, after pressing a button?
问:
我是 plotly/dash 的全新手,我正在努力设计一个关于 3D 体积的主观实验。 基本上,我正在尝试加载一堆 3D 图像(来自预定义的播放列表),显示每个图像(一次一个),为显示的图像打分,然后切换到播放列表中的下一个。 我想出了以下代码,它显示了第一张图像,但不知何故,我找不到为下一个显示器更新图像的方法。
任何帮助将不胜感激。 谢谢。
from dash import Dash, dcc, html, Input, Output, State, callback
import dash_bootstrap_components as dbc
import dash
import dash_vtk
from dash_vtk.utils import to_volume_state
import itk
import sys
import webbrowser
import numpy as np
try:
# VTK 9+
from vtkmodules.vtkImagingCore import vtkRTAnalyticSource
except ImportError:
# VTK =< 8
from vtk.vtkImagingCore import vtkRTAnalyticSource
lines = []
# Read image playlist :
with open(r"playlist.txt", 'r') as fp:
lines = fp.readlines()
NbImages = len(lines)
# Load vtk image (3D) :
volname = lines[0][:-1]
def loadVol(volname):
itkImage = itk.imread(volname)
vtkImage = itk.vtk_image_from_image(itkImage)
vtkVol = to_volume_state(vtkImage)
return(vtkVol)
vtkVol = loadVol(volname)
# Build the vtk View from the 3D image :
vtk_view = dash_vtk.View(
id="vtk-view",
background = [0.4,0.4,0.4],
children=[
dash_vtk.VolumeRepresentation([
dash_vtk.VolumeController(),
dash_vtk.Volume(state=vtkVol),
dash_vtk.GeometryRepresentation()
]),
],
)
# Dash setup
app = Dash(__name__)
server = app.server
Scores = []
# Prepare the html page layout :
app.layout = html.Div([
html.Div(style={"width": "100%", "height": "400px"},children=[vtk_view]),
html.Div(dcc.Input(id='input-on-submit', type='text')),
html.A(html.Button('NEXT', id='submit-val', n_clicks=0),href='/')
])
@app.callback(
Output('vtk-view', 'children'),
Input('submit-val', 'n_clicks'),
State('input-on-submit', 'value'),
prevent_initial_call=True
)
def update_output(n_clicks, value):
Scores.append(value)
volname = lines[len(Scores)-1][:-1]
print("\nprocessing image : " + volname + "\tScore = " +str(value))
vtkVol = loadVol(volname)
vtk_view = dash_vtk.View(
id="vtk-view",
background = [0.4,0.4,0.4],
children=[
dash_vtk.VolumeRepresentation([
dash_vtk.VolumeController(),
dash_vtk.Volume(state=vtkVol),
dash_vtk.GeometryRepresentation()
]),
],
)
if len(Scores) == NbImages:
with open(r'output.txt', 'w') as fp:
for item in Scores:
fp.write("%s\n" % item)
print('Done')
sys.exit()
return(vtk_view)
if __name__ == "__main__":
app.run(debug=True)
我设法读取了播放列表,加载并显示了第一张图像,从该图像的观察者那里收集了分数,但我无法切换到播放列表中的下一张图像。
答: 暂无答案
评论