使用 Dash 通过经过训练和保存的图像分类模型处理 image/images 和 运行 并在 Dashboard 中显示结果
Using Dash to process an image/images and running it through a trained and saved image classification model and display results in the Dashboard
到目前为止,我有一个 Dash 脚本可以在 Dashboard 上拖放图像。我已经创建了函数来加载和预处理图像,并通过我保存的图像分类模型转向 numpy 数组和 运行 它来预测。感谢任何帮助!
当我加载图像时,出现错误'OSError: [Errno 36] File name too long:....' 我相信短划线的 'contents' html 是 base64 编码的,所以可能需要解码?但我不确定或如何。
import io
import dash
import time
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import base64
import tensorflow as tf
from matplotlib import image
from glob import glob
import os
import numpy as np
from PIL import Image
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow import keras
from matplotlib import pyplot
import time
model = keras.models.load_model(filename)
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Upload(
id='upload-image',
children=html.Div([
'Drag and Drop or ',
html.A('Select Files')
]),
style={
'width': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
'margin': '10px'
},
# Allow multiple files to be uploaded
multiple=True
),
html.Div(id='output-image-upload'),
])
def load_and_preprocess(image):
image1 = Image.open(image)
rgb = Image.new('RGB', image1.size)
rgb.paste(image1)
image = rgb
test_image = image.resize((256,256))
return test_image
def np_array_normalise(test_image):
np_image = np.array(test_image)
np_image = np_image / no_of_pixels
final_image = np.expand_dims(np_image, 0)
return final_image
@app.callback(Output('output-prediction', 'children'),
Input('upload-image', 'contents'))
def prediction(image):
final_img = load_and_preprocess(image)
final_img = np_array_normalise(final_img)
Y = model.predict(final_img)
return Y
if __name__ == '__main__':
app.run_server(debug=True)
我能否获得有关如何解决此问题的帮助?
Attribute error: 'NoneType' object has no attribute 'read'
您的回调可能正在初始化,并将它初始化的 None
值发送到下一个函数。这应该可以解决该问题:
@app.callback(Output('output-prediction', 'children'),
Input('upload-image', 'contents'))
def prediction(image):
if image is None:
raise dash.exceptions.PreventUpdate
final_img = load_and_preprocess(image)
final_img = np_array_normalise(final_img)
Y = model.predict(final_img)
return Y
Here's the docs for the Upload,其中显示了一些如何使用它的示例,包括图像。
我想我明白了!我没有尝试解码 'contents',而是在阅读后找到了一个更简单的解决方案。 HTML dash 组件还有 'filename'..
import io
import dash
import time
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import base64
import tensorflow as tf
from matplotlib import image
from glob import glob
import os
import numpy as np
from PIL import Image
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow import keras
from matplotlib import pyplot
import time
model = keras.models.load_model(filename)
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Upload(
id='upload-image',
children=html.Div([
'Drag and Drop or ',
html.A('Select Files')
]),
style={
'width': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
'margin': '10px'
},
# Allow multiple files to be uploaded
multiple=True
),
html.Div(id='output-image-upload'),
])
def load_and_preprocess(image):
image1 = Image.open(image)
rgb = Image.new('RGB', image1.size)
rgb.paste(image1)
image = rgb
test_image = image.resize((256,256))
return test_image
def np_array_normalise(test_image):
np_image = np.array(test_image)
np_image = np_image / no_of_pixels
final_image = np.expand_dims(np_image, 0)
return final_image
@app.callback(Output('output-prediction', 'children'),
Input('upload-image', 'filename'))
def prediction(image):
if image is None:
raise dash.exceptios.PreventUpdate()
final_img = load_and_preprocess(image)
final_img = np_array_normalise(final_img)
Y = model.predict(final_img)
return Y
if __name__ == '__main__':
app.run_server(debug=True)
到目前为止,我有一个 Dash 脚本可以在 Dashboard 上拖放图像。我已经创建了函数来加载和预处理图像,并通过我保存的图像分类模型转向 numpy 数组和 运行 它来预测。感谢任何帮助!
当我加载图像时,出现错误'OSError: [Errno 36] File name too long:....' 我相信短划线的 'contents' html 是 base64 编码的,所以可能需要解码?但我不确定或如何。
import io
import dash
import time
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import base64
import tensorflow as tf
from matplotlib import image
from glob import glob
import os
import numpy as np
from PIL import Image
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow import keras
from matplotlib import pyplot
import time
model = keras.models.load_model(filename)
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Upload(
id='upload-image',
children=html.Div([
'Drag and Drop or ',
html.A('Select Files')
]),
style={
'width': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
'margin': '10px'
},
# Allow multiple files to be uploaded
multiple=True
),
html.Div(id='output-image-upload'),
])
def load_and_preprocess(image):
image1 = Image.open(image)
rgb = Image.new('RGB', image1.size)
rgb.paste(image1)
image = rgb
test_image = image.resize((256,256))
return test_image
def np_array_normalise(test_image):
np_image = np.array(test_image)
np_image = np_image / no_of_pixels
final_image = np.expand_dims(np_image, 0)
return final_image
@app.callback(Output('output-prediction', 'children'),
Input('upload-image', 'contents'))
def prediction(image):
final_img = load_and_preprocess(image)
final_img = np_array_normalise(final_img)
Y = model.predict(final_img)
return Y
if __name__ == '__main__':
app.run_server(debug=True)
我能否获得有关如何解决此问题的帮助?
Attribute error: 'NoneType' object has no attribute 'read'
您的回调可能正在初始化,并将它初始化的 None
值发送到下一个函数。这应该可以解决该问题:
@app.callback(Output('output-prediction', 'children'),
Input('upload-image', 'contents'))
def prediction(image):
if image is None:
raise dash.exceptions.PreventUpdate
final_img = load_and_preprocess(image)
final_img = np_array_normalise(final_img)
Y = model.predict(final_img)
return Y
Here's the docs for the Upload,其中显示了一些如何使用它的示例,包括图像。
我想我明白了!我没有尝试解码 'contents',而是在阅读后找到了一个更简单的解决方案。 HTML dash 组件还有 'filename'..
import io
import dash
import time
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import base64
import tensorflow as tf
from matplotlib import image
from glob import glob
import os
import numpy as np
from PIL import Image
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow import keras
from matplotlib import pyplot
import time
model = keras.models.load_model(filename)
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Upload(
id='upload-image',
children=html.Div([
'Drag and Drop or ',
html.A('Select Files')
]),
style={
'width': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
'margin': '10px'
},
# Allow multiple files to be uploaded
multiple=True
),
html.Div(id='output-image-upload'),
])
def load_and_preprocess(image):
image1 = Image.open(image)
rgb = Image.new('RGB', image1.size)
rgb.paste(image1)
image = rgb
test_image = image.resize((256,256))
return test_image
def np_array_normalise(test_image):
np_image = np.array(test_image)
np_image = np_image / no_of_pixels
final_image = np.expand_dims(np_image, 0)
return final_image
@app.callback(Output('output-prediction', 'children'),
Input('upload-image', 'filename'))
def prediction(image):
if image is None:
raise dash.exceptios.PreventUpdate()
final_img = load_and_preprocess(image)
final_img = np_array_normalise(final_img)
Y = model.predict(final_img)
return Y
if __name__ == '__main__':
app.run_server(debug=True)