【问题标题】: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 处理图像/图像并通过经过训练和保存的图像分类模型运行它并在仪表板中显示结果
【发布时间】:2021-01-04 01:03:50
【问题描述】:

到目前为止,我有一个 Dash 脚本可以在仪表板上拖放图像。我已经创建了函数来加载和预处理图像,并转向 numpy 数组并通过我保存的图像分类模型运行它来预测。感谢任何帮助!

当我加载图像时,它会给出错误“OSError: [Errno 36] File name too long:....”我相信破折号 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)

我可以就如何解决这个问题寻求帮助吗?

【问题讨论】:

    标签: python plotly dashboard plotly-dash image-classification


    【解决方案1】:

    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,其中显示了一些如何使用它的示例,包括图像。

    【讨论】:

    • 感谢@coralvanda,在上传图片之前消除了错误。但是在我上传图像后,它有一个“回调错误更新 output-prediction.children”,其中文件名太长。我相信这是因为 base64 字符串编码。有什么我可以从那里去的想法吗?
    • 你能分享回溯吗?
    【解决方案2】:

    我想我明白了!我没有尝试解码“内容”,而是在阅读后找到了一个更简单的解决方案。 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)
    

    【讨论】:

      猜你喜欢
      • 2016-05-14
      • 1970-01-01
      • 1970-01-01
      • 2020-04-18
      • 1970-01-01
      • 2020-10-05
      • 2021-07-09
      • 2011-07-30
      • 1970-01-01
      相关资源
      最近更新 更多