common.hpp 4.3 KB

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