【问题标题】:How do I convert a list of parsed statements to a list or string that I can use in if statements?如何将已解析语句的列表转换为可在 if 语句中使用的列表或字符串?
【发布时间】:2020-04-01 20:25:00
【问题描述】:

我正在使用来自https://github.com/tensorflow/models/blob/master/tutorials/image/imagenet/classify_image.py的教程imagenet图像识别码

我已经设法让一切正常工作,但我想知道如何以列表或字符串的形式获取最后的参数,而不是解析的参数,以便我可以使用普通的 if 命令。

def main(_):
  maybe_download_and_extract()
  image = (FLAGS.image_file if FLAGS.image_file else
           os.path.join(FLAGS.model_dir, 'cropped_panda.jpg'))
  run_inference_on_image(image)


if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  # classify_image_graph_def.pb:
  #   Binary representation of the GraphDef protocol buffer.
  # imagenet_synset_to_human_label_map.txt:
  #   Map from synset ID to a human readable string.
  # imagenet_2012_challenge_label_map_proto.pbtxt:
  #   Text representation of a protocol buffer mapping a label to synset ID.
  parser.add_argument(
      '--model_dir',
      type=str,
      default='/tmp/imagenet',
      help="""\
      Path to classify_image_graph_def.pb,
      imagenet_synset_to_human_label_map.txt, and
      imagenet_2012_challenge_label_map_proto.pbtxt.\
      """
  )
  parser.add_argument(
      '--image_file',
      type=str,
      default='',
      help='Absolute path to image file.'
  )
  parser.add_argument(
      '--num_top_predictions',
      type=int,
      default=5,
      help='Display this many predictions.'
  )
  #how do i get a variable that i can interact with from this

  FLAGS, unparsed = parser.parse_known_args()
  tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)

我对解析完全没有经验,因此将不胜感激。

【问题讨论】:

    标签: python tensorflow parsing image-recognition


    【解决方案1】:

    编辑

    在我查看ArgumentParser 的评论之后。如果您插入方法parser.parse_args,您将返回一个命名空间。现在您可以访问它的属性,并且可以获取用户传递的值。

    #Every parameter can be accessed using namespace.parameter_name, for example
    # with namepsace.model_dir, and you get the string inserted by the user
    
    namespace = parser.parse_args()
    if namespace.verbose:
        print("Verbose: ", + str(verbose))
    

    如果你想迭代所有属性,你可以使用THIS POST 中所说的字典。从字典传递到列表很容易。


    旧答案

    为了解析输入参数,我使用getopt。难懂的部分是如何指定参数和可选参数,但并不难。

    getopt 将返回一个参数列表,您可以在其上迭代和应用条件。 (请参阅getopt documentation for python 3.7.5,也适用于 python 3.6 和 2)。我举个例子:

    def main():
        options, remainder = getopt.getopt(sys.argv[1:], 'tci:', ['train', 'classify', 'id'])
        for opt, arg in options:
            #This is a bool optional parameter
            if opt in ('-t', '--train'):
                train = True
    
            #This is a bool optional parameter
            elif opt in ('-c', '--classify'):
                predict = True
    
            #This is an integer required parameter
            elif opt in ('-i', '--id'):
                id= arg
    
        if train:
            funtion1()
        elif predict:
            function2(id)
    
    if __name__ == '__main__':
        main()
    

    文档说:

    getopt.getopt(args, shortopts, longopts=[]) 解析命令行选项和参数列表。 args 是要解析的参数列表,没有对正在运行的程序的前导引用。通常,这意味着sys.argv[1:]shortopts 是脚本想要识别的选项字母字符串,其中选项需要一个参数后跟一个冒号(':';即,与 Unix 相同的格式) getopt() 使用)。 longopts,如果指定,必须是字符串列表,其中包含应支持的长选项名称。选项名称中不应包含前导“--”字符。需要参数的长选项后应跟一个等号 ('=')。

    请注意,用户可以将任何他想要的作为参数,您需要检查它是否正确。

    【讨论】:

    • 很抱歉,我什至不知道原始代码是如何工作的,更不用说如何更改它了。你能给我更多的信息吗?这是我正在使用的代码github.com/tensorflow/models/blob/master/tutorials/image/…
    • 等待进一步检查,这不是我想要的。我知道这会返回用户设置的参数,但我想返回解析器返回的东西,即 imagenet 所做的预测。非常感谢您的帮助 - 您似乎知道自己在做什么。
    猜你喜欢
    • 1970-01-01
    • 2022-10-06
    • 2014-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多