From eccfcd11929e8acf7bcef38f37c84909f1dcc451 Mon Sep 17 00:00:00 2001 From: Masaki Kozuki Date: Tue, 28 Oct 2025 11:58:52 +0900 Subject: [PATCH 1/2] add pyproject.toml for pep517 Signed-off-by: Masaki Kozuki --- PythonAPI/pyproject.toml | 34 ++++++++++++++++++++++++++++++++++ PythonAPI/setup.py | 33 +++++++++++++++++++++++++-------- 2 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 PythonAPI/pyproject.toml 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..4a57ca41 100644 --- a/PythonAPI/setup.py +++ b/PythonAPI/setup.py @@ -1,20 +1,33 @@ from os.path import abspath, join, exists from setuptools import setup, Extension from sys import version_info -import numpy as np +import os + +# 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 -# 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" +# 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 = abspath(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(), abspath(join('..', 'common'))], ), Extension( name='ext', @@ -22,19 +35,23 @@ 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'] + 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, ) From 97d4f148d7bc330e2ecdc6dbb7beed3f04558b74 Mon Sep 17 00:00:00 2001 From: Masaki Kozuki Date: Tue, 28 Oct 2025 12:14:26 +0900 Subject: [PATCH 2/2] don't use abspath Signed-off-by: Masaki Kozuki --- PythonAPI/setup.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/PythonAPI/setup.py b/PythonAPI/setup.py index 4a57ca41..9e6c0234 100644 --- a/PythonAPI/setup.py +++ b/PythonAPI/setup.py @@ -1,7 +1,6 @@ -from os.path import abspath, join, exists +from os.path import join, exists from setuptools import setup, Extension from sys import version_info -import os # Provide cythonize fallback: if Cython is available build from .pyx, # otherwise use the checked-in .c/.cpp files. @@ -20,21 +19,21 @@ open("pycocotools/_mask.c", "w").close() mask_source = 'pycocotools/_mask.pyx' if USE_CYTHON else 'pycocotools/_mask.c' -ext_source_maskapi = abspath(join('..', 'common', 'maskApi.c')) +ext_source_maskapi = join('..', 'common', 'maskApi.c') ext_modules = [ Extension( name='pycocotools._mask', sources=[ext_source_maskapi, mask_source], extra_compile_args=['-Wno-cpp', '-Wno-unused-function', '-std=c99'], - include_dirs=[np.get_include(), abspath(join('..', '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'))], + library_dirs=[join(np.get_include(), '..', 'lib')], libraries=['npymath', 'gomp'], language='c++', )