/
setup.py
97 lines (84 loc) · 3.57 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
from Cython.Build import cythonize
import numpy
import sys
import os
import subprocess
import sys
import glob
pthlist = []
# search in python(conda) folder
lib_folder = os.path.join(sys.prefix, 'lib')
# Find opencv libraries in lib_folder
cvlibs = list()
for file in glob.glob(os.path.join(lib_folder, 'libopencv_*')):
cvlibs.append(file.split('.')[0])
cvlibs = list(set(cvlibs))
cvlibs = ['-L{}'.format(lib_folder)] + \
['opencv_{}'.format(lib.split(os.path.sep)[-1].split('libopencv_')[-1]) for lib in cvlibs]
conda_path = os.path.join(sys.prefix, 'include', 'opencv2')
if os.path.exists(conda_path):
pthlist.append(conda_path)
include_path = os.path.join(sys.prefix, 'include')
if os.path.exists(include_path):
pthlist.append(include_path)
lib_folders = [lib_folder]
if not pthlist:
# search in system folder
libs = os.popen('ldconfig -p | grep libopencv')
libs = libs.readlines()
lib_paths = [l.split('=>')[-1].strip('\n').strip(' ') for l in libs]
# Find opencv libraries in lib_folder
cvlibs = list()
lib_folders = set()
for file in lib_paths:
file_split = file.split('.')
cvlibs.append(file_split[0])
lib_folders.add(file.split('libopencv')[0])
lib_folders = list(lib_folders)
cvlibs = list(set(cvlibs))
cvlibs = ['-L{}'.format(lib_folder) for lib_folder in lib_folders] + \
['opencv_{}'.format(lib.split(os.path.sep)[-1].split('libopencv_')[-1]) for lib in cvlibs]
# Find opencv.hpp in include_folder
proc_incs = subprocess.check_output("pkg-config --cflags opencv".split())
proc_incs = str(proc_incs, 'utf-8').split()
for i in range(len(proc_incs)):
proc_incs[i] = proc_incs[i][2:]
if proc_incs[i].split('/')[-1] == 'opencv':
proc_incs[i] = proc_incs[i][:-7]
proc_incs = list(set(proc_incs))
for inc in proc_incs:
inc = inc+'/opencv2'
if os.path.exists(inc):
break
default_path = os.path.join('/usr/include/opencv2')
default_local_path = os.path.join('/usr/local/include/opencv2')
if os.path.exists(default_path):
pthlist.append(default_path)
if os.path.exists(default_path):
pthlist.append(default_local_path)
if os.path.exists(inc):
pthlist.append(inc)
assert pthlist, 'opencv headers not found, you may refer this doc in [https://github.com/GothicAi/cython-global-matting#environment-settings-linux] to solve this error.'
assert cvlibs, 'opencv libs not found, you may refer this doc in [https://github.com/GothicAi/cython-global-matting#environment-settings-linux] to solve this error.'
pthlist.append(numpy.get_include())
setup(
name='opencv_mat',
version='0.1',
description='A cython interface for paper: A global sampling method for alpha matting',
author='Runzhong Wang, Jianhua Sun, Haoshu Fang, Minghao Gou',
author_email='1751196720@qq.com',
url='https://github.com/GothicAi/cython-global-matting',
cmdclass={'build_ext': build_ext},
ext_modules=cythonize(Extension("opencv_mat",
sources=["opencv_mat.pyx", "globalmatting.cpp", "guidedfilter.cpp"],
language="c++",
include_dirs=pthlist,
library_dirs=lib_folders,
libraries=cvlibs,
),
compiler_directives={'language_level' : "3"}
)
)