Upload 2 files
Browse files- cv2/__init__.py +181 -0
- cv2/__init__.pyi +0 -0
cv2/__init__.py
ADDED
@@ -0,0 +1,181 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
'''
|
2 |
+
OpenCV Python binary extension loader
|
3 |
+
'''
|
4 |
+
import os
|
5 |
+
import importlib
|
6 |
+
import sys
|
7 |
+
|
8 |
+
__all__ = []
|
9 |
+
|
10 |
+
try:
|
11 |
+
import numpy
|
12 |
+
import numpy.core.multiarray
|
13 |
+
except ImportError:
|
14 |
+
print('OpenCV bindings requires "numpy" package.')
|
15 |
+
print('Install it via command:')
|
16 |
+
print(' pip install numpy')
|
17 |
+
raise
|
18 |
+
|
19 |
+
# TODO
|
20 |
+
# is_x64 = sys.maxsize > 2**32
|
21 |
+
|
22 |
+
|
23 |
+
def __load_extra_py_code_for_module(base, name, enable_debug_print=False):
|
24 |
+
module_name = "{}.{}".format(__name__, name)
|
25 |
+
export_module_name = "{}.{}".format(base, name)
|
26 |
+
native_module = sys.modules.pop(module_name, None)
|
27 |
+
try:
|
28 |
+
py_module = importlib.import_module(module_name)
|
29 |
+
except ImportError as err:
|
30 |
+
if enable_debug_print:
|
31 |
+
print("Can't load Python code for module:", module_name,
|
32 |
+
". Reason:", err)
|
33 |
+
# Extension doesn't contain extra py code
|
34 |
+
return False
|
35 |
+
|
36 |
+
if not hasattr(base, name):
|
37 |
+
setattr(sys.modules[base], name, py_module)
|
38 |
+
sys.modules[export_module_name] = py_module
|
39 |
+
# If it is C extension module it is already loaded by cv2 package
|
40 |
+
if native_module:
|
41 |
+
setattr(py_module, "_native", native_module)
|
42 |
+
for k, v in filter(lambda kv: not hasattr(py_module, kv[0]),
|
43 |
+
native_module.__dict__.items()):
|
44 |
+
if enable_debug_print: print(' symbol({}): {} = {}'.format(name, k, v))
|
45 |
+
setattr(py_module, k, v)
|
46 |
+
return True
|
47 |
+
|
48 |
+
|
49 |
+
def __collect_extra_submodules(enable_debug_print=False):
|
50 |
+
def modules_filter(module):
|
51 |
+
return all((
|
52 |
+
# module is not internal
|
53 |
+
not module.startswith("_"),
|
54 |
+
not module.startswith("python-"),
|
55 |
+
# it is not a file
|
56 |
+
os.path.isdir(os.path.join(_extra_submodules_init_path, module))
|
57 |
+
))
|
58 |
+
if sys.version_info[0] < 3:
|
59 |
+
if enable_debug_print:
|
60 |
+
print("Extra submodules is loaded only for Python 3")
|
61 |
+
return []
|
62 |
+
|
63 |
+
__INIT_FILE_PATH = os.path.abspath(__file__)
|
64 |
+
_extra_submodules_init_path = os.path.dirname(__INIT_FILE_PATH)
|
65 |
+
return filter(modules_filter, os.listdir(_extra_submodules_init_path))
|
66 |
+
|
67 |
+
|
68 |
+
def bootstrap():
|
69 |
+
import sys
|
70 |
+
|
71 |
+
import copy
|
72 |
+
save_sys_path = copy.copy(sys.path)
|
73 |
+
|
74 |
+
if hasattr(sys, 'OpenCV_LOADER'):
|
75 |
+
print(sys.path)
|
76 |
+
raise ImportError('ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.')
|
77 |
+
sys.OpenCV_LOADER = True
|
78 |
+
|
79 |
+
DEBUG = False
|
80 |
+
if hasattr(sys, 'OpenCV_LOADER_DEBUG'):
|
81 |
+
DEBUG = True
|
82 |
+
|
83 |
+
import platform
|
84 |
+
if DEBUG: print('OpenCV loader: os.name="{}" platform.system()="{}"'.format(os.name, str(platform.system())))
|
85 |
+
|
86 |
+
LOADER_DIR = os.path.dirname(os.path.abspath(os.path.realpath(__file__)))
|
87 |
+
|
88 |
+
PYTHON_EXTENSIONS_PATHS = []
|
89 |
+
BINARIES_PATHS = []
|
90 |
+
|
91 |
+
g_vars = globals()
|
92 |
+
l_vars = locals().copy()
|
93 |
+
|
94 |
+
if sys.version_info[:2] < (3, 0):
|
95 |
+
from . load_config_py2 import exec_file_wrapper
|
96 |
+
else:
|
97 |
+
from . load_config_py3 import exec_file_wrapper
|
98 |
+
|
99 |
+
def load_first_config(fnames, required=True):
|
100 |
+
for fname in fnames:
|
101 |
+
fpath = os.path.join(LOADER_DIR, fname)
|
102 |
+
if not os.path.exists(fpath):
|
103 |
+
if DEBUG: print('OpenCV loader: config not found, skip: {}'.format(fpath))
|
104 |
+
continue
|
105 |
+
if DEBUG: print('OpenCV loader: loading config: {}'.format(fpath))
|
106 |
+
exec_file_wrapper(fpath, g_vars, l_vars)
|
107 |
+
return True
|
108 |
+
if required:
|
109 |
+
raise ImportError('OpenCV loader: missing configuration file: {}. Check OpenCV installation.'.format(fnames))
|
110 |
+
|
111 |
+
load_first_config(['config.py'], True)
|
112 |
+
load_first_config([
|
113 |
+
'config-{}.{}.py'.format(sys.version_info[0], sys.version_info[1]),
|
114 |
+
'config-{}.py'.format(sys.version_info[0])
|
115 |
+
], True)
|
116 |
+
|
117 |
+
if DEBUG: print('OpenCV loader: PYTHON_EXTENSIONS_PATHS={}'.format(str(l_vars['PYTHON_EXTENSIONS_PATHS'])))
|
118 |
+
if DEBUG: print('OpenCV loader: BINARIES_PATHS={}'.format(str(l_vars['BINARIES_PATHS'])))
|
119 |
+
|
120 |
+
applySysPathWorkaround = False
|
121 |
+
if hasattr(sys, 'OpenCV_REPLACE_SYS_PATH_0'):
|
122 |
+
applySysPathWorkaround = True
|
123 |
+
else:
|
124 |
+
try:
|
125 |
+
BASE_DIR = os.path.dirname(LOADER_DIR)
|
126 |
+
if sys.path[0] == BASE_DIR or os.path.realpath(sys.path[0]) == BASE_DIR:
|
127 |
+
applySysPathWorkaround = True
|
128 |
+
except:
|
129 |
+
if DEBUG: print('OpenCV loader: exception during checking workaround for sys.path[0]')
|
130 |
+
pass # applySysPathWorkaround is False
|
131 |
+
|
132 |
+
for p in reversed(l_vars['PYTHON_EXTENSIONS_PATHS']):
|
133 |
+
sys.path.insert(1 if not applySysPathWorkaround else 0, p)
|
134 |
+
|
135 |
+
if os.name == 'nt':
|
136 |
+
if sys.version_info[:2] >= (3, 8): # https://github.com/python/cpython/pull/12302
|
137 |
+
for p in l_vars['BINARIES_PATHS']:
|
138 |
+
try:
|
139 |
+
os.add_dll_directory(p)
|
140 |
+
except Exception as e:
|
141 |
+
if DEBUG: print('Failed os.add_dll_directory(): '+ str(e))
|
142 |
+
pass
|
143 |
+
os.environ['PATH'] = ';'.join(l_vars['BINARIES_PATHS']) + ';' + os.environ.get('PATH', '')
|
144 |
+
if DEBUG: print('OpenCV loader: PATH={}'.format(str(os.environ['PATH'])))
|
145 |
+
else:
|
146 |
+
# amending of LD_LIBRARY_PATH works for sub-processes only
|
147 |
+
os.environ['LD_LIBRARY_PATH'] = ':'.join(l_vars['BINARIES_PATHS']) + ':' + os.environ.get('LD_LIBRARY_PATH', '')
|
148 |
+
|
149 |
+
if DEBUG: print("Relink everything from native cv2 module to cv2 package")
|
150 |
+
|
151 |
+
py_module = sys.modules.pop("cv2")
|
152 |
+
|
153 |
+
native_module = importlib.import_module("cv2")
|
154 |
+
|
155 |
+
sys.modules["cv2"] = py_module
|
156 |
+
setattr(py_module, "_native", native_module)
|
157 |
+
|
158 |
+
for item_name, item in filter(lambda kv: kv[0] not in ("__file__", "__loader__", "__spec__",
|
159 |
+
"__name__", "__package__"),
|
160 |
+
native_module.__dict__.items()):
|
161 |
+
if item_name not in g_vars:
|
162 |
+
g_vars[item_name] = item
|
163 |
+
|
164 |
+
sys.path = save_sys_path # multiprocessing should start from bootstrap code (https://github.com/opencv/opencv/issues/18502)
|
165 |
+
|
166 |
+
try:
|
167 |
+
del sys.OpenCV_LOADER
|
168 |
+
except Exception as e:
|
169 |
+
if DEBUG:
|
170 |
+
print("Exception during delete OpenCV_LOADER:", e)
|
171 |
+
|
172 |
+
if DEBUG: print('OpenCV loader: binary extension... OK')
|
173 |
+
|
174 |
+
for submodule in __collect_extra_submodules(DEBUG):
|
175 |
+
if __load_extra_py_code_for_module("cv2", submodule, DEBUG):
|
176 |
+
if DEBUG: print("Extra Python code for", submodule, "is loaded")
|
177 |
+
|
178 |
+
if DEBUG: print('OpenCV loader: DONE')
|
179 |
+
|
180 |
+
|
181 |
+
bootstrap()
|
cv2/__init__.pyi
ADDED
The diff for this file is too large to render.
See raw diff
|
|