Exemplo n.º 1
0
 def install(self):
     self.install_numpy()
     self.log.write('installing opencv {}'.format(self.branch))
     self.ocv_git.clone_to(os.path.join(self.local_dir, OPENCV_DIR))
     ocv_contrib = GitManager(self.branch, self.contrib_url)
     ocv_contrib.clone_to(os.path.join(self.local_dir, OPENCV_CONTRIB_DIR))
     self.configure_opencv()
     self.compile_opencv()
     self.compile_ffopencv()
     self.copy_compiled_files()
Exemplo n.º 2
0
 def __init__(self, local_dir, venv_dir, branch='3.1.0'):
     self.branch = branch
     self.local_dir = local_dir
     self.venv_dir = venv_dir
     self.ocv_dir = os.path.join(local_dir, '{}'.format(OPENCV_DIR))
     self.ocv_build_dir = os.path.join(local_dir, '{}/{}'.format(BUILD_DIR, OPENCV_DIR))
     self.ocv_contrib_dir = os.path.join(local_dir, '{}'.format(OPENCV_CONTRIB_DIR))
     self.install_dir = os.path.join(self.local_dir, INSTALL_DIR)
     self.ffmpeg_dir = os.path.join(local_dir, 'ffmpeg')
     self.opencv_url = 'https://github.com/Itseez/opencv.git'
     self.contrib_url = 'https://github.com/Itseez/opencv_contrib.git'
     self.log = BuildLog()
     self.ocv_git = GitManager(self.branch, self.opencv_url)
     self.ocv_contrib_git = GitManager(self.branch, self.contrib_url)
Exemplo n.º 3
0
class OpencvCatcher:
    def __init__(self, local_dir, venv_dir, branch='3.1.0'):
        self.branch = branch
        self.local_dir = local_dir
        self.venv_dir = venv_dir
        self.ocv_dir = os.path.join(local_dir, '{}'.format(OPENCV_DIR))
        self.ocv_build_dir = os.path.join(local_dir, '{}/{}'.format(BUILD_DIR, OPENCV_DIR))
        self.ocv_contrib_dir = os.path.join(local_dir, '{}'.format(OPENCV_CONTRIB_DIR))
        self.install_dir = os.path.join(self.local_dir, INSTALL_DIR)
        self.ffmpeg_dir = os.path.join(local_dir, 'ffmpeg')
        self.opencv_url = 'https://github.com/Itseez/opencv.git'
        self.contrib_url = 'https://github.com/Itseez/opencv_contrib.git'
        self.log = BuildLog()
        self.ocv_git = GitManager(self.branch, self.opencv_url)
        self.ocv_contrib_git = GitManager(self.branch, self.contrib_url)

    def copy_file(self, source, target):
        try:
            self.log.write('copying {} to {}'.format(source, target))
            copy_file(source, target)
        except Exception as e:
            self.log.exit_nicely('fail while copying {}'.format(source), e)

    def check_ffmpeg(self):
        pass
        '''lib_exists = os.path.exists(os.path.join(self.ffmpeg_dir, '/usr/lib/libavcodec.a'))
        self.log.write('have ffmpeg ? {}'.format('yes' if lib_exists else 'no'))
        if not lib_exists:
            self.log.write('it will not re-build ffopencv, webm codec will be absent')
            self.log.write('https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu')
            self.log.write('compile ffmpeg and put it\'s libs and includes here: {}'.format(self.ffmpeg_dir))
            self.log.write('for windows you can get ffmpeg pre-built binaries: https://ffmpeg.zeranoe.com/builds/')
            self.log.exit_nicely('you dont have ffmpeg libraries and/or includes')'''

    def compile_ffopencv(self):
        if not is_windows():
            return
        self.check_ffmpeg()
        ffmpeg_idir = os.path.join(self.ffmpeg_dir, 'include')
        ffmpeg_ldir = os.path.join(self.ffmpeg_dir, 'lib')
        opencv_sdir = os.path.join(self.local_dir, '{}/modules/highgui/include'.format(OPENCV_DIR))
        opencv_cap_dir = os.path.join(self.local_dir, '{}/modules/videoio/src'.format(OPENCV_DIR))
        ffopencv_src = os.path.join(self.local_dir, '{}/3rdparty/ffmpeg/ffopencv.c'.format(OPENCV_DIR))
        ffopencv_out = os.path.join(self.install_dir, 'opencv_ffmpeg.dll')
        if not os.path.exists(self.install_dir):
            os.makedirs(self.install_dir)

        build_command = [
            'gcc', '-Wall', '-shared', '-o', ffopencv_out, '-O2', '-x', 'c++',
            '-I{}'.format(opencv_cap_dir), '-I{}'.format(ffmpeg_idir), '-I{}'.format(opencv_sdir),
            ffopencv_src, '-L{}'.format(ffmpeg_ldir), '-lavformat.dll', '-lavcodec.dll', '-lavdevice.dll',
            '-lswscale.dll', '-lavutil.dll'
        ]
        try:
            run(build_command)
        except Exception as e:
            self.log.exit_nicely('fail compiling opencv_ffmpeg.dll comando {}'.format(str(build_command)), e)

    def configure_opencv(self):
        self.check_ffmpeg()
        if os.path.exists(self.ocv_build_dir):
            self.log.write(
                'the opencv was already configured.' +
                ' if you want to reconfigure it remove the directory {}'.format(self.ocv_build_dir))
            return
        self.log.write('Configuring opencv')
        try:
            os.makedirs(self.ocv_build_dir)
            current_dir = os.getcwd()
            os.chdir(self.ocv_build_dir)
            cmake_params = [
                'cmake', '-D', 'CMAKE_BUILD_TYPE=RELEASE',
                '-D', 'CMAKE_INSTALL_PREFIX={}'.format(self.ocv_build_dir),
                '-D', 'INSTALL_C_EXAMPLES=OFF',
                '-D', 'INSTALL_PYTHON_EXAMPLES=OFF',
                '-D', 'BUILD_EXAMPLES=OFF',
                '-D', 'USE_FFMPEG=ON',
                '-D', 'BUILD_opencv_python2=OFF',
                '-D', 'BUILD_opencv_python3=ON',
                '-D', 'OPENCV_EXTRA_MODULES_PATH={}/modules'.format(self.ocv_contrib_dir),
                '-D', 'PYTHON3_EXECUTABLE={}'.format(os.path.join(get_first_caller_dir(), 'python3.5')),
                '-D', 'FFMPEG_INCLUDE_DIR={}'.format(os.path.join(self.ffmpeg_dir, 'include'))
            ]
            if is_windows():
                cmake_params += [
                    '-D', 'WITH_IPP=OFF',
                    '-D', 'BUILD_opencv_line_descriptor=OFF',
                    '-D', 'BUILD_opencv_datasets=OFF',
                    '-D', 'WITH_DSHOW=OFF',
                    '-D', 'BUILD_opencv_python2=OFF',
                    '-D', 'PYTHON_INCLUDE_DIR={}'.format(get_python_include_dir()),
                    '-D', 'PYTHON_LIBRARY={}'.format(os.path.join(get_python_libs_dir(), 'libpython35.a'))
                ]
                cmake_params += ['-D', 'CMAKE_SH=']
                cmake_params += ['-G', 'MinGW Makefiles']
            cmake_params = cmake_params + [self.ocv_dir]
            run(cmake_params)
            os.chdir(current_dir)
        except Exception as e:
            print(str(e))
            self.log.exit_nicely('falha configurando opencv.', e)

    def compile_opencv(self):
        self.log.write('compiling opencv')
        opencv_path = os.path.join(self.ocv_build_dir, 'lib/libopencv_core.so.3.1.0')
        if os.path.exists(opencv_path):
            self.log.write(
                'the opencv was already compiled.' +
                ' if you want re-compile it remove the directory {}'.format(self.ocv_build_dir), True
            )
            return
        try:
            current_dir = os.getcwd()
            os.chdir(self.ocv_build_dir)
            # run(['cmake', '--build', '.', '--config', 'Release', '--target', 'install'])
            run(['make'])
            os.chdir(current_dir)
        except Exception as e:
            self.log.exit_nicely('falha ao compilar opencv', e)

    def copy_compiled_files(self):
        files = []
        source_dir = os.path.join(self.ocv_build_dir, "lib/")
        for root, dirs, lib_files in os.walk(source_dir):
            for f in lib_files:
                if f.startswith('libopencv_') and (
                            f.endswith('.so.3.1') or
                            f.endswith('.so.3.1.0') or
                            f.endswith('.so')
                        ):
                    files.append(f)
        try:
            for f in files:
                self.copy_file(os.path.join(self.ocv_build_dir, "lib/" + f), os.path.join(self.venv_dir, 'bin/' + f))
            self.copy_file(os.path.join(self.ocv_build_dir,
                           "lib/python3.5/dist-packages/cv2.cpython-35m-x86_64-linux-gnu.so"),
                           os.path.join(self.venv_dir, 'lib/python3.5/cv2.cpython-35m-x86_64-linux-gnu.so'))
        except Exception as e:
            self.log.exit_nicely('fail copying opencv files', e)

    def install_numpy(self):
        try:
            pip.main(['install', 'numpy'])
        except Exception as E:
            return self.log.exit_nicely('fail installing numpy', e)

    def install(self):
        self.install_numpy()
        self.log.write('installing opencv {}'.format(self.branch))
        self.ocv_git.clone_to(os.path.join(self.local_dir, OPENCV_DIR))
        ocv_contrib = GitManager(self.branch, self.contrib_url)
        ocv_contrib.clone_to(os.path.join(self.local_dir, OPENCV_CONTRIB_DIR))
        self.configure_opencv()
        self.compile_opencv()
        self.compile_ffopencv()
        self.copy_compiled_files()