Blame view

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