Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/dnn/common.hpp
16337 views
1
#include <opencv2/core/utils/filesystem.hpp>
2
3
using namespace cv;
4
5
std::string genArgument(const std::string& argName, const std::string& help,
6
const std::string& modelName, const std::string& zooFile,
7
char key = ' ', std::string defaultVal = "");
8
9
std::string genPreprocArguments(const std::string& modelName, const std::string& zooFile);
10
11
std::string findFile(const std::string& filename);
12
13
std::string genArgument(const std::string& argName, const std::string& help,
14
const std::string& modelName, const std::string& zooFile,
15
char key, std::string defaultVal)
16
{
17
if (!modelName.empty())
18
{
19
FileStorage fs(zooFile, FileStorage::READ);
20
if (fs.isOpened())
21
{
22
FileNode node = fs[modelName];
23
if (!node.empty())
24
{
25
FileNode value = node[argName];
26
if (!value.empty())
27
{
28
if (value.isReal())
29
defaultVal = format("%f", (float)value);
30
else if (value.isString())
31
defaultVal = (std::string)value;
32
else if (value.isInt())
33
defaultVal = format("%d", (int)value);
34
else if (value.isSeq())
35
{
36
for (size_t i = 0; i < value.size(); ++i)
37
{
38
FileNode v = value[(int)i];
39
if (v.isInt())
40
defaultVal += format("%d ", (int)v);
41
else if (v.isReal())
42
defaultVal += format("%f ", (float)v);
43
else
44
CV_Error(Error::StsNotImplemented, "Unexpected value format");
45
}
46
}
47
else
48
CV_Error(Error::StsNotImplemented, "Unexpected field format");
49
}
50
}
51
}
52
}
53
return "{ " + argName + " " + key + " | " + defaultVal + " | " + help + " }";
54
}
55
56
std::string findFile(const std::string& filename)
57
{
58
if (filename.empty() || utils::fs::exists(filename))
59
return filename;
60
61
const char* extraPaths[] = {getenv("OPENCV_DNN_TEST_DATA_PATH"),
62
getenv("OPENCV_TEST_DATA_PATH")};
63
for (int i = 0; i < 2; ++i)
64
{
65
if (extraPaths[i] == NULL)
66
continue;
67
std::string absPath = utils::fs::join(extraPaths[i], utils::fs::join("dnn", filename));
68
if (utils::fs::exists(absPath))
69
return absPath;
70
}
71
CV_Error(Error::StsObjectNotFound, "File " + filename + " not found! "
72
"Please specify a path to /opencv_extra/testdata in OPENCV_DNN_TEST_DATA_PATH "
73
"environment variable or pass a full path to model.");
74
}
75
76
std::string genPreprocArguments(const std::string& modelName, const std::string& zooFile)
77
{
78
return genArgument("model", "Path to a binary file of model contains trained weights. "
79
"It could be a file with extensions .caffemodel (Caffe), "
80
".pb (TensorFlow), .t7 or .net (Torch), .weights (Darknet), .bin (OpenVINO).",
81
modelName, zooFile, 'm') +
82
genArgument("config", "Path to a text file of model contains network configuration. "
83
"It could be a file with extensions .prototxt (Caffe), .pbtxt (TensorFlow), .cfg (Darknet), .xml (OpenVINO).",
84
modelName, zooFile, 'c') +
85
genArgument("mean", "Preprocess input image by subtracting mean values. Mean values should be in BGR order and delimited by spaces.",
86
modelName, zooFile) +
87
genArgument("scale", "Preprocess input image by multiplying on a scale factor.",
88
modelName, zooFile, ' ', "1.0") +
89
genArgument("width", "Preprocess input image by resizing to a specific width.",
90
modelName, zooFile, ' ', "-1") +
91
genArgument("height", "Preprocess input image by resizing to a specific height.",
92
modelName, zooFile, ' ', "-1") +
93
genArgument("rgb", "Indicate that model works with RGB input images instead BGR ones.",
94
modelName, zooFile);
95
}
96
97