首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Streamlit解压文件

Streamlit解压文件
EN

Stack Overflow用户
提问于 2022-07-20 21:17:24
回答 1查看 149关注 0票数 0

我想部署我的ML模型使用流光,但流式只能允许用户一次上传一个文件。因此,我试图允许用户上传包含图像的zip文件,以便在后端提取图像并用于预测。每当我测试以下代码时,它就会一直抛出:

AttributeError:“列表”对象没有属性“查找”。

我该怎么解决这个问题。

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

发布于 2022-07-20 23:20:19

问题在于代码的这一部分:

代码语言:javascript
复制
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循环可以修复此错误。

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73058225

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档