我想使用 Streamlit 部署我的 ML 模型,但 Streamlit 只能允许用户一次上传一个文件。因此,我尝试允许用户上传包含图像的 zip 文件,以便可以在后端提取图像并用于预测。每当我测试下面的代码时,它都会不断抛出:
AttributeError:“list”对象没有属性“seek”。
我该如何解决这个问题。
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()
问题出在代码的这一部分:
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 extensions
您将一个列表(由
st.file_uploader
返回)传递给 ZipFile,这是行不通的,这是一个工作版本:
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')
st.markdown("""
#CLASSIFICATION
This application helps to classify different classes
***
""")
# pylint: enable=line-too-long
from typing import Dict
import streamlit as st
def main():
"""Run this function to run the app"""
st.info(__doc__)
file_uploaded = st.file_uploader("Upload", type=["png","jpg","jpeg", "zip"],
accept_multiple_files=True)
if file_uploaded:
# Ensure the file is a zip file and handle the uploaded files correctly
if isinstance(file_uploaded, list):
for uploaded_file in file_uploaded:
if uploaded_file.type == "application/zip":
with zipfile.ZipFile(uploaded_file, "r") as z:
z.extractall(".")
else:
st.warning("Please upload a zip file.")
else:
st.warning("Please upload multiple files as a list.")
selected_model = st.sidebar.selectbox(
'Pick an image classifier model',
('CNN_CLASSIFIER', 'Neural Network'))
st.write('You selected:', selected_model)
main()