Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flexible requirements (allow for opencv-python or opencv-python-headless) #371

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
torch
torchvision
transformers
addict
yapf
timm
numpy
opencv-python
supervision>=0.22.0
pycocotools
-r requirements/runtime.txt
-r requirements/cv2.txt
7 changes: 7 additions & 0 deletions requirements/cv2-headless.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Generated dynamically via: ~/code/xcookie/xcookie/main.py::TemplateApplier._build_special_requirements
# xdev availpkg opencv-python-headless
# --prefer-binary
opencv-python-headless>=4.10.0.84 ; python_version < '4.0' and python_version >= '3.13'
opencv-python-headless>=4.5.5.64 ; python_version < '3.13' and python_version >= '3.11'
opencv-python-headless>=4.5.4.58 ; python_version < '3.11' and python_version >= '3.10'
opencv-python-headless>=3.4.15.55 ; python_version < '3.10' and python_version >= '3.9'
7 changes: 7 additions & 0 deletions requirements/cv2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Generated dynamically via: ~/code/xcookie/xcookie/main.py::TemplateApplier._build_special_requirements
# xdev availpkg opencv-python
# --prefer-binary
opencv-python>=4.10.0.84 ; python_version < '4.0' and python_version >= '3.13'
opencv-python>=4.5.5.64 ; python_version < '3.13' and python_version >= '3.11'
opencv-python>=4.5.4.58 ; python_version < '3.11' and python_version >= '3.10'
opencv-python>=3.4.15.55 ; python_version < '3.10' and python_version >= '3.9'
9 changes: 9 additions & 0 deletions requirements/runtime.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
torch>=2.0.1
torchvision>=0.15.2
transformers>=4.33.2
addict>=2.4.0
yapf>=0.40.1
timm>=0.9.7
numpy>=1.26.0
supervision>=0.22.0
pycocotools>=2.0.6
95 changes: 64 additions & 31 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import glob
import os
import subprocess

import subprocess
import re
import sys


def install_torch():
try:
import torch
Expand Down Expand Up @@ -115,79 +115,101 @@ def get_extensions():
return ext_modules


def parse_requirements(fname="requirements.txt", with_version=True):
"""Parse the package dependencies listed in a requirements file but strips
def parse_requirements(fname="requirements.txt", versions=False):
"""
Parse the package dependencies listed in a requirements file but strips
specific versioning information.

Args:
fname (str): path to requirements file
with_version (bool, default=False): if True include version specs
versions (bool | str):
If true include version specs.
If strict, then pin to the minimum version.

Returns:
List[str]: list of requirements items

CommandLine:
python -c "import setup; print(setup.parse_requirements())"
"""
import re
import sys
from os.path import exists

require_fpath = fname

def parse_line(line):
"""Parse information from a line in a requirements text file."""
def parse_line(line, dpath=""):
"""
Parse information from a line in a requirements text file
"""
# Remove inline comments
comment_pos = line.find(" #")
if comment_pos > -1:
line = line[:comment_pos]

if line.startswith("-r "):
# Allow specifying requirements in other files
target = line.split(" ")[1]
target = os.path.join(dpath, line.split(" ")[1])
for info in parse_require_file(target):
yield info
else:
# See: https://www.python.org/dev/peps/pep-0508/
info = {"line": line}
if line.startswith("-e "):
info["package"] = line.split("#egg=")[1]
elif "@git+" in line:
info["package"] = line
else:
if "--find-links" in line:
# setuptools does not seem to handle find links
line = line.split("--find-links")[0]
if ";" in line:
pkgpart, platpart = line.split(";")
# Handle platform specific dependencies
# setuptools.readthedocs.io/en/latest/setuptools.html
# #declaring-platform-specific-dependencies
plat_deps = platpart.strip()
info["platform_deps"] = plat_deps
else:
pkgpart = line
platpart = None

# Remove versioning from the package
pat = "(" + "|".join([">=", "==", ">"]) + ")"
parts = re.split(pat, line, maxsplit=1)
parts = re.split(pat, pkgpart, maxsplit=1)
parts = [p.strip() for p in parts]

info["package"] = parts[0]
if len(parts) > 1:
op, rest = parts[1:]
if ";" in rest:
# Handle platform specific dependencies
# http://setuptools.readthedocs.io/en/latest/setuptools.html#declaring-platform-specific-dependencies
version, platform_deps = map(str.strip, rest.split(";"))
info["platform_deps"] = platform_deps
else:
version = rest # NOQA
version = rest # NOQA
info["version"] = (op, version)
yield info

def parse_require_file(fpath):
dpath = os.path.dirname(fpath)
with open(fpath, "r") as f:
for line in f.readlines():
line = line.strip()
if line and not line.startswith("#"):
for info in parse_line(line):
for info in parse_line(line, dpath=dpath):
yield info

def gen_packages_items():
if exists(require_fpath):
if os.path.exists(require_fpath):
for info in parse_require_file(require_fpath):
parts = [info["package"]]
if with_version and "version" in info:
parts.extend(info["version"])
if versions and "version" in info:
if versions == "strict":
# In strict mode, we pin to the minimum version
if info["version"]:
# Only replace the first >= instance
verstr = "".join(info["version"]).replace(">=", "==", 1)
parts.append(verstr)
else:
parts.extend(info["version"])
if not sys.version.startswith("3.4"):
# apparently package_deps are broken in 3.4
platform_deps = info.get("platform_deps")
if platform_deps is not None:
parts.append(";" + platform_deps)
plat_deps = info.get("platform_deps")
if plat_deps is not None:
parts.append(";" + plat_deps)
item = "".join(parts)
yield item
if item:
yield item

packages = list(gen_packages_items())
return packages
Expand All @@ -208,7 +230,18 @@ def gen_packages_items():
url="https://github.com/IDEA-Research/GroundingDINO",
description="open-set object detector",
license=license,
install_requires=parse_requirements("requirements.txt"),
# Note: does not include cv2 due to headless ambiguitiy.
install_requires=parse_requirements("requirements/runtime.txt", versions="loose"),
extras_require={
"all": parse_requirements("runtime.txt", versions="loose"),
# Use can choose which type of cv2 to install
"cv2": parse_requirements("requirements/cv2.txt", versions="loose"),
"cv2-headless": parse_requirements("requirements/cv2-headless.txt", versions="loose"),
# Strict variant of requirements
"runtime-strict": parse_requirements("requirements/runtime.txt", versions="strict"),
"cv2-strict": parse_requirements("requirements/cv2.txt", versions="strict"),
"cv2-headless-strict": parse_requirements("requirements/cv2-headless.txt", versions="strict"),
},
packages=find_packages(
exclude=(
"configs",
Expand Down