Skip to content

Remove OpenCV as core dependency #588

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

Open
wants to merge 1 commit into
base: master
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
17 changes: 14 additions & 3 deletions packages/core/pitop/core/ImageFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ def image_format_check(format):


def convert(image, format="PIL"):
cv2 = import_opencv()
try:
cv2 = import_opencv()
except ModuleNotFoundError:
cv2 = None

image_format_check(format)
format = format.lower()
Expand All @@ -30,13 +33,21 @@ def convert(image, format="PIL"):
# Convert PIL to OpenCV
cv_image = asarray(image)
if image.mode == "RGB":
cv_image = cv2.cvtColor(cv_image, cv2.COLOR_RGB2BGR)
if cv2:
cv_image = cv2.cvtColor(cv_image, cv2.COLOR_RGB2BGR)
else:
cv_image = cv_image[..., ::-1].copy()

return cv_image
elif isinstance(image, ndarray) and format == "pil":
# Convert OpenCV to PIL
if len(image.shape) > 2 and image.shape[2] == 3:
# If incoming image has 3 channels, convert from BGR to RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
if cv2:
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
else:
image = image[..., ::-1].copy()

return Image.fromarray(image)


Expand Down
6 changes: 5 additions & 1 deletion packages/core/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@
f"pitop.common=={__version__}",
"Pillow>=8.1.2,<9.0",
"numpy>1.19.5,<2.0.0",
"opencv-python>=4.5.1,<4.6.0",
]

__extra_requires__ = {
"computer_vision": ["opencv-python>=4.5.1,<4.6.0"],
}


def main():
import io
Expand All @@ -71,6 +74,7 @@ def main():
include_package_data=True,
platforms=__platforms__,
install_requires=__requires__,
extras_require=__extra_requires__,
)


Expand Down
2 changes: 1 addition & 1 deletion packages/pitop/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
]

__extra_requires__ = {
"computer_vision": ["opencv"],
"computer_vision": ["opencv-python>=4.5.1,<4.6.0"],
"doc": ["sphinx"],
}

Expand Down