到目前为止,我有一个 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)
我可以就如何解决这个问题寻求帮助吗?