通过Jupyter Notebook发出运行终端命令

时间:2020-06-02 06:08:25

标签: python opencv jupyter-notebook

我当前正在从github存储库运行jupyter notebook。一大块是这样的:

for file in os.listdir('data/'):
    path = 'data/' + file

    os.system(f"python OpticalFlowGen.py --type {target} --file {path}")

已定义变量目标和路径,并且未引发任何错误。

当使用python OpticalFlowGen.py ---type 'Train' -- file 'data/video.mp4'在终端上运行OpticalFlowGen.py文件时,由openCV处理视频文件后,会出现一个弹出窗口并关闭,然后将.jpg文件保存在系统中。但是,在jupyter笔记本上运行此命令时,不会弹出任何对话框,也不会保存任何文件。您可以从同一存储库here中访问此.py文件。

当前,我必须逐个文件在终端文件上手动运行,因此请保存所有图像输出,然后才能正确运行笔记本电脑。但是,当我有太多视频文件时,这将成为一个问题,不使用for循环会很麻烦。关于如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:0)

在Jupyter笔记本中,您不必使用os.system,而应尝试使用!

for file in os.listdir('data/'):
    path = 'data/' + file

    # Use
    # ! - for terminal commands
    # {} - for a variable in the terminal command
    !python OpticalFlowGen.py --type {target} --file {path}