FileNotFoundError 在 streamlit.io 平台部署 Python Streamlit 应用时发生。

huangapple go评论83阅读模式
英文:

FileNotFoundError When Deploying a Python Streamlit Application on the streamlit.io Platform

问题

这段代码在我的本地计算机上运行正常,但在streamlit.io平台上不起作用。它显示以下错误消息:

解决的代码:

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

# 将Streamlit应用程序的布局设置为wide
st.set_page_config(layout='wide')

# 设置侧边栏
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) 

# 生成选项或视频列表
code_dir = pathlib.Path(__file__).parent.resolve()
files_location = code_dir / ".." / "data" / "s1"  
files_location = files_location.resolve()  

# 将files_location转换为文件列表
options = os.listdir(files_location)

selected_video = st.selectbox('Choose video', options)

# 生成两列
col1, col2 = st.columns(2)

if options: 

    # 渲染视频 
    with col1: 
        st.info('The video below displays the converted video in mp4 format')
        file_path = str(files_location / selected_video)
        output_path = str(code_dir / 'test_video.mp4')

        # 使用moviepy将视频转换为mp4格式
        video_clip = VideoFileClip(file_path)
        video_clip.write_videofile(output_path, codec='libx264')

        # 在应用程序中显示视频
        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)

        # 将预测转换为文本
        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)

输出:
FileNotFoundError 在 streamlit.io 平台部署 Python Streamlit 应用时发生。

问题的代码:

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

# 将布局设置为Streamlit应用程序的wide
st.set_page_config(layout='wide')

# 设置侧边栏
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)

# 生成选项或视频列表
options = os.listdir(os.path.join('..', 'data', 's1'))
selected_video = st.selectbox('Choose video', options)

# 生成两列
col1, col2 = st.columns(2)

if options:
    # 渲染视频
    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')

        # 使用moviepy将视频转换为mp4格式
        video_clip = VideoFileClip(file_path)
        video_clip.write_videofile(output_path, codec='libx264')

        # 在应用程序中显示视频
        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)

        # 将预测转换为文本
        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)

这段代码是一个Streamlit应用程序,实现了LipNet深度学习模型用于唇语识别和语音识别。当执行时,该应用程序以宽布局启动,显示包含图像和项目简介段落的侧边栏。应用程序的主要部分展示了LipNet模型,具有标题,并允许用户从选项列表中选择视频。选择视频后,应用程序在第一列中以mp4视频的形式渲染它,并在第二列显示帧和注释。帧经过LipNet模型处理,该模型预测输出令牌并显示它们,以及转换后的文本预测。原始令牌进一步解码为单词。总体而言,该应用程序提供了一个用户友好

英文:

This code works on my local computer, but it is not working on the streamlit.io platform. It displays the following error message:

> File “/home/appuser/venv/lib/python3.8/site-packages/streamlit/runtime/scriptrunner/script_runner.py”, line 565, in _run_script exec(code, module.dict) File “/app/lipreading/app/streamlitapp.py”, line 20, in options = os.listdir(os.path.join(‘…’, ‘data’, ‘s1’))

Solved code:

import streamlit as st
import os 
import pathlib
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 of the Streamlit app as wide 
st.set_page_config(layout=&#39;wide&#39;)
# Setup the sidebar
with st.sidebar: 
st.image(&#39;https://www.onepointltd.com/wp-content/uploads/2020/03/inno2.png&#39;)
st.markdown(&quot;&lt;h1 style=&#39;text-align: center; color: white;&#39;&gt;Abstract&lt;/h1&gt;&quot;, unsafe_allow_html=True) 
st.info(&#39;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.&#39;)
st.markdown(&quot;&lt;h1 style=&#39;text-align: center; color: white;&#39;&gt;LipNet&lt;/h1&gt;&quot;, unsafe_allow_html=True) 
# Generating a list of options or videos 
code_dir = pathlib.Path(__file__).parent.resolve()
files_location = code_dir / &quot;..&quot; / &quot;data&quot; / &quot;s1&quot;  
files_location = files_location.resolve()  
# Convert the files_location to a list of files
options = os.listdir(files_location)
selected_video = st.selectbox(&#39;Choose video&#39;, options)
# Generate two columns 
col1, col2 = st.columns(2)
if options: 
# Rendering the video 
with col1: 
st.info(&#39;The video below displays the converted video in mp4 format&#39;)
file_path = str(files_location / selected_video)
output_path = str(code_dir / &#39;test_video.mp4&#39;)
# Convert the video using moviepy
video_clip = VideoFileClip(file_path)
video_clip.write_videofile(output_path, codec=&#39;libx264&#39;)
# Display the video in the app
video = open(output_path, &#39;rb&#39;)
video_bytes = video.read()
st.video(video_bytes)
with col2: 
st.info(&#39;This is all the machine learning model sees when making a prediction&#39;)
video, annotations = load_data(tf.convert_to_tensor(file_path))
imageio.mimsave(&#39;animation.gif&#39;, video, fps=10)
st.image(&#39;animation.gif&#39;, width=400) 
st.info(&#39;This is the output of the machine learning model as tokens&#39;)
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(&#39;Decode the raw tokens into words&#39;)
converted_prediction = tf.strings.reduce_join(num_to_char(decoder)).numpy().decode(&#39;utf-8&#39;)
st.text(converted_prediction)

Ouput:
FileNotFoundError 在 streamlit.io 平台部署 Python Streamlit 应用时发生。

Problem code:

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=&#39;wide&#39;)
# Setup the sidebar
with st.sidebar:
st.image(&#39;https://www.onepointltd.com/wp-content/uploads/2020/03/inno2.png&#39;)
st.markdown(&quot;&lt;h1 style=&#39;text-align: center; color: white;&#39;&gt;Abstract&lt;/h1&gt;&quot;, unsafe_allow_html=True)
st.info(&#39;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.&#39;)
st.markdown(&quot;&lt;h1 style=&#39;text-align: center; color: white;&#39;&gt;LipNet&lt;/h1&gt;&quot;, unsafe_allow_html=True)
# Generating a list of options or videos
options = os.listdir(os.path.join(&#39;..&#39;, &#39;data&#39;, &#39;s1&#39;))
selected_video = st.selectbox(&#39;Choose video&#39;, options)
# Generate two columns
col1, col2 = st.columns(2)
if options:
# Rendering the video
with col1:
st.info(&#39;The video below displays the converted video in mp4 format&#39;)
file_path = os.path.join(&#39;..&#39;, &#39;data&#39;, &#39;s1&#39;, selected_video)
output_path = os.path.join(&#39;test_video.mp4&#39;)
# Convert the video using moviepy
video_clip = VideoFileClip(file_path)
video_clip.write_videofile(output_path, codec=&#39;libx264&#39;)
# Display the video in the app
video = open(output_path, &#39;rb&#39;)
video_bytes = video.read()
st.video(video_bytes)
with col2:
st.info(&#39;This is all the machine learning model sees when making a prediction&#39;)
video, annotations = load_data(tf.convert_to_tensor(file_path))
imageio.mimsave(&#39;animation.gif&#39;, video, fps=10)
st.image(&#39;animation.gif&#39;, width=400)
st.info(&#39;This is the output of the machine learning model as tokens&#39;)
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(&#39;Decode the raw tokens into words&#39;)
converted_prediction = tf.strings.reduce_join(num_to_char(decoder)).numpy().decode(&#39;utf-8&#39;)
st.text(converted_prediction)

Here's an inline link to GitHubRepository<br/>

Requirement:
imageio==2.9.0<br/>
numpy== 1.22.2<br/>
moviepy== 1.0.3<br/>
opencv-python==4.7.0.72<br/>
streamlit== 1.22.0<br/>
tensorflow==2.12.0<br/>

Code explaination:
The provided code is a Streamlit application that implements the LipNet deep learning model for lip-reading and speech recognition. When executed, the application launches with a wide layout and displays a sidebar containing an image and an introductory paragraph about the project. The main section of the application showcases the LipNet model with a heading and allows users to choose a video from a list of options. Upon selecting a video, the application renders it in the first column as an mp4 video and presents frames and annotations in the second column. The frames are processed by the LipNet model, which predicts output tokens and displays them, along with the converted text prediction. The raw tokens are further decoded into words. Overall, the application provides a user-friendly interface to explore the lip-reading and speech recognition capabilities of LipNet, offering visual representations and insights into the model's predictions.

oswalk :

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

答案1

得分: 2

# 代码部分不要翻译

# Resolve converts the path to absolute, for safety.
# Pathlib allows do what os.path.join did using / operator and strings:

files_location = code_dir / ".." / "data" / "s1"  
files_location = files_location.resolve()  # 因为我们在路径中使用了 .. ,所以最好解析一下以获取绝对路径

# `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'`,以将其放在与代码文件相同的位置(就像你在本地使用的那样)。
英文:

The path is relative to the current working directory, which is not the same as file's location. When you run your code locally, you're probably in that directory and just doing python my_file.py, but that's not what the platform is doing, so the current path is different.

The current python file's path can be accessed using __file__. Then we could use remove filename from that to get the directory. I like using pathlib for that:

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

Resolve converts the path to absolute, for safety.
Pathlib allows do what os.path.join did using / operator and strings:

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

Alternative forms of the files_location = code_dir / &quot;..&quot; / &quot;data&quot; / &quot;s1&quot; line:

  • files_location = code_dir / &quot;../data/s1&quot;
  • files_location = code_dir.parent / &quot;data&quot; / &quot;s1&quot;
  • files_location = code_dir.parent / &quot;data/s1&quot;

Use whichever seems the most intuitive for your usecase.

Listing the dir: os.listdir accepts path-like object and pathlib.Path is pathlike, so we can just do os.listdir(files_location)

Other lines to convert:

  • instead of file_path = os.path.join(&#39;..&#39;, &#39;data&#39;, &#39;s1&#39;, selected_video), we can do file_path = files_location / selected_video
  • and output_path = os.path.join(&#39;test_video.mp4&#39;) becomes output_path = code_dir / &#39;test_video.mp4&#39;to dump it in the same location as the code file is (what you had locally)

答案2

得分: 0

解决方案:
我在这个项目中将相对路径更改为绝对路径,以便通过更改 options = os.listdir(os.path.join(&#39;..&#39;, &#39;data&#39;, &#39;s1&#39;))(此代码将相对路径 &#39;../data/s1&#39; 中的文件和目录列表分配给变量 options )来修复它,更改为:

code_dir = pathlib.Path(__file__).parent.resolve()
files_location = code_dir / &quot;..&quot; / &quot;data&quot; / &quot;s1&quot;  
files_location = files_location.resolve()

总的来说,此代码确定当前脚本文件的目录路径,然后通过附加相对目录名称构建到特定位置的绝对路径。生成的绝对路径存储在 files_location 变量中。

然后,我们需要将类型更改为字符串,因为当您使用 pathlib.Path 模块创建路径或解析路径时,它返回一个 Path 对象。因此,我使用了下面的代码:

file_path = str(files_location / selected_video)
output_path = str(code_dir / &#39;test_video.mp4&#39;)

然后,将其他Python文件中的相对路径更改为绝对路径。请查看我提供的GitHub链接以获取帖子或帖子中提供的更新代码供您参考。

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

英文:

Solution:
I changed the relative path to the absolute path in this project inorder to fix it by changing options = os.listdir(os.path.join(&#39;..&#39;, &#39;data&#39;, &#39;s1&#39;)) (this code assigns the list of files and directories in the relative path &#39;../data/s1&#39; to the variable options )to

code_dir = pathlib.Path(__file__).parent.resolve()
files_location = code_dir / &quot;..&quot; / &quot;data&quot; / &quot;s1&quot;  
files_location = files_location.resolve()

Overall, this code determines the directory path of the current script file, and then constructs an absolute path to a specific location by appending relative directory names. The resulting absolute path is stored in the files_location variable.

Then we need to change type to string because when you use the pathlib.Path module to create a path or resolve a path, it returns a Path object.Therefore i used code given below:

file_path = str(files_location / selected_video)
output_path = str(code_dir / &#39;test_video.mp4&#39;)

Then changed relative path written in other python file to absolute path.
Checked out the github link that i provide the post or updated code given in the post for your reference

@h4z3 @doneforaiur Thank you very much for your invaluable help and guidance.

huangapple
  • 本文由 发表于 2023年6月15日 20:57:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482724.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定