- 1. CommandLineParser构造类简介
- 2. CommandLineParser的使用方法
- 3. 实际运行与命令行输入技巧
- 3.1 直接Visual Studio运行
- 3.2 命令行输入
OpenCV的语义分割代码里面,随处可见的CommandLineParser类。将不同的模型和配置文件,都单独拎出来,用命令行输入进行配置。因此,需要了解基本的使用方法。
OpenCV中CommandLineParse类主要是命令行解析类。OpenCV中出现此类的主要目:方便用户在命令行使用过程中减少工作量。具体使用方式如下。
我们将从三个方面介绍。首先是CommandLineParser类的构造函数,说明一下这个类的一些常用成员函数;随后介绍如何按需添加和配置参数和变量;最后直接使用Visual Studio 或者生成的exe文件,来按照命令行的方式,输入参数,并运行得到结果。
1. CommandLineParser构造类简介class CV_EXPORTS CommandLineParser
{
public:
CommandLineParser(int argc, const char* const argv[], const String& keys);
CommandLineParser(const CommandLineParser& parser);
CommandLineParser& operator = (const CommandLineParser& parser);
~CommandLineParser();
String getPathToApplication() const;
template
T get(const String& name, bool space_delete = true) const
{
T val = T();
getByName(name, space_delete, ParamType::type, (void*)&val);
return val;
}
template
T get(int index, bool space_delete = true) const
{
T val = T();
getByIndex(index, space_delete, ParamType::type, (void*)&val);
return val;
}
bool has(const String& name) const;
bool check() const;
void about(const String& message);
void printMessage() const;
void printErrors() const;
protected:
void getByName(const String& name, bool space_delete, int type, void* dst) const;
void getByIndex(int index, bool space_delete, int type, void* dst) const;
struct Impl;
Impl* impl;
};
2. CommandLineParser的使用方法
在keys中,添加需要命令行输入的参数,如下,OpenCV的语义分割模块c++代码中的片段。我们需要添加模型文件:model;输入图像:image;输出结果:result;是否展示image:show。
单个参数,是按照如下方式定义的:
“{ arg_name | default number | argument description}”
{输入参数的名称 m | 默认值 | 参数的描述性文字}.
const String keys =
"{help h || Sample app for loading ENet Torch model. "
"The model and class names list can be downloaded here: "
"https://www.dropbox.com/sh/dywzk3gyb12hpe5/AAD5YkUa8XgMpHs2gCRgmCVCa }"
"{model m |./model-cityscapes.net| path to Torch .net model file (model_best.net) }"
"{image i |./car.jpg | path to image file }"
"{result r | out_result.pb | path to save output blob (optional, binary format, NCHW order) }"
"{show s |true | whether to show all output channels or not}"
"{o_blob || output blob's name. If empty, last blob's name in net is used}";
要想直接运行,可以将参数的默认值添加进去。如代码所示,我们将model的名称,model-cityscapes.net添加进去了。将image 设置为car.jpg。我们指定了显示结果,show设置为true。
在下面的代码中,可以直接获取相关参数和变量的值。
int main(int argc, char **argv)
CommandLineParser parser(argc, argv, keys);
if (parser.has("help"))
{
parser.printMessage();
return 0;
}
String modelFile = parser.get("model");
String imageFile = parser.get("image");
if (!parser.check())
{
parser.printErrors();
return 0;
}
3. 实际运行与命令行输入技巧
有两种方式,可以运行代码。
3.1 直接Visual Studio运行Ctrl+F5 release模型,或者F5,debug模式。
此时,直接将改变的参数,在 String keys里面设置好。就不用输入命令行。跟日常的写代码,调试一样。将配置的模型与参数,直接修改在CommandLineParser的默认值里面,这样方便调试。
当代码调试好,需要只用使用。此时,需要生成exe文件,然后将不同的image采用命令行的方式,输入给exe,运行并观察结果就行。
首先,需要生成代码的可执行文件,例如,SemanticSeg.exe。 这个文件在./x64/release/ 下面(如果你是release模型生成的exe)。
然后,在Windows的cmd命令行输入里面(Win+R快捷键打开。)
cd到exe文件夹下面。
SemanticSeg.exe -m=…/path/to/modelfile.net -i=…/path/to/image.jpg -s=true
回车,就可以将数据输入到exe里面,并执行。
记住,每个参数,在命令行里面,用等于=赋值。例如:-m=model-cityscapes.net , --image=car.jpg。而不是,python里面的, -m ./path/to/model.net,中间用空格来输入。
例如:下图就是实际案例。使用生成的OpenCVTest.exe,指定了model -m,指定了处理图像 -i。



