我正在使用 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)
我完全没有解析经验,因此我们将不胜感激。
最佳答案
编辑
在评论之后我查看了ArgumentParser
。如果您插入方法parser.parse_args
,您将返回一个 namespace 。现在您可以访问它的属性,并且可以获得用户传递的值。
#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=[]) Parses command line options and parameter list.
args
is the argument list to be parsed, without the leading reference to the running program. Typically, this meanssys.argv[1:]
. shortopts is the string of option letters that the script wants to recognize, with options that require an argument followed by a colon (':'; i.e., the same format that Unix getopt() uses). longopts, if specified, must be a list of strings with the names of the long options which should be supported. The leading '--' characters should not be included in the option name. Long options which require an argument should be followed by an equal sign ('=').
请注意,用户可以将任何他想要的内容作为参数,您需要检查它是否正确。
关于python - 如何将已解析语句的列表转换为可以在 if 语句中使用的列表或字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59236920/