common.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import sys
  2. import os
  3. import cv2 as cv
  4. def add_argument(zoo, parser, name, help, required=False, default=None, type=None, action=None, nargs=None):
  5. if len(sys.argv) <= 1:
  6. return
  7. modelName = sys.argv[1]
  8. if os.path.isfile(zoo):
  9. fs = cv.FileStorage(zoo, cv.FILE_STORAGE_READ)
  10. node = fs.getNode(modelName)
  11. if not node.empty():
  12. value = node.getNode(name)
  13. if not value.empty():
  14. if value.isReal():
  15. default = value.real()
  16. elif value.isString():
  17. default = value.string()
  18. elif value.isInt():
  19. default = int(value.real())
  20. elif value.isSeq():
  21. default = []
  22. for i in range(value.size()):
  23. v = value.at(i)
  24. if v.isInt():
  25. default.append(int(v.real()))
  26. elif v.isReal():
  27. default.append(v.real())
  28. else:
  29. print('Unexpected value format')
  30. exit(0)
  31. else:
  32. print('Unexpected field format')
  33. exit(0)
  34. required = False
  35. if action == 'store_true':
  36. default = 1 if default == 'true' else (0 if default == 'false' else default)
  37. assert(default is None or default == 0 or default == 1)
  38. parser.add_argument('--' + name, required=required, help=help, default=bool(default),
  39. action=action)
  40. else:
  41. parser.add_argument('--' + name, required=required, help=help, default=default,
  42. action=action, nargs=nargs, type=type)
  43. def add_preproc_args(zoo, parser, sample):
  44. aliases = []
  45. if os.path.isfile(zoo):
  46. fs = cv.FileStorage(zoo, cv.FILE_STORAGE_READ)
  47. root = fs.root()
  48. for name in root.keys():
  49. model = root.getNode(name)
  50. if model.getNode('sample').string() == sample:
  51. aliases.append(name)
  52. parser.add_argument('alias', nargs='?', choices=aliases,
  53. help='An alias name of model to extract preprocessing parameters from models.yml file.')
  54. add_argument(zoo, parser, 'model', required=True,
  55. help='Path to a binary file of model contains trained weights. '
  56. 'It could be a file with extensions .caffemodel (Caffe), '
  57. '.pb (TensorFlow), .t7 or .net (Torch), .weights (Darknet), .bin (OpenVINO)')
  58. add_argument(zoo, parser, 'config',
  59. help='Path to a text file of model contains network configuration. '
  60. 'It could be a file with extensions .prototxt (Caffe), .pbtxt or .config (TensorFlow), .cfg (Darknet), .xml (OpenVINO)')
  61. add_argument(zoo, parser, 'mean', nargs='+', type=float, default=[0, 0, 0],
  62. help='Preprocess input image by subtracting mean values. '
  63. 'Mean values should be in BGR order.')
  64. add_argument(zoo, parser, 'scale', type=float, default=1.0,
  65. help='Preprocess input image by multiplying on a scale factor.')
  66. add_argument(zoo, parser, 'width', type=int,
  67. help='Preprocess input image by resizing to a specific width.')
  68. add_argument(zoo, parser, 'height', type=int,
  69. help='Preprocess input image by resizing to a specific height.')
  70. add_argument(zoo, parser, 'rgb', action='store_true',
  71. help='Indicate that model works with RGB input images instead BGR ones.')
  72. add_argument(zoo, parser, 'classes',
  73. help='Optional path to a text file with names of classes to label detected objects.')
  74. def findFile(filename):
  75. if filename:
  76. if os.path.exists(filename):
  77. return filename
  78. fpath = cv.samples.findFile(filename, False)
  79. if fpath:
  80. return fpath
  81. samplesDataDir = os.path.join(os.path.dirname(os.path.abspath(__file__)),
  82. '..',
  83. 'data',
  84. 'dnn')
  85. if os.path.exists(os.path.join(samplesDataDir, filename)):
  86. return os.path.join(samplesDataDir, filename)
  87. for path in ['OPENCV_DNN_TEST_DATA_PATH', 'OPENCV_TEST_DATA_PATH']:
  88. try:
  89. extraPath = os.environ[path]
  90. absPath = os.path.join(extraPath, 'dnn', filename)
  91. if os.path.exists(absPath):
  92. return absPath
  93. except KeyError:
  94. pass
  95. print('File ' + filename + ' not found! Please specify a path to '
  96. '/opencv_extra/testdata in OPENCV_DNN_TEST_DATA_PATH environment '
  97. 'variable or pass a full path to model.')
  98. exit(0)