Skip to content

Commit

Permalink
Merge pull request ra1nty#46 from abhiTronix/main
Browse files Browse the repository at this point in the history
Made opencv-python dependency optional (Fixes ra1nty#45)
  • Loading branch information
ra1nty authored Jan 16, 2023
2 parents fa51f1f + 5d716c3 commit fc0a257
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ Compared to these existed solutions, DXcam provides:
```bash
pip install dxcam
```

**Note:** OpenCV is required by DXcam for colorspace conversion. If you don't already have OpenCV, install it easily with command `pip install dxcam[cv2]`.

### From source:
```bash
pip install --editable .

# for installing OpenCV also
pip install --editable .[cv2]
```

## Usage
Expand Down
45 changes: 26 additions & 19 deletions dxcam/processor/numpy_processor.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
import ctypes
import numpy as np
import cv2
from .base import Processor


class NumpyProcessor(Processor):

color_mapping = {
"RGB": cv2.COLOR_BGRA2RGB,
"RGBA": cv2.COLOR_BGRA2RGBA,
"BGR": cv2.COLOR_BGRA2BGR,
"GRAY": cv2.COLOR_BGRA2GRAY,
"BGRA": None,
}

def __init__(self, color_mode):
cv2_code = self.color_mapping[color_mode]
self.cvtcolor = None
if cv2_code is not None:
if cv2_code != cv2.COLOR_BGRA2GRAY:
self.cvtcolor = lambda image: cv2.cvtColor(image, cv2_code)
self.color_mode = color_mode

def process_cvtcolor(self, image):
import cv2

# only one time process
if self.cvtcolor is None:
color_mapping = {
"RGB": cv2.COLOR_BGRA2RGB,
"RGBA": cv2.COLOR_BGRA2RGBA,
"BGR": cv2.COLOR_BGRA2BGR,
"GRAY": cv2.COLOR_BGRA2GRAY,
"BGRA": None,
}
cv2_code = color_mapping[self.color_mode]
if cv2_code is not None:
if cv2_code != cv2.COLOR_BGRA2GRAY:
self.cvtcolor = lambda image: cv2.cvtColor(image, cv2_code)
else:
self.cvtcolor = lambda image: cv2.cvtColor(image, cv2_code)[
..., np.newaxis
]
else:
self.cvtcolor = lambda image: cv2.cvtColor(image, cv2_code)[
..., np.newaxis
]
return image
return self.cvtcolor(image)

def process(self, rect, width, height, region, rotation_angle):
pitch = int(rect.Pitch)
Expand All @@ -40,8 +47,8 @@ def process(self, rect, width, height, region, rotation_angle):
elif rotation_angle in (90, 270):
image = np.ndarray((width, pitch, 4), dtype=np.uint8, buffer=buffer)

if self.cvtcolor is not None:
image = self.cvtcolor(image)
if not self.color_mode is None:
image = self.process_cvtcolor(image)

if rotation_angle == 90:
image = np.rot90(image, axes=(1, 0))
Expand Down
2 changes: 2 additions & 0 deletions examples/capture_to_video.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import dxcam

# install OpenCV using `pip install dxcam[cv2]` command.
import cv2

TOP = 0
Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ packages = find:
python_requires = >=3.7
install_requires =
numpy
opencv-python
comtypes

[options.extras_require]
cv2 = opencv-python
[options.packages.find]
include = dxcam*

0 comments on commit fc0a257

Please sign in to comment.