diff --git a/PythonAPI/pyproject.toml b/PythonAPI/pyproject.toml new file mode 100644 index 00000000..8e32a1a2 --- /dev/null +++ b/PythonAPI/pyproject.toml @@ -0,0 +1,34 @@ +[build-system] +# Required at build time so wheels can be built in an isolated environment. +# numpy and cython are needed at build-time because the project builds C/Cython extensions. +requires = [ + "setuptools>=61.0", + "wheel", + "cython>=0.27.3", + "numpy>=1.16" +] +build-backend = "setuptools.build_meta" + +[project] +name = "pycocotools" +version = "2.0+nv0.8.1" +description = "Python API for the Microsoft COCO dataset (NVIDIA fork)." +readme = "README.md" +requires-python = ">=3.7" +authors = [ + { name = "NVIDIA" } +] +keywords = ["coco", "computer-vision", "annotation", "dataset"] +classifiers = [ + "Programming Language :: Python :: 3", + "License :: OSI Approved :: Apache Software License", + "Operating System :: OS Independent" +] + +dependencies = [ + "matplotlib>=2.1.0" +] + +[tool.setuptools] +packages = ["pycocotools"] +package-dir = { "pycocotools" = "pycocotools" } diff --git a/PythonAPI/setup.py b/PythonAPI/setup.py index 5a0c7acd..9e6c0234 100644 --- a/PythonAPI/setup.py +++ b/PythonAPI/setup.py @@ -1,40 +1,56 @@ -from os.path import abspath, join, exists +from os.path import join, exists from setuptools import setup, Extension from sys import version_info -import numpy as np -# To compile and install locally run "python setup.py build_ext --inplace" -# To install library to Python site-packages run "python setup.py build_ext install" +# Provide cythonize fallback: if Cython is available build from .pyx, +# otherwise use the checked-in .c/.cpp files. +try: + from Cython.Build import cythonize + USE_CYTHON = True +except Exception: + cythonize = None + USE_CYTHON = False + +# Ensure numpy is available at build time (pyproject.toml should include numpy in build-system.requires) +import numpy as np +# Workaround for Python 3.12 on this repo (keeps compatibility with existing behavior) if (version_info.major, version_info.minor) >= (3, 12) and not exists("pycocotools/_mask.c"): open("pycocotools/_mask.c", "w").close() +mask_source = 'pycocotools/_mask.pyx' if USE_CYTHON else 'pycocotools/_mask.c' +ext_source_maskapi = join('..', 'common', 'maskApi.c') + ext_modules = [ Extension( name='pycocotools._mask', - sources=['../common/maskApi.c', 'pycocotools/_mask.pyx'], + sources=[ext_source_maskapi, mask_source], extra_compile_args=['-Wno-cpp', '-Wno-unused-function', '-std=c99'], - include_dirs=[np.get_include(), '../common'], + include_dirs=[np.get_include(), join('..', 'common')], ), Extension( name='ext', sources=['pycocotools/ext.cpp', 'pycocotools/simdjson.cpp'], extra_compile_args=['-O3', '-Wall', '-shared', '-fopenmp', '-std=c++17', '-fPIC'], include_dirs=[np.get_include(), 'pycocotools'], - library_dirs=[abspath(join(np.get_include(), '..', 'lib'))], - libraries=['npymath', 'gomp'] + library_dirs=[join(np.get_include(), '..', 'lib')], + libraries=['npymath', 'gomp'], + language='c++', ) ] +# If Cython is present, cythonize the extensions so .pyx is compiled +if USE_CYTHON: + ext_modules = cythonize(ext_modules, language_level=3) + setup( name='pycocotools', packages=['pycocotools'], package_dir={'pycocotools': 'pycocotools'}, install_requires=[ 'setuptools>=18.0', - 'cython>=0.27.3', 'matplotlib>=2.1.0', ], version='2.0+nv0.8.1', - ext_modules=ext_modules + ext_modules=ext_modules, )