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