不使用命令行将命令行python代码转换为普通python脚本

不使用命令行将命令行python代码转换为普通python脚本,python,command-line-arguments,streamlit,Python,Command Line Arguments,Streamlit,我有以下从命令行获取输入的代码。我希望在streamlight中运行此代码,而不是从命令行获取参数值,而是将它们设置为一些默认值,如“-I”,我希望它在默认情况下打开相机。我如何才能做到这一点? def build_argparser(): parser = ArgumentParser() general = parser.add_argument_group('General') general.add_argument('-i', '--input', metav

我有以下从命令行获取输入的代码。我希望在streamlight中运行此代码,而不是从命令行获取参数值,而是将它们设置为一些默认值,如“-I”,我希望它在默认情况下打开相机。我如何才能做到这一点?

def build_argparser():
    parser = ArgumentParser()

    general = parser.add_argument_group('General')
    general.add_argument('-i', '--input', metavar="PATH", default='0',
                         help="(optional) Path to the input video " \
                         "('0' for the camera, default)")
    general.add_argument('-o', '--output', metavar="PATH", default="",
                         help="(optional) Path to save the output video to")
    general.add_argument('--no_show', action='store_true',
                         help="(optional) Do not display output")
    general.add_argument('-tl', '--timelapse', action='store_true',
                         help="(optional) Auto-pause after each frame")
    general.add_argument('-cw', '--crop_width', default=0, type=int,
                         help="(optional) Crop the input stream to this width " \
                         "(default: no crop). Both -cw and -ch parameters " \
                         "should be specified to use crop.")
    general.add_argument('-ch', '--crop_height', default=0, type=int,
                         help="(optional) Crop the input stream to this height " \
                         "(default: no crop). Both -cw and -ch parameters " \
                         "should be specified to use crop.")
    general.add_argument('--match_algo', default='HUNGARIAN', choices=MATCH_ALGO,
                         help="(optional)algorithm for face matching(default: %(default)s)")
见:

通常我们所做的是:

parser = argparse.ArgumentParser()
# ... define what to expect
arg = parser.parse_args()
arg
将是参数对象,它是从您在命令行中输入的
sys.argv
解析的。您还可以将字符串列表放入函数中,例如

arg = parser.parse_args(["--match_algo", "-ch"])

上面的链接提供了更多关于您可能使用的参数的不同变体的示例。

您只希望使用脚本名称运行脚本,而不希望运行其他内容,还是希望在运行脚本时指定标志,并希望这些标志默认运行某些操作?我不希望在运行脚本时指定标志。我想在默认情况下设置所有标志。如果不需要任何命令行参数,则不要使用任何。简单地说,定义脚本中的所有路径和引用。此外,如果您正在使用camera,请查看opencv库,它可以帮助您运行和更改视频帧。