简体   繁体   English

Python-如何在IDE中使用不同的命令行参数调用脚本?

[英]Python - How can i call the script with different command line arguments from within my IDE?

I am doing a nudity detection using nueral networks for my final year project. 我正在为我的最后一年的项目使用神经网络进行裸露检测。 So far i found a github link to finish my project. 到目前为止,我找到了一个github链接来完成我的项目。 To run this project i need to pass 2 arguments such as input_file and -m/--model_weights . 要运行此项目,我需要传递2个参数,例如input_file-m/--model_weights i am using Pycharm IDE. 我正在使用Pycharm IDE。 i found a SO answer to pass argument. 我发现了一个SO回答传递参数。 But in my case i need to pass arguments by using 2 lines of code. 但就我而言,我需要使用两行代码来传递参数。 Not like giving parameter in Script Parameters box in pycharm. 不像在pycharm的“脚本参数”框中提供参数。 How can i do it? 我该怎么做?

input_file means path of the image. input_file表示图像的路径。

-m/--model_weights means path of the weights. -m/--model_weights表示权重的路径。 in my case weights path is data/nsw_weights.npy. 在我的情况下,权重路径为data/nsw_weights.npy.

and this is my project file. 这是我的项目文件。

import sys
import argparse
import tensorflow as tf

from model import OpenNsfwModel, InputType
from image_utils import create_tensorflow_image_loader
from image_utils import create_yahoo_image_loader

import numpy as np


IMAGE_LOADER_TENSORFLOW = "tensorflow"
IMAGE_LOADER_YAHOO = "yahoo"


def main(argv):
    parser = argparse.ArgumentParser()

    parser.add_argument("input_file", help="Path to the input image.\
                        Only jpeg images are supported.")
    parser.add_argument("-m", "--model_weights", required=True,
                        help="Path to trained model weights file")

    parser.add_argument("-l", "--image_loader",
                        default=IMAGE_LOADER_YAHOO,
                        help="image loading mechanism",
                        choices=[IMAGE_LOADER_YAHOO, IMAGE_LOADER_TENSORFLOW])

    parser.add_argument("-t", "--input_type",
                        default=InputType.TENSOR.name.lower(),
                        help="input type",
                        choices=[InputType.TENSOR.name.lower(),
                                 InputType.BASE64_JPEG.name.lower()])

    args = parser.parse_args()

    model = OpenNsfwModel()

    with tf.Session() as sess:

        input_type = InputType[args.input_type.upper()]
        model.build(weights_path=args.model_weights, input_type=input_type)

        fn_load_image = None

        if input_type == InputType.TENSOR:
            if args.image_loader == IMAGE_LOADER_TENSORFLOW:
                fn_load_image = create_tensorflow_image_loader(sess)
            else:
                fn_load_image = create_yahoo_image_loader()
        elif input_type == InputType.BASE64_JPEG:
            import base64
            fn_load_image = lambda filename: np.array([base64.urlsafe_b64encode(open(filename, "rb").read())])

        sess.run(tf.global_variables_initializer())

        image = fn_load_image(args.input_file)

        predictions = \
            sess.run(model.predictions,
                     feed_dict={model.input: image})

        print("Results for '{}'".format(args.input_file))
        print("\tSFW score:\t{}\n\tNSFW score:\t{}".format(*predictions[0]))

if __name__ == "__main__":
    main(sys.argv)

In pycharm you can setup multiple run configurations which will allow you to pass in different arguments. 在pycharm中,您可以设置多个运行配置,以允许您传递不同的参数。

The documentation has a section that shows you ways to set this up. 该文档的一节介绍了设置方法。

You may also want to consider writing tests if you have a set of specific inputs you want to confirm the results for. 如果您有一组要确认结果的特定输入,则可能还需要考虑编写测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM