Project import generated by Copybara.
GitOrigin-RevId: d8caa66de45839696f5bd0786ad3bfbcb9cff632
This commit is contained in:
@@ -17,6 +17,7 @@ Setup for MediaPipe package with setuptools.
|
||||
|
||||
import glob
|
||||
import os
|
||||
import platform
|
||||
import posixpath
|
||||
import re
|
||||
import shutil
|
||||
@@ -32,21 +33,30 @@ from distutils import spawn
|
||||
import distutils.command.build as build
|
||||
import distutils.command.clean as clean
|
||||
|
||||
__version__ = '0.7'
|
||||
__version__ = '0.8'
|
||||
IS_WINDOWS = (platform.system() == 'Windows')
|
||||
MP_ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
|
||||
ROOT_INIT_PY = os.path.join(MP_ROOT_PATH, '__init__.py')
|
||||
MP_DIR_INIT_PY = os.path.join(MP_ROOT_PATH, 'mediapipe/__init__.py')
|
||||
MP_DIR_INIT_PY_BACKUP = os.path.join(MP_ROOT_PATH,
|
||||
'mediapipe/__init__.py.backup')
|
||||
MP_THIRD_PARTY_BUILD = os.path.join(MP_ROOT_PATH, 'third_party/BUILD')
|
||||
MP_THIRD_PARTY_BUILD_BACKUP = os.path.join(MP_ROOT_PATH,
|
||||
'third_party/BUILD.backup')
|
||||
MP_CALCULATORS_DIR_INIT_PY = os.path.join(MP_ROOT_PATH,
|
||||
'mediapipe/calculators/__init__.py')
|
||||
SUBDIR_INIT_PY_FILES = [
|
||||
os.path.join(MP_ROOT_PATH, 'mediapipe/calculators/__init__.py'),
|
||||
os.path.join(MP_ROOT_PATH, 'mediapipe/modules/__init__.py'),
|
||||
os.path.join(MP_ROOT_PATH,
|
||||
'mediapipe/modules/holistic_landmark/__init__.py')
|
||||
]
|
||||
if not os.path.exists(ROOT_INIT_PY):
|
||||
open(ROOT_INIT_PY, 'w').close()
|
||||
|
||||
|
||||
def _normalize_path(path):
|
||||
return path.replace('\\', '/') if IS_WINDOWS else path
|
||||
|
||||
|
||||
def _get_backup_file(path):
|
||||
return path + '.backup'
|
||||
|
||||
|
||||
def _parse_requirements(path):
|
||||
with open(os.path.join(MP_ROOT_PATH, path)) as f:
|
||||
return [
|
||||
@@ -61,7 +71,8 @@ def _get_long_description():
|
||||
return re.sub(
|
||||
r'(docs/images/|docs/images/mobile/)([A-Za-z0-9_]*\.(png|gif))',
|
||||
r'https://github.com/google/mediapipe/blob/master/\g<1>\g<2>?raw=true',
|
||||
open(os.path.join(MP_ROOT_PATH, 'README.md')).read())
|
||||
open(os.path.join(MP_ROOT_PATH, 'README.md'),
|
||||
'rb').read().decode('utf-8'))
|
||||
|
||||
|
||||
def _check_bazel():
|
||||
@@ -93,6 +104,25 @@ def _check_bazel():
|
||||
sys.exit(-1)
|
||||
|
||||
|
||||
def _modify_opencv_cmake_rule(link_opencv):
|
||||
"""Modify opencv_cmake rule to build the static opencv libraries."""
|
||||
|
||||
# Ask the opencv_cmake rule to build the static opencv libraries for the
|
||||
# mediapipe python package. By doing this, we can avoid copying the opencv
|
||||
# .so file into the package.
|
||||
# On Windows, the opencv_cmake rule may need Visual Studio to compile OpenCV
|
||||
# from source. For simplicity, we continue to link the prebuilt version of
|
||||
# the OpenCV library through "@windows_opencv//:opencv".
|
||||
if not link_opencv and not IS_WINDOWS:
|
||||
content = open(MP_THIRD_PARTY_BUILD,
|
||||
'r').read().replace('OPENCV_SHARED_LIBS = True',
|
||||
'OPENCV_SHARED_LIBS = False')
|
||||
shutil.move(MP_THIRD_PARTY_BUILD, _get_backup_file(MP_THIRD_PARTY_BUILD))
|
||||
build_file = open(MP_THIRD_PARTY_BUILD, 'w')
|
||||
build_file.write(content)
|
||||
build_file.close()
|
||||
|
||||
|
||||
class ModifyInitFiles(setuptools.Command):
|
||||
"""Modify the init files for building MediaPipe Python package."""
|
||||
|
||||
@@ -106,7 +136,7 @@ class ModifyInitFiles(setuptools.Command):
|
||||
|
||||
def run(self):
|
||||
# Save the original init file.
|
||||
shutil.copyfile(MP_DIR_INIT_PY, MP_DIR_INIT_PY_BACKUP)
|
||||
shutil.copyfile(MP_DIR_INIT_PY, _get_backup_file(MP_DIR_INIT_PY))
|
||||
mp_dir_init_file = open(MP_DIR_INIT_PY, 'a')
|
||||
mp_dir_init_file.writelines(
|
||||
['\n', 'from mediapipe.python import *\n',
|
||||
@@ -137,21 +167,23 @@ class GeneratePyProtos(setuptools.Command):
|
||||
'-compiler\' (linux) or \'brew install protobuf\'(macos) to install '
|
||||
'protobuf compiler binary.')
|
||||
sys.exit(-1)
|
||||
# Add __init__.py to make the generated py proto files visiable.
|
||||
for init_py in SUBDIR_INIT_PY_FILES:
|
||||
if not os.path.exists(init_py):
|
||||
sys.stderr.write('adding __init__ file: %s\n' % init_py)
|
||||
open(init_py, 'w').close()
|
||||
# Build framework and calculator protos.
|
||||
if not os.path.exists(MP_CALCULATORS_DIR_INIT_PY):
|
||||
sys.stderr.write('adding __init__ file: %s\n' %
|
||||
MP_CALCULATORS_DIR_INIT_PY)
|
||||
open(MP_CALCULATORS_DIR_INIT_PY, 'w').close()
|
||||
for pattern in [
|
||||
'mediapipe/framework/**/*.proto', 'mediapipe/calculators/**/*.proto',
|
||||
'mediapipe/gpu/**/*.proto', 'mediapipe/util/**/*.proto'
|
||||
'mediapipe/gpu/**/*.proto', 'mediapipe/modules/**/*.proto',
|
||||
'mediapipe/util/**/*.proto'
|
||||
]:
|
||||
for proto_file in glob.glob(pattern, recursive=True):
|
||||
# Ignore test protos.
|
||||
if proto_file.endswith('test.proto'):
|
||||
continue
|
||||
# Ignore tensorflow protos.
|
||||
if 'mediapipe/calculators/tensorflow' in proto_file:
|
||||
if 'tensorflow' in proto_file:
|
||||
continue
|
||||
proto_dir = os.path.dirname(os.path.abspath(proto_file))
|
||||
# Ignore testdata dir.
|
||||
@@ -189,7 +221,8 @@ class BuildBinaryGraphs(build.build):
|
||||
binary_graphs = [
|
||||
'face_landmark/face_landmark_front_cpu',
|
||||
'hand_landmark/hand_landmark_tracking_cpu',
|
||||
'pose_landmark/pose_landmark_upper_body_smoothed_cpu'
|
||||
'holistic_landmark/holistic_landmark_cpu',
|
||||
'pose_landmark/pose_landmark_cpu'
|
||||
]
|
||||
for binary_graph in binary_graphs:
|
||||
sys.stderr.write('generating binarypb: %s\n' %
|
||||
@@ -204,9 +237,11 @@ class BuildBinaryGraphs(build.build):
|
||||
'build',
|
||||
'--compilation_mode=opt',
|
||||
'--define=MEDIAPIPE_DISABLE_GPU=1',
|
||||
'--action_env=PYTHON_BIN_PATH=' + sys.executable,
|
||||
'--action_env=PYTHON_BIN_PATH=' + _normalize_path(sys.executable),
|
||||
os.path.join('mediapipe/modules/', graph_path),
|
||||
]
|
||||
if not self.link_opencv and not IS_WINDOWS:
|
||||
bazel_command.append('--define=OPENCV=source')
|
||||
if subprocess.call(bazel_command) != 0:
|
||||
sys.exit(-1)
|
||||
output_name = graph_path + '.binarypb'
|
||||
@@ -253,27 +288,17 @@ class BuildBazelExtension(build_ext.build_ext):
|
||||
def bazel_build(self, ext):
|
||||
if not os.path.exists(self.build_temp):
|
||||
os.makedirs(self.build_temp)
|
||||
bazel_argv = [
|
||||
bazel_command = [
|
||||
'bazel',
|
||||
'build',
|
||||
'--compilation_mode=opt',
|
||||
'--define=MEDIAPIPE_DISABLE_GPU=1',
|
||||
'--action_env=PYTHON_BIN_PATH=' + sys.executable,
|
||||
'--action_env=PYTHON_BIN_PATH=' + _normalize_path(sys.executable),
|
||||
str(ext.bazel_target + '.so'),
|
||||
]
|
||||
if not self.link_opencv:
|
||||
# Ask the opencv_cmake rule to build the static opencv libraries for
|
||||
# mediapipe python package. By doing this, we can avoid copying the opencv
|
||||
# .so file into the package.
|
||||
content = open(MP_THIRD_PARTY_BUILD,
|
||||
'r').read().replace('OPENCV_SHARED_LIBS = True',
|
||||
'OPENCV_SHARED_LIBS = False')
|
||||
shutil.move(MP_THIRD_PARTY_BUILD, MP_THIRD_PARTY_BUILD_BACKUP)
|
||||
build_file = open(MP_THIRD_PARTY_BUILD, 'w')
|
||||
build_file.write(content)
|
||||
build_file.close()
|
||||
bazel_argv.append('--define=OPENCV=source')
|
||||
self.spawn(bazel_argv)
|
||||
if not self.link_opencv and not IS_WINDOWS:
|
||||
bazel_command.append('--define=OPENCV=source')
|
||||
self.spawn(bazel_command)
|
||||
ext_bazel_bin_path = os.path.join('bazel-bin', ext.relpath,
|
||||
ext.target_name + '.so')
|
||||
ext_dest_path = self.get_ext_fullpath(ext.name)
|
||||
@@ -281,6 +306,10 @@ class BuildBazelExtension(build_ext.build_ext):
|
||||
if not os.path.exists(ext_dest_dir):
|
||||
os.makedirs(ext_dest_dir)
|
||||
shutil.copyfile(ext_bazel_bin_path, ext_dest_path)
|
||||
if IS_WINDOWS:
|
||||
for opencv_dll in glob.glob(
|
||||
os.path.join('bazel-bin', ext.relpath, '*opencv*.dll')):
|
||||
shutil.copyfile(opencv_dll, ext_dest_dir)
|
||||
|
||||
|
||||
class Build(build.build):
|
||||
@@ -299,6 +328,10 @@ class Build(build.build):
|
||||
build.build.finalize_options(self)
|
||||
|
||||
def run(self):
|
||||
_modify_opencv_cmake_rule(self.link_opencv)
|
||||
build_binary_graphs_obj = self.distribution.get_command_obj(
|
||||
'build_binary_graphs')
|
||||
build_binary_graphs_obj.link_opencv = self.link_opencv
|
||||
build_ext_obj = self.distribution.get_command_obj('build_ext')
|
||||
build_ext_obj.link_opencv = self.link_opencv
|
||||
self.run_command('build_binary_graphs')
|
||||
@@ -324,6 +357,10 @@ class Install(install.install):
|
||||
install.install.finalize_options(self)
|
||||
|
||||
def run(self):
|
||||
_modify_opencv_cmake_rule(self.link_opencv)
|
||||
build_binary_graphs_obj = self.distribution.get_command_obj(
|
||||
'build_binary_graphs')
|
||||
build_binary_graphs_obj.link_opencv = self.link_opencv
|
||||
build_ext_obj = self.distribution.get_command_obj('build_ext')
|
||||
build_ext_obj.link_opencv = self.link_opencv
|
||||
self.run_command('build_binary_graphs')
|
||||
@@ -337,21 +374,27 @@ class RemoveGenerated(clean.clean):
|
||||
"""Remove the generated files."""
|
||||
|
||||
def run(self):
|
||||
for py_file in glob.glob('mediapipe/framework/**/*.py', recursive=True):
|
||||
sys.stderr.write('removing generated files: %s\n' % py_file)
|
||||
os.remove(py_file)
|
||||
for pattern in [
|
||||
'mediapipe/framework/**/*pb2.py', 'mediapipe/calculators/**/*pb2.py',
|
||||
'mediapipe/gpu/**/*pb2.py', 'mediapipe/util/**/*pb2.py'
|
||||
]:
|
||||
for py_file in glob.glob(pattern, recursive=True):
|
||||
sys.stderr.write('removing generated files: %s\n' % py_file)
|
||||
os.remove(py_file)
|
||||
for binarypb_file in glob.glob(
|
||||
'mediapipe/modules/**/*.binarypb', recursive=True):
|
||||
sys.stderr.write('removing generated binary graphs: %s\n' % binarypb_file)
|
||||
os.remove(binarypb_file)
|
||||
# Restore the original init file from the backup.
|
||||
if os.path.exists(MP_DIR_INIT_PY_BACKUP):
|
||||
if os.path.exists(_get_backup_file(MP_DIR_INIT_PY)):
|
||||
os.remove(MP_DIR_INIT_PY)
|
||||
shutil.move(MP_DIR_INIT_PY_BACKUP, MP_DIR_INIT_PY)
|
||||
shutil.move(_get_backup_file(MP_DIR_INIT_PY), MP_DIR_INIT_PY)
|
||||
# Restore the original BUILD file from the backup.
|
||||
if os.path.exists(MP_THIRD_PARTY_BUILD_BACKUP):
|
||||
if os.path.exists(_get_backup_file(MP_THIRD_PARTY_BUILD)):
|
||||
os.remove(MP_THIRD_PARTY_BUILD)
|
||||
shutil.move(MP_THIRD_PARTY_BUILD_BACKUP, MP_THIRD_PARTY_BUILD)
|
||||
shutil.move(_get_backup_file(MP_THIRD_PARTY_BUILD), MP_THIRD_PARTY_BUILD)
|
||||
for init_py in SUBDIR_INIT_PY_FILES:
|
||||
os.remove(init_py)
|
||||
clean.clean.run(self)
|
||||
|
||||
|
||||
@@ -387,6 +430,7 @@ setuptools.setup(
|
||||
'Intended Audience :: Science/Research',
|
||||
'License :: OSI Approved :: Apache Software License',
|
||||
'Operating System :: MacOS :: MacOS X',
|
||||
'Operating System :: Microsoft :: Windows',
|
||||
'Operating System :: POSIX :: Linux',
|
||||
'Programming Language :: Python :: 3.6',
|
||||
'Programming Language :: Python :: 3.7',
|
||||
|
||||
Reference in New Issue
Block a user