0

到目前为止,我有一个 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)

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

上传图片后回调错误

4

2 回答 2

2

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

这是 Upload 的文档,其中显示了如何使用它的一些示例,包括图像。

于 2021-01-04T02:24:48.873 回答
1

我想我明白了!我没有尝试解码“内容”,而是在阅读后找到了一个更简单的解决方案。HTML dash 组件也有'文件名'..

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)
于 2021-01-05T23:45:12.583 回答