在streamlit.io平台上部署Python Streamlit应用程序时出现FileNotFoundError

问题描述 投票:0回答:2

此代码可以在我的本地计算机上运行,但不能在streamlit.io 平台上运行。它显示以下错误消息:

文件“/home/appuser/venv/lib/python3.8/site-packages/streamlit/runtime/scriptrunner/script_runner.py”,第 565 行,在 _run_script exec(code, module.dict) 文件“/app/lipreading” /app/streamlitapp.py”,第 20 行,在 options = os.listdir(os.path.join('...', 'data', 's1'))

问题代码:

import streamlit as st
import os
from moviepy.editor import VideoFileClip
import imageio
import tensorflow as tf
from utils import load_data, num_to_char
from modelutil import load_model

# Set the layout to the Streamlit app as wide
st.set_page_config(layout='wide')

# Setup the sidebar
with st.sidebar:
    st.image('https://www.onepointltd.com/wp-content/uploads/2020/03/inno2.png')
    st.markdown("<h1 style='text-align: center; color: white;'>Abstract</h1>", unsafe_allow_html=True)
    st.info('This project, developed by Amith A G as his MCA final project at KVVS Institute Of Technology, focuses on implementing the LipNet deep learning model for lip-reading and speech recognition. The project aims to demonstrate the capabilities of the LipNet model through a Streamlit application.')

st.markdown("<h1 style='text-align: center; color: white;'>LipNet</h1>", unsafe_allow_html=True)

# Generating a list of options or videos
options = os.listdir(os.path.join('..', 'data', 's1'))
selected_video = st.selectbox('Choose video', options)

# Generate two columns
col1, col2 = st.columns(2)

if options:
    # Rendering the video
    with col1:
        st.info('The video below displays the converted video in mp4 format')
        file_path = os.path.join('..', 'data', 's1', selected_video)
        output_path = os.path.join('test_video.mp4')

        # Convert the video using moviepy
        video_clip = VideoFileClip(file_path)
        video_clip.write_videofile(output_path, codec='libx264')

        # Display the video in the app
        video = open(output_path, 'rb')
        video_bytes = video.read()
        st.video(video_bytes)

    with col2:
        st.info('This is all the machine learning model sees when making a prediction')
        video, annotations = load_data(tf.convert_to_tensor(file_path))
        imageio.mimsave('animation.gif', video, fps=10)
        st.image('animation.gif', width=400)

        st.info('This is the output of the machine learning model as tokens')
        model = load_model()
        yhat = model.predict(tf.expand_dims(video, axis=0))
        decoder = tf.keras.backend.ctc_decode(yhat, [75], greedy=True)[0][0].numpy()
        st.text(decoder)

        # Convert prediction to text
        st.info('Decode the raw tokens into words')
        converted_prediction = tf.strings.reduce_join(num_to_char(decoder)).numpy().decode('utf-8')
        st.text(converted_prediction)

这是GitHubRepository

的内联链接

要求: 图像io==2.9.0
numpy== 1.22.2
moviepy== 1.0.3
opencv-python==4.7.0.72
流线化== 1.22.0
张量流==2.12.0

代码解释: 提供的代码是一个 Streamlit 应用程序,它实现了用于唇读和语音识别的 LipNet 深度学习模型。执行时,应用程序以宽布局启动,并显示一个侧边栏,其中包含图像和有关项目的介绍性段落。该应用程序的主要部分展示了带有标题的 LipNet 模型,并允许用户从选项列表中选择视频。选择视频后,应用程序将其在第一列中呈现为 mp4 视频,并在第二列中显示帧和注释。这些帧由 LipNet 模型处理,该模型预测输出标记并显示它们以及转换后的文本预测。原始标记被进一步解码为单词。总体而言,该应用程序提供了一个用户友好的界面来探索 LipNet 的唇读和语音识别功能,提供视觉表示和对模型预测的见解。

奥斯沃克:

Current Directory: D:\LipReading
Number of subdirectories: 3
Subdirectories: app, data, models
Number of files: 3
Files: .gitattributes, oswalk.py, requirements.txt

Current Directory: D:\LipReading\app
Number of subdirectories: 0
Subdirectories: 
Number of files: 5
Files: animation.gif, modelutil.py, streamlitapp.py, test_video.mp4, utils.py

Current Directory: D:\LipReading\data
Number of subdirectories: 2
Subdirectories: alignments, s1
Number of files: 0
Files: 

Current Directory: D:\LipReading\data\alignments
Number of subdirectories: 1
Subdirectories: s1
Number of files: 0
Files: 

Current Directory: D:\LipReading\data\alignments\s1
Number of subdirectories: 0
Subdirectories:
Number of files: 1000
Files: bbaf2n.align, bbaf3s.align, bbaf4p.align, bbaf5a.align, bbal6n.align, bbal7s.align, bbal8p.align, bbal9a.align, bbas1s.align, bbas2p.align, bbas3a.align, bbaszn.align, bbaz4n.align, bbaz5s.align, bbaz6p.align, bbaz7a.align, bbbf6n.align, bbbf7s.align, bbbf8p.align, bbbf9a.align....ect
Current Directory: D:\LipReading\data\s1
Number of subdirectories: 0
Subdirectories:
Number of files: 1001
Files: bbaf2n.mpg, bbaf3s.mpg, bbaf4p.mpg, bbaf5a.mpg, bbal6n.mpg, bbal7s.mpg, bbal8p.mpg, bbal9a.mpg, bbas1s.mpg, bbas2p.mpg, bbas3a.mpg, bbaszn.mpg, bbaz4n.mpg, bbaz5s.mpg, bbaz6p.mpg, bbaz7a.mpg, bbbf6n.mpg, bbbf7s.mpg, bbbf8p.mpg, bbbf9a.mpg, bbbm1s.mpg, bbbm2p.mpg, bbbm3a.mpg, bbbmzn.mpg, bbbs4n.mpg, bbbs5s.mpg, bbbs6p.mpg, bbbs7a.mpg, bbbz8n.mpg, bbbz9s.mpg, bbie8n.mpg, bbie9s.mpg, bbif1a.mpg, bbifzp.mpg, bbil2n.mpg, bbil3s.mpg, bbil4p.mpg, bbil5a.mpg, bbir6n.mpg, bbir7s.mpg, bbir8p.mpg, bbir9a.mpg, bbiz1s.mpg, bbiz2p.mpg, bbiz3a.mpg, bbizzn.mpg, bbwg1s.mpg, bbwg2p.mpg, bbwg3a.mpg, bbwgzn.mpg, bbwm4n.mpg, bbwm5s.mpg, bbwm6p.mpg, bbwm7a.mpg, bbws8n.mpg, bbws9s.mpg, bbwt1a.mpg, bbwtzp.mpg, bgaa6n.mpg, bgaa7s.mpg, bgaa8.......etc

Current Directory: D:\LipReading\models
Number of subdirectories: 1
Subdirectories: __MACOSX
Number of files: 3
Files: checkpoint, checkpoint.data-00000-of-00001, checkpoint.index

Current Directory: D:\LipReading\models\__MACOSX
Number of subdirectories: 0
Subdirectories:
Number of files: 3
Files: ._checkpoint, ._checkpoint.data-00000-of-00001, ._checkpoint.index
python deep-learning streamlit filenotfounderror
2个回答
2
投票

路径是相对于当前工作目录的,与文件的位置不同。当您在本地运行代码时,您可能在该目录中并且只是执行

python my_file.py
,但这不是平台正在执行的操作,因此当前路径是不同的。

可以使用

__file__
访问当前Python文件的路径。然后我们可以使用从中删除文件名来获取目录。我喜欢使用 pathlib 来实现这一点:

import pathlib
code_dir = pathlib.Path(__file__).parent.resolve()

为了安全起见,Resolve 将路径转换为绝对路径。 Pathlib 允许使用 / 运算符和字符串执行 os.path.join 所做的操作:

files_location = code_dir / ".." / "data" / "s1"  
files_location = files_location.resolve()  # because we used .. in path, it's safer to resolve so we get absolute path

files_location = code_dir / ".." / "data" / "s1"
线的替代形式:

  • files_location = code_dir / "../data/s1"
  • files_location = code_dir.parent / "data" / "s1"
  • files_location = code_dir.parent / "data/s1"

使用对您的用例来说最直观的选项。

列出目录:os.listdir 接受类似路径的对象,而 pathlib.Path 是类似路径的,所以我们可以这样做

os.listdir(files_location)

其他要转换的行:

  • 代替
    file_path = os.path.join('..', 'data', 's1', selected_video)
    ,我们可以做
    file_path = files_location / selected_video
  • 并且
    output_path = os.path.join('test_video.mp4')
    变成
    output_path = code_dir / 'test_video.mp4'
    将其转储到与代码文件相同的位置(您本地的)

0
投票

解决方案: 我将本项目中的相对路径更改为绝对路径,以便通过更改

options = os.listdir(os.path.join('..', 'data', 's1'))
(此代码将相对路径
'../data/s1'
中的文件和目录列表分配给变量
options
)来修复它。

code_dir = pathlib.Path(__file__).parent.resolve() files_location = code_dir / ".." / "data" / "s1" files_location = files_location.resolve()
总的来说,这段代码确定了当前脚本文件的目录路径,然后通过附加相对目录名构造到特定位置的绝对路径。生成的绝对路径存储在 

files_location

 变量中。

然后我们需要将类型更改为字符串,因为当您使用

pathlib.Path

模块创建路径或解析路径时,它会返回一个
Path
对象。因此我使用了下面给出的代码:

file_path = str(files_location / selected_video) output_path = str(code_dir / 'test_video.mp4')
然后将其他python文件中写入的相对路径更改为绝对路径。
查看了我提供的帖子或帖子中给出的更新代码的github链接,供您参考

@h4z3 @doneforaiur 非常感谢您的宝贵帮助和指导。

© www.soinside.com 2019 - 2024. All rights reserved.