我想部署我的ML模型使用流光,但流式只能允许用户一次上传一个文件。因此,我试图允许用户上传包含图像的zip文件,以便在后端提取图像并用于预测。每当我测试以下代码时,它就会一直抛出:
AttributeError:“列表”对象没有属性“查找”。
我该怎么解决这个问题。
import altair as alt
import streamlit as st
from PIL import Image
from pathlib import Path
import base64
import io
import pandas as pd
import zipfile
import filetype
st.set_page_config(page_title='Br Classifier', page_icon = 'nm.jpg', layout = 'wide',
initial_sidebar_state = 'expanded')
image = Image.open(r"C:\Users\taiwo\Desktop\image.jfif")
st.image(image, use_column_width=True)
st.write("""
#CLASSIFICATION
This application helps to classify different classes
***
""")
# pylint: enable=line-too-long
from typing import Dict
import streamlit as st
@st.cache(allow_output_mutation=True)
def get_static_store() -> Dict:
"""This dictionary is initialized once and can be used to store the files uploaded"""
return {}
def main():
"""Run this function to run the app"""
static_store = get_static_store()
st.info(__doc__)
file_uploaded = st.file_uploader("Upload", type=["png","jpg","jpeg", "zip"],
accept_multiple_files=True,)
with zipfile.ZipFile(file_uploaded,"r") as z:
z.extractall(".")
selected_model = st.sidebar.selectbox(
'Pick an image classifier model',
('CNN_CLASSIFIER', 'Neural Network'))
st.write('You selected:', selected_model)
if __name__ == "__main__":
main()发布于 2022-07-20 23:20:19
问题在于代码的这一部分:
file_uploaded = st.file_uploader("Upload", type=["png","jpg","jpeg", "zip"],
accept_multiple_files=True,)
with zipfile.ZipFile(file_uploaded,"r") as z:
z.extractall(".")file_uploaded返回一个list,使用with部件,您将尝试读取整个列表,而不是文件本身。
使用for循环可以修复此错误。
file_uploaded = st.file_uploader("Upload", type=["png","jpg","jpeg", "zip"],
accept_multiple_files=True,)
# iterate over each file uploaded
for file in file_uploaded:
if file is not None:
if file.endswith(".zip"):
with zipfile.ZipFile(file_uploaded,"r") as z:
z.extractall(".")
else:
# the part of your code which deals with img extensionshttps://stackoverflow.com/questions/73058225
复制相似问题