mirror of
https://github.com/opencv/opencv.git
synced 2026-07-29 07:13:02 +04:00
Merge branch 4.x
This commit is contained in:
@@ -218,6 +218,28 @@ if(NOT OPENCV_SKIP_PYTHON_LOADER)
|
||||
endif()
|
||||
configure_file("${PYTHON_SOURCE_DIR}/package/template/config-x.y.py.in" "${__python_loader_install_tmp_path}/cv2/${__target_config}" @ONLY)
|
||||
install(FILES "${__python_loader_install_tmp_path}/cv2/${__target_config}" DESTINATION "${OPENCV_PYTHON_INSTALL_PATH}/cv2/" COMPONENT python)
|
||||
|
||||
# handle Python extra code
|
||||
foreach(m ${OPENCV_MODULES_BUILD})
|
||||
if (";${OPENCV_MODULE_${m}_WRAPPERS};" MATCHES ";python;" AND HAVE_${m}
|
||||
AND EXISTS "${OPENCV_MODULE_${m}_LOCATION}/misc/python/package"
|
||||
)
|
||||
set(__base "${OPENCV_MODULE_${m}_LOCATION}/misc/python/package")
|
||||
file(GLOB_RECURSE extra_py_files
|
||||
RELATIVE "${__base}"
|
||||
"${__base}/**/*.py"
|
||||
)
|
||||
if(extra_py_files)
|
||||
list(SORT extra_py_files)
|
||||
foreach(f ${extra_py_files})
|
||||
configure_file("${__base}/${f}" "${__loader_path}/cv2/_extra_py_code/${f}" COPYONLY)
|
||||
install(FILES "${__base}/${f}" DESTINATION "${OPENCV_PYTHON_INSTALL_PATH}/cv2/_extra_py_code/${f}" COMPONENT python)
|
||||
endforeach()
|
||||
else()
|
||||
message(WARNING "Module ${m} has no .py files in misc/python/package")
|
||||
endif()
|
||||
endif()
|
||||
endforeach(m)
|
||||
endif() # NOT OPENCV_SKIP_PYTHON_LOADER
|
||||
|
||||
unset(PYTHON_SRC_DIR)
|
||||
|
||||
@@ -4,6 +4,8 @@ OpenCV Python binary extension loader
|
||||
import os
|
||||
import sys
|
||||
|
||||
__all__ = []
|
||||
|
||||
try:
|
||||
import numpy
|
||||
import numpy.core.multiarray
|
||||
@@ -13,6 +15,14 @@ except ImportError:
|
||||
print(' pip install numpy')
|
||||
raise
|
||||
|
||||
|
||||
py_code_loader = None
|
||||
if sys.version_info[:2] >= (3, 0):
|
||||
try:
|
||||
from . import _extra_py_code as py_code_loader
|
||||
except:
|
||||
pass
|
||||
|
||||
# TODO
|
||||
# is_x64 = sys.maxsize > 2**32
|
||||
|
||||
@@ -34,7 +44,7 @@ def bootstrap():
|
||||
import platform
|
||||
if DEBUG: print('OpenCV loader: os.name="{}" platform.system()="{}"'.format(os.name, str(platform.system())))
|
||||
|
||||
LOADER_DIR=os.path.dirname(os.path.abspath(__file__))
|
||||
LOADER_DIR = os.path.dirname(os.path.abspath(os.path.realpath(__file__)))
|
||||
|
||||
PYTHON_EXTENSIONS_PATHS = []
|
||||
BINARIES_PATHS = []
|
||||
@@ -97,6 +107,11 @@ def bootstrap():
|
||||
except:
|
||||
pass
|
||||
|
||||
if DEBUG: print('OpenCV loader: binary extension... OK')
|
||||
|
||||
if py_code_loader:
|
||||
py_code_loader.init('cv2')
|
||||
|
||||
if DEBUG: print('OpenCV loader: DONE')
|
||||
|
||||
bootstrap()
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import sys
|
||||
import importlib
|
||||
|
||||
__all__ = ['init']
|
||||
|
||||
|
||||
DEBUG = False
|
||||
if hasattr(sys, 'OpenCV_LOADER_DEBUG'):
|
||||
DEBUG = True
|
||||
|
||||
|
||||
def _load_py_code(base, name):
|
||||
try:
|
||||
m = importlib.import_module(__name__ + name)
|
||||
except ImportError:
|
||||
return # extension doesn't exist?
|
||||
|
||||
if DEBUG: print('OpenCV loader: added python code extension for: ' + name)
|
||||
|
||||
if hasattr(m, '__all__'):
|
||||
export_members = { k : getattr(m, k) for k in m.__all__ }
|
||||
else:
|
||||
export_members = m.__dict__
|
||||
|
||||
for k, v in export_members.items():
|
||||
if k.startswith('_'): # skip internals
|
||||
continue
|
||||
if isinstance(v, type(sys)): # don't bring modules
|
||||
continue
|
||||
if DEBUG: print(' symbol: {} = {}'.format(k, v))
|
||||
setattr(sys.modules[base + name ], k, v)
|
||||
|
||||
del sys.modules[__name__ + name]
|
||||
|
||||
|
||||
# TODO: listdir
|
||||
def init(base):
|
||||
_load_py_code(base, '.cv2') # special case
|
||||
prefix = base
|
||||
prefix_len = len(prefix)
|
||||
|
||||
modules = [ m for m in sys.modules.keys() if m.startswith(prefix) ]
|
||||
for m in modules:
|
||||
m2 = m[prefix_len:] # strip prefix
|
||||
if len(m2) == 0:
|
||||
continue
|
||||
if m2.startswith('._'): # skip internals
|
||||
continue
|
||||
if m2.startswith('.load_config_'): # skip helper files
|
||||
continue
|
||||
_load_py_code(base, m2)
|
||||
|
||||
del sys.modules[__name__]
|
||||
@@ -25,10 +25,12 @@ endif()
|
||||
set(PYTHON_LOADER_FILES
|
||||
"setup.py" "cv2/__init__.py"
|
||||
"cv2/load_config_py2.py" "cv2/load_config_py3.py"
|
||||
"cv2/_extra_py_code/__init__.py"
|
||||
)
|
||||
foreach(fname ${PYTHON_LOADER_FILES})
|
||||
get_filename_component(__dir "${fname}" DIRECTORY)
|
||||
file(COPY "${PYTHON_SOURCE_DIR}/package/${fname}" DESTINATION "${__loader_path}/${__dir}")
|
||||
# avoid using of file(COPY) to rerun CMake on changes
|
||||
configure_file("${PYTHON_SOURCE_DIR}/package/${fname}" "${__loader_path}/${fname}" COPYONLY)
|
||||
if(fname STREQUAL "setup.py")
|
||||
if(OPENCV_PYTHON_SETUP_PY_INSTALL_PATH)
|
||||
install(FILES "${PYTHON_SOURCE_DIR}/package/${fname}" DESTINATION "${OPENCV_PYTHON_SETUP_PY_INSTALL_PATH}" COMPONENT python)
|
||||
|
||||
@@ -211,6 +211,7 @@ simple_argtype_mapping = {
|
||||
"double": ArgTypeInfo("double", FormatStrings.double, "0", True),
|
||||
"c_string": ArgTypeInfo("char*", FormatStrings.string, '(char*)""'),
|
||||
"string": ArgTypeInfo("std::string", FormatStrings.object, None, True),
|
||||
"Stream": ArgTypeInfo("Stream", FormatStrings.object, 'Stream::Null()', True),
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -26,6 +26,15 @@ class cuda_test(NewOpenCVTests):
|
||||
|
||||
self.assertTrue(np.allclose(cuMat.download(), npMat))
|
||||
|
||||
def test_cuda_upload_download_stream(self):
|
||||
stream = cv.cuda_Stream()
|
||||
npMat = (np.random.random((128, 128, 3)) * 255).astype(np.uint8)
|
||||
cuMat = cv.cuda_GpuMat(128,128, cv.CV_8UC3)
|
||||
cuMat.upload(npMat, stream)
|
||||
npMat2 = cuMat.download(stream=stream)
|
||||
stream.waitForCompletion()
|
||||
self.assertTrue(np.allclose(npMat2, npMat))
|
||||
|
||||
def test_cuda_interop(self):
|
||||
npMat = (np.random.random((128, 128, 3)) * 255).astype(np.uint8)
|
||||
cuMat = cv.cuda_GpuMat()
|
||||
|
||||
Reference in New Issue
Block a user